Materials and glDrawElements

Started by
12 comments, last by DerAndiY 19 years, 9 months ago
Hi guys... I've got a problem that's really screwing me up: I'm drawing a simple (Milkshape) model. When I'm using immediate mode, the lighting etc.. works fine, when I'm using glDrawElements it doesn't. I posted the relevant code (the rest is identical). glPushMatrix (); glInterleavedArrays ( GL_T2F_N3F_V3F, sizeof ( Vertex ) , m_pVertices ); for ( int i = 0; i < m_numMeshes; i++ ) { int materialIndex = m_pMeshes.m_materialIndex; if ( materialIndex >= 0 ) { glMaterialfv( GL_FRONT, GL_AMBIENT, m_pMaterials[materialIndex].m_ambient ); glMaterialfv( GL_FRONT, GL_DIFFUSE, m_pMaterials[materialIndex].m_diffuse ); glMaterialfv( GL_FRONT, GL_SPECULAR, m_pMaterials[materialIndex].m_specular ); glMaterialfv( GL_FRONT, GL_EMISSION, m_pMaterials[materialIndex].m_emissive ); glMaterialf( GL_FRONT, GL_SHININESS, m_pMaterials[materialIndex].m_shininess ); } glDrawElements ( GL_TRIANGLES, m_pMeshes.m_numTriangles * 3, GL_UNSIGNED_SHORT, m_pMeshes.m_pVertexIndices ); } glPopMatrix (); ok, the code for immediate mode: for ( int i = 0; i < m_numMeshes; i++ ) { int materialIndex = m_pMeshes.m_materialIndex; if ( materialIndex >= 0 ) { glMaterialfv( GL_FRONT, GL_AMBIENT, m_pMaterials[materialIndex].m_ambient ); glMaterialfv( GL_FRONT, GL_DIFFUSE, m_pMaterials[materialIndex].m_diffuse ); glMaterialfv( GL_FRONT, GL_SPECULAR, m_pMaterials[materialIndex].m_specular ); glMaterialfv( GL_FRONT, GL_EMISSION, m_pMaterials[materialIndex].m_emissive ); glMaterialf( GL_FRONT, GL_SHININESS, m_pMaterials[materialIndex].m_shininess ); } glBegin( GL_TRIANGLES ); { for ( int j = 0; j < m_pMeshes.m_numTriangles; j++ ) { int triangleIndex = m_pMeshes.m_pTriangleIndices[j]; const Triangle* pTri = &m_pTriangles[triangleIndex]; for ( int k = 0; k < 3; k++ ) { int index = pTri->m_vertexIndices[k]; glNormal3fv( pTri->m_vertexNormals[k] ); glTexCoord2f( pTri->m_s[k], pTri->m_t[k] ); glVertex3fv( m_pVertices[index].m_location ); } } } glEnd(); }
Advertisement
1. why is there glPush/glPop in the vertex array code sample only ?

2. Have you packed your data so that texture coordinate, normal, and vertex coordinate are sequential for each vertex, or are they separated indifferent locations (ie not packed) ?

3. Why is there an indirection in immediate mode with m_pTriangleIndices ? This indirection never appear in vertex array rendering. That's weird.

4. what do you mean by "it doesn't work fine" ? Lighting is screwed up ? Vertex are completely messed up ? Texture is not mapped correctly ?
Hi,
1) sorry, the code is just a snippet thus not complete at all. Just think the push/pop wouldn't be there... (doesn't make a difference at all, does it?)
2) The data is packed (in one struct vertex)
3) In immediate mode i have to render each triange... that's why. Don't bother about the immediate mode too much. It's working fine!
4) The image seems to glow... All vertices are drawn perfectly, the lighting just isn't the same.

Cheers
1) Granted the fact that vertices are in the right "place", yes the push/pop is not a problem

2) Is texturing correct ? If so, this means that the data is correcty packed. Are you sure you fill the normal buffer the same way you fill normals with immediate mode ? (eg don't forget to normalize).

3) since immediate mode works, I think that we should "bother" about it :)

4) if lighting isn't correct, this means normals are messed up. Is texture mapping correct ?

5) What's your system specs (esp. graphics cards, driver version, and OS)
Hi back,
ta for you reply.

Texturing works. Normals should be alright as well ( I suppose Milkshape already does the work?!?). Anyway I'm using the same data to draw in immediate mode (only the draw routine is different). Thus the normal *should* be ok. If you like I can send you the source for that bit. Just mail me: koestler@ee.uwa.edu.au

I don't know the specs by heart as it is the PC at work... It's a Athlon XP something with a GForce 4 MX something...

Any suggestions?

Cheers
Replace your immediate mode rendering path with :
glBegin( GL_TRIANGLES );
{
GLushort* ptr = (GLushort*)m_pMeshes.m_pVertexIndices;
for ( int j = 0; j < m_pMeshes.m_numTriangles*3; j++ )
{
glArrayElement(ptr[j]);
}
}

and see what happens.
Hi,
so I tried what you suggested... It doesn't work either. The picture is still 'flat' lightened. The reference pic has shiny bits and the colors look the way they are supposed too :( Maybe this helps: If I scale my object smaller than a certain value (ratio of 0.5 e.g) the colors are totally screwed. That means blue turns into some sort of whitish-cyan and the gold material is white.
Cheers
Andy
Sorry, the last issue was due to a missing GL_NORMALIZE and hence should'nt cause any trouble anymore.
Cheers
So, is everything working now ?
Na, still the same. If I only could post a pic... The color is right but it looks flat.
Cheers

This topic is closed to new replies.

Advertisement