涉及我的代码的奇怪故障

时间:2017-08-02 23:50:54

标签: c++ opengl sfml

所以今天,我正在搞乱一些例子,并决定我想制作一个应用程序。所以我试过了。当我单击一次时,屏幕变为黑色,另一次点击则变为白色,并且在出现一些垃圾邮件之后,再次单击会使背景变黑,再次单击,然后变为白色。经过一些垃圾邮件后,街区内还有另一个街区,附上图片。

CODE:

#include "SFML/Graphics.hpp"
#include "SFML/OpenGL.hpp"
#include <GL\GLU.h>
#include <iostream>
#include "cube.cpp"

void createblock() {

    gluPerspective(90.f, 1.f, 1.f, 300.0f);
    glClearColor(0.3f, 0.3f, 0.3f, 0.f);
    glMatrixMode(GL_PROJECTION);
    glEnable(GL_DEPTH_TEST);
    glDepthMask(GL_TRUE);

    glBegin(GL_QUADS);
    glVertex3f(-50.f, -50.f, -50.f);
    glVertex3f(-50.f, 50.f, -50.f);
    glVertex3f(50.f, 50.f, -50.f);
    glVertex3f(50.f, -50.f, -50.f);


    glVertex3f(-50.f, -50.f, 50.f);
    glVertex3f(-50.f, 50.f, 50.f);
    glVertex3f(50.f, 50.f, 50.f);
    glVertex3f(50.f, -50.f, 50.f);

    glColor3f(1, 0, 1);
    glVertex3f(-50.f, -50.f, -50.f);
    glVertex3f(-50.f, 50.f, -50.f);
    glVertex3f(-50.f, 50.f, 50.f);
    glVertex3f(-50.f, -50.f, 50.f);

    glColor3f(0, 1, 0);
    glVertex3f(50.f, -50.f, -50.f);
    glVertex3f(50.f, 50.f, -50.f);
    glVertex3f(50.f, 50.f, 50.f);
    glVertex3f(50.f, -50.f, 50.f);

    glColor3f(1, 1, 0);
    glVertex3f(-50.f, -50.f, 50.f);
    glVertex3f(-50.f, -50.f, -50.f);
    glVertex3f(50.f, -50.f, -50.f);
    glVertex3f(50.f, -50.f, 50.f);

    glColor3f(1, 0, 0);
    glVertex3f(-50.f, 50.f, 50.f);
    glVertex3f(-50.f, 50.f, -50.f);
    glVertex3f(50.f, 50.f, -50.f);
    glVertex3f(50.f, 50.f, 50.f);

    glEnd();
}

sf::RenderWindow window(sf::VideoMode(800, 600), "SimpleBlocks");
sf::Event maine;

int main() {

    while (window.isOpen()) {
        // load defaults here

        while (window.pollEvent(maine)) {
            // events here
            if (maine.type == sf::Event::Closed) {
                window.close();
            } 
            if (maine.type == sf::Event::MouseButtonPressed) { 
                createblock();
                window.display();

                // make block later   
            }
        }
    }
}

图片:

imgur album here

2 个答案:

答案 0 :(得分:1)

在绘制之前,你永远不会调用glClear(GL_COLOR_BUFFER_BIT),所以总是有剩余的片段或深度信息有些重叠。添加明确的通话应该可以解决您的问题。

答案 1 :(得分:0)

glPushMatrix之前致电gluPerspective,在glPopMatrix结束时致电createblock