PDA

View Full Version : About querying texture coordinates



abarrionuevo
05-19-2015, 03:28 AM
I'm working with Qt 4.8 and its OpenGL module, with the fixed pipeline and I have a sphere that was cubemapped, with the tex coords for each cube face auto-generated by OpenGL via
glTexGenf (R, S and T coordinates), like this:


glEnableClientState(GL_NORMAL_ARRAY);
const GLenum textgt = GL_TEXTURE_CUBE_MAP;
const GLfloat param = GL_REFLECTION_MAP;

glTexGenf( GL_S, GL_TEXTURE_GEN_MODE, param );
GLenum error = glGetError();
if(GL_NO_ERROR != error) { ... }

glTexGenf( GL_T, GL_TEXTURE_GEN_MODE, param );
error = glGetError();
if(GL_NO_ERROR != error) { ... }

glTexGenf( GL_R, GL_TEXTURE_GEN_MODE, param );
error = glGetError();
if(GL_NO_ERROR != error) { ... }

glEnable( GL_TEXTURE_GEN_S );
glEnable( GL_TEXTURE_GEN_T );
glEnable( GL_TEXTURE_GEN_R );

When clicking in the scene, I'm retrieving the vertex coordinates under the mouse cursor via
glReadPixels and
gluUnProject.

What I'm looking for is a way to retrieve the texture coordinate associated to that position, instead of getting the vertex value.
I didn't find anything regarding this topic, so I was wondering if there was a way to achieve this.

Thanks for your time!

mhagain
05-19-2015, 05:05 AM
You can use the GL specification to source the formulas used for texcoord auto-generation and recalculate them yourself from the retrieved vertex position value.

abarrionuevo
05-19-2015, 06:42 AM
Thanks mhagain!

I've downloaded the specification and I'm reading the texture generation part.

I've also found this: https://www.opengl.org/wiki/Mathematics_of_glTexGen. But I'm not sure if the formulas in that link are "deprecated".

mhagain
05-19-2015, 09:08 AM
They look fine; I wouldn't worry about deprecation in this case since glTexGen itself is also deprecated, so at least you've satisfied the condition that your calculations are going to be consistent.

abarrionuevo
05-20-2015, 02:17 AM
Ok! Thanks a lot! :)

abarrionuevo
05-21-2015, 02:31 AM
Hi again!

I've been using the formulas in https://www.opengl.org/wiki/Mathematics_of_glTexGen (also tried the formula in the specification in Octave. It gave the same result).

The vertex is the value I got after calling gluUnProject and, since this is a sphere, I'm using the vertex as the normal.

So after the computations, for a vertex V = {-0.577350 0.572447 0.577350}, the computed texels are STR = {0.572455 -0.567593 -0.572455}.

I have some doubts with this.
First, in every test I took, the texel coords were very close to the vertex coords and with the opposite sign. Is this expected?

Second, I have this texture setup:


glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL);
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE);

having negative texels, I'd expect to see some strange effects in the cube when clamping the values of the texel to the edge, yet the cubemap is (apparently) perfect.

Do you have any thoughts on this?