Texturing on triangles issue

Hello, I am currently taking an opengl class and have hit a wall with my texture coordinates. I am attempting to have my texture drawn onto a vehicle made up of triangles but no matter what combination of coordinates I use there is always unwanted stretching on some parts and working perfectly on others.

This is what I am currently using to populate my texture coordinates:

for (unsigned int z = 0; z < m_texture.getWidth(); z++)
{
m_texCoords.push_back(TexCoord(0.0f, 1.0f));
m_texCoords.push_back(TexCoord(1.0f, 1.0f));
m_texCoords.push_back(TexCoord(0.0f, 0.0f));
m_texCoords.push_back(TexCoord(1.0f, 0.0f));
}

I have also tried:

m_texCoords.push_back(TexCoord(0.0f, 0.0f));
m_texCoords.push_back(TexCoord(1.0f, 0.0f));
m_texCoords.push_back(TexCoord(0.0f, 1.0f));

m_texCoords.push_back(TexCoord(1.0f, 0.0f));
m_texCoords.push_back(TexCoord(0.0f, 1.0f));
m_texCoords.push_back(TexCoord(1.0f, 1.0f));

The first is the only combination I have found that gives me the least amount of stretching on multiple parts.

Here is how I set up the texture:

glGenTextures(1, &m_textureID);
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, m_textureID);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_MIRRORED_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_MIRRORED_REPEAT);

glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, m_texture.getWidth(), m_texture.getHeight(), 0, GL_RGBA, GL_UNSIGNED_BYTE, m_texture.getImageData());
If anyone could give me a hand I would greatly appreciate it.

You don’t show how your texture coordinates are tied to your vertex coordinates. But if you are applying a texture to a complex shape, it is very unlikely that you would be able to do that using only texture coordinates of ones and zeros (i.e. using only the corners of the texture).

The key thing to remember is that the texture coordinates are “stapled to” the vertex coordinates. If you have a large triangle defined, it will need a correspondingly large area of the texture stretched over it.

Try starting with a simple 2D shape (say a square made of 8 triangles like this:


*-----*-----*
|   / |   / |
| /   | /   |
*-----*-----*
|   / |   / |
| /   | /   |
*-----*-----*

Try attaching a simple 2D texture image to it. Think of the texture as a rubber sheet with each texture coordinate saying what part of the texture gets attached to what triangle point. For a undistorted mapping, the center point would have texture coordinate (0.5,0.5). Try tweaking the texture coordinates to see what the effect is. Try moving the vertices around, too (start with the center one).

With a little experimenting, you should get the feeling for how it works. Then you can tackle the harder problem of finding the correct texture coordinates to use for the car.

Ok, I can apply a texture to a single 2d plane without too much trouble, however whenever I try to apply a texture to something like a cube at least one side will always be stretched even when I specify the texture coordinates for each side instead of using a loop to do it for me.

Then you are doing it wrong.
Can you show the code for the cube vertices and texcoords ?

Here is the vertices:

// Right

-1.2 -0.1 0.4 // Bottom Left 0
-0.5 -0.1 0.4 // Bottom Right 1
-1.2 0.3 0.4 // Top Left 2
-0.5 0.3 0.4 // Top Right 3

// Left

-1.2 -0.1 -0.4 // Bottom Right 4
-0.5 -0.1 -0.4 // Bottom Left 5
-1.2 0.3 -0.4 // Top Right 6
-0.5 0.3 -0.4 // Top Left 7

These are the coords:
m_texCoords.push_back(TexCoord(0.0f, 1.0f));
m_texCoords.push_back(TexCoord(0.0f, 0.0f));
m_texCoords.push_back(TexCoord(1.0f, 1.0f));

m_texCoords.push_back(TexCoord(1.0f, 0.0f));
m_texCoords.push_back(TexCoord(0.0f, 0.0f));
m_texCoords.push_back(TexCoord(1.0f, 1.0f));

After binding the shaders attributes they are binded and sent to the glsl shader then drawn onto the screen.

glEnableVertexAttribArray(0); //Enable the vertex attribute
glEnableVertexAttribArray(1); //Enable the vertex attribute

glBindBuffer(GL_ARRAY_BUFFER, m_bufferID[0]);
glVertexAttribPointer((GLint)0, 3, GL_FLOAT, GL_FALSE, 0, 0);
	
glBindBuffer(GL_ARRAY_BUFFER, m_texCoordBuffer);
glVertexAttribPointer((GLint)1, 2, GL_FLOAT, GL_FALSE, 0, 0);

glBindTexture(GL_TEXTURE_2D, m_textureID);

glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_bufferID[1]);
glDrawElements(GL_TRIANGLES, m_indices.size(), GL_UNSIGNED_INT, 0);

glDisableVertexAttribArray(0); //Disable the vertex attribute
glDisableVertexAttribArray(1); //Disable the vertex attribute

Some mesh have different attributes on the same vertex. Every vertex of the cube is shared between three faces, the position is the same, but the UV can be different.
You have to slit the vertex that have different UV.