Java-Gaming.org
Play Revenge of the Titans! The situation is critical. We need fancy commanders to defend Earth, the moon, Mars!
Featured games (78)
games approved by the League of Dukes
Games in Showcase (404)
games submitted by our members
Games in WIP (289)
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 1288 times)
0 Members and 1 Guest are viewing this topic.
Offline theagentd
« Posted 2011-09-22 09: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-24 00: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.
Online pitbuller
« Reply #2 - Posted 2012-04-10 13: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 17: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.
Online pitbuller
« Reply #4 - Posted 2012-04-11 19: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 10: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.

Play Revenge of the Titans! The situation is critical. We need fancy commanders to defend Earth, the moon, Mars!
 
Get high quality music tracks for your game!

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

The first screenshot will be displayed as a thumbnail.

The invasion has landed! On Mars! And you're there to beat 'em!
cubemaster21 (44 views)
2013-05-17 21:29:12

alaslipknot (53 views)
2013-05-16 21:24:48

gouessej (82 views)
2013-05-16 00:53:38

gouessej (81 views)
2013-05-16 00:17:58

theagentd (90 views)
2013-05-15 15:01:13

theagentd (83 views)
2013-05-15 15:00:54

StreetDoggy (127 views)
2013-05-14 15:56:26

kutucuk (149 views)
2013-05-12 17:10:36

kutucuk (149 views)
2013-05-12 15:36:09

UnluckyDevil (159 views)
2013-05-12 05:09:57
Complex number cookbook
by Roquen
2013-04-24 12:47:31

2D Dynamic Lighting
by Oskuro
2013-04-17 16:46:12

2D Dynamic Lighting
by Oskuro
2013-04-17 16:45:57

2D Dynamic Lighting
by Oskuro
2013-04-17 16:23:20

Noise (bandpassed white)
by Roquen
2013-04-05 17:36:01

Noise (bandpassed white)
by Roquen
2013-04-03 16:17:38

Java Data structures
by Roquen
2013-03-29 13:21:12

Topic Request
by kutucuk
2013-03-22 21:42:01
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!
Page created in 0.171 seconds with 20 queries.