PDA

View Full Version : texture coords in vertex arrays



07-22-2004, 12:17 PM
I can't seem to get texture mapping to work properly with vertex arrays.
Example:
I have a sphere with 114 vertices, 224 faces (triangles), and 166 texture coordinates.

I know that the vertex buffer is arranged like {x,y,z,x,y,z,x,y,z...}
and the faces would be
{1st corner, 2nd corner, 3rd corner, 1st corner, 2nd corner, 3rd corner...}
how do I arrange the texture coordinates?
I know it would be like {u,v,u,v,u,v,u,v...}
but do I need a group (u,v) for each corner of the triangle. This is what I am doing now and it does not work.

So just to clarify by vertex buffer has 342 entries, my faces buffer has 672 entries, and my texture buffer has 1344 entries.

grimoire
07-22-2004, 01:59 PM
maybe something like this
static GLfloat verticies[]={1.0,2.0,
2.0,2.0,
2.0,3.0,
1.0,3.0};
static GLfloat texcoords[]={0.0,0.0,
1.0,0.0,
1.0,1.0,
0.0,1.0};

/*displayFunc*/
GLint x=0;
glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_TEXTURE_COORD_ARRAY);

glVertexPointer(2,GL_FLOAT,0,verticies);
glTexCoordPointer(2,GL_FLOAT,0,texcoords);
glBegin(GL_QUADS);
while(x<4){
glArrayElement(x);
x++;
}
glEnd();It would nice to have all this in a list then just call the list(note you cannot put the glEnableClientState function in a list though Hope that helps

07-22-2004, 04:22 PM
So I guess that there should be one set of texture coordinates for each vertex in a polygon? Is this right?

Because I tried this and it didn't work for me. Maybe someone has had similar problems.

I don't see any difference between your example and my code. thanks for the example but I still haven't found the solution.

grimoire
07-22-2004, 07:19 PM
post your code so we can see what is wrong. I tested mine and it worked fine.

P.S. If you want to get fancy you can have a stride and have one big array.

07-22-2004, 07:38 PM
It's not very elegant but here it is:


import java.nio.*;

import org.lwjgl.opengl.GL11;
import org.lwjgl.BufferUtils;


import org.lwjgl.opengl.*;

public class Polyhedron {

int face[][];
int faceTex[][];
int faceNorm[][];
float vertexX[];
float vertexY[];
float vertexZ[];
float textureU[];
float textureV[];
float normalX[];
float normalY[];
float normalZ[];

FloatBuffer bufferVertex;
FloatBuffer bufferTexture;
FloatBuffer bufferNormal;
IntBuffer bufferIndice;


public Polyhedron ( int numVertices, int numFaces, int numTexCoords ) {

face = new int[3][numFaces];
faceTex = new int[3][numFaces];
faceNorm = new int[3][numFaces];
vertexX = new float[numVertices];
vertexY = new float[numVertices];
vertexZ = new float[numVertices];
textureU = new float[numTexCoords];
textureV = new float[numTexCoords];
normalX = new float[numVertices];
normalY = new float[numVertices];
normalZ = new float[numVertices];

bufferVertex = BufferUtils.createFloatBuffer(numVertices*3);
bufferTexture = BufferUtils.createFloatBuffer(numFaces*6);
bufferNormal = BufferUtils.createFloatBuffer(numVertices*3);
bufferIndice = BufferUtils.createIntBuffer(numFaces*3);



}

public void draw () {

GL11.glVertexPointer(3, 0, bufferVertex);
GL11.glNormalPointer(0, bufferNormal);
GL11.glTexCoordPointer(2, 0, bufferTexture);

GL11.glDrawElements(GL11.GL_TRIANGLES, bufferIndice);


}

public void init() {

for (int i = 0; i < vertexX.length; i++) {
bufferVertex.put(vertexX[i]);
bufferVertex.put(vertexY[i]);
bufferVertex.put(vertexZ[i]);

bufferNormal.put(normalX[i]);
bufferNormal.put(normalY[i]);
bufferNormal.put(normalZ[i]);
}

for (int i = 0; i < faceNorm[0].length; i++) {
bufferTexture.put(textureU[faceTex[0][i]]);
bufferTexture.put(textureV[faceTex[0][i]]);
bufferTexture.put(textureU[faceTex[1][i]]);
bufferTexture.put(textureV[faceTex[1][i]]);
bufferTexture.put(textureU[faceTex[2][i]]);
bufferTexture.put(textureV[faceTex[2][i]]);
}

for (int i = 0; i < face[0].length; i++) {
bufferIndice.put(face[0][i]);
bufferIndice.put(face[1][i]);
bufferIndice.put(face[2][i]);
}

bufferVertex.flip();
bufferTexture.flip();
bufferNormal.flip();
bufferIndice.flip();
}
}

07-23-2004, 09:49 AM
I had such problem. But I've solved it. You should write every vertex with its texture coords(UV). So you will 166 vertices with 166 texcoords.
Ex.
for i := 0 to FaceCount - 1 do
for j := 0 to 2 do begin
WriteToSomeBuffer ( Vertex[Face[i][j]].x, y, z);
WriteToSomeBuffer ( TexVert[TexFace[i][j]].u, v);
end;
Then delete all repeating vertices from the buffer and modificate Face[i][j] to the rest vertices.
PS
Sorry for my poor English.

07-23-2004, 02:21 PM
Thanks, that would definitely work. I guess the problem comes into play whenever there are less vertices than texture coords.