Roy Varon
Roy Varon

Reputation: 608

Increase camera view distance

I wrote a small program just to see how OpenGL works with 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;
}

But when I run it, part of the object disappears:

enter image description here

Is there a way to increase the viewing distance?

Upvotes: 1

Views: 529

Answers (2)

TinfoilPancakes
TinfoilPancakes

Reputation: 248

OpenGL assumes a max in any dimension from -1 to 1, so you need to normalize your values relative to that.

ex you have a line segment from 0,0,-2.0 to 0,0,-32.3: (assuming -z away from camera) you would have to scale each vector to fit accordingly:

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}

Upvotes: 1

LeDYoM
LeDYoM

Reputation: 980

Just change the one before the last glVertex call, from: glVertex3f(0.0f, 0.0f, -0.5f); to glVertex3f(0.0f, 0.0f, 0.0f);

and then, please, please, read this:

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

Upvotes: 1

Related Questions