I’m trying to attach a texture to a cube. Currently, the texture is stretching.
I know it’s an issue with my texture coordinates but I cannot figure out what.
For all of the vertices positions (x,y,z), I made the texture coordinate (x,y).
float [] texCoords = new float [2*allPoints.size()];
int index= 0;
for(int i = 0; index< allPoints.size()-1; i=i+2){
texCoords[i] = (float)allPoints.get(index).getX();
texCoords[i+1] = (float)allPoints.get(index).getY();
index++;
}
glTexParameters:
gl2.glTexParameteri( GL.GL_TEXTURE_2D, GL.GL_TEXTURE_MIN_FILTER,GL.GL_NEAREST);
gl2.glTexParameteri( GL.GL_TEXTURE_2D, GL.GL_TEXTURE_MAG_FILTER,GL.GL_NEAREST);
gl2.glTexParameteri(GL.GL_TEXTURE_2D,GL.GL_TEXTURE_WRAP_S,GL.GL_REPEAT);
gl2.glTexParameteri(GL.GL_TEXTURE_2D,GL.GL_TEXTURE_WRAP_T,GL.GL_REPEAT);
Current output:

Texture coordinates in OpenGL are scaled 0-1, if you go past that boundary, according to your parameter of
GL_REPEATforGL_TEXTURE_WRAP_SandGL_TEXTURE_WRAP_T.I don’t know about the organization of your
allPointslist, but for a cube, you’re going to have multiple unique texture coordinates per vertex, so you’re going to need to duplicate your vertices. If you draw it out by hand or think about it, a texture coordinate of1on a vertex also has to be0for an adjacent face.Each face should have the following texture coordinates:
Now think about the faces on a cube, the
(1, 1)point is the(0, 1)point of the adjacent face. Duplicate your vertices to fix that issue.