I am working on an opengl project and I have been running into access violation errors. I managed to isolate the error into the code below, but I do not understand why it errors. Any help would be appreciated.
BTW: I know that the genbuffers and deletebuffers are used in a dumb way. The code below just replicates the error.
#include <gl/glew.h>
#include <gl/glut.h>
void draw()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
glEnableClientState(GL_VERTEX_ARRAY);
float v[]={
0,0,0,
1,1,1,
2,2,2,
};
unsigned char i[]={
0,1,2
};
unsigned int a;
unsigned int b;
glGenBuffers(1,&a);
glGenBuffers(1,&b);
glBindBuffer(GL_ARRAY_BUFFER,a);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER,b);
glBufferData(GL_ARRAY_BUFFER,36,v,GL_STATIC_DRAW);
glBufferData(GL_ELEMENT_ARRAY_BUFFER,3,i,GL_STATIC_DRAW);
glVertexPointer(3,GL_FLOAT,0,0);
glDrawElements(GL_TRIANGLES,3,GL_UNSIGNED_BYTE,0);
glDeleteBuffers(1,&a);
glDeleteBuffers(1,&b);
glBegin(GL_LINES);
glVertex3f(0,0,0);
glVertex3f(0,0,0);
glEnd();
glutSwapBuffers();
}
int window;
int width,height;
void changeSize(int w, int h)
{
if(h == 0)
h = 1;
double ratio = 1.0* w / h;
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glViewport(0, 0, w, h);
gluPerspective(45,ratio,.01f,1000);
glMatrixMode(GL_MODELVIEW);
width = w;
height = h;
}
void initWindow()
{
glEnable(GL_CULL_FACE);
glDepthFunc(GL_LEQUAL);
glEnable(GL_DEPTH_TEST);
glClearColor(0.1f,0.1f,0.1f,1);
glEnableClientState(GL_VERTEX_ARRAY);
}
int main(int argc, char **argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGBA);
glutInitWindowSize(800,600);
window = glutCreateWindow("OpenGl");
glewInit();
initWindow();
glutIdleFunc(draw);
glutDisplayFunc(draw);
glutReshapeFunc(changeSize);
glutMainLoop();
return 0;
}
Thanks,
Qcrist