pBook‎ > ‎OpenGl‎ > ‎

Simple Example

posted Sep 21, 2014, 5:56 AM by Javad Taghia   [ updated Sep 21, 2014, 5:56 AM ]
1. Download OpenGL.zip.
2. Create an empty project.
3. Set the path according to the fig. Lib and Include directory must be set and linker library input as well.
4. Run the following code to draw you a moving dot.

#include <iostream> //dot.cpp
#include <GL/glut.h>

using namespace std;

GLsizei wh = 500 ; // initial height of window
GLsizei ww = 500 ; // initial width of window

int i=9; 
int j=8;
void Displaydot ( void );

void MyInit ( );

void Displaydot () {
glClear ( GL_COLOR_BUFFER_BIT ); //clear pixel buffer
glBegin(GL_POINTS); // render with points
glVertex2i(i,j); //display a point
glEnd();
glFlush();
}


void MyInit ( void ) {
cout << glGetString(GL_VERSION) << endl;
cout << glGetString(GL_VENDOR) << endl;
glClearColor ( 1.0, 1.0, 1.0, 0.0 ); //white background
glColor3f(0.0f, 1.0f,0.0f); // green drawing colour
glPointSize(5.0); // 10 pixel dot!
glMatrixMode ( GL_PROJECTION );
glLoadIdentity ( );
gluOrtho2D ( 0.0, (GLdouble)ww, 0.0, (GLdouble)wh ); //Display area
}
void update(int value) {
i = i +1;
j = j;
wh++;
ww++;
glutPostRedisplay();
glutTimerFunc(250, update, 0);
}

void main(int argc, char **argv) {
glutInit ( &argc, argv );
glutInitDisplayMode ( GLUT_SINGLE | GLUT_RGB );
glutInitWindowSize ( ww, wh ); // window size
glutInitWindowPosition ( 180,180 ); // & position on screen
glutCreateWindow ( "Display a Dot" );
MyInit ();
glutTimerFunc(2, update, 0);
glutDisplayFunc ( Displaydot );
glutMainLoop();

}
ċ
OpenGL.zip
(503k)
Javad Taghia,
Sep 21, 2014, 5:56 AM
ą
Javad Taghia,
Sep 21, 2014, 5:56 AM
Comments