简体   繁体   English

SFML + OpenGL:无法绘制立方体

[英]SFML + OpenGL: Unable to draw cube

I've worked with OpenGL before, and it has been a while, but I didn't think it was this bad. 我以前使用过OpenGL,并且已经有一段时间了,但是我并不认为这很糟糕。 There's something I'm missing in this code. 这段代码中我缺少一些东西。 I'm just trying to see a cube that I'm drawing using the cube() function. 我只是想看看正在使用cube()函数绘制的cube()

Things I've done/tried... 我做过/尝试过的事情...

  • flipping the translation between + and - z in the main loop 在主循环中翻转+和-z之间的转换
  • turning backface culling on/off before main loop 在主循环之前打开/关闭背面剔除
  • glLoadIdentity() at the beginning 开头是glLoadIdentity()
  • set up my projection at the beginning 一开始就设定我的投影
  • window.resetGLStates() at the beginning 开头的window.resetGLStates()
  • window.setActive(true) at the beginning 开头的window.setActive(true)
  • made sure I stayed in glMatrixMode(GL_MODELVIEW) 确保我呆在glMatrixMode(GL_MODELVIEW)

What am I missing? 我想念什么? All I can see is the clear color I've specified, filling up the screen and taunting me. 我所看到的只是我指定的清晰颜色,填满了屏幕并嘲笑我。

#include <SFML/System.hpp>
#include <SFML/Window.hpp>
#include <SFML/Graphics.hpp>
#include <SFML/OpenGL.hpp>

void cube();

int gltest()
{
    sf::RenderWindow window(sf::VideoMode(800, 600), "SFML works!", sf::Style::Default);
    window.setVerticalSyncEnabled(true);
    window.setActive(true);
    window.resetGLStates();
    sf::Vector2u wsize = window.getSize();

    glClearColor(0.3f, 0.3f, 0.3f, 1.f);
    glDepthMask(true);
    glEnable(GL_DEPTH_TEST);
    //to make sure I'm not missing anything here.
    glDisable(GL_CULL_FACE);

    glMatrixMode(GL_PROJECTION);
    glViewport(0, 0, wsize.x, wsize.y);
    gluPerspective(60, (float)wsize.x / (float)wsize.y, 0.1f, 512.f);
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();

    bool running = true;
    while(running)
    {
        sf::Event event;
        while(window.pollEvent(event))
        {
            switch(event.type)
            {
                case sf::Event::KeyPressed:
                    if(event.key.code != sf::Keyboard::Escape) { break; }
                case sf::Event::Closed:
                    running = false;
                    break;
                case sf::Event::Resized:
                    wsize.x = event.size.width;
                    wsize.y = event.size.height;
                    glMatrixMode(GL_PROJECTION);
                    glViewport(0, 0, wsize.x, wsize.y);
                    gluPerspective(60.f, (float)wsize.x / (float)wsize.y, 0.1f, 512.f);
                    glMatrixMode(GL_MODELVIEW);
                    break;
                default:
                    break;
            }
        }

        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

            glPushMatrix();
            glTranslatef(0.f, 0.f, 5.f);

                cube();

            glPopMatrix();

        window.display();
    }

    return 0;
}

