Jump to content
  • Advertisement
Sign in to follow this  
leeway

OpenGL about the calculation of texturecoordinate

This topic is 2530 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I can not understand how the texture coordinates are generated,when we call "glMatrixMode(GL_TEXTURE)".for example,if I just set the matrix as IDENTITY matrix.and I have a plane on the x0z of the world coordinates,the 4 vertices are: (512,0,-512,1),(-512,0,-512,1),(512,0,512,1),(-512,0,512,1).then how the final texture coordinates is generated by OpenGL?currently my understanding is as follows:
the vertices do not change after multiply IDENTITY matrix,so the generated 4 texture coordinates is:
s t r q
512 0 -512 1
-512 0 -512 1
512 0 512 1
-512 0 512 1
the thing I want is:s,t.I just care about 2D texture.how the final s,t is calculated?thanks a lot.

Share this post


Link to post
Share on other sites
Advertisement
Don't confuse the position of a vertex (w.r.t. the current MODELVIEW matrix) and its texture co-ordinates. That are 2 different things. A vertex is a compound object consisting of a mandatory position and additional optional parameters to which also texture co-ordinates count.

You usually don't need to alter the TEXTURE matrix. And notice that for "power of two" textures the texture co-ordinates are normalized, i.e. they range from 0 to 1 regardless of how many pixels the texture is wide / high.

Share this post


Link to post
Share on other sites
hi haegarr,
thanks for your kindly answer. I want to achieve the reflection effect by using an inverted camera to render the scene into a texture,then apply this texture to my plane on the ground.I use projective texturing,but the ground is all white.If I do not use projective texturing the contents in texture can appear on the ground(of course very ugly no reflection).so I guess there is something wrong with the texture coordinate generating matrix,and I feel I should first figure out how does this texture generating thing work.
1.what is the input?the position of the vertices in world coordinate?
2.does OpenGL use the Matrix Multiplication result directly?or still some other processing before the result can be thought as a valid texture coordinate?the result in my first post made me very confused.as it is surely not in the (0,1)range and in (s,t,r,q) t is always 0,this makes me crazy.
can you explain the generating process in more detail?I would really appreciate your help.

Share this post


Link to post
Share on other sites
Excuse me if I mention things here that you already know, but it is my impression that there is a principle misunderstanding. Perhaps there is a special usage I don't see ATM...

When you want to render a textured face you have to provide vertices that have both a position accordingly to the current MODELVIEW matrix (what usually means co-ordinates in the model (a.k.a. local) space) as well as texture co-ordinates accordingly to the current TEXTURE matrix (which is identity at default). Hence you invoke e.g.

glVertex3f( x, y, z ); // providing vertex position
glTexCoord2f( s, t ); // providing texture co-ordinate pair

or perhaps

glVertex3f( x, y, z); // providing vertex position
glMultiTexCoord2f( unit, s, t ); // providing texture co-ordinate pair for texture unit "unit"

or any of the various other glTex or glMultiTex routines (assuming you still use the legacy API), and that for each vertex. You have to enable the used texture units, of course. (s,t) is independent of (x,y,z), so that you can move the face (i.e. alter (x,y,z)) without changing its coloring since (s,t) is left untouched. If the texture of interest has a power-of-two size, then s in [0,1] ranges from the left to the right border (similarly for t).

When you want to render-to-texture (RTT) the texture is the render target as a temporary replacement for the regular frame buffer. You don't access that texture like a texture but like a frame buffer. Hence PROJECTION, MODELVIEW, glScissor, and so on play a role.

Later then you render to another target using the above texture actually as a texture.

Share this post


Link to post
Share on other sites
Thanks,I have checked my vertex info, just 4 vertices very simple.I use RTT ,the content in the texture is updated correctly.Let me describe my case in more detail:

Here is my scenario:
position normal texture coordinate
vertex 0: 512, 0, -512, 0,1,0, 0,1
vertex 1: -512, 0, -512, 0,1,0, 1,1
vertex 2: 512, 0, 512, 0,1,0, 0,0
vertex 3: -512, 0, 512, 0,1,0, 1,0
so you can see I am trying to attache the texture to a plane on x0z with the size of 1024*1024.I try to change the texture Matrix by:
glMatrixMode(GL_TEXTURE);
then load my calculated Matrix T.
Because in GLES1.1 there is i no glTexGen() API, I have to calculate the Matrix myself.In order to achieve the desired reflection effect, the Matrix should be:
T=Matrix4::CLIPSPACE2DTOIMAGESPACE*ProjectionMatrix*ViewMatrix*WorldMatrix.
so for every vertex position v, the corresponding texture coordinates should be : (s,t,r,q)=T*v
But to my disappointment, the effect is totally wrong. If I revert T to IDENTITY matrix, then texture can appear on the plane(of course no reflection effect).so the only thing I want to know is:
T=Matrix4::IDENTITY
v=(512,0,-512,1)
texture coordinate = T*v = (512,0,-512,1)
how can this be used as the final texture coordinate,I know my understanding may likely to be totally wrong,but please help explain what really happens for my simple case.thanks.

Share this post


Link to post
Share on other sites
It my misunderstanding!The final calculated texture coordinate(S,T,R,Q) by OpenGL should be:
(S,T,R,Q)=T*(s,t,r,q) //(s,t,r,q) is the value in the vertex buffer

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!