Hi Fungus,
I've had the texture projection working well in the fixed pipe line
but wanted to move it over to the shaders to allow me a little more
versatility. I know this is possible as there are plenty of examples
and talk from people who have done this. However despite following
these examples to the letter my rendering doesn't seem to work out
right.. I think I know where the problem is but I'm unsure of how to
solve it and was hoping you could again give me a pointer with your
greater knowledge of all things OpenGL? :)
With my fixed pipeline I do the following:
gl.glMatrixMode(GL.GL_TEXTURE);
gl.glLoadIdentity();
gl.glTranslatef(0.5f, 0.5f, 0.0f); //Bias
gl.glScalef(0.25f, -0.25f, 1.0f); //Scale
glu.gluPerspective(45, frameX / frameY, 0.1f, distance * 2.0f);
glu.gluLookAt(0,0,0,imageCentre.x,imageCentre.y,imageCentre.z,up.x,up.y,up.z);
float eyePlaneS[] = new float[]{1.0f, 0.0f, 0.0f, 0.0f};
float eyePlaneT[] = new float[]{0.0f, 1.0f, 0.0f, 0.0f};
float eyePlaneR[] = new float[]{0.0f, 0.0f, 1.0f, 0.0f};
float eyePlaneQ[] = new float[]{0.0f, 0.0f, 0.0f, 1.0f};
//set up texture generation mode and set the corresponding planes
gl.glTexGeni(GL.GL_S, GL.GL_TEXTURE_GEN_MODE, GL.GL_EYE_LINEAR);
gl.glTexGenfv(GL.GL_S, GL.GL_EYE_PLANE, eyePlaneS, 0);
gl.glTexGeni(GL.GL_T, GL.GL_TEXTURE_GEN_MODE, GL.GL_EYE_LINEAR);
gl.glTexGenfv(GL.GL_T, GL.GL_EYE_PLANE, eyePlaneT, 0);
gl.glTexGeni(GL.GL_R, GL.GL_TEXTURE_GEN_MODE, GL.GL_EYE_LINEAR);
gl.glTexGenfv(GL.GL_R, GL.GL_EYE_PLANE, eyePlaneR, 0);
gl.glTexGeni(GL.GL_Q, GL.GL_TEXTURE_GEN_MODE, GL.GL_EYE_LINEAR);
gl.glTexGenfv(GL.GL_Q, GL.GL_EYE_PLANE, eyePlaneQ, 0);
gl.glEnable(GL.GL_TEXTURE_GEN_S);
gl.glEnable(GL.GL_TEXTURE_GEN_T);
gl.glEnable(GL.GL_TEXTURE_GEN_R);
gl.glEnable(GL.GL_TEXTURE_GEN_Q);
As I said this works fine.. from my understanding to achieve the same
in the shaders I have to:
1) Access the texture matrix that I setup in the above code
2) Transform the texture coordinates in the vertex shader
3) Apply the texture in the fragment shader
I think it is my step 2 that is going wrong.. and I can see what's
wrong.. but can't quite get my head round why or what to do.
Here is the code for my fragment shader:
uniform sampler2D tex;
void main()
{
// Suppress the reverse projection.
if( gl_TexCoord[0].q>0.0 )
{
gl_FragColor = texture2DProj(tex,gl_TexCoord[0]);
}
}
And here is my vertex shader:
void main()
{
vec4 ecPosition= gl_Vertex * gl_ModelViewMatrix;
gl_Position = ftransform();
gl_TexCoord[0] = gl_TextureMatrix[0] * ecPosition;
}
The data I am displaying is in an Octree and I glTranslate to each
node before rendering it. The shader is setup before the Octree is
rendered.
1) If I use this vertex shader then I have a weird local projection
problem applied on each node of the Octree and the texture is repeated
across each node rather than across the whole tree as one texture.
2) If I substitute gl_Position for ecPosition then the texture is
applied in a global fashion to the Octree (correct) but moves and
rotates with the camera movement.
3) If I substitue gl_Vertex for ecPosition then the texture is visible
in the correct projection.. but applied to each node locally rather
than over the whole data set globally.. e.g. I have the same texture
repeating again and again at a fraction of its correct size on each
Node.
All the examples I have seen generate the TexCoord[0] value using
gl_Vertex * gl_ModelViewMatrix.. but it just doesn't work for me...
What's the obvious thing I appear to have missed?
Cheers
Mark
|