What is OpenGL's positionTexture (cameraSpacePosition) in jme filter shader?

What’s the positionTexture in jme?? Here’s some code in OpenGL.

uniform sampler2D positionTexture;//I dont know
uniform sampler2D normalTexture;//in jme is m_Normal
uniform sampler2D depthTexture;//in jme is m_DepthTexture
uniform sampler2D colorBufferTexture;//in jme is m_Texture

//Convert something in camera space to screen space
vec3 convertCameraSpaceToScreenSpace(in vec3 cameraSpace)
	vec4 clipSpace = m_ProjectionMatrix * vec4(cameraSpace, 1);
	vec3 NDCSpace = clipSpace.xyz / clipSpace.w;
	vec3 screenSpace = 0.5 * NDCSpace + 0.5;
	return screenSpace;

//Values from textures
vec2 screenSpacePosition2D = getScreenSpacePosition();
vec3 cameraSpacePosition = texture(positionTexture,screenSpacePosition2D).xyz;
vec3 cameraSpaceNormal = texture(normalTexture,screenSpacePosition2D).xyz;
float roughness = texture(otherTexture,screenSpacePosition2D).x;
float reflectivity = texture(otherTexture,screenSpacePosition2D).y;

//Screen space vector
vec3 cameraSpaceViewDir = normalize(cameraSpacePosition);
vec3 cameraSpaceVector = normalize(reflect(cameraSpaceViewDir,cameraSpaceNormal));
vec3 screenSpacePosition = convertCameraSpaceToScreenSpace(cameraSpacePosition);
vec3 cameraSpaceVectorPosition = cameraSpacePosition + cameraSpaceVector;
vec3 screenSpaceVectorPosition = convertCameraSpaceToScreenSpace(cameraSpaceVectorPosition);
vec3 screenSpaceVector = initialStepAmount*normalize(screenSpaceVectorPosition - screenSpacePosition);

“positionTexture” is a demonstration that this is an inefficient shader.

Storing positions in a texture requires precision and hence memory bandwidth, something that we don’t really have these days, especially not in the age of 4K displays and 90 FPS VR headsets.

On the other hand, we have lots of ALU (aka, math), and we also have the depth texture which is “free” – the GPU already uses it to perform depth testing so there’s no extra work required to obtain it. There’s code here which shows how you can obtain camera space position (and direction) by using the depth texture:

1 Like

Thank you!

I’m still working on it! Do you know the method getPosition() return value is in which space? View space?
What’s the difference between view space and clip space?

Definitely view space… since zbuffer values are in view space.

Do you know how to get a position(in view space) 's coord(between 0~1) in screen?

Either I don’t understand your question or you completely misunderstand the getPosition() function… as it’s right in there.

I’m ray tracing in view space.
Now the getPosition() can convert from texCoord to view space position. I mean it convert from screen space to view space.
Each loop, the position in view space added.
How can I convert NEW view space position to NEW texCoord ? Because I want to sample the new position texture.
like this:

texture2D(m_Texture, newTexCoord);

One of three things is happening here and I’m hesitant to even respond.

  1. I still don’t understand your question. That’s very possible since they so far only provide the bare minimum information.

  2. You understand how to reverse the math of getPosition() but seem to be missing some piece that you haven’t explained.

  3. You are in way over your head and need to go back to the math and maybe run some simulations to see what is happening… in which case I probably should have kept my response to myself.

Excuse me…
I want to make sure, have I made some mistakes when I do reflect in view space(camera space)?
I doubt the eyePosition is not exactly right.

vec3 viewSpacePosition = getPosition(texCoord);
// Eye position, camera is at (0, 0, 0), we look along negative z, add near plane to correct parallax
vec3 viewSpaceEyePosition = vec3(0., 0., m_FrustumNearFar.x);
vec3 viewSpaceIncidentVector = normalize(viewSpacePosition - viewSpaceEyePosition);
vec3 viewSpaceReflectionVector = reflect(viewSpaceIncidentVector ,normal);

I’m working on this: http://www.gamedev.net/topic/638355-screen-space-reflections-issues/

The reflection in floor in not right as above picture.

WIth normal map:

1 Like