Indexed Vertex Arrays. PLEASE HELP!!
Hi there!
I''m switching from D3D to OpenGL... and it''s painful!!
Well... the problem is that I define a vertex structure like this:
struct CUSTOMVERTEX {
GLfloat x, y, z; // Position
GLfloat nx, ny, nz; // Normal
GLfloat tu, tv; // Texture coordinates
};
and init the indices for my mesh:
GLushort indices[] =
{ 0+0, 0+1, 0+2,
0+2, 0+3, 0+0,
4+0, 4+1, 4+2,
4+2, 4+3, 4+0,
8+0, 8+1, 8+2,
8+2, 8+3, 8+0,
12+0, 12+1, 12+2,
12+2, 12+3, 12+0,
16+0, 16+1, 16+2,
16+2, 16+3, 16+0,
20+0, 20+1, 20+2,
20+2, 20+3, 20+0
};
Then, I init some shape (a cube) this way:
void Create_Cube()
{
// Define normals
VECTOR n0( 0.0f, 1.0f, 0.0f ); // Top face
VECTOR n1( 0.0f, -1.0f, 0.0f ); // Bottom face
VECTOR n2( -1.0f, 0.0f, 0.0f ); // Left face
VECTOR n3( 1.0f, 0.0f, 0.0f ); // Right face
VECTOR n4( 0.0f, 0.0f, -1.0f ); // Back face
VECTOR n5( 0.0f, 0.0f, 1.0f ); // Front face
// Top face
vertices[0] = Init_Vertex( VECTOR(-1.0f, 1.0f, 1.0f), n0, 0.0, 1.0);
vertices[1] = Init_Vertex( VECTOR(-1.0f, 1.0f, -1.0f), n0, 0.0, 0.0);
vertices[2] = Init_Vertex( VECTOR(1.0f, 1.0f, -1.0f), n0, 1.0, 0.0);
vertices[3] = Init_Vertex( VECTOR(1.0f, 1.0f, 1.0f), n0, 1.0, 1.0);
// Bottom face
vertices[4] = Init_Vertex( VECTOR(-1.0f, -1.0f, -1.0f), n1, 1.0, 0.0);
vertices[5] = Init_Vertex( VECTOR(-1.0f, -1.0f, 1.0f), n1, 1.0, 1.0);
vertices[6] = Init_Vertex( VECTOR(1.0f, -1.0f, 1.0f), n1, 0.0, 1.0);
vertices[7] = Init_Vertex( VECTOR(1.0f, -1.0f, -1.0f), n1, 0.0, 0.0);
// Left face
vertices[8] = Init_Vertex( VECTOR(-1.0f, -1.0f, -1.0f), n2, 0.0, 1.0);
vertices[9] = Init_Vertex( VECTOR(-1.0f, 1.0f, -1.0f), n2, 0.0, 0.0);
vertices[10] = Init_Vertex( VECTOR(-1.0f, 1.0f, 1.0f), n2, 1.0, 0.0);
vertices[11] = Init_Vertex( VECTOR(-1.0f, -1.0f, 1.0f), n2, 1.0, 1.0);
// Right face
vertices[12] = Init_Vertex( VECTOR(1.0f, -1.0f, 1.0f), n3, -1.0, 0.0);
vertices[13] = Init_Vertex( VECTOR(1.0f, 1.0f, 1.0f), n3, -1.0, -1.0);
vertices[14] = Init_Vertex( VECTOR(1.0f, 1.0f, -1.0f), n3, 0.0, -1.0);
vertices[15] = Init_Vertex( VECTOR(1.0f, -1.0f, -1.0f), n3, 0.0, 0.0);
// Back face
vertices[16] = Init_Vertex( VECTOR(1.0f, -1.0f, -1.0f), n4, 0.0, 1.0);
vertices[17] = Init_Vertex( VECTOR(1.0f, 1.0f, -1.0f), n4, 0.0, 0.0);
vertices[18] = Init_Vertex( VECTOR(-1.0f, 1.0f, -1.0f), n4, 1.0, 0.0);
vertices[19] = Init_Vertex( VECTOR(-1.0f, -1.0f, -1.0f), n4, 1.0, 1.0);
// Front face
vertices[20] = Init_Vertex( VECTOR(-1.0f, -1.0f, 1.0f), n5, 0.0, 1.0);
vertices[21] = Init_Vertex( VECTOR(-1.0f, 1.0f, 1.0f), n5, 0.0, 0.0);
vertices[22] = Init_Vertex( VECTOR(1.0f, 1.0f, 1.0f), n5, 1.0, 0.0);
vertices[23] = Init_Vertex( VECTOR(1.0f, -1.0f, 1.0f), n5, 1.0, 1.0);
}
By the way, VECTOR is a simple structure:
typedef struct _VECTOR
{
GLfloat x, y, z;
} VECTOR;
And Init_Vertex returns a valid CUSTOMVERTEX structure.
Now, how can I use Indexed Vertex Arrays with this data?
I tryed the following:
// Enable and specify the vertex array
glEnableClientState(GL_VERTEX_ARRAY);
glVertexPointer(3, GL_FLOAT, sizeof(CUSTOMVERTEX), vertices);
// Using Drawarrays
glDrawElements(GL_TRIANGLES, 24, GL_UNSIGNED_SHORT, indices);
... but failed.
Please give me a hand. I really need it.
Thanks in advance, and sorry for the long post.
Cristián Ramírez (gameovercl)
ICQ #38991565
Viña del Mar
CHILE
Doh! It was so easy! I solved my problem.
If anyone is interested on the solution, e-mail me at ramirez2@entelchile.net
Hummm... sometimes is hard to think in OpenGL terms when you worked with D3D for years...
Cristián Ramírez (gameovercl)
ICQ #38991565
Viña del Mar
CHILE
If anyone is interested on the solution, e-mail me at ramirez2@entelchile.net
Hummm... sometimes is hard to think in OpenGL terms when you worked with D3D for years...
Cristián Ramírez (gameovercl)
ICQ #38991565
Viña del Mar
CHILE
Hey,
Great that you solved the problem
You said that switching from d3d to openGL is painful. What is exactly the hardest part in it, if I may ask?
-tee
Great that you solved the problem

