Before I have introduced the tools and programming languages to implement Virtual Reality, apparently OpenGL is one of the most efficient method. Recently I began to learn this programming library under C and C++, and I would like to share my experience with you. Basically my code and programming structure are based on the book "OpenGL Programming Guide" as well as its sample code. As said here is just a study note for me.

Here the first example is to generate a polygon with determined color, the color of background is also changable. When you click the left mouse this polygon will rotate on an axis(x, y or z), with the righi clicking the rotation will be stoped. The head files to be included are:
#include <stdlib.h>
#include <GL/glut.h>
And the main fuction structure looks like this:
int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGB);
glutInitWindowSize (250, 250);
glutInitWindowPosition (100, 100);
glutCreateWindow (argv[0]);
init ();
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutMouseFunc(mouse);
glutMainLoop();
return 0;
}
Let's look at the each method with a short description:
glutInit( ) initializes the GLUT and processes any command line arguments, this is applied when we have some requirements on the input parameter to exchange the information or data;
glutInitDisplayMode( ) specifies whether to use an RGBA or color-index color model, whether to use a single- or double-buffered window as well as whether to use a depth buffer. Here is a detailed introduction;
glutInitWindowPosition( ) and glutInitWindowSize( ) are used to specify the screen location and its size, with the intuitive parameters we are able to use them easily;
glutCreatWindow( ) creates an expected window, until the glutMainLoop( ) is called, this window is able to display;
init( ) is used to initialize the settings of environment, e.g. setting the background color. As image illustrates, the background is set as black (0 0 0), and the shading mode is set as flat, so it can be initialized as:
void init(void)
{
glClearColor (0.0, 0.0, 0.0, 0.0);
glShadeModel (GL_FLAT);
}
Of course the background color can be modified as wish, e.g. red (1, 0, 0). And some other environment settings can be done here, too. So far what we have done is just an initialization work, what we want to do is to draw a picture or animate a video, so here we need a callback function to carry out it.
glutDisplayFunc( void (*func)(void)) sets the "func" function to be displayed in the current window. It is the first and most important callback function. In our example we want to draw a white rectangle in a determined location which is able to rotate on an axis after a triggle. So in our display function it is implemented as follows:
void display(void)
{
glClear(GL_COLOR_BUFFER_BIT);
glPushMatrix();
glRotatef(spin, 0.0, 0.0, 1.0);
glColor3f(1.0, 1.0, 1.0);
glRectf(-25.0, -25.0, 25.0, 25.0);
glPopMatrix();
glutSwapBuffers();
}
and definitely the shape and color of the polynol is able to be modified. The rotated axis is also changable. What we have to do is just modify the values from glRotate3f(), glColor3f() and glRectf(). While the glPushMatrix and glPopMatrix have something to do with the current matrix stack management. Usually in the windows applications the size of the applications is flexible, that is to say we are able to change the size of window by dragging. To enable this function, we need to call the function glutReshapeFunc(). And the detailed rules of the reshape functions can be implemented in an additional method. With the help of glViewport() and glOrtho() the reshape function is carried out.As described below:
void reshape(int w, int h)
{
glViewport (0, 0, (GLsizei) w, (GLsizei) h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-50.0, 50.0, -50.0, 50.0, -1.0, 1.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
In our example the application is also able to react of the mouse movement, so we need to call the glutMouseFunc() callback functions to set the mouse triggle performance. And here what we want is when we click the left button to start the polynon rotation and with the right click to stop it. So the mouse controlling performance is implemented as follows:
void mouse(int button, int state, int x, int y)
{
switch (button) {
case GLUT_LEFT_BUTTON:
if (state == GLUT_DOWN)
glutIdleFunc(spinDisplay);
break;
case GLUT_MIDDLE_BUTTON:
case GLUT_RIGHT_BUTTON:
if (state == GLUT_DOWN)
glutIdleFunc(NULL);
break;
default:
break;
}
}
with the rotation function, in which the rotation speed is able to change(modify the spin = spin + x) as wish.
void spinDisplay(void)
{
spin = spin + 2.0;
if (spin > 360.0)
spin = spin - 360.0;
glutPostRedisplay();
}
And I have made a video to compare the difference with the parameters' change, in the video there are different modifications of diverse parameters to achieve the distinct results.
ontheweg
Posted in: 

0 comments:
Post a Comment