Sign in to follow this  
DerAndiY

Materials and glDrawElements

Recommended Posts

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[i].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[i].m_numTriangles * 3, GL_UNSIGNED_SHORT, m_pMeshes[i].m_pVertexIndices ); } glPopMatrix (); ok, the code for immediate mode: for ( int i = 0; i < m_numMeshes; i++ ) { int materialIndex = m_pMeshes[i].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[i].m_numTriangles; j++ ) { int triangleIndex = m_pMeshes[i].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(); }

Share this post


Link to post
Share on other sites
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 ?

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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)

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
Replace your immediate mode rendering path with :
glBegin( GL_TRIANGLES );
{
GLushort* ptr = (GLushort*)m_pMeshes[i].m_pVertexIndices;
for ( int j = 0; j < m_pMeshes[i].m_numTriangles*3; j++ )
{
glArrayElement(ptr[j]);
}
}

and see what happens.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
Fine, that's exactly what I wanted to hear. :)

So this means that :
for ( int j = 0; j < m_pMeshes[i].m_numTriangles; j++ )
{
int triangleIndex = m_pMeshes[i].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 );
}
}

is different than :
for ( int j = 0; j < m_pMeshes[i].m_numTriangles*3; j++ )
{
glArrayElement(ptr[j]);
}

So, in other words, your data is not packed correctly.

Share this post


Link to post
Share on other sites
Hmmm, sounds reasonable... I thought I doublechecked this issue, though. I will triplecheck it at work tomorrow. Thanks alot :)
I'll post the results tomorrow!

Good night guys
Andy

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this