PDA

View Full Version : Perspective problems, FoV and movement



Zawarudo
05-27-2015, 07:51 PM
I've been trying to follow various perspective tutorials on the internet.

One problem I've noticed is that, depending on what I set my FoV to, it affects whether pressing A makes me strafe left vs strafe right (it should be left, D causes right strafe).
For example,
if I set FoV = 47.0f; it causes my left and right movement direction be reverse (bad).
if I set my FoV = 48.0f; it causes correct movement left and right.

(Forward/back movement is correct no matter what the FoV is).

Anyone have a clue as to what's going on?

Also a less-major concern, people suggest a FoV of 60 degrees, but this seems to make my 1x1x1 cube waaay too stretched:
http://i.cubeupload.com/vclEli.png

Relevant code:

int main()
{
const int Width = 1600/2;
const int Height = 900/2;

Shader shader("mvp");

GraphicArray cube = make_rectangle(1, 1, 1, Vertex(-1, -1, -1));
cube.send_to_GPU();

glm::vec3 sight_direction(0, 0, 1);

auto winsize = window.getSize();
float FoV = 49.0f;

glm::mat4 Projection = glm::perspective(
FoV, // The horizontal Field of View, in degrees
static_cast<float>(winsize.x) / winsize.y, // Aspect Ratio.
0.1f, // Near clipping plane.
100.0f // Far clipping plane.
);
glm::vec3 cameraPosition(0, 0, 0);
glm::vec3 upVector(0, 1, 0); //upVector(0, 1, 0);
glm::mat4 View = glm::lookAt(
cameraPosition, // the position of your camera, in world space
cameraPosition + sight_direction, // where you want to look at, in world space
upVector // +y = up
);
glm::mat4 Model = glm::mat4(1.0f);

glm::mat4 MVP = Projection * View * Model;
GLuint MatrixID = glGetUniformLocation(shader.getProgram(), "MVP");

bool movement_forward = false;
bool movement_backward = false;
bool movement_left = false;
bool movement_right = false;

glEnable(GL_DEPTH_TEST);
glClearColor(0.0, 0.0, 0.2, 1.0);
while(window.isOpen())
{
// Handle events for player movement (WASD keys)
// ...

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glViewport(0, 0, window.getSize().x, window.getSize().y);
shader.bind();

// W/A movement:
if (movement_forward)
cameraPosition += sight_direction / 100.f;
if (movement_backward)
cameraPosition -= sight_direction / 100.f;

// A/D Strafing:
if (movement_left)
cameraPosition += glm::normalize(glm::cross(sight_direction, upVector)) / 100.f;
if (movement_right)
cameraPosition -= glm::normalize(glm::cross(sight_direction, upVector)) / 100.f;


sight_direction = glm::vec3(0, 0, 1);
View = glm::lookAt(
cameraPosition,
cameraPosition + sight_direction,
upVector);

MVP = Projection * View * Model;
glUniformMatrix4fv(MatrixID, 1, GL_FALSE, &MVP[0][0]);

// Render:
cube.draw();

// Display:
glFlush();
window.display();
}
}

Shader:


#version 430 core

in vec3 in_Position;
in vec3 in_Color;

uniform mat4 MVP;

out vec3 pass_Color;

void main(void)
{
vec4 v = vec4(in_Position, 1);
gl_Position = MVP * v;
pass_Color = in_Color;
}

GClements
05-28-2015, 04:29 AM
My first guess is that the angle is being interpreted as radians rather than degrees.

Earlier versions of GLM used degrees for some functions (typically those which mimic an OpenGL or GLU function) and radians for others. Later versions always use radians; any angles which are in degrees must be explicitly converted via e.g. glm::radians().

Zawarudo
05-28-2015, 09:42 AM
Yes, that seems to work so far! I changed FoV to be Pi / 3, and it seems to look okay so far. Also, increasing the y axis actually makes me go up instead of down now, I knew something was wrong with my orientation.

Now I just need to focus on the theta/phi movement for polar coordinates and my perspective should be good, thank you!