glDrawArrays works but glDrawElements doesn't

Started by
2 comments, last by pjcard 14 years, 1 month ago
I'm just starting out with OpenGL, and have hit a bit of a brick wall. I'm not sure whether this issue is down to my setup or code. I'm using the following code under windows:

// defines coords for each vertex
static float s_aafVertexPosDat[8][3]=
{
	{0.5f,	-0.5f,	0.5f},
	{-0.5f,	-0.5f,	0.5f}, 
	{-0.5f,	0.5f,	0.5f},
	{0.5f,	0.5f,	0.5f},
	{0.5f,	0.5f,	-0.5f},
	{0.5f,	-0.5f,	-0.5f},
	{-0.5f,	-0.5f,	-0.5f},
	{-0.5f,	0.5f,	-0.5f}
};

//defines the colour of each vertex
static float s_aafVertexColDat[8][3]=
{
	{0.5, 0 ,0},   //dark red
	{1, 1, 0.3},   //yellow
	{1, 0, 0},     //red
	{0.5, 1, 0.2}, //dull yellow??
	{1, 1, 0},     //yellow
	{0.9, 0.5, 0}, //orange
	{1, 0.9, 0.1}, //yellow
	{1, 0, 0},     //red
};

//defines the vertexes of each quad in anti-clockwise order
static unsigned int s_aafQuadVerDat[6][4]=
{
	{0,1,2,3},  //top
	{0,3,7,4},  //left
	{3,2,6,7},  //back
	{2,1,5,6},  //right
	{0,4,5,1},  //front
	{4,7,6,5},  //bottom
};

void Render()
{
    glClearColor( 0.0f, 0.0f, 0.0f, 0.0f );
    glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
			
    glPushMatrix();
    glTranslatef( g_fXOff, g_fYOff, -2.0f + g_fZOff );
    glRotatef( theta, 1.0f, 0.5f, 1.5f );			
			
    if (g_bWire)
    {
        //Wireframe renderer works ok :)
    }
    else
    {
        GLenum eErr = 0;

        glEnableClientState(GL_VERTEX_ARRAY);
        glEnableClientState(GL_COLOR_ARRAY); 

        glColorPointer(3, GL_FLOAT, 0, s_aafVertexColDat);

        glVertexPointer(3, GL_FLOAT, 0, s_aafVertexPosDat);

        if (g_bUseDrawArrays)
	{
            //This produces the front and back face of the cube
            //As I would expect
            glDrawArrays(GL_QUADS, 0, 8);
        }
        else
        {
            // This produces nothing
            glDrawElements(GL_QUADS, 24, GL_UNSIGNED_BYTE, s_aafQuadVerDat);
        }

    glDisableClientState(GL_COLOR_ARRAY);
    glDisableClientState(GL_VERTEX_ARRAY);
	
    glPopMatrix();
	
    SwapBuffers( hDC );
}

I've checked glGetError() after each call, and it always returns 0. Am I doing something silly?
Advertisement
You appear to have the wrong winding order for your primitives - OpenGL assumes that the primitives are wound counter-clockwise by default.

Either change OpenGL's behaviour with glFrontFace(GL_CW), or rewind your indices in the opposite direction.

Tristam MacDonald. Ex-BigTech Software Engineer. Future farmer. [https://trist.am]

Quote:Original post by swiftcoder
You appear to have the wrong winding order for your primitives - OpenGL assumes that the primitives are wound counter-clockwise by default.

Either change OpenGL's behaviour with glFrontFace(GL_CW), or rewind your indices in the opposite direction.


Thanks for the reply, I've tried adding glFrontFace(GL_CW) and reversing the
indicies, but to no effect.

Edit: Of course, not at the same time :D

//defines the vertexes of each quad in anti-clockwise orderstatic unsigned int s_aafQuadVerDat[6][4]={// 	{0,1,2,3},  //top// 	{0,3,7,4},  //left// 	{3,2,6,7},  //back// 	{2,1,5,6},  //right// 	{0,4,5,1},  //front// 	{4,7,6,5},  //bottom	{3,2,1,0},  //top	{4,7,3,0},  //left	{7,6,2,3},  //back	{6,5,1,2},  //right	{2,5,4,0},  //front	{5,6,7,4},  //bottom};
Aha! If I change GL_UNSIGNED_BYTE to GL_UNSIGNED_INT, I get a rather broken cube (which is fine, I'm sure I've just put the wrong verts in!)

edit: That'll teach me for mixing tutorials.

This topic is closed to new replies.

Advertisement