Results 1 to 8 of 8

Thread: Can't display my texture properly

  1. #1
    Junior Member Newbie
    Join Date
    Jan 2011
    Posts
    5

    Can't display my texture properly

    Hi,

    I'm trying to add a jpeg texture to my project but it only work on 2 sides of my shape. On the other sides it appears to be like stretched and I don't understand why... Normally my texture should be the same on every sides.

    You can see what I'm talking about if you follow this link : http://img573.imageshack.us/i/testd.png/

    That's the link to my texture (552 x 463) : http://img816.imageshack.us/i/fenetre1.jpg/

    And here's the code where I define where the texture will be applied :
    for (i = 0; i < 6; i++)
    {
    glBegin(GL_QUADS);
    glNormal3fv(tabSommet[facette[i][0]]);
    glNormal3fv(tabSommet[facette[i][1]]);
    glNormal3fv(tabSommet[facette[i][2]]);
    glNormal3fv(tabSommet[facette[i][3]]);

    glTexCoord3fv(tabSommet[facette[i][0]]); glVertex3fv(tabSommet[facette[i][0]]);
    glTexCoord3fv(tabSommet[facette[i][1]]); glVertex3fv(tabSommet[facette[i][1]]);
    glTexCoord3fv(tabSommet[facette[i][2]]); glVertex3fv(tabSommet[facette[i][2]]);
    glTexCoord3fv(tabSommet[facette[i][3]]); glVertex3fv(tabSommet[facette[i][3]]);

    }
    glEnd();
    }

    Hope you can help me

  2. #2
    Senior Member Frequent Contributor
    Join Date
    Mar 2009
    Location
    Karachi, Pakistan
    Posts
    810

    Re: Can't display my texture properly

    Hi,
    1) Bring the glBegin(GL_QUADS) call before the loop.
    2) It seems that you want to assign per vertex normal however the way you are doing it right now the last normal will be used for all your vertices. So i think the code should be organized as follows,
    Code :
       glBegin(GL_QUADS);
       for (i = 0; i < 6; i++)
       {
          glNormal3fv(tabSommet[facette[i][0]]);   
          glTexCoord3fv(tabSommet[facette[i][0]]); 
          glVertex3fv(tabSommet[facette[i][0]]);
     
          glNormal3fv(tabSommet[facette[i][1]]);
          glTexCoord3fv(tabSommet[facette[i][1]]); 
          glVertex3fv(tabSommet[facette[i][1]]);
     
          glNormal3fv(tabSommet[facette[i][2]]);
          glTexCoord3fv(tabSommet[facette[i][2]]); 
          glVertex3fv(tabSommet[facette[i][2]]);
     
          glNormal3fv(tabSommet[facette[i][3]]);
          glTexCoord3fv(tabSommet[facette[i][3]]); 
          glVertex3fv(tabSommet[facette[i][3]]);
       }
       glEnd();
    }
    3) You are assigning the same variable (tabSommet) as normals, texture coordinates and positions. Can we see what tabSommet contains? We can't say much unless we see what u r putting into it.
    4) From the output it seems to me that the same texture coordinates are repeated.
    Regards,
    Mobeen

  3. #3
    Junior Member Newbie
    Join Date
    Jan 2011
    Posts
    5

    Re: Can't display my texture properly

    First thank's for your quick answer, I tried your code but it's the same result

    Here's the code of the entire function :
    Code :
    void dessineBloc(GLfloat x, GLfloat y, GLfloat z)
    {    
       // Sommets
       const GLfloat tabSommet[8][3] =
          {
             {0.0 + x, 0.0 + y, 1.0 + z},
             {2.0 + x, 0.0 + y, 1.0 + z},
             {2.0 + x, 2.0 + y, 1.0 + z},
             {0.0 + x, 2.0 + y, 1.0 + z},
             {0.0 + x, 0.0 + y, 0.0 + z},
             {2.0 + x, 0.0 + y, 0.0 + z},
             {2.0 + x, 2.0 + y, 0.0 + z},
             {0.0 + x, 2.0 + y, 0.0 + z}
          };
     
       // Facettes
       const GLubyte facette[6][4] =
          {
             {0, 1, 2, 3}, {4, 7, 6, 5}, {5, 6, 2, 1},
             {7, 3, 2, 6}, {4, 0, 3, 7}, {4, 5, 1, 0}
          };
     
       int i;
     
       for (i = 0; i < 6; i++)
       {
          glBegin(GL_QUADS);
             glNormal3fv(tabSommet[facette[i][0]]);
             glNormal3fv(tabSommet[facette[i][1]]);
             glNormal3fv(tabSommet[facette[i][2]]);
             glNormal3fv(tabSommet[facette[i][3]]);
     
             glTexCoord3fv(tabSommet[facette[i][0]]); glVertex3fv(tabSommet[facette[i][0]]);
             glTexCoord3fv(tabSommet[facette[i][1]]); glVertex3fv(tabSommet[facette[i][1]]);
             glTexCoord3fv(tabSommet[facette[i][2]]); glVertex3fv(tabSommet[facette[i][2]]);
             glTexCoord3fv(tabSommet[facette[i][3]]); glVertex3fv(tabSommet[facette[i][3]]);
          glEnd();
       }   
     
    }

    The normal texture is on the top and on the bottom of the shape and the streched texture is around the shape.

    I'd like to have to normal texture everywhere.

  4. #4
    Senior Member Frequent Contributor
    Join Date
    Mar 2009
    Location
    Karachi, Pakistan
    Posts
    810

    Re: Can't display my texture properly

    I tried your code but it's the same result
    Ofcourse it will be I told u this is how u should organize it.

    And what values for x y z are u passing into this function (dessineBloc)?

    Ok change the code to this and see if this is what you wanted.
    The texture coordinates are usually specified normalized to 0-1 range and these are repeated in each dimension. Try this and see if this is what you were looking for.
    Code :
     // Facettes
    const GLubyte facette[6][4] =
    {
      {0, 1, 2, 3}, {4, 7, 6, 5}, {5, 6, 2, 1},
      {7, 3, 2, 6}, {4, 0, 3, 7}, {4, 5, 1, 0}
    };
     
    const GLfloat texCoord[4][3]={{0,0,0},{1,0,0},{1,1,0},{0,1,0}};
    void dessineBloc(GLfloat x, GLfloat y, GLfloat z)
    {    
       // Sommets
       const GLfloat tabSommet[8][3] =
       {
             {0.0 + x, 0.0 + y, 1.0 + z},
             {2.0 + x, 0.0 + y, 1.0 + z},
             {2.0 + x, 2.0 + y, 1.0 + z},
             {0.0 + x, 2.0 + y, 1.0 + z},
             {0.0 + x, 0.0 + y, 0.0 + z},
             {2.0 + x, 0.0 + y, 0.0 + z},
             {2.0 + x, 2.0 + y, 0.0 + z},
             {0.0 + x, 2.0 + y, 0.0 + z}
          };
     
         int i;
     
       glBegin(GL_QUADS);
       for (i = 0; i < 6; i++)
       {
          glNormal3fv(tabSommet[facette[i][0]]);   
          glTexCoord3fv(texCoord[0]); 
          glVertex3fv(tabSommet[facette[i][0]]);
     
          glNormal3fv(tabSommet[facette[i][1]]);
          glTexCoord3fv(texCoord[1]); 
          glVertex3fv(tabSommet[facette[i][1]]);
     
          glNormal3fv(tabSommet[facette[i][2]]);
          glTexCoord3fv(texCoord[2]); 
          glVertex3fv(tabSommet[facette[i][2]]);
     
          glNormal3fv(tabSommet[facette[i][3]]);
          glTexCoord3fv(texCoord[3]); 
          glVertex3fv(tabSommet[facette[i][3]]);
       }
       glEnd();
    }
    Note I have moved all the constant stuff out. Moreover the inner code could be reduced by using another loop like this,
    Code :
    glBegin(GL_QUADS);
       for (i = 0; i < 6; i++)
       {
          for(int j=0;j<4;j++) {
             glNormal3fv(tabSommet[facette[i][j]]);   
             glTexCoord3fv(texCoord[j]); 
             glVertex3fv(tabSommet[facette[i][j]]);
          }
       }
    glEnd();
    Note that the texture coords. might not work for all three axes and u might need a difft. array for each axis.
    Regards,
    Mobeen

  5. #5
    Junior Member Newbie
    Join Date
    Jan 2011
    Posts
    5

    Re: Can't display my texture properly

    Quote Originally Posted by mobeen
    And what values for x y z are u passing into this function (dessineBloc)?
    "Random" values to put every block where I want


    Thank's a lot by the way, your solution is working perfectly (Except that the texture is upside down but I will find out why), you are a life saver


    Edit : I changed texCoord to :
    Code :
    const GLfloat texCoord[4][3]={{1,1,0},{0,1,0},{0,0,0},{1,0,0}};
    And now my texture is not anymore upside down.

    Thank's you again

  6. #6
    Junior Member Newbie
    Join Date
    Jan 2011
    Posts
    5

    Re: Can't display my texture properly

    Hi it's me again...

    I tried another texture (I only changed that in my project) and now it appears like that (See below)... I tried to changed texCoord with other values but it's the same

    Now it's this result on every block : http://img6.imageshack.us/i/sshot1na.png/

    And that's my new texture : http://img535.imageshack.us/i/fenetre2c.jpg/

    Can you help me (again, sorry !)

  7. #7
    Senior Member Frequent Contributor
    Join Date
    Mar 2009
    Location
    Karachi, Pakistan
    Posts
    810

    Re: Can't display my texture properly

    Hi,
    I think the texture your are loading is not aligned to the 4 byte boundary. In that case, u need to add this call before you call glTexImage2D.
    Code :
    glPixelStorei(GL_UNPACK_ALIGNMENT,1);
    see if this helps.
    Regards,
    Mobeen

  8. #8
    Junior Member Newbie
    Join Date
    Jan 2011
    Posts
    5

    Re: Can't display my texture properly

    Yes it works, but I don't use glTexImage2D.

    That's my code

    Code :
      if(!strFileName) return;
    	tImageJPG *pImage = LoadJPG(strFileName); 
      if(pImage == NULL) exit(0);
      glGenTextures(1, &amp;textureArray[textureID]);
      glBindTexture(GL_TEXTURE_2D, textureArray[textureID]);
      glPixelStorei(GL_UNPACK_ALIGNMENT,1); // Ajout pour que texture n'apparaisse pas déformée
      gluBuild2DMipmaps(GL_TEXTURE_2D, 3, pImage->sizeX, pImage->sizeY, 
    						GL_RGB, GL_UNSIGNED_BYTE, pImage->data);
    	glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,
    						GL_LINEAR_MIPMAP_LINEAR);
      glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,
    						GL_LINEAR);

    Thank's again, I hope it was my last question

Similar Threads

  1. Texture not properly visible
    By Dhanush in forum OpenGL: Basic Coding
    Replies: 6
    Last Post: 04-06-2015, 01:09 PM
  2. for loop in display function not working properly
    By qawded in forum OpenGL: Basic Coding
    Replies: 3
    Last Post: 07-10-2013, 05:40 PM
  3. Why display list doesn't work properly?
    By Asura in forum OpenGL: Basic Coding
    Replies: 3
    Last Post: 04-07-2006, 11:14 AM
  4. 2D polygon wont display properly
    By tim00 in forum OpenGL: Basic Coding
    Replies: 4
    Last Post: 12-07-2004, 02:37 PM
  5. Display Buffer not clearing properly
    By in forum OpenGL: Basic Coding
    Replies: 0
    Last Post: 06-06-2003, 05:31 AM

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