Bhavya Arora
Bhavya Arora

Reputation: 800

OpenGL Draw Points with indeces

I was trying to draw a single point at 0,0 but my screen turns white and stops responding.

Can someone look into the code and see where I am making a mistake?

float *vertices;
GLubyte *pindices;
int width, height;

GLuint vboHandlePoints[1];
GLuint indexVBOPoints;

int numberOfPoints;
GLuint buf;

void initVboPoints(){

    GLenum err = glewInit();

    if (err != GLEW_OK)
    {
        fprintf(stderr, "Error: %s\n", glewGetErrorString(err));
    }

    glGenBuffers(1, &vboHandlePoints[0]);   // create a VBO handle
    glBindBuffer(GL_ARRAY_BUFFER, vboHandlePoints[0]);   // bind the handle to the current VBO 
    glBufferData(GL_ARRAY_BUFFER, 1 * 2 * 4, vertices, GL_STATIC_DRAW); // allocate space and copy the data over
    glBindBuffer(GL_ARRAY_BUFFER, 0);   // clean up 

    glGenBuffers(1, &indexVBOPoints);
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, indexVBOPoints);
    glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(GLubyte)*1 * 2, pindices, GL_STATIC_DRAW);
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);  //clean up 


}
void display(){
    glClearColor(0, 1, 0, 1);
    glClear(GL_COLOR_BUFFER_BIT);
    glColor4f(0, 0, 0, 1);

    glPointSize(5);
    glEnableClientState(GL_VERTEX_ARRAY);


    glBindBuffer(GL_ARRAY_BUFFER, vboHandlePoints[0]);
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, indexVBOPoints);
    glVertexPointer(2, GL_FLOAT, 0, 0);// 2-dimension

    glDrawElements(GL_POINTS, 1, GL_UNSIGNED_BYTE, (char*)NULL + 0);
}

void initializeGlut(int argc, char** argv){

    glutInit(&argc, argv);
    width = 400;
    height = 400;
    glutInitDisplayMode(GLUT_RGB | GLUT_SINGLE);
    glutInitWindowSize(width, height);
    glutCreateWindow("Bhavya's Program");
    glutDisplayFunc(display);

}

void initNormal(){
    vertices = new float[2];
    pindices = new GLubyte[1];
    vertices[0] = 0;
    vertices[1] = 0;
    pindices[0] = 0;
}

int main(int argc, char** argv){

    initNormal();
    initializeGlut(argc, argv);
    initVboPoints();

    glutMainLoop();
    return 0;
}

Upvotes: 0

Views: 154

Answers (2)

derhass
derhass

Reputation: 45322

You have no GL_ARRAY_BUFFER bound at the time of the glVertexPointer call. All of the gl*Pointer functions reference the currently bound GL_ARRAY_BUFFER, the buffer name becomes part of the pointer. That way, you can source each attribute from a different VBO. The GL_ARRAY_BUFFER binding is totally irrelevant for the glDraw* family of functions.

Since you use deprecated legacy GL, binding VBO 0 is still valid, and your pointer references the client memory. So your application is likely to just crash because you told the GL to read the data at memory address 0...

Upvotes: 1

Bhavya Arora
Bhavya Arora

Reputation: 800

The problem is that glFlush(); is required

Upvotes: 0

Related Questions