I am trying to implement a software in OpenGl which is able to draw a Lorenz system. I achieved my purpose but in a static way: the system is drawn once and that's all. Now I want to move my camera around the system and show the 3D-ness of the system itself. What I noticed is that I cannot update the drawn image because if I do update the points of the system, they keep changing in each update (Lorenz system is the result of mathematical equations, therefore I have big floats number as results). I then realized that I have to draw the system just once and then move the camera around it somehow. Unfortunately I don't know how to do it. I especially have problems in changing that gluLookAt call for my purposes. Let's say that I want to move the camera according to an input given by keyboard. Can you kindly help me? Here you can have a look to my simple code.
Initialization method:
void myinit() {
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
glColor4f(1.0f, 0.0f, 0.0f, 0.09f);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glEnable(GL_POINT_SMOOTH);
glPointSize(1.0f);
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST); // Really Nice Perspective Calculations
glViewport(0, 0, 400, 400); //glViewport(0, 0, width_of_window_rendering_area, height_of_window_rendering area);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45.0f, (GLfloat)400/400, 0.1, 100); //Sets the frustum to perspective mode, sets up the way in which objects
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
Drawing method
void mydisplay() {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); //if perspective
glLoadIdentity();
gluLookAt(0.0, 0.0, 100.0, //position
0.0, 0.0, 0.0, //where we are looking
0.0, 1.0, 0.0); //up vector
glBegin(GL_POINTS);
for (int i = 0; i < iterations; i++) {
if(i == 200000){
glColor4f(1.0f, 0.0f, 0.0f, 0.09f);
}
if(i == 400000){
glColor4f(1.0f, 0.0f, 1.0f, 0.09f);
}
if(i == 600000){
glColor4f(0.0f, 0.0f, 1.0f, 0.09f);
}
if(i == 800000){
glColor4f(0.0f, 1.0f, 1.0f, 0.09f);
}
// compute a new point using the strange attractor equations
float xnew=x + h*(s*(y - x));
float ynew=y + h*(x*(p - z) - y);
float znew=z + h*(x*y - b*z);
x = xnew;
y = ynew;
z = znew;
glVertex3f(x, y, z);
}
glEnd();
glutSwapBuffers();
}
main
int main (int argc, char **argv){
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE);
glutInitWindowSize(400, 400);
glutCreateWindow("Strange Attractors in C++ and OpenGL Tutorial");
glutDisplayFunc(mydisplay);
glutKeyboardFunc(mykey);
myinit();
glutMainLoop();
while(esc != true){
glutDisplayFunc(mydisplay);
}
return 0;
}
This is the result:
Use a timer callback to increment an angle and post a redraw:
It's also a good idea to calculate the point positions/colors ahead of time instead of in the display callback.