Blank screen while using glGenBuffers in openGL

2019-03-06 19:04发布

#include <stdio.h>
#include <stdlib.h>
#include <GL/glew.h>
#include <GL/glut.h>

void changeSize(int w, int h)
{
    if(h == 0) 
        h = 1;
    float ratio = w / h;
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    glViewport(0, 0, w, h);
    gluPerspective(40,ratio,1.5,20);
    glMatrixMode(GL_MODELVIEW);
}

void renderScene(void)
{
    glClear(GL_COLOR_BUFFER_BIT );
    glLoadIdentity();
    glTranslatef(0.0,0.0,-5.0);
    glDrawArrays(GL_TRIANGLES,0,3);
    glutSwapBuffers();
}

void init()
{
    GLfloat verts[] = {
        0.0,   1.0,
       -1.0,  -1.0,
        1.0,  -1.0
    };

    GLuint bufferid;
    glGenBuffers(1,&bufferid);
    glBindBuffer(GL_ARRAY_BUFFER,bufferid);
    glBufferData(GL_ARRAY_BUFFER,sizeof(verts),verts,GL_STATIC_DRAW);
    glEnableVertexAttribArray(0);
    glVertexAttribPointer(0,2,GL_FLOAT,GL_FALSE,0,0);

    if(glGetError()==GL_NO_ERROR)
        printf("no error");
}

int main(int argc, char **argv)
{
    glutInit(&argc, argv);
    glutInitDisplayMode( GLUT_DOUBLE | GLUT_RGBA);
    glutInitWindowPosition(100,100);
    glutInitWindowSize(500,500);
    glutCreateWindow("MM 2004-05");
    glewInit();

    init();

    glutDisplayFunc(renderScene);
    glutReshapeFunc(changeSize);

    if (GLEW_ARB_vertex_program && GLEW_ARB_fragment_program)
        printf("Ready for GLSL\n");
    else {
        printf("No GLSL support\n");
        //exit(1);
    }

    glutMainLoop();
    return 0;
}

When using glGenBuffers my screen turns out black and shows no error. If i draw some other shape without using buffers they are displayed but not with buffer objects.

openGL version:3.0 operating system:ubuntu IDE:eclipse

2条回答
放我归山
2楼-- · 2019-03-06 19:51

When using glGenBuffers you're using the OpenGL-3.0 specification. To draw anything in OpenGL-3.0+ you need to use shaders, hence why the screen is black; your triangle isn't being shaded.

查看更多
在下西门庆
3楼-- · 2019-03-06 20:00

You are using calls for generic vertex attributes here:

glEnableVertexAttribArray(0);
glVertexAttribPointer(0,2,GL_FLOAT,GL_FALSE,0,0);

Generic vertex attributes can only be used in combination with shaders. As long as you're using the fixed function pipeline, you also have to use fixed function vertex attributes.

The corresponding calls using fixed function attributes are:

glEnableClientState(GL_VERTEX_ARRAY);
glVertexPointer(2, GL_FLOAT, 0, 0);
查看更多
登录 后发表回答