VBO's (Big problem)

Hey all :) I am an intermediate C++ programmer and have written some small programs and am currently working on a moderately big game (who knows if i'll finish or not...)
I use OpenGL for rendering and will incorporate DirectX for input&sound later. With OpenGL I use view frustum culling, z-buffering, and a nice algorithm for hidden surface removal (removes ~95% of cube faces in my current program). However I am trying to incorporate Vertex Buffered Objects. I discovered that my base OpenGL install (well the gl.h/glu.h includes) only support up to OpenGL 1.1 so I've got the newest version of GLee working.

My problem:
Code without VBO's compile and run PERFECT.
Code incorporating VBO's compile fine, but on runtime it simply breaks saying "Access violation reading 0x0000".


Here is my code that involves VBO's:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
//Init code
glGenBuffers(sizeof(GLuint)*2,bufID);
glBindBuffer(GL_ARRAY_BUFFER,bufID[VERTEX_OBJECT]);
glBufferData(GL_ARRAY_BUFFER,sizeof(vertices),vertices,GL_STATIC_DRAW);

//Drawing code:

void Render()
{
        glEnable(GL_DEPTH_TEST);
        glDepthMask(GL_TRUE);
        glClearColor(0.0f, 0.0f, 1.0f, 0.0f);
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
        glLoadIdentity();
        Camera();
        GetFrustum();
        for(int i=0;i<nBlocks;i++)
        {
                voxel bv=Block[i].GetPos();
                if(PointInFrustum(bv)&&Block[i].GetBlock()!=NULL_BLOCK)
                        Block[i].DrawBlock();
        }
        glDisableClientState(GL_VERTEX_ARRAY);

        glFlush();
        SwapBuffers(g_HDC);
}



void CBlock::DrawBlock()
{
        if(type!=NULL_BLOCK)
        {
                glPushMatrix();

                GLubyte indices[24];
                ZeroMemory(indices,sizeof(indices));
                int count=0;
                
                glColor3f(r,g,b);
                glTranslatef(xPos,yPos,zPos);
                for(int i=0;i<6;i++)
                {
                        if(!faceCovered[i])
                        {
                                indices[count]=face[i][0];
                                count++;
                                indices[count]=face[i][1];
                                count++;
                                indices[count]=face[i][2];
                                count++;
                                indices[count]=face[i][3];
                                count++;
                        }
                }
                
                glBindBuffer(GL_ARRAY_BUFFER,bufID[VERTEX_OBJECT]);
                glBufferData(GL_ARRAY_BUFFER,sizeof(vertices),vertices,GL_STREAM_DRAW);
                glEnableClientState(GL_VERTEX_ARRAY);
                glVertexPointer(3,GL_FLOAT,0,0);
                glBindBuffer(GL_ELEMENT_ARRAY_BUFFER,bufID[INDEX_OBJECT]);
                glBufferData(GL_ELEMENT_ARRAY_BUFFER,sizeof(indices),indices,GL_STREAM_DRAW);
                if(count>0)
                        glDrawElements(GL_QUADS,count,GL_UNSIGNED_BYTE,0);  //Here is the access violation. When I set the VertexPointer above to the actual pointer of the vertices[] and pass the indices[] param to glDrawElements the program runs fine. Remember that the code compiles and starts fine, but crashes on this call to glDrawElements().
                
                glPopMatrix();
        }
}
Ok guys can you please offer me anything? Like I have no idea what's causing it.

Has anyone had issues with GLee and VBO's? I need any idea... I've updated my code to make it cleaner and less function calls but glDrawElements() still crashes....

and it works fine if i don't use VBO's...wtf

All my function calls are valid, the params are right, the var's are set fine...i dont get it.
1
2
if(count>0)
     glDrawElements(GL_QUADS,count,GL_UNSIGNED_BYTE,0);  //Here is the access violation. 

You should pass in count/4 to glDrawElements because that's how many quads there are to be rendered.
Hrmm I tried that and it's still crashing then...want me to post my new, cleaner code?

What bothers me is that it compiles and starts fine, just crashing on render. I'm using GLee if i didn't mention before...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
void CBlock::DrawBlock()
{
	int count=0;
        if(type!=NULL_BLOCK)
        {
                GLubyte indices[24];
                ZeroMemory(indices,sizeof(indices));
                
                glColor3f(r,g,b);
                glTranslatef(xPos,yPos,zPos);
                for(int i=0;i<6;i++)
                {
                        if(!faceCovered[i])
                        {
                                indices[count]=face[i][0];
                                count++;
                                indices[count]=face[i][1];
                                count++;
                                indices[count]=face[i][2];
                                count++;
                                indices[count]=face[i][3];
                                count++;
                        }
                }
                if(count>0)
                        glDrawElements(GL_QUADS,count/4,GL_UNSIGNED_BYTE,indices);  //Here is the access violation. When I set the VertexPointer above to the actual pointer of the vertices[] and pass the indices[] param to glDrawElements the program runs fine. Remember that the code compiles and starts fine, but crashes on this call to glDrawElements().
        }
}

void Render()
{
        glEnable(GL_DEPTH_TEST);
		glDepthMask(GL_TRUE);
		glEnableClientState(GL_VERTEX_ARRAY);

        glClearColor(0.0f, 0.0f, 1.0f, 0.0f);
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
        glLoadIdentity();
		Camera();

		glBindBuffer(GL_ARRAY_BUFFER,bufID[VERTEX_OBJECT]);
		glVertexPointer(3,GL_FLOAT,0,0);

		GetFrustum();

		for(int i=0;i<nBlocks;i++)
		{
			voxel bv=Block[i].GetPos();
			if(PointInFrustum(bv)&&Block[i].GetBlock()!=NULL_BLOCK)
				Block[i].DrawBlock();
		}

		glDisableClientState(GL_VERTEX_ARRAY);
		glBindBuffer(GL_ARRAY_BUFFER,0);

		glFlush();
		SwapBuffers(g_HDC);
}

This way i only bind the buffer once and only enable/disable GL_VERTEX_ARRAY once per render.
Maybe this could be the issue?

glGenBuffers(sizeof(GLuint)*2,bufID);

You're asking for 8 buffers but bufID stores only two.
Topic archived. No new replies allowed.