PDA

View Full Version : Using gluLookAt() for animation



rohitbapat
06-12-2010, 10:07 PM
Hi , I am trying to use gluLookAt() function for run time animation for that I take coordinates from a text file and pass those coordinates to gluLookAt(). But somehow my is not working. I have recently started working on OpenGL and GLUT .

When I run my code it gives a transparent window in the beginning and then shows the cube. I want to move the camera to give effect as if object is moving. I am posting my code ...
Can anybody help me?

Thank you
Rohit
__________________________________________________ _________

#include <math.h>
#include <GL/glut.h>
#include <iostream>
#include <fstream>
#include <stdlib.h>
#include <time.h>
#include <dos.h>
#include <windows.h>
using namespace std;

float angle=0.0,deltaAngle = 0.0,ratio;
float x=0.0f,y=1.75f,z=5.0f;
float lx=0.0f,ly=0.0f,lz=-1.0f;
GLint snowman_display_list;
int deltaMove = 0;
float a = 0.0f;
int b = 0;

void changeSize(int w, int h)
{

// Prevent a divide by zero, when window is too short
// (you cant make a window of zero width).
if(h == 0)
h = 1;

ratio = 1.0f * w / h;
// Reset the coordinate system before modifying
glMatrixMode(GL_PROJECTION);
glLoadIdentity();

// Set the viewport to be the entire window
glViewport(0, 0, w, h);

// Set the clipping volume
gluPerspective(45,ratio,1,1000);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
void renderScene(void)
{


FILE *file;
float numbers[3];
float rx,ry,rz;
int i;

file = fopen("C:\\Users\\RoBa\\Desktop\\test.txt", "r");

while(feof(file)== NULL)
{ //b = 0;
fscanf(file, "%f %f %f ", &amp;numbers[B],&amp;numbers[b+1], &amp;numbers[b+2]);

rx = (numbers[B])/10;
ry = (numbers[b+1])/10;
rz = (numbers[b+2])/10;
cout<<rx<<endl;
cout<<ry<<endl;
cout<<rz<<endl;
glLoadIdentity();
gluLookAt(0.0f , 0.0f , -(10.0*rz) , 0.0f,0.0f,0.f,0.0f,1.0f,0.0f);
glFlush();


}
fclose(file);
cout<<rx<<endl;
cout<<ry<<endl;
cout<<rz<<endl;



glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glutWireCube(2.0f);
glutPostRedisplay();
glutSwapBuffers();
}


int main(int argc, char **argv)
{
glutInit(&amp;argc, argv);
glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGBA);
glutInitWindowPosition(100,100);
glutInitWindowSize(640,360);
glutCreateWindow("My animation");
glutDisplayFunc(renderScene);
glutReshapeFunc(changeSize);

glutMainLoop();

return(0);
}
__________________________________________________ ___________

GL Starter
06-13-2010, 05:56 AM
Hi Rohith,

To create an animation feeling you need to change the camera position. I think your code always set camera position to the last x,y,z available in your file. ie always you look from same position.

Pls change your file reading logic.

I removed your file reading logic and changed camera position with a static value.

Pls check with the modified renderScene function.


void renderScene(void)
{
FILE *file;
float numbers[3];
float rx,ry,rz;
int i;
// THis is used to set Z position.
static float nZ = -10.0;
glLoadIdentity();
gluLookAt(0.0f , 0.0f , nZ, 0.0f,0.0f,0.f,0.0f,1.0f,0.0f);
glFlush();

// Next time Z position changes.
nZ -= 0.125;
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glutWireCube(2.0f);
glutPostRedisplay();
glutSwapBuffers();
}

rohitbapat
06-13-2010, 02:06 PM
Thank you for your reply GL_starter but I want to use gluLookat()for dynamic positioning . I take dynamic changing coordinates from text file ..

So , I must take values from text file.. Any suggestions regarding my reading logic ?

Thank you
Rohit

GL Starter
06-14-2010, 12:30 AM
Pls use fseek() before reading x,y,z value from file.

rohitbapat
06-14-2010, 10:06 AM
Thank you again ... I will study and use fseek() and will let you know the proceedings.

Cheers
Rohit

rohitbapat
06-14-2010, 03:22 PM
Well GL_starter I tried using fseek , but that is not what I really want to do.

According to my understanding the big thing is whole display is always in a loop and I am implementing another loop in display function. I think this might create an issue. Like for example when I use same file reading logic and pass it to glTranslatef(x,y,z) and then draw cube glSolidCube(5.0f) in that loop cube is drawn like almost forever ... so it might happen that glLookAt is not responding to those changes and loops. Somehow it has become a complicated thing...

I was thinking if it is possible to create object at a point then destroy it after some time then redraw it at next possition.

I searched for that but I don't think there is any utility that does that (destroying :P ) ...

Well thank you for your help. If you get any solution let me know.

GL Starter
06-14-2010, 08:59 PM
glLoadIdentity();
gluLookAt(0.0f , 0.0f , -(10.0*rz) , 0.0f,0.0f,0.f,0.0f,1.0f,0.0f);

glLoadIdentity() will reset the current matrix with the identity matrix.

In above code each time you reset current matrix and load ur gluLookAt values. Therefore each looping reset matrix state of previous iteration, and matrix state of last iteration will be the output of this loop.