PDA

View Full Version : Camera Rotation



ncikw
12-12-2011, 05:59 PM
Hi,

I'm just starting to get to grips with camera movement. At the moment I can move the camera forward and backwards. And strafe left or right. I'm doing this by changing the variables in gluLookAt ie:



void display(void)
{
glClear(GL_COLOR_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(Xeye,Yeye,Zeye,XCentre,YCentre,ZCentre,0 .0,1.0, 1.0);




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

case 's':
Zeye += 0.1;
glutPostRedisplay();
break;

case 'w':
Zeye -= 0.1;
glutPostRedisplay();
break;

case 'a':
Xeye -= 0.1;
XCentre -= 0.1;
glutPostRedisplay();
break;

case 'd':
Xeye += 0.1;
XCentre += 0.1;
glutPostRedisplay();
break;


However I'm struggling to figure out how to change the angle at which I'm looking. That is to say imagine the camera was strapped to your head you would remain stationary but turn your head left or right. Really can't figure out how to do this. Could anyone give me some pointers?

Thanks so much.

mobeen
12-12-2011, 10:34 PM
Hi,
However I'm struggling to figure out how to change the angle at which I'm looking.

You are changing the look direction. This can be achieved by changing the camera target (4,5, and 6 parameter of gluLookAt).

ncikw
12-13-2011, 11:32 AM
Thanks for that.

I've gone down another route of solving this. However the way I'm rotating the camera means i can only rotate as far as the window goes. Is there a way to remove the cursor and to rotate left whenever the mouse is moved left.

I really can't think of a way

Here is the entirety of the code:

<div class="ubbcode-block"><div class="ubbcode-header">Click to reveal.. <input type="button" class="form-button" value="Show me!" onclick="toggle_spoiler(this, 'Yikes, my eyes!', 'Show me!')" />]<div style="display: none;">



#include <GL/gl.h>
#include <GL/glu.h>
#include <GL/glut.h>
#include <stdlib.h>
#include <math.h>

//angle of rotation
float xpos = 0, ypos = 0, zpos = 0, xrot = 0, yrot = 0, angle=0.0;

float lastx, lasty;

//positions of the cubes
float positionz[10];
float positionx[10];

void cubepositions (void) { //set the positions of the cubes

for (int i=0;i<10;i++)
{
positionz[i] = rand()%5 + 5;
positionx[i] = rand()%5 + 5;
}
}

//draw crosshair
void crosshair (void)
{
glDisable(GL_LIGHTING);
glColor3f(1,0,0);
glBegin(GL_QUADS);
glVertex3f(0.0,-1.0,0.0);
glColor3f(0,1,0);
glVertex3f(-1000,-1.0,0.0);
glColor3f(0,0,1);
glVertex3f(-1000,-1.0,-1000);
glColor3f(1,1,1);
glVertex3f(0.0,-1.0,-1000);
glEnd();
glEnable(GL_LIGHTING);
}

//draw the cube
void cube (void) {
for (int i=0;i<10;i++)
{
glPushMatrix();
glTranslated(-positionx[i + 1] * 10, 0, -positionz[i + 1] *10); //translate the cube
// GLfloat red[] = {1,0,0,1};
// glMaterialf(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, red);
glutSolidCube(2); //draw the cube
glPopMatrix();
}
}

void init (void) {
cubepositions();
}

void enable (void) {
glEnable (GL_DEPTH_TEST); //enable the depth testing
glEnable (GL_LIGHTING); //enable the lighting
glEnable (GL_LIGHT0); //enable LIGHT0, our Diffuse Light
glShadeModel (GL_SMOOTH); //set the shader to smooth shader

}

void camera (void) {
glRotatef(xrot,1.0,0.0,0.0); //rotate our camera on teh x-axis (left and right)
glRotatef(yrot,0.0,1.0,0.0); //rotate our camera on they-axis (up and down)
glTranslated(-xpos,0,-zpos); //translate the screen to the position of our camera


}

void display (void) {
glClearColor (0.0,0.0,0.0,1.0); //clear the screen toblack
glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); //clear the color buffer and the depth buffer
glLoadIdentity();
camera();
crosshair();
enable();
cube(); //call the cube drawing function
glutSwapBuffers(); //swap the buffers
angle++; //increase the angle
}

void reshape (int w, int h) {
glViewport (0, 0, (GLsizei)w, (GLsizei)h); //set the viewport to the current window specifications
glMatrixMode (GL_PROJECTION); //set the matrix to projection

glLoadIdentity ();
gluPerspective (60, (GLfloat)w / (GLfloat)h, 1.0, 1000.); //set the perspective (angle of sight, width, height, ,depth)
glMatrixMode (GL_MODELVIEW); //set the matrix back to model

}

void keyboard (unsigned char key, int x, int y) {
if (key=='q')
{
xrot += 1;
if (xrot >360) xrot -= 360;
}

if (key=='z')
{
xrot -= 1;
if (xrot < -360) xrot += 360;
}

if (key=='w')
{
float xrotrad, yrotrad;
yrotrad = (yrot / 180 * 3.141592654f);
xrotrad = (xrot / 180 * 3.141592654f);
xpos += float(sin(yrotrad)) ;
zpos -= float(cos(yrotrad)) ;
ypos -= float(sin(xrotrad)) ;
}

if (key=='s')
{
float xrotrad, yrotrad;
yrotrad = (yrot / 180 * 3.141592654f);
xrotrad = (xrot / 180 * 3.141592654f);
xpos -= float(sin(yrotrad));
zpos += float(cos(yrotrad)) ;
ypos += float(sin(xrotrad));
}

if (key=='d')
{
float yrotrad;
yrotrad = (yrot / 180 * 3.141592654f);
xpos += float(cos(yrotrad)) * 0.2;
zpos += float(sin(yrotrad)) * 0.2;
}

if (key=='a')
{
float yrotrad;
yrotrad = (yrot / 180 * 3.141592654f);
xpos -= float(cos(yrotrad)) * 0.2;
zpos -= float(sin(yrotrad)) * 0.2;
}

if (key==27)
{
exit(0);
}
}

void mouseMovement(int x, int y) {
int diffx=x-lastx; //check the difference between thecurrent x and the last x position
int diffy=y-lasty; //check the difference between thecurrent y and the last y position
lastx=x; //set lastx to the current x position
lasty=y; //set lasty to the current y position
xrot += (float) diffy /2; //set the xrot to xrot with the additionof the difference in the y position
if (xrot > 45) xrot=45;
if (xrot<-45) xrot = -45;
yrot += (float) diffx /2; //set the xrot to yrot with the additionof the difference in the x position
}

int main (int argc, char **argv) {
glutInit (&amp;argc, argv);
glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
glutInitWindowSize (500, 500);
glutInitWindowPosition (100, 100);
glutCreateWindow ("Camera Movement");
init ();
glutDisplayFunc (display);
glutIdleFunc (display);
glutReshapeFunc (reshape);

glutPassiveMotionFunc(mouseMovement); //check for mouse movement

glutKeyboardFunc (keyboard);
glutMainLoop ();
return 0;
}

[/QUOTE]</div>