Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (487)
Games in Android Showcase (112)
games submitted by our members
Games in WIP (553)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Unprojecting...  (Read 1743 times)
0 Members and 1 Guest are viewing this topic.
Offline theagentd
« Posted 2011-09-22 07:31:01 »

I'm trying to go from clip space to world space and then to light clip space in a fragment shader (what you would want to do if you do shadow mapping with a deferred shader), but I can't get it working. My generated world space coordinates don't stand still. They get more and more misaligned the farther I move the camera from the world origin.
This is a fragment shader that only goes from clip space to world space, but it still doesn't work very well (same problem).

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
uniform sampler2D depth; //The depth buffer of the scene, verified to be bound correctly and containing correct data.

uniform mat4x4 inverseProjectionMatrix, inverseViewMatrix; //Correctly set.

in vec2 position; //Interpolated from a fullscreen quad. Clip space position [-1, 1], verified to be working.

layout(location = 0) out vec4 fragColor;

void main()
{
    float z = texture2D(depth, texCoords).z; //Get depth (DEPTH_COMPONENT texture), verified to be working.
   
    vec4 eyeSpace = inverseProjectionMatrix * vec4(position.x, position.y, z, 1);
    //eyeSpace /= eyeSpace.w; //Doesn't seem to affect it as I do a w-divide for worldSpace too.
   vec4 worldSpace = inverseViewMatrix * eyeSpace;
    worldSpace /= worldSpace.w;
   
    fragColor = vec4(worldSpace.xyz, 1); //Colors of the same point in world space change when the camera moves.
}

Am I doing something wrong?

Matrix setup:
1  
2  
3  
4  
5  
6  
7  
8  
9  
Matrix4f.invert(projectionMatrix, null).store(matrixBuffer);
matrixBuffer.flip();
GL20.glUniformMatrix4(volumetricShader.getUniformLocation("inverseProjectionMatrix"), false, matrixBuffer);
matrixBuffer.clear();

Matrix4f.invert(viewMatrix, null).store(matrixBuffer);
matrixBuffer.flip();
GL20.glUniformMatrix4(volumetricShader.getUniformLocation("inverseViewMatrix"), false, matrixBuffer);
matrixBuffer.clear();


Jeez... Matrices are so annoying sometimes...

Myomyomyo.
Offline theagentd
« Reply #1 - Posted 2011-09-23 22:15:41 »

Jeez. Stupid OpenGL.
http://www.opengl.org/wiki/Vertex_Transformation
Quote
Step 4 : Getting to window space

Now the final stage of the transformation pipeline:
The z is transformed to the 0.0 to 1.0 range.
Compensating for this yields perfect results. -_- Seems like DirectX doesn't do this.


Fullscreen pass

Okay, I'm drawing the following coordinates as a GL_QUAD (or as quads are deprecated, a GL_TRIANGLE_STRIP) to get a fullscreen quad:
            0, 0
            1, 0
            1, 1
            0, 1

Vertex shader:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
#version 330

in vec2 inVertex;

out vec2 texCoords;

void main(){
    gl_Position = vec4(inVertex * 2 - 1, 0, 1); //Expand from [0,1] to [-1,1]
   texCoords = inVertex;
}


Fragment Shader:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
#version 330

uniform sampler2D depth;

uniform mat4x4 inverseProjectionMatrix, inverseViewMatrix;

in vec2 texCoords;

void main()
{
    float z = texture2D(depth, texCoords).z; //Sample scene depth

    vec4 normalizedClipSpace = vec4(vec3(texCoords, z) * 2 - 1, 1); //Expand xyz from [0,1] to [-1, 1]
   
    vec4 eyeSpace = inverseProjectionMatrix * normalizedClipSpace;
    //eyeSpace /= eyeSpace.w; //If you just want eye space coordinates, uncomment this line
   vec4 worldSpace = inverseViewMatrix * eyeSpace;
    worldSpace /= worldSpace.w;
   
    //Do whatever you want with worldSpace...
}


I hope this helps someone! =D

Myomyomyo.
Offline pitbuller
« Reply #2 - Posted 2012-04-10 11:56:54 »

Sorry for resurrecting old topic but I have battle with this too and I think I figured better or at least faster way to do this.


@vertex shader
1  
v_viewRay = worldPos - camPos;


@fragment shader.
1  
2  
vec3 viewPos = fromEye * depth;
vec3 worldPos = viewPos + camPos;



Actually there is even better ways to do this but its dependant in which coordination system you want to do this math. More info http://mynameismjp.wordpress.com/2010/09/05/position-from-depth-3/

Currently I am doing everything in view space and got my prelight pass system work on android with real time framerates.
http://www.youtube.com/watch?v=_Qt0ylYX9pU
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline theagentd
« Reply #3 - Posted 2012-04-11 15:45:50 »

Yeah, getting rid of the matrix multiplication per pixel is a good idea. I can't test it myself at the moment though since my laptop's power jack is broken. 50 hours of abstinence and I'm already at my limit...

Myomyomyo.
Offline pitbuller
« Reply #4 - Posted 2012-04-11 17:10:45 »

http://code.google.com/p/gles20prelightpass/

Full source can be found here. Maybe 6-12 months and this gonna be viable choise for mobile developing in terms of marketshare of viable devices.
Offline theagentd
« Reply #5 - Posted 2012-04-12 08:59:54 »

Will check it out if I survive until my laptop is fixed...

Myomyomyo.
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

TehJavaDev (11 views)
2014-08-28 18:26:30

CopyableCougar4 (24 views)
2014-08-22 19:31:30

atombrot (37 views)
2014-08-19 09:29:53

Tekkerue (30 views)
2014-08-16 06:45:27

Tekkerue (29 views)
2014-08-16 06:22:17

Tekkerue (18 views)
2014-08-16 06:20:21

Tekkerue (27 views)
2014-08-16 06:12:11

Rayexar (65 views)
2014-08-11 02:49:23

BurntPizza (41 views)
2014-08-09 21:09:32

BurntPizza (33 views)
2014-08-08 02:01:56
List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06

List of Learning Resources
by SilverTiger
2014-07-31 11:54:12

HotSpot Options
by dleskov
2014-07-08 01:59:08
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!