You said that switching from d3d to openGL is painful. What is exactly the hardest part in it, if I may ask?
-tee
-tee
Well, my friend, OpenGL is far easier than Direct3D (any version of it). But switching from a left-handed coordinate system to a right-handed coordinate system is a pain (it''s not that easy to only change the sign of the Z coordinate).
On the other side, DX offers the D3DX library, which supports loading differents file formats, does math calculations (dot product, cross product, matrix algebra, etc.). So, without D3DX, you start to feel a bit without a great tool :=)
On the other side, D3D is much "C++ friendly" - I mean - it was made thinking on a C++ basis. OpenGL - trying to be portable - is like "another" language, if you know what I mean. One example is the idea of the "state machine", which does not exist on D3D; the bahavior in this sense on D3D is more C++ oriented than OpenGL.
Anyway, switching to OpenGL is a nice trip
If you want to port your code to D3D, feel free to e-mail me. So, you will not suffer as I am suffering now (LOL).
Cristián Ramírez (gameovercl)
ICQ #38991565
Viña del Mar
CHILE
On the other side, DX offers the D3DX library, which supports loading differents file formats, does math calculations (dot product, cross product, matrix algebra, etc.). So, without D3DX, you start to feel a bit without a great tool :=)
On the other side, D3D is much "C++ friendly" - I mean - it was made thinking on a C++ basis. OpenGL - trying to be portable - is like "another" language, if you know what I mean. One example is the idea of the "state machine", which does not exist on D3D; the bahavior in this sense on D3D is more C++ oriented than OpenGL.
Anyway, switching to OpenGL is a nice trip

If you want to port your code to D3D, feel free to e-mail me. So, you will not suffer as I am suffering now (LOL).
Cristián Ramírez (gameovercl)
ICQ #38991565
Viña del Mar
CHILE
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement
Recommended Tutorials
Advertisement