Hi all , today I like to share some code example about a opengl animation with you.
#include <stdio.h>
#include <stdlib.h>
#include <GL/gl.h>
#include <GL/glut.h>
static double s_line_length = 0.5;
void display_function(void)
{
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(0.0 ,1.0,0.0);
glBegin (GL_LINES);
glVertex3f (0.0 ,s_line_length , 0.0);
glVertex3f (s_line_length, 0.0 ,0.0);
glColor3f(0.0,0.0,1.0);
glVertex3f( 0.0 , -s_line_length , 0.0);
glVertex3f(-s_line_length , 0.0 , 0.0);
glEnd();
glFlush();
}
void keyboard_function( unsigned char ch , int x , int y)
{
// exit (0);
}
void timer_function( int value)
{
static bool flag = true ;
static int count = 5;
if ( count == 10 )
{
flag=false;
}
if (count == 0){
flag=true;
}
if (flag )
{
s_line_length += 0.1;
count++;
}
else
{
s_line_length -= 0.1;
count--;
}
glutPostRedisplay();
glutTimerFunc( 200,timer_function , 0);
}
void mouse_func ( int button , int status , int x , int y)
{
}
int main ( int argc , char ** argv)
{
glutInit( &argc , argv);
glutInitDisplayMode(GLUT_SINGLE |GLUT_RGB);
glutInitWindowSize( 250,250);
glutInitWindowPosition(100,100);
glutCreateWindow("Line Animation");
glutKeyboardFunc( keyboard_function);
glutDisplayFunc ( display_function);
glutTimerFunc (1, timer_function,0);
glutMouseFunc( mouse_func);
glutMainLoop();
}
and to compile this , Just hit these commands.
$gcc -o ch04 ch04.cc -lglut -g
Assuming that you had already installed the GNU “C” compiler and you type and saved the source file as ch04.cc in your source code directory.

And here’s how it’s running , BINGO !
Oky let’s enjoy this in the windows plactform.
in the windows you need that dirty #define GLUT_DISABLE_ATEXIT_HACK ,
and I saved the library files from glut and opengl unzipped to C:\mingw\lib
those files named as GLUT32.lib and OPENGL32.lib and you should save the
GLUT32.dll file to your source directory.
#include <stdio.h>
#include <stdlib.h>
#define GLUT_DISABLE_ATEXIT_HACK
#include <GL/gl.h>
#include <GL/glut.h>
static double s_line_length = 0.5;
void display_function(void)
{
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(0.0 ,1.0,0.0);
glBegin (GL_LINES);
glVertex3f (0.0 ,s_line_length , 0.0);
glVertex3f (s_line_length, 0.0 ,0.0);
glColor3f(0.0,0.0,1.0);
glVertex3f( 0.0 , -s_line_length , 0.0);
glVertex3f(-s_line_length , 0.0 , 0.0);
glEnd();
glFlush();
}
void keyboard_function( unsigned char ch , int x , int y)
{
exit (0);
}
void timer_function( int value)
{
static bool flag = true ;
static int count = 5;
if ( count == 10 )
{
flag=false;
}
if (count == 0){
flag=true;
}
if (flag )
{
s_line_length += 0.1;
count++;
}
else
{
s_line_length -= 0.1;
count--;
}
glutPostRedisplay();
glutTimerFunc( 200,timer_function , 0);
}
void mouse_func ( int button , int status , int x , int y)
{
}
int main ( int argc , char ** argv)
{
glutInit( &argc , argv);
glutInitDisplayMode(GLUT_SINGLE |GLUT_RGB);
glutInitWindowSize( 250,250);
glutInitWindowPosition(100,100);
glutCreateWindow("Line Animation -Press any key to Exit");
glutKeyboardFunc( keyboard_function);
glutDisplayFunc ( display_function);
glutTimerFunc (1, timer_function,0);
glutMouseFunc( mouse_func);
glutMainLoop();
}
And to compile it and run,
J:\EXERCISES\opengl\glut\ch04>gcc -o ch04.exe ch04_.cc -mwindows -lGLUT32 -lOPEN GL32 J:\EXERCISES\opengl\glut\ch04> ch04.exe
Here you can download the sources and executables,
http://rapidshare.com/files/364072990/ch04.zip.html
–Enjoy And Happy Coding–