PDA

View Full Version : Problem with Camera ?



Ngoc Sang
05-11-2008, 10:53 AM
This is my code :


#include <GL\glut.h>
#include <stdlib.h>
#include <iostream.h>
#include <math.h>
void Init();
void Display();
void Reshape(int Width,int Height);
void Keyboard(unsigned char Key,int X,int Y);
void time(int value);


// Viewer
float x = 1.0,y=3.0,z=20.0;
float lx = 0.0, ly = 0.0,lz = 0.0;
float angle=0.0,deltaAngle = 0.0;
int deltaMove = 0;



void orientMe(float ang) {


lx = sin(ang);
lz = -cos(ang);
glLoadIdentity();
gluLookAt(x, y, z,
x + lx,y + ly,z + lz,
0.0f,1.0f,0.0f);
}


void moveMeFlat(int i) {
x = x + i*(lx)*0.1;
z = z + i*(lz)*0.1;
glLoadIdentity();
gluLookAt(x, y, z,
x + lx,y + ly,z + lz,
0.0f,1.0f,0.0f);
}



void pressKey(int key, int x, int y) {

switch (key) {
case GLUT_KEY_LEFT :
deltaAngle = -0.01f;break;
case GLUT_KEY_RIGHT :
deltaAngle = 0.01f;break;
case GLUT_KEY_UP :
deltaMove = 1;break;
case GLUT_KEY_DOWN :
deltaMove = -1;break;
}
}

void releaseKey(int key, int x, int y) {

switch (key) {
case GLUT_KEY_LEFT :
case GLUT_KEY_RIGHT :
deltaAngle = 0.0f;break;
case GLUT_KEY_UP :
case GLUT_KEY_DOWN :
deltaMove = 0;break;
}
}


void setPosViewer()
{
if (deltaMove)
moveMeFlat(deltaMove);
if (deltaAngle) {
angle += deltaAngle;
orientMe(angle);
}

}
void main(int Argc,char** Argv)
{
glutInit(&amp;Argc,Argv);
glutInitDisplayMode(GLUT_DOUBLE| GLUT_RGB);
glutInitWindowSize(500,500);
glutInitWindowPosition(300,200);
glutCreateWindow("Camera Lesson");
Init();

glutDisplayFunc(Display);
glutReshapeFunc(Reshape);

glutIgnoreKeyRepeat(1);
glutSpecialFunc(pressKey);
glutSpecialUpFunc(releaseKey);

glutMainLoop();


}

void Init()
{
glClearColor(0.0,0.0,0.0,0.0);
}
void Display()
{
setPosViewer();

glClearColor(0.0,0.0,0.0,0.0);
glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

glutWireCube(3);

glutSwapBuffers();

}
void Reshape(int Width,int Height)
{
glViewport(0,0,(GLsizei)Width,(GLsizei)Height);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();

gluPerspective(30,1.0,1.0,51.0);
gluLookAt(x,y,z,lx,ly,lz,0.0,1.0,0.0);



}



It don't work well.
I set camery at x,y,z
And I draw a obj ( Cube )
Then I control arrow key to change camera position and loking at point.

What's my error ?