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();
}
}
|