Page 1 of 2 12 LastLast
Results 1 to 10 of 19

Thread: glTexCoord2f texture mapping weird

  1. #1
    Junior Member Newbie
    Join Date
    Aug 2006
    Posts
    22

    glTexCoord2f texture mapping weird

    Hi all.

    Right this is the scenario. Ive created a 2D square using GL_POLYGON to it takes up the entire screen (by setting the view point so it does). Ive then successfully mapped a BMP onto this polygon so the bmp takes up the entire screen (kind of like a wallpaper). This works correctly and this is the code ive used:
    Code :
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);		
     
    	glPushMatrix();
    	glEnable(GL_TEXTURE_2D);	
    	Texture t = Texture();
    	int i = t.LoadBitmapW("c:/board.bmp");
    	glBindTexture(GL_TEXTURE_2D, i);
     
    	//DRAW THE BACKGROUND
    	glBegin(GL_POLYGON);
    		glTexCoord2f(0.0,0.0);glVertex3f(0.0,0.0,0.0);
    		glTexCoord2f(1.0,0.0);glVertex3f(1.0,0.0,0.0);
    		glTexCoord2f(1.0,1);glVertex3f(1.0,1,0.0);
    		glTexCoord2f(0.0,1);glVertex3f(0.0,1,0.0);		
    	glEnd();
     
    	glPopMatrix();
    	glPushMatrix();
    	glEnable(GL_TEXTURE_2D);
    	i = t.LoadBitmapW("c:/img.bmp");
    	glBindTexture(GL_TEXTURE_2D, i);
     
     
    	glBegin(GL_POLYGON);
    		glTexCoord2f(0.0,0.0);glVertex3f(0.77, 0.628, 0.1);
    		glTexCoord2f(1.0,0.0);glVertex3f(0.92, 0.628,0.1);
    		glTexCoord2f(1.0,1.0);glVertex3f(0.92, 0.885, 0.1);
    		glTexCoord2f(0.0,1.0);glVertex3f(0.77, 0.885,0.1);
    	glEnd();
     
    	glPopMatrix();
    	glutSwapBuffers();
    	glFlush();
    The code should be easy enough to understand. A big polygon is created and the file "c:/board.jpg" is mapped onto it, this works fine. Then i create a smaller polygon and place it near the top right hand corner the the viewpoint and map the bmp "c:/card.bmp" onto it. However this image isnt mapped correctly onto the polygon, it is stretched to the corners. I believe I have my rotation correct of the polygons (counter clockwise) and the glTexCoord2f is also constructed counter clockwise, however this second polygons mapping is wrong unlike the first polygon. Can anyone see what I've done wrong.

    Also I have another question for you. Im very new to OpenGL and this is my first program. Would you say that the current way of made my program is untidy, unclean and unprofessional? If so then how can I improve it?

    Thanks guys

  2. #2
    Senior Member OpenGL Pro Zengar's Avatar
    Join Date
    Sep 2001
    Location
    Germany
    Posts
    1,931

    Re: glTexCoord2f texture mapping weird

    Texturing works this way: each vertex is supplied with a texture coordinate (like you do it). When the polygon is rendered, the texture is mapped onto the polygon using this coordinates for edges (vertices) and interpolating them inbetween. Consider following: the texture is like a thin streching film, and your polygon is defined through its vertices. OpenGL fixes the points of the texture given by the texture coordinates on the corresponding vertices, so that the image is streched/shrinken (or partly invisible) according to them. So, you do nothing wrong, but the texture coordinates you specify are actually the edges of the texture. With other words, you map the whole texture to smaller polygon. So, the part of the texture rendered does not depend on the size of the polygon, but on the texture coordinates you send in. You can consider looking into the OpenGL specification, there section about texturing there is written pretty well and should explain it.

    Second: there is no need to call glPush/glPop. This functions are used to save the transformation matrix before applying changes like glTraslate/glRotate etc. This way you can build a hierarchy of objects:

    for i = 0 to n:
    glPush;
    object[i].ApplyTransorm;
    object[i].render;
    glPop;

    In your case you don't modify the matrix, so you can delete them.

    Third: it is not a good idea to load the textures inside the rendering loop as this is a very slow process. You should create a loading step (I bet you have something like the setup function where you load the matrices etc) and place the texture loading there. When rendering, just use the saved texture ID to bind it (with glBindTexture) - the image is automatically saved by OpenGL when you associate the texture with an ID.

    Hope this helps...

  3. #3
    Junior Member Newbie
    Join Date
    Aug 2006
    Posts
    22

    Re: glTexCoord2f texture mapping weird

    Hi thanks for the reply

    I must admit I am slightly confused because I am relatively new to OpenGL and its concepts. Are you saying that the problem Im having isnt to do with the theory behind my programming but the actual size of the second polygon?

    Thanks for the tip about taking the loading out of the main rendering loop.

  4. #4
    Senior Member OpenGL Pro Zengar's Avatar
    Join Date
    Sep 2001
    Location
    Germany
    Posts
    1,931

    Re: glTexCoord2f texture mapping weird

    Ok, maybe I undesrtood your question wrong. What do you mean by "streched to teh corners"? Do you exept only part of the texture to be visible? Your code should procuse teh image when the texture is completely mapped onto the rectangle. Can you post a screenshot?

  5. #5
    Junior Member Newbie
    Join Date
    Aug 2006
    Posts
    22

    Re: glTexCoord2f texture mapping weird

    Yeah sure:

    This first image is the bmp thats being mapped to the first polygon
    image1

    This second image is the bmp thats being mapped to the second polygon

    image1

    This final image is a screenshot of my opengl program.

    image3

    As you can see the second image isnt mapping correctly as it comes out diagonally.

    Thanks for your help

    Note, im not concerned with the change in font colour from red to yellow on the second image.

  6. #6
    Senior Member Frequent Contributor
    Join Date
    May 2005
    Location
    Prague, Czech Republic
    Posts
    913

    Re: glTexCoord2f texture mapping weird

    The second image is uploaded incorrectly to the OGL because of its odd size. Try to use glPixelStore( GL_UNPACK_ALIGNMENT, 1)

  7. #7
    Junior Member Newbie
    Join Date
    Aug 2006
    Posts
    22

    Re: glTexCoord2f texture mapping weird

    thanks. Where would I use that in the program?

    Also should it be glPixelStoref not glPixelStore? Thanks

  8. #8
    Senior Member Frequent Contributor
    Join Date
    May 2005
    Location
    Prague, Czech Republic
    Posts
    913

    Re: glTexCoord2f texture mapping weird

    Somewhere in the initialization part before you create the textures.

  9. #9
    Senior Member Frequent Contributor
    Join Date
    May 2005
    Location
    Prague, Czech Republic
    Posts
    913

    Re: glTexCoord2f texture mapping weird

    Use the glPixelStorei since that is most appropriate for integer values like this.

    One more comment to the texture dimensions you are usings. Older cards (including the GeForce FX famility) do not support textures with dimensions that are not power of two trough the GL_TEXTURE_2D target. EDIT: If they expose the OGL 2.0, they have to accept them however it is likely that the rendering will run in sw emulation. To use such textures on such cards you have to use the (ARB|EXT|NV)_texture_rectangle extension.

  10. #10
    Junior Member Newbie
    Join Date
    Aug 2006
    Posts
    22

    Re: glTexCoord2f texture mapping weird

    Hi thanks again

    I threw glPixelStorei( GL_UNPACK_ALIGNMENT, 1); into pretty much all sections of my code with no luck

    With your second comment do you mean that I may need to reedit my picture so instead of having dimension of 99x164 i may need to have it as 128x164? Thanks again

Page 1 of 2 12 LastLast

Similar Threads

  1. Texture failure with glTexCoord2f but not glTexCoordPointer
    By mr_leerman in forum OpenGL: Basic Coding
    Replies: 2
    Last Post: 08-26-2006, 11:48 PM
  2. Problems with glTexCoord2f(...)
    By TheBlackAdder in forum OpenGL: Basic Coding
    Replies: 3
    Last Post: 11-17-2005, 02:41 AM
  3. glTexCoord2f and glMultiTexCoord2fARB
    By JanHH in forum OpenGL: Basic Coding
    Replies: 11
    Last Post: 07-07-2003, 09:47 AM
  4. glTexCoord2f()
    By jankri in forum OpenGL: Basic Coding
    Replies: 4
    Last Post: 07-24-2001, 08:41 AM
  5. Weird colors when texture mapping
    By Ace_Man in forum OpenGL: Basic Coding
    Replies: 1
    Last Post: 10-30-2000, 11:40 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Proudly hosted by Digital Ocean