openGL仅在z轴上渲染一个图层

时间:2017-10-01 10:44:10

标签: c++ opengl 3d z-index sfml

我正在写一部FPS游戏。我正在使用sfml用于窗口,声音等和OpenGL用于3D图形,但我发现了一个问题。 OpenGL仅渲染此z轴上具有0的顶点。我做错了什么?我的结构包含topLefttopRightbottomLeftbottomRight,而且它们是sf::Vector3f

//engine

using namespace DlsE;

DlsE::Engine::Engine()
{
    quad = Quad(
        Vector3x3<float>(
            sf::Vector3f(200 - 200, 200 - 200, 50),
            sf::Vector3f(200 + 200, 200 - 200, 50),
            sf::Vector3f(200 - 200, 200 + 200, -50),
            sf::Vector3f(200 + 200, 200 + 200, -50)
        )
    );
}


DlsE::Engine::~Engine()
{
}

void DlsE::Engine::Draw(sf::RenderWindow * okno)
{
    okno->popGLStates();
    glColor3f(1, 0, 0);

    quad.Draw();
    quad.RotateByDeg(2.f*manager::deltaTime.asSeconds(), 0, 0,quad.MakeCenterContext());

    glFlush();
    okno->pushGLStates();
}

//Quad.cpp

Quad::Quad()
{
}

DlsE::Quad::Quad(Vector3x3<float> p) : p(p)
{
}

Quad::~Quad()
{
}

void DlsE::Quad::Draw()
{
    glBegin(GL_TRIANGLES);
    glVertex3f(p.topLeft.x, p.topLeft.y, p.topLeft.z);
    glVertex3f(p.topRight.x, p.topRight.y, p.topRight.z);
    glVertex3f(p.bottomRight.x, p.bottomRight.y, p.bottomRight.z);

    glVertex3f(p.topLeft.x, p.topLeft.y, p.topLeft.z);
    glVertex3f(p.bottomRight.x, p.bottomRight.y, p.bottomRight.z);
    glVertex3f(p.bottomLeft.x, p.bottomLeft.y, p.bottomLeft.z);

    glEnd();
}

void DlsE::Quad::RotateByDeg(float x, float y, float z, sf::Vector3f context)
{
    p = RotateX(x*PI / 180.f, p, context);
    p = RotateY(y*PI / 180.f, p, context);
    p = RotateZ(z*PI / 180.f, p, context);
}

sf::Vector3f DlsE::Quad::MakeCenterContext()
{
    return sf::Vector3f(
       (p.bottomLeft.x + p.bottomRight.x + p.topLeft.x + p.topRight.x) / 4, 
       (p.bottomLeft.y + p.bottomRight.y + p.topLeft.y + p.topRight.y) / 4, 
       (p.bottomLeft.z + p.bottomRight.z + p.topLeft.z + p.topRight.z) / 4);
}

//Game.cpp

std::string Game::Update(sf::RenderWindow * okno)
{
    return "GAME";
}

void Game::Draw(sf::RenderWindow * okno)
{
    glClearColor(0, 1, 1, 0);
    glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT );
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();

    engine.Draw(okno);
}

Game::Game()
{
}

Game::~Game()
{
}

截图

enter image description here

0 个答案:

没有答案