vertex, normal and texcoordinate indices

Started by
13 comments, last by VansFannel 13 years, 4 months ago
What is the relationship between not reading the posts and saying I'm using OpenGL ES 2.0?

I don't understand.
Advertisement
Lets say you have a vertex attribute arrays called posArray,normalArray and uvArray. The way normal rendering works is by specifying a list of indices into these arrays that forms your primitives, so assuming you are rendering triangles and lets say your indices list is 0,1,2,3,4,5 then you got two triangles here
first one is 0,1,2 which means that it's position is posArray[0],posArray[1],posArray[2] it's normals are normalArray[0],normalArray[1],normalArray[2] and so on.
The problem lies if for example, you are given a triangle which has different indices for the different arrays for example 0,1,2 for position 7,100,2 for normals and so on. In this case you will have to duplicate you vertices.

Hope this clarifies a bit.
It really isn't difficult to write the algorithm for duplicating the vertex and normals and texcoords.
If you have a line like
f 1/3/4 5/5/5 2/4/3

that is a triangle.
First, we must subtract by 1 so we get
f 0/2/3 4/4/4 1/3/2

now, let's do it

j=0;
k=0;
m=0;
for(i=0; i<3; i++)
{
new_vertexarray[k]=vertexarray[vertex_index[j]*3]; //X
new_vertexarray[k+1]=vertexarray[vertex_index[j]*3+1]; //Y
new_vertexarray[k+2]=vertexarray[vertex_index[j]*3+2]; //Z

new_normalarray[k]=normalarray[normal_index[j]*3]; //NX
new_normalarray[k+1]=normalarray[normal_index[j]*3+1]; //NY
new_normalarray[k+2]=normalarray[normal_index[j]*3+2]; //NZ

new_texcoordarray[m]=texcoordarray[tex_index[j]*2]; //S
new_texcoordarray[m+1]=texcoordarray[tex_index[j]*2+1]; //T
k+=3;
m+=2;
j++;
}


in the end, new_vertexarray would have to be of size 9 (3 vertex)
in the end, new_normalarray would have to be of size 9 (3 vertex)
in the end, new_texcoordarray would have to be of size 6 (3 vertex)

You also need a new_indexarray.
Needs to be size 3.
The values will start from 0 and go up.

I suggest you to think and write whatever algorithm you want. If you are not good with algorithms, forget about programming.
Sig: http://glhlib.sourceforge.net
an open source GLU replacement library. Much more modern than GLU.
float matrix[16], inverse_matrix[16];
glhLoadIdentityf2(matrix);
glhTranslatef2(matrix, 0.0, 0.0, 5.0);
glhRotateAboutXf2(matrix, angleInRadians);
glhScalef2(matrix, 1.0, 1.0, -1.0);
glhQuickInvertMatrixf2(matrix, inverse_matrix);
glUniformMatrix4fv(uniformLocation1, 1, FALSE, matrix);
glUniformMatrix4fv(uniformLocation2, 1, FALSE, inverse_matrix);
Quote:Original post by V-man
...


Nice, but there are shared vertices anyway, so I don't think duplicating every vertex is a good idea.
I've tried this method and it doesn't work for me.

I'm trying to draw a cube and I'm getting a flat square.

These are my vertex and fragment shader:

static const char* vertexShader = "   attribute vec4 vertexPosition;  uniform mat4 modelViewProjectionMatrix;  void main() {    gl_Position = modelViewProjectionMatrix * vertexPosition; } ";static const char* fragmentShader = "  precision mediump float;  void main() {    gl_FragColor = vec4(0.0, 0.0, 1.0, 1.0); } ";



[Edited by - VansFannel on December 10, 2010 5:59:40 AM]

This topic is closed to new replies.

Advertisement