OpenGL绘制麻烦

时间:2014-01-24 00:46:44

标签: c++ opengl sfml

我为opengl创建了一个加载器,它按顺序将顶点,顶点法线和所有类型的东西读入顶点。装载机是正确的我检查了所有的值,它应该是什么。出于某种原因,我仍然无法在屏幕上看到任何东西。

    #include <SFML/Graphics.hpp>
#include <SFML/OpenGL.hpp>
#include "Loader.h"

#define SC_WIDTH 600
#define SC_HEIGHT 600
#define REFRESH_RATE 0.03f

loader load;

int main()
{
    // Create the main window
     sf::Window App(sf::VideoMode(SC_WIDTH, SC_HEIGHT, 32), "SFML OpenGL");

    // Create a clock for measuring time elapsed
    sf::Clock clock;

    //load file
    load.openFile();

    // Set color and depth clear value
    glClearDepth(1.f);
    glClearColor(0.f, 0.f, 0.f, 0.f);

    // Enable Z-buffer read and write
    glEnable(GL_DEPTH_TEST);
    glDepthMask(GL_TRUE);


    //lighing
    glEnable(GL_LIGHTING);
    glEnable(GL_LIGHT0);

    glEnableClientState(GL_VERTEX_ARRAY);
    glEnableClientState(GL_NORMAL_ARRAY);

    glVertexPointer(3,GL_FLOAT,0,&load.totalVertices);
    glNormalPointer(GL_FLOAT,0,&load.faceNormal);

     // Setup a perspective projection
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluPerspective(90.f, 1.f, 1.f, 500.f);

    // Start game loop
    while (App.isOpen())
    {
        // Process events
        sf::Event Event;
        while (App.pollEvent(Event))
        {
            // Close window : exit
            if (Event.type == sf::Event::Closed)
                App.close();

            // Escape key : exit
            if ((Event.type == sf::Event::KeyPressed) && (Event.key.code == sf::Keyboard::Escape))
                App.close();

            // Resize event : adjust viewport
            if (Event.type == sf::Event::Resized)
               glViewport(0, 0, Event.size.width, Event.size.height);
        }

        // Set the active window before using OpenGL commands
        // It's useless here because active window is always the same,
        // but don't forget it if you use multiple windows or controls
        App.setActive();


        if((float)clock.getElapsedTime().asSeconds()>REFRESH_RATE){
            // Clear colour and depth buffer
            glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

            glMatrixMode(GL_MODELVIEW);
            glLoadIdentity();
            glPopMatrix();
            glTranslatef(0.f,0.f,-10.f);
            glDrawArrays(GL_TRIANGLES, 0,(GLsizei)(load.totalVertices.size()/3));
            glPushMatrix();

            clock.restart();
        }


        // Finally, display rendered frame on screen
        App.display();
    }

    glDisableClientState(GL_VERTEX_ARRAY);
    glDisableClientState(GL_NORMAL_ARRAY);
    return EXIT_SUCCESS;
}

1 个答案:

答案 0 :(得分:1)

首先,你在调用glDrawArrays (...)时将顶点数除以3,这让我相信你认为参数是三角形的数量(或更一般地说,是基元的数量) 。它不是,它是顶点的数量。你不应该除以3。

另一点需要注意的是,您以错误的顺序呼叫glPopMatrix (...)glPushMatrix (...)。第一次尝试绘制任何东西时,基本上都会使矩阵堆栈下溢。你可以交换这两个调用并解决这个问题,但是老实说它们首先不是必需的,因为你在调用glTranslatef (...)之前加载了一个单位矩阵。