增加相机视图距离

时间:2017-04-23 17:29:14

标签: c++ opengl sfml

我写了一个小程序,只是为了看看OpenGL如何与SFML协同工作:

#include <SFML/Window.hpp>
#include <SFML/OpenGL.hpp>
#include <SFML/Graphics.hpp>
int main(){
    sf::RenderWindow window(sf::VideoMode(700,700),
        "OpenGL test", sf::Style::Default);
    window.setFramerateLimit(60);   

    sf::Clock clock;
    sf::Time time;
    sf::Event event;
    float dt;
    bool run=true;
    glClearColor(50/255.0f, 75/255.0f, 50/255.0f, 1.0f);
    glTranslatef(0.0f, 0.0f, 1.0f);
    while(run){
        while(window.pollEvent(event))
            if(event.type==sf::Event::Closed)run=false;
        glRotatef(1.0f, 1.0f, 1.0f, -1.0f);
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
        glBegin(GL_LINE_STRIP);
        glVertex3f(-0.5f, -0.5f, 0.0f);
        glVertex3f(-0.5f, 0.5f, 0.0f);
        glVertex3f(0.5f, 0.5f, 0.0f);
        glVertex3f(0.5f, -0.5f, 0.0f);
        glVertex3f(-0.5f, -0.5f, 0.0f);
        glVertex3f(0.0f, 0.0f, -0.5f);
        glVertex3f(0.5f, 0.5f, 0.0f);
        glEnd();

        window.display();
    }
    return 0;
}

但是当我运行它时,部分对象消失了:

enter image description here

有没有办法增加观看距离?

2 个答案:

答案 0 :(得分:1)

只需在最后一次glVertex调用之前更改一个,来自: glVertex3f(0.0f,0.0f,-0.5f); 至 glVertex3f(0.0f,0.0f,0.0f);

然后,拜托,请阅读:

https://www.sfml-dev.org/tutorials/2.4/window-opengl.php

答案 1 :(得分:1)

OpenGL假设从-1到1的任何维度都有最大值,因此您需要相对于该值标准化您的值。

ex你有一个从0,0,-2.0到0,0,-32.3的线段:(假设-z远离相机) 你必须按比例缩放每个向量:

p1 := {0, 0, -2.0}
p2 := {0, 0, -32.3}
p2len := length(p2) //In this case just 32.3
p1 := {p1.x / p2len, p1.y / p2len, p1.z / p2len} // {0, 0, -0.0619}
p2 := {p2.x / p2len, p2.y / p2len, p2.z / p2len} // {0, 0, -1}