在 openGL 中使用 glGenBuffers 时出现空白屏幕

问题描述:

#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
");
    else {
        printf("No GLSL support
");
        //exit(1);
    }

    glutMainLoop();
    return 0;
}

当使用 glGenBuffers 时,我的屏幕变黑并且没有显示错误.如果我在不使用缓冲区的情况下绘制其他形状,它们会显示,但不会显示缓冲区对象.

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 版本:3.0操作系统:ubuntuIDE:日食

openGL version:3.0 operating system:ubuntu IDE:eclipse

使用 glGenBuffers 时,您使用的是 OpenGL-3.0 规范.要在 OpenGL-3.0+ 中绘制任何内容,您需要使用着色器,因此屏幕是黑色的;你的三角形没有被着色.

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.