void cube()
{
glBegin(GL_QUADS);      // Draw The Cube Using quads
    glColor3f(0.0f,1.0f,0.0f);  // Color Green
    glNormal3f(0.f, 1.f, 0.f);
    glVertex3f( 1.0f, 1.0f,-1.0f);  // Top Right Of The Quad (Top)
    glNormal3f(0.f, 1.f, 0.f);
    glVertex3f(-1.0f, 1.0f,-1.0f);  // Top Left Of The Quad (Top)
    glNormal3f(0.f, 1.f, 0.f);
    glVertex3f(-1.0f, 1.0f, 1.0f);  // Bottom Left Of The Quad (Top)
    glNormal3f(0.f, 1.f, 0.f);
    glVertex3f( 1.0f, 1.0f, 1.0f);  // Bottom Right Of The Quad (Top)
    glColor3f(1.0f,0.5f,0.0f);  // Color Orange
    glNormal3f(0.f, -1.f, 0.f);
    glVertex3f( 1.0f,-1.0f, 1.0f);  // Top Right Of The Quad (Bottom)
    glNormal3f(0.f, -1.f, 0.f);
    glVertex3f(-1.0f,-1.0f, 1.0f);  // Top Left Of The Quad (Bottom)
    glNormal3f(0.f, -1.f, 0.f);
    glVertex3f(-1.0f,-1.0f,-1.0f);  // Bottom Left Of The Quad (Bottom)
    glNormal3f(0.f, -1.f, 0.f);
    glVertex3f( 1.0f,-1.0f,-1.0f);  // Bottom Right Of The Quad (Bottom)
    glColor3f(1.0f, 0.0f, 0.0f);    // Color Red
    glNormal3f(0.f, 0.f, 1.f);
    glVertex3f( 1.0f, 1.0f, 1.0f);  // Top Right Of The Quad (Front)
    glNormal3f(0.f, 0.f, 1.f);
    glVertex3f(-1.0f, 1.0f, 1.0f);  // Top Left Of The Quad (Front)
    glNormal3f(0.f, 0.f, 1.f);
    glVertex3f(-1.0f,-1.0f, 1.0f);  // Bottom Left Of The Quad (Front)
    glNormal3f(0.f, 0.f, 1.f);
    glVertex3f( 1.0f,-1.0f, 1.0f);  // Bottom Right Of The Quad (Front)
    glColor3f(1.0f, 1.0f, 0.0f);    // Color Yellow
    glNormal3f(0.f, 0.f, -1.f);
    glVertex3f( 1.0f,-1.0f,-1.0f);  // Top Right Of The Quad (Back)
    glNormal3f(0.f, 0.f, -1.f);
    glVertex3f(-1.0f,-1.0f,-1.0f);  // Top Left Of The Quad (Back)
    glNormal3f(0.f, 0.f, -1.f);
    glVertex3f(-1.0f, 1.0f,-1.0f);  // Bottom Left Of The Quad (Back)
    glNormal3f(0.f, 0.f, -1.f);
    glVertex3f( 1.0f, 1.0f,-1.0f);  // Bottom Right Of The Quad (Back)
    glColor3f(0.0f, 0.0f, 1.0f);    // Color Blue
    glNormal3f(-1.f, 0.f, 0.f);
    glVertex3f(-1.0f, 1.0f, 1.0f);  // Top Right Of The Quad (Left)
    glNormal3f(-1.f, 0.f, 0.f);
    glVertex3f(-1.0f, 1.0f,-1.0f);  // Top Left Of The Quad (Left)
    glNormal3f(-1.f, 0.f, 0.f);
    glVertex3f(-1.0f,-1.0f,-1.0f);  // Bottom Left Of The Quad (Left)
    glNormal3f(-1.f, 0.f, 0.f);
    glVertex3f(-1.0f,-1.0f, 1.0f);  // Bottom Right Of The Quad (Left)
    glColor3f(1.0f, 0.0f, 1.0f);    // Color Violet
    glNormal3f(1.f, 0.f, 0.f);
    glVertex3f( 1.0f, 1.0f,-1.0f);  // Top Right Of The Quad (Right)
    glNormal3f(1.f, 0.f, 0.f);
    glVertex3f( 1.0f, 1.0f, 1.0f);  // Top Left Of The Quad (Right)
    glNormal3f(1.f, 0.f, 0.f);
    glVertex3f( 1.0f,-1.0f, 1.0f);  // Bottom Left Of The Quad (Right)
    glNormal3f(1.f, 0.f, 0.f);
    glVertex3f( 1.0f,-1.0f,-1.0f);  // Bottom Right Of The Quad (Right)
  glEnd();          // End Drawing The Cube
}

Here: 这里:

glMatrixMode(GL_PROJECTION);
glViewport(0, 0, wsize.x, wsize.y);
gluPerspective(60, (float)wsize.x / (float)wsize.y, 0.1f, 512.f);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();

and here: 和这里:

wsize.x = event.size.width;
wsize.y = event.size.height;
glMatrixMode(GL_PROJECTION);
glViewport(0, 0, wsize.x, wsize.y);
gluPerspective(60.f, (float)wsize.x / (float)wsize.y, 0.1f, 512.f);
glMatrixMode(GL_MODELVIEW);

add a glLoadIdentity() after the glMatrixMode(GL_PROJECTION) and glMatrixMode(GL_MODELVIEW) calls. glMatrixMode(GL_PROJECTION)glMatrixMode(GL_MODELVIEW)调用之后添加glLoadIdentity()

gluPerspective() multiplies a matrix against the current matrix. gluPerspective() 矩阵与当前矩阵相乘。 The code seems to be assuming it overwrites the current matrix. 该代码似乎是假设它覆盖了当前矩阵。

A gluPerspective() matrix multiplied by itself multiple times isn't a meaningful projection matrix. 一个gluPerspective()矩阵自身乘以多次并不是一个有意义的投影矩阵。

And while you're there you can remove the resize handler: 在此期间,您可以删除调整大小处理程序:

#include <SFML/System.hpp>
#include <SFML/Window.hpp>
#include <SFML/Graphics.hpp>
#include <SFML/OpenGL.hpp>

