PDA

View Full Version : Can't display my texture properly



Fr4nz
01-25-2011, 01:49 AM
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 :)

mobeen
01-25-2011, 02:23 AM
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,


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.

Fr4nz
01-25-2011, 02:45 AM
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 :


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.

mobeen
01-25-2011, 02:55 AM
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.


// 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,


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.

Fr4nz
01-25-2011, 05:39 AM
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 :p


Edit : I changed texCoord to :

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

Fr4nz
01-26-2011, 04:54 AM
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 !)

mobeen
01-26-2011, 05:50 AM
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.


glPixelStorei(GL_UNPACK_ALIGNMENT,1);

see if this helps.

Fr4nz
01-26-2011, 06:59 AM
Yes it works, but I don't use glTexImage2D.

That's my 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_FILTE R,
GL_LINEAR_MIPMAP_LINEAR);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTE R,
GL_LINEAR);

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