Camera converging towards origin instead of line of sight

Started by
4 comments, last by ivincent 13 years, 8 months ago
I have a simple camera that I'm writing with an teapot at the origin. Whenever I move the camera goes to the origin instead of the line of sight when the camera is looking:


#include <stdlib.h>
#include <iostream>
#include <GL/glut.h>
#include <math.h>
#include "Vector3d.h"

#define PI 3.14159265

Vector3D camera(0.0, 0.0, 10.0);
Vector3D los (0.0,0.0,-1.0);
GLfloat heading_angle = 0.0, roll_angle = 0.0, pitch_angle = 0.0;
GLfloat old_x = 0.0, old_y = 0.0, old_z = 0.0;

void init()
{
glEnable(GL_DEPTH_TEST);
glClearColor(0.0, 0.0, 0.0, 1.0);
glShadeModel(GL_SMOOTH );
}

void drawGround()
{
glColor3f(0.9f, 0.9f, 0.9f);

glBegin(GL_QUADS);
glVertex3f(-100.0f, 0.0f, -100.0f);
glVertex3f(-100.0f, 0.0f, 100.0f);
glVertex3f( 100.0f, 0.0f, 100.0f);
glVertex3f( 100.0f, 0.0f, -100.0f);
glEnd();

}

void orientCamera()
{
glLoadIdentity();

glRotatef(heading_angle,0.0,1.0,0.0);
glRotatef(pitch_angle,1.0,0.0,0.0);
glTranslatef(-camera.x,-camera.y,-camera.z);

}

void display()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

orientCamera();
glPushMatrix();
glTranslatef(0.0,-1.0,0.0);
drawGround();
glPopMatrix();

glPushMatrix();
glTranslatef(0.0,1.0,0.0);
glColor3f(1,1,1);
glutWireTeapot(2);
glPopMatrix();



glutSwapBuffers();
}

void reshape(int w, int h)
{
if(h <= 0)
h = 1;

glMatrixMode(GL_PROJECTION);
glLoadIdentity();

glViewport(0, 0, (GLsizei) w, (GLsizei) h);
gluPerspective(45,w/h,1,1000);

gluLookAt(camera.x,camera.y,camera.z,
los.x,los.y,los.z,
0.0,1.0,0.0);

glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}

void mouse(int x, int y)
{
GLfloat window_size = glutGet(GLUT_WINDOW_WIDTH);
GLfloat new_x = 0.0, new_y = 0.0, new_z = 0.0;
Vector3D new_los;

new_x = x - glutGet(GLUT_WINDOW_WIDTH)/2;
new_y = glutGet(GLUT_WINDOW_HEIGHT)/2 - y;

if(new_x > old_x)
heading_angle+=.5;
else if (new_x < old_x)
heading_angle-=.5;

if(new_y > old_y)
pitch_angle-=.5;
else if(new_y < old_y)
pitch_angle+=.5;


old_x = new_x;
old_y = new_y;

if(heading_angle > 360.0)
heading_angle = 0.0;
if(pitch_angle > 360.0)
pitch_angle = 0.0;

los.x = sin(heading_angle/(180*PI));
los.y = sin(pitch_angle/(180*PI));
los.z = -cos(heading_angle/(180*PI));

}

void keyboard(unsigned char key,int x, int y)
{

bool valid_key = true;

switch(key){
case 'w': //FORWARD
camera = camera + los;

break;
case 's': //BACKWARD
/*camera.x -= los.x;
camera.z -= los.z;*/
camera = camera - los;
break;
case 'a': //STRAFE LEFT
//camera.x -= v.x;
break;
case 'd': //STRAFE RIGHT
//camera.x += v.x;
break;
default:
valid_key = false;
}
}

int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH );
glutInitWindowSize(500, 500);
glutInitWindowPosition(100, 100);
glutCreateWindow(argv[0]);
init();
glutDisplayFunc(display);
glutIdleFunc(display);
glutReshapeFunc(reshape);
glutKeyboardFunc(keyboard);
glutPassiveMotionFunc(mouse);
glutSetCursor(GLUT_CURSOR_NONE);

glutMainLoop();
return 0;
}
Advertisement
You should probably spend some time with your debugger and then come back with a more specific question.

Put a breakpoint here: camera = camera + los;

and then look to see what is happening. Look at the values of 'camera' before and after the addition and 'los', compare them to what you would expect them to be. You should be able to track down why you aren't getting what you expected.
[size=2]My Projects:
[size=2]Portfolio Map for Android - Free Visual Portfolio Tracker
[size=2]Electron Flux for Android - Free Puzzle/Logic Game
I think the problem is this:

gluLookAt(camera.x,camera.y,camera.z,los.x,los.y,los.z,0.0,1.0,0.0);


It looks like los is a unit vector that is your direction.
But gluLookAt wants the absolute position of what you are pointing at, so that is your camera position plus this los vector:

gluLookAt(camera.x,camera.y,camera.z,camera.x + los.x, camera.y + los.y, camera.z + los.z,0.0,1.0,0.0);


Because if you think about it and you are "looking at" los, a unit vector, it's always going to be very close to the origin. What you really want to be looking at is unit vector relative to your position.
Quote:Original post by PrestoChung
I think the problem is this:

gluLookAt(camera.x,camera.y,camera.z,los.x,los.y,los.z,0.0,1.0,0.0);


It looks like los is a unit vector that is your direction.
But gluLookAt wants the absolute position of what you are pointing at, so that is your camera position plus this los vector:

gluLookAt(camera.x,camera.y,camera.z,camera.x + los.x, camera.y + los.y, camera.z + los.z,0.0,1.0,0.0);


Because if you think about it and you are "looking at" los, a unit vector, it's always going to be very close to the origin. What you really want to be looking at is unit vector relative to your position.




This is likely correct. I will try it out tomorrow and see.

That didn't seem to help. The line of sight vector seems to be correct, and I can look around just fine. However the camera only moves towards the origin no matter which way I'm facing.
Quote:Original post by ivincent

los.x = sin(heading_angle/(180*PI));
los.y = sin(pitch_angle/(180*PI));
los.z = -cos(heading_angle/(180*PI));




I figured it out. The problem is with the way I'm converting degrees to radians. It should be:


los.x = sin(heading_angle*PI/180);
los.y = sin(pitch_angle*PI/180);
los.z = -cos(heading_angle*PI/180);

This topic is closed to new replies.

Advertisement