void cube()
{
    glBegin(GL_QUADS);      // Draw The Cube Using quads

    glColor3f(0.0f,1.0f,0.0f);  // Color Green
    glNormal3f(0.f, 1.f, 0.f);
    glVertex3f( 1.0f, 1.0f,-1.0f);  // Top Right Of The Quad (Top)
    glNormal3f(0.f, 1.f, 0.f);
    glVertex3f(-1.0f, 1.0f,-1.0f);  // Top Left Of The Quad (Top)
    glNormal3f(0.f, 1.f, 0.f);
    glVertex3f(-1.0f, 1.0f, 1.0f);  // Bottom Left Of The Quad (Top)
    glNormal3f(0.f, 1.f, 0.f);
    glVertex3f( 1.0f, 1.0f, 1.0f);  // Bottom Right Of The Quad (Top)

    glColor3f(1.0f,0.5f,0.0f);  // Color Orange
    glNormal3f(0.f, -1.f, 0.f);
    glVertex3f( 1.0f,-1.0f, 1.0f);  // Top Right Of The Quad (Bottom)
    glNormal3f(0.f, -1.f, 0.f);
    glVertex3f(-1.0f,-1.0f, 1.0f);  // Top Left Of The Quad (Bottom)
    glNormal3f(0.f, -1.f, 0.f);
    glVertex3f(-1.0f,-1.0f,-1.0f);  // Bottom Left Of The Quad (Bottom)
    glNormal3f(0.f, -1.f, 0.f);
    glVertex3f( 1.0f,-1.0f,-1.0f);  // Bottom Right Of The Quad (Bottom)

    glColor3f(1.0f, 0.0f, 0.0f);    // Color Red
    glNormal3f(0.f, 0.f, 1.f);
    glVertex3f( 1.0f, 1.0f, 1.0f);  // Top Right Of The Quad (Front)
    glNormal3f(0.f, 0.f, 1.f);
    glVertex3f(-1.0f, 1.0f, 1.0f);  // Top Left Of The Quad (Front)
    glNormal3f(0.f, 0.f, 1.f);
    glVertex3f(-1.0f,-1.0f, 1.0f);  // Bottom Left Of The Quad (Front)
    glNormal3f(0.f, 0.f, 1.f);
    glVertex3f( 1.0f,-1.0f, 1.0f);  // Bottom Right Of The Quad (Front)

    glColor3f(1.0f, 1.0f, 0.0f);    // Color Yellow
    glNormal3f(0.f, 0.f, -1.f);
    glVertex3f( 1.0f,-1.0f,-1.0f);  // Top Right Of The Quad (Back)
    glNormal3f(0.f, 0.f, -1.f);
    glVertex3f(-1.0f,-1.0f,-1.0f);  // Top Left Of The Quad (Back)
    glNormal3f(0.f, 0.f, -1.f);
    glVertex3f(-1.0f, 1.0f,-1.0f);  // Bottom Left Of The Quad (Back)
    glNormal3f(0.f, 0.f, -1.f);
    glVertex3f( 1.0f, 1.0f,-1.0f);  // Bottom Right Of The Quad (Back)

    glColor3f(0.0f, 0.0f, 1.0f);    // Color Blue
    glNormal3f(-1.f, 0.f, 0.f);
    glVertex3f(-1.0f, 1.0f, 1.0f);  // Top Right Of The Quad (Left)
    glNormal3f(-1.f, 0.f, 0.f);
    glVertex3f(-1.0f, 1.0f,-1.0f);  // Top Left Of The Quad (Left)
    glNormal3f(-1.f, 0.f, 0.f);
    glVertex3f(-1.0f,-1.0f,-1.0f);  // Bottom Left Of The Quad (Left)
    glNormal3f(-1.f, 0.f, 0.f);
    glVertex3f(-1.0f,-1.0f, 1.0f);  // Bottom Right Of The Quad (Left)

    glColor3f(1.0f, 0.0f, 1.0f);    // Color Violet
    glNormal3f(1.f, 0.f, 0.f);
    glVertex3f( 1.0f, 1.0f,-1.0f);  // Top Right Of The Quad (Right)
    glNormal3f(1.f, 0.f, 0.f);
    glVertex3f( 1.0f, 1.0f, 1.0f);  // Top Left Of The Quad (Right)
    glNormal3f(1.f, 0.f, 0.f);
    glVertex3f( 1.0f,-1.0f, 1.0f);  // Bottom Left Of The Quad (Right)
    glNormal3f(1.f, 0.f, 0.f);
    glVertex3f( 1.0f,-1.0f,-1.0f);  // Bottom Right Of The Quad (Right)

    glEnd();          // End Drawing The Cube
}

int main()
{
    sf::RenderWindow window(sf::VideoMode(800, 600), "SFML works!", sf::Style::Default);
    window.setVerticalSyncEnabled(true);
    window.setActive(true);
    window.resetGLStates();

    bool running = true;
    while(running)
    {
        sf::Event event;
        while(window.pollEvent(event))
        {
            switch(event.type)
            {
            case sf::Event::KeyPressed:
                if(event.key.code != sf::Keyboard::Escape) { break; }
            case sf::Event::Closed:
                running = false;
                break;
            default:
                break;
            }
        }

        glClearColor(0.3f, 0.3f, 0.3f, 1.f);
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

        glMatrixMode(GL_PROJECTION);
        glLoadIdentity();
        sf::Vector2u wsize = window.getSize();
        glViewport(0, 0, wsize.x, wsize.y);
        gluPerspective(60, (float)wsize.x / (float)wsize.y, 0.1f, 512.f);

        glMatrixMode(GL_MODELVIEW);
        glLoadIdentity();

        glPushMatrix();
        glTranslatef(0.f, 0.f, -5.f);

        cube();

        glPopMatrix();

        GLenum err = glGetError();

        window.display();
    }

    return 0;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM