gldrawarrays不会绘制任何东西

时间:2015-08-06 12:19:10

标签: opengl

我试图用VBO绘制一个三角形。我在窗口上看不到任何像素。我也没有看到任何GL_ERROR。

以下是我尝试运行的代码:

#include <stdio.h>
#include <GL/glew.h>
#include <GL/freeglut.h>
#include "ogldev_math_3d.h"

GLuint VBO;
int idx = 0;

static void RenderSceneCB()
{   
    glClear(GL_COLOR_BUFFER_BIT);
    printf("Error(%d): %s\n", idx, gluErrorString(glGetError())); idx++;

    glEnableVertexAttribArray(0);
    printf("Error(%d): %s\n", idx, gluErrorString(glGetError())); idx++;

     glBindBuffer(GL_ARRAY_BUFFER, VBO);
     printf("Error(%d): %s\n", idx, gluErrorString(glGetError())); idx++;
     glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, 0);
     printf("Error(%d): %s\n", idx, gluErrorString(glGetError())); idx++;


     GLfloat data[10]; 
     glGetBufferSubData(GL_ARRAY_BUFFER, 0, 36, data);
     printf("Error(%d): %s\n", idx, gluErrorString(glGetError())); idx++;
     for (int i=0; i< 9; i++) {
      fprintf(stdout, "data[%d]:%f\n", i, data[i]);
     }

     glDrawArrays(GL_TRIANGLES, 0, 3);
     printf("Error(%d): %s\n", idx, gluErrorString(glGetError())); idx++;


     glDisableVertexAttribArray(0);
     printf("Error(%d): %s\n", idx, gluErrorString(glGetError())); idx++;

    glutSwapBuffers();
    printf("Error(%d): %s\n", idx, gluErrorString(glGetError())); idx++;
}


static void InitializeGlutCallbacks()
{
    glutDisplayFunc(RenderSceneCB);
}

static void CreateVertexBuffer()
{

    Vector3f Vertices[3];
    Vertices[0] = Vector3f(-1.0f, -1.0f, 0.0f);
    Vertices[1] = Vector3f(1.0f, -1.0f, 0.0f);
    Vertices[2] = Vector3f(0.0f, 1.0f, 0.0f);
    glGenBuffers(1, &VBO);
    printf("Error(%d): %s\n", idx, gluErrorString(glGetError())); idx++;
    glBindBuffer(GL_ARRAY_BUFFER, VBO);
    printf("Error(%d): %s\n", idx, gluErrorString(glGetError())); idx++;
    glBufferData(GL_ARRAY_BUFFER, sizeof(Vertices), Vertices, GL_STATIC_DRAW);
    printf("Error(%d): %s\n", idx, gluErrorString(glGetError())); idx++;

}


int main(int argc, char** argv)
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGBA| GLUT_DEPTH);
    glutInitWindowSize(1024, 768);
    glutInitWindowPosition(100, 100);
    glutCreateWindow("Tutorial 03");

    InitializeGlutCallbacks();

    // Must be done after glut is initialized!
    GLenum res = glewInit();
    if (res != GLEW_OK) {
      fprintf(stderr, "Error: '%s'\n", glewGetErrorString(res));
      return 1;
    }
     const GLubyte* renderer = glGetString (GL_RENDERER); // get renderer string
     const GLubyte* version = glGetString (GL_VERSION); // version as a string
     printf("Renderer: %s\n", renderer);
     printf("OpenGL version supported %s\n", version);
    glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
    printf("Error(%d): %s\n", idx, gluErrorString(glGetError())); idx++;

    CreateVertexBuffer();

    glutMainLoop();

    return 0;
}

由于没有报告所有错误检查的GL_NO_ERROR,我不确定此代码有什么问题。一个简单的非VBO绘制调用可以工作,例如在代码片段之后:

 GLfloat myvertices[9] = 
 {
 -1.0f, -1.0f, 0.0f,
 1.0f, -1.0f, 0.0f,
 0.0f, 1.0f, 0.0f
 };

 glEnableClientState(GL_VERTEX_ARRAY);
 glVertexPointer(3, GL_FLOAT, 0, myvertices);
 glDrawArrays(GL_TRIANGLES, 0, 3);
 glDisableClientState(GL_VERTEX_ARRAY);

帮助我发现代码中的错误。报告的OpenGL版本是2.1,渲染器是软件光栅化器。为linux64编译的代码。

1 个答案:

答案 0 :(得分:3)

当不使用着色器时(我假设你没有),不能使用glEnableVertexAttribArray/glVertexAttribPointer方法。

您可以做的是使用glVertexPointer,它在绑定VBO时构造绑定VBO。在这种情况下,最后一个参数指定缓冲区中的偏移量,在您的情况下将为0。 (reference

这样的事情可以解决问题:

glClear(GL_COLOR_BUFFER_BIT);
glEnableClientState(GL_VERTEX_ARRAY);
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glVertexPointer(3, GL_FLOAT, 0, 0);
glDrawArrays(GL_TRIANGLES, 0, 3);
glDisableClientState(GL_VERTEX_ARRAY);