gpt4 book ai didi

c++ - SFML + OpenGL : Unable to draw cube

转载 作者:行者123 更新时间:2023-11-30 01:22:11 25 4
gpt4 key购买 nike

我以前使用过 OpenGL,已经有一段时间了,但我不认为它会这么糟糕。这段代码中缺少一些东西。我只是想看看我正在使用 cube() 函数绘制的立方体。

我做过/尝试过的事情...

  • 在主循环中翻转 + 和 - z 之间的翻译
  • 在主循环之前打开/关闭背面剔除
  • glLoadIdentity() 开头
  • 在开始时设置我的投影
  • 以window.resetGLStates()开头
  • window.setActive(true) 开头
  • 确保我留在 glMatrixMode(GL_MODELVIEW)

我错过了什么?我只能看到我指定的清晰颜色,填满屏幕并 mock 我。

#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
}

最佳答案

这里:

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();

这里:

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);

glMatrixMode(GL_PROJECTION)glMatrixMode(GL_MODELVIEW) 调用之后添加一个 glLoadIdentity()

gluPerspective() 将一个矩阵与当前矩阵相乘。代码似乎假设它覆盖当前矩阵。

gluPerspective() 矩阵乘以自身多次不是有意义的投影矩阵。

当你在那里时,你可以删除调整大小的处理程序:

#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;
}

关于c++ - SFML + OpenGL : Unable to draw cube,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16863454/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com