Sign in to follow this  
dgrat

Program breaks when using vertex arrays

Recommended Posts

dgrat    118
Always when i try to use vertex arrays i get an memory access error. My program has the following properties, i init now with this code:

int SceneGraph::initGL()
{
	glMatrixMode(GL_PROJECTION);
	glViewport(0, 0, m_iScreenWidth, m_iScreenHeight);

	/*
	 * Calculate The Aspect Ratio Of The Window
	 */
	gluPerspective(45.0f,(GLfloat)m_iScreenWidth/(GLfloat)m_iScreenHeight,0.1f,50000.0f);
	glMatrixMode(GL_MODELVIEW);

    glShadeModel(GL_SMOOTH);                    // shading mathod: GL_SMOOTH or GL_FLAT
    glPixelStorei(GL_UNPACK_ALIGNMENT, 4);      // 4-byte pixel alignment

    /*
     * enable /disable features
     */
    glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
    //glHint(GL_LINE_SMOOTH_HINT, GL_NICEST);
    //glHint(GL_POLYGON_SMOOTH_HINT, GL_NICEST);
    glEnable(GL_DEPTH_TEST);
    glEnable(GL_LIGHTING);
    glEnable(GL_TEXTURE_2D);					// makes the colors appear darker too
	glEnable(GL_VERTEX_ARRAY);
	glEnable(GL_TEXTURE_COORD_ARRAY);

     /*
      * track material ambient and diffuse from surface color, call it before glEnable(GL_COLOR_MATERIAL)
      */
    glColorMaterial(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE);
    glEnable(GL_COLOR_MATERIAL);

    glClearColor(0, 0, 0, 0);                   // background color
    glClearStencil(0);                          // clear stencil buffer
    glClearDepth(1.0f);                         // 0 is near, 1 is far
    glDepthFunc(GL_LEQUAL);

	return 1;
}

Furthermore i draw my scene with this code:

void SceneGraph::RenderGraph() {
	/*
	 * Check for errors while execution
	 */
	DisplayErrorMessage();

	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);	// lösche Screen und Depth Buffer
	glLoadIdentity();									// Resette die Modelview Matrix

	glColor3f(1.f, 1.f, 1.f);

	m_pCamera->setPerspective();

	if(m_Scene != NULL)
		m_Scene->RenderScene();

	SDL_GL_SwapBuffers();
}

RenderScene() calls a function which tries to draw a cube via vertex arrays. The function is draw3(), always if this function reaches the line containing "glDrawArrays(GL_QUADS, 0, 24);" the program breaks with memory access error.

// vertex coords array
static GLfloat vertices[] = {1,1,1,  -1,1,1,  -1,-1,1,  1,-1,1,        // v0-v1-v2-v3
                      1,1,1,  1,-1,1,  1,-1,-1,  1,1,-1,        // v0-v3-v4-v5
                      1,1,1,  1,1,-1,  -1,1,-1,  -1,1,1,        // v0-v5-v6-v1
                      -1,1,1,  -1,1,-1,  -1,-1,-1,  -1,-1,1,    // v1-v6-v7-v2
                      -1,-1,-1,  1,-1,-1,  1,-1,1,  -1,-1,1,    // v7-v4-v3-v2
                      1,-1,-1,  -1,-1,-1,  -1,1,-1,  1,1,-1};   // v4-v7-v6-v5

// normal array
static GLfloat normals[] = {0,0,1,  0,0,1,  0,0,1,  0,0,1,             // v0-v1-v2-v3
                     1,0,0,  1,0,0,  1,0,0, 1,0,0,              // v0-v3-v4-v5
                     0,1,0,  0,1,0,  0,1,0, 0,1,0,              // v0-v5-v6-v1
                     -1,0,0,  -1,0,0, -1,0,0,  -1,0,0,          // v1-v6-v7-v2
                     0,-1,0,  0,-1,0,  0,-1,0,  0,-1,0,         // v7-v4-v3-v2
                     0,0,-1,  0,0,-1,  0,0,-1,  0,0,-1};        // v4-v7-v6-v5

// color array
static GLfloat  colors[] = {1,1,1,  1,1,0,  1,0,0,  1,0,1,              // v0-v1-v2-v3
                    1,1,1,  1,0,1,  0,0,1,  0,1,1,              // v0-v3-v4-v5
                    1,1,1,  0,1,1,  0,1,0,  1,1,0,              // v0-v5-v6-v1
                    1,1,0,  0,1,0,  0,0,0,  1,0,0,              // v1-v6-v7-v2
                    0,0,0,  0,0,1,  1,0,1,  1,0,0,              // v7-v4-v3-v2
                    0,0,1,  0,0,0,  0,1,0,  0,1,1};             // v4-v7-v6-v5

inline void draw3()
{
	std::cout<<vertices[3]<<std::endl;
    // enable and specify pointers to vertex arrays
    //glEnableClientState(GL_NORMAL_ARRAY);
    //glEnableClientState(GL_COLOR_ARRAY);
    glEnableClientState(GL_VERTEX_ARRAY);

    //glNormalPointer(GL_FLOAT, 0, normals);
    //glColorPointer(3, GL_FLOAT, 0, colors);
    glVertexPointer(3, GL_FLOAT, 0, vertices);

    glPushMatrix();
    //glTranslatef(2, 2, 0);                  // move to upper-right

    glDrawArrays(GL_QUADS, 0, 24);            // BREAK ):
    //glDrawElements(GL_QUADS, 24, GL_UNSIGNED_BYTE, indices);

    glPopMatrix();

    glDisableClientState(GL_VERTEX_ARRAY);  // disable vertex arrays
    //glDisableClientState(GL_COLOR_ARRAY);
    //glDisableClientState(GL_NORMAL_ARRAY);
}

My glError-functions says nothing.
inline void DisplayErrorMessage() {
	GLenum error = glGetError();

	if(error == GL_INVALID_ENUM)
		std::cout<<"ERROR: GL_INVALID_ENUM"<<std::endl;
	else if(error == GL_INVALID_VALUE)
		std::cout<<"ERROR: GL_INVALID_VALUE"<<std::endl;
	else if(error == GL_INVALID_OPERATION)
		std::cout<<"ERROR: GL_INVALID_OPERATION"<<std::endl;
	else if(error == GL_STACK_OVERFLOW)
		std::cout<<"ERROR: GL_STACK_OVERFLOW"<<std::endl;
	else if(error == GL_STACK_UNDERFLOW)
		std::cout<<"ERROR: GL_STACK_UNDERFLOW"<<std::endl;
	else if(error == GL_OUT_OF_MEMORY)
		std::cout<<"ERROR: GL_OUT_OF_MEMORY"<<std::endl;
}

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