OpenGL VBO法线/照明问题

时间:2012-10-26 02:00:00

标签: c++ opengl vbo

我无法使用VBO正常渲染法线。下面是我正在使用的代码,顶点是一个包含顶点的数组,而法线是一个包含法线的数组:

//Create the buffers and such
GLuint VBOID;
glGenBuffersARB(1, &VBOID);
glBindBufferARB(GL_ARRAY_BUFFER_ARB, VBOID);
glBufferDataARB(GL_ARRAY_BUFFER_ARB, sizeof(vertices) + sizeof(normals), 0, GL_STATIC_DRAW_ARB);
glBufferSubDataARB(GL_ARRAY_BUFFER_ARB, 0, sizeof(vertices), vertices);
glBindBufferARB(GL_ARRAY_BUFFER_ARB, 0);

///Start loop
cout << "Starting" << endl;
while( window.isOpen() ) {
    sf::Event event;
    while( window.pollEvent( event ) ) {
        if( event.type == sf::Event::Closed )
            window.close();
    }
    fps = FPS.getElapsedTime().asSeconds();
    fps = 1 / fps;
    FPS.restart();
    if(ShowFPS.getElapsedTime().asSeconds() > 1)
    {
        cout << "FPS: " << fps << "\t FrameTime: " << 1000 / fps << endl;
        ShowFPS.restart();
    }

    glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluPerspective(50.0, 1.0, 1.0, 5000);
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
    gluLookAt(5, 5, 5, 0, 0, 0, 0, 1, 0);

    //VBO
    glBindBufferARB(GL_ARRAY_BUFFER_ARB, VBOID);
    glEnableClientState(GL_NORMAL_ARRAY);
    glEnableClientState(GL_VERTEX_ARRAY);

    glColor3f(1, 0, 0);
    glRotatef(25 * TurnTimer.getElapsedTime().asSeconds(), 0, 1, 0);
    glRotatef(15 * TurnTimer.getElapsedTime().asSeconds(), 1, 0, 0);
    glNormalPointer(GL_FLOAT, 0, (void*)sizeof(vertices));
    glVertexPointer(3, GL_FLOAT, 0, 0);
    glDrawArrays(GL_TRIANGLES, 0, NumOfF*3);

    glDisableClientState(GL_VERTEX_ARRAY);
    glDisableClientState(GL_NORMAL_ARRAY);
    glBindBufferARB(GL_ARRAY_BUFFER_ARB, 0);

    window.display();
}

1 个答案:

答案 0 :(得分:9)

glBufferSubDataARB(GL_ARRAY_BUFFER_ARB, 0, sizeof(vertices), vertices);

好的,那么您为法线执行此操作的部分在哪里?

另外,请停止使用ARB扩展功能。它已经成为核心OpenGL功能近十年了;删除ARB后缀。