GLUT and OpenGL
by Robby T. Tan1. GLUT window init
#include <GL/glut.h> int main(int argc, char **argv) { // #1: GLUT window initialization glutInit(&argc, argv); glutInitDisplayMode(GLUT_RGB|GLUT_DOUBLE); glutInitWindowSize(600, 600); glutInitWindowPosition(0, 0); glutCreateWindow("Window's Title"); // #2: Registering callbacks // #3: GLUT main loop glutMainLoop(); return 0; }
2. Reshape + Display in 2D
#include <GL/glut.h> void reshape(int width, int height); void display(); int main(int argc, char **argv) { // #1: GLUT window initialization glutInit(&argc, argv); glutInitDisplayMode(GLUT_RGB|GLUT_DOUBLE); glutInitWindowSize(600, 600); glutInitWindowPosition(0, 0); glutCreateWindow("Window's Title"); //glutFullScreen(); //optional // #2: Registering callbacks glutReshapeFunc(reshape); glutDisplayFunc(display); // #3: GLUT main loop glutMainLoop(); return 0; } void reshape( int w, int h) { glViewport(0, 0, w, h); glMatrixMode(GL_PROJECTION); glLoadIdentity(); glOrtho( 0, w, 0, h , -1.0, 1.0 ); // the origin is at the bottom left!! glMatrixMode(GL_MODELVIEW); } void display() { glClear( GL_COLOR_BUFFER_BIT); // to draw the 2D objects glutSwapBuffers(); }
3. Draw 2D point
#include <GL/glut.h> void reshape(int width, int height); void display(); void draw_point(int x, int y); int main(int argc, char **argv) { // #1: GLUT window initialization glutInit(&argc, argv); glutInitDisplayMode(GLUT_RGB|GLUT_DOUBLE); glutInitWindowSize(600, 600); glutInitWindowPosition(0, 0); glutCreateWindow("Window's Title"); //glutFullScreen(); //optional // #2: Registering callbacks glutReshapeFunc(reshape); glutDisplayFunc(display); // #3: GLUT main loop glutMainLoop(); return 0; } void reshape( int w, int h) { glViewport(0, 0, w, h); glMatrixMode(GL_PROJECTION); glLoadIdentity(); glOrtho( 0, w, 0, h , -1.0, 1.0 ); // the origin is at the bottom left!! glMatrixMode(GL_MODELVIEW); } void display() { glClear( GL_COLOR_BUFFER_BIT); // to draw a point draw_point(300,300); glutSwapBuffers(); } void draw_point( int x, int y ) { // to set the color of the point GLfloat RED[]= { 1.0, 0.0, 0.0 }; glColor3fv(RED); // to set the size of the point (or square, precisely) int point_size = 10; glPointSize(point_size); // to draw the point glBegin( GL_POINTS ); glVertex2f( x,y ); glEnd(); }
4. Draw 2D line
#include <GL/glut.h> void reshape(int width, int height); void display(); void draw_closed_curve(int x, int y); void draw_open_curve(int x, int y); int main(int argc, char **argv) { // #1: GLUT window initialization glutInit(&argc, argv); glutInitDisplayMode(GLUT_RGB|GLUT_DOUBLE); glutInitWindowSize(600, 600); glutInitWindowPosition(0, 0); glutCreateWindow("Window's Title"); //glutFullScreen(); //optional // #2: Registering callbacks glutReshapeFunc(reshape); glutDisplayFunc(display); // #3: GLUT main loop glutMainLoop(); return 0; } void reshape( int w, int h) { glViewport(0, 0, w, h); glMatrixMode(GL_PROJECTION); glLoadIdentity(); glOrtho( 0, w, 0, h , -1.0, 1.0 ); // the origin is at the bottom left!! glMatrixMode(GL_MODELVIEW); } void display() { glClear( GL_COLOR_BUFFER_BIT); // to draw lines draw_open_curve(400,400); draw_closed_curve(200,200); glutSwapBuffers(); } void draw_open_curve(int x, int y) { // set the color of the line: GLfloat GREEN[]= { 0.0, 1.0, 0.0 }; glColor3fv(GREEN); int SQUARE_SIZE = 50; // draw an open curve: glBegin(GL_LINE_STRIP); glVertex2f( x - SQUARE_SIZE, y - SQUARE_SIZE); glVertex2f( x + SQUARE_SIZE, y - SQUARE_SIZE); glVertex2f( x + SQUARE_SIZE, y + SQUARE_SIZE); glVertex2f( x - SQUARE_SIZE, y + SQUARE_SIZE); glEnd( ); } void draw_closed_curve(int x, int y) { // set the color of the line: GLfloat GREEN[]= { 0.0, 1.0, 0.0 }; glColor3fv(GREEN); int SQUARE_SIZE = 50; // draw a closed curve: glBegin(GL_LINE_LOOP); glVertex2f( x - SQUARE_SIZE, y - SQUARE_SIZE); glVertex2f( x + SQUARE_SIZE, y - SQUARE_SIZE); glVertex2f( x + SQUARE_SIZE, y + SQUARE_SIZE); glVertex2f( x - SQUARE_SIZE, y + SQUARE_SIZE); glEnd( ); }
5. Mouse
Note: pay attention to the reshape function: we change the origin position due to the coordinate system used by the mouse.
#include <vector> #include <GL/glut.h> using namespace std; #define POINT_SIZE 20 // Declaring the functions: void reshape(int width, int height); void display(); void mouseClick(int button, int state, int x, int y); void mouseMotion(int x, int y); void draw_point(int x, int y); void draw_all_points(); void modify_point(int j, int x, int y); void add_point(int x, int y); int doesPointExist(int x, int y); // Declaring the global data structure struct s_point{ float x, y; }; // Setting the global variables: int m_modify; vector<s_point> m_points; GLfloat RED[] = { 1.0, 0.0, 0.0 }; int main(int argc, char **argv) { // #1: GLUT window initialization glutInit(&argc, argv); glutInitDisplayMode(GLUT_RGB|GLUT_DOUBLE); glutInitWindowSize(600, 600); glutInitWindowPosition(0, 0); glutCreateWindow("Window's Title"); //glutFullScreen(); //optional // #2: Registering callbacks glutReshapeFunc(reshape); glutDisplayFunc(display); glutMouseFunc(mouseClick); // mouse click input glutMotionFunc(mouseMotion); // mouse motion input // #3: GLUT main loop glutMainLoop(); return 0; } void reshape(int w, int h) { glViewport(0, 0, w, h); glMatrixMode(GL_PROJECTION); glLoadIdentity(); // glOrtho( 0, w, 0, h , -1.0, 1.0 ); // the origin is at the bottom left!! // below, we change the origin to the top left, // since the mouse motion is based on the top-left origin glOrtho( 0, w, h, 0 , -1.0, 1.0 ); glMatrixMode(GL_MODELVIEW); } void display() { glClear( GL_COLOR_BUFFER_BIT); // to draw points draw_all_points(); glutSwapBuffers(); } void mouseClick(int button, int state, int x, int y) { m_modify = -1; if( button == GLUT_LEFT_BUTTON && state == GLUT_DOWN ){ // to check if the point exists: int j = doesPointExist(x,y); if(j<0) add_point(x,y); else m_modify = j; } glutPostRedisplay(); } void mouseMotion(int x, int y) { if(m_modify>=0) modify_point(m_modify, x, y); glutPostRedisplay(); } int doesPointExist(int x, int y) { for(int i = 0 ; i < m_points.size();i++){ if( (x > m_points.at(i).x - POINT_SIZE) && (x < m_points.at(i).x + POINT_SIZE) && (y > m_points.at(i).y - POINT_SIZE) && (y < m_points.at(i).y + POINT_SIZE)) return i; } return -1; } void draw_point(int x, int y) { glColor3fv(RED); glPointSize(POINT_SIZE); glBegin(GL_POINTS); glVertex2f(x,y); glEnd(); } void draw_all_points() { for(int i = 0; i< m_points.size(); i++){ draw_point(m_points.at(i).x, m_points.at(i).y); } } void modify_point(int j, int x, int y) { m_points.at(j).x = x; m_points.at(j).y = y; } void add_point(int x, int y) { s_point np; np.x = x; np.y = y; m_points.push_back(np); }
6. Keyboard
#include <GL/glut.h> // Declaring the functions: void reshape(int width, int height); void display(); void keyboard(unsigned char key, int x, int y); // Declaring the global variables: int m_glwindow; int main(int argc, char **argv) { // #1: GLUT window initialization glutInit(&argc, argv); glutInitDisplayMode(GLUT_RGB|GLUT_DOUBLE); glutInitWindowSize(600, 600); glutInitWindowPosition(0, 0); m_glwindow = glutCreateWindow("Window's Title"); //glutFullScreen(); //optional // #2: Registering callbacks glutReshapeFunc(reshape); glutDisplayFunc(display); glutKeyboardFunc(keyboard); // #3: GLUT main loop glutMainLoop(); return 0; } void reshape(int w, int h) { glViewport(0, 0, w, h); glMatrixMode(GL_PROJECTION); glLoadIdentity(); glOrtho( 0, w, 0, h , -1.0, 1.0 ); // the origin is at the bottom left!! glMatrixMode(GL_MODELVIEW); } void display() { glClear( GL_COLOR_BUFFER_BIT); // to draw points glutSwapBuffers(); } void keyboard(unsigned char key, int x, int y) { if (key == 27 || key == 'q'){ // 27 = esc glutDestroyWindow(m_glwindow); exit(0); } }
7. Reshape + Display in 3D
gluLookAt(camx, camy, camz, posx, posy, posz,ux, uy, uz):

- [camx, camy, camz] = the position of the camera or eye.
- [posx, posy, posz] = the position of the target point.
- [ux, uy, uz] = the camera up vector. It basically allows the camera to be tilted or rotated even though it keeps its position and its gazing direction. Imagine if we look at a point and then tilt our head. The up vector can define this tilting condition. Normally, it is set to [0,0,1] if the z-axis is the vertical axis (as you do not often tilt your head). Or, [0,1,0] if the y-axis is the vertical axis.
gluPerspective(fovy,w/h,near,far):

#include <GL/glut.h> void reshape(int width, int height); void display(); float zoom = 1.0; int main(int argc, char **argv) { // #1: GLUT window initialization glutInit(&argc, argv); glutInitDisplayMode(GLUT_RGB|GLUT_DOUBLE); glutInitWindowSize(600, 600); glutInitWindowPosition(0, 0); glutCreateWindow("Window's Title"); glutFullScreen(); //optional // #2: Registering callbacks glutReshapeFunc(reshape); glutDisplayFunc(display); // #3: GLUT main loop glutMainLoop(); return 0; } void reshape(int w, int h) { glViewport(0, 0, w, h); glMatrixMode(GL_PROJECTION); glLoadIdentity(); // to prevent a divide by zero if (h==0) h=1; gluPerspective(zoom*45.0f,-(GLfloat)w/(GLfloat)h,0.1f,100.0f); glMatrixMode(GL_MODELVIEW); } void display() { glClear(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT); glLoadIdentity(); gluLookAt(0,50,0, // eye position 0,0,0, // target point's position 0,0,1); // up vector // #: to draw coordinates float length = 10.0f; // Z axis glColor3f(1,1,0); // yellow glBegin(GL_LINES); glVertex3f(0.0,0.0,length); glVertex3f(0.0,0.0,0); glEnd(); // Y axis glColor3f(0,0,1); // blue glBegin(GL_LINES); glVertex3f(0.0,length,0.0); glVertex3f(0.0,0.0,0.0); glEnd(); // X axis glColor3f(1,0,0); // red glBegin(GL_LINES); glVertex3f(length,0.0,0.0); glVertex3f(0.0,0.0,0.0); glEnd(); // swap the buffers to display, since double buffering is used. glutSwapBuffers(); }
8. Idle
#include <math.h> #include <GL/glut.h> void reshape(int width, int height); void display(); void idle(); const float rad = 0.01745; float move=0; float camerarotationx = 2 * sin(rad * move); float camerarotationy = 2 * cos(rad * move); float zoom = 1.0; int main(int argc, char **argv) { // #1: GLUT window initialization glutInit(&argc, argv); glutInitDisplayMode(GLUT_RGB|GLUT_DOUBLE); glutInitWindowSize(600, 600); glutInitWindowPosition(0, 0); glutCreateWindow("Window's Title"); //glutFullScreen(); //optional // #2: Registering callbacks glutReshapeFunc(reshape); glutDisplayFunc(display); glutIdleFunc(idle); // #3: GLUT main loop glutMainLoop(); return 0; } void reshape(int w, int h) { glViewport(0, 0, w, h); glMatrixMode(GL_PROJECTION); glLoadIdentity(); // to prevent a divide by zero if (h==0) h=1; gluPerspective(zoom*45.0f,(GLfloat)w/(GLfloat)h,0.1f,1000.0f); glMatrixMode(GL_MODELVIEW); } void display() { glClear(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT); glLoadIdentity(); gluLookAt(100*camerarotationx,100*camerarotationy,0, // eye position 0,0,0, // target point's position 0,0,1); // the up vector // #: to draw coordinates float length = 30.0f; // Z axis glColor3f(1,1,0); glBegin(GL_LINES); glVertex3f(0.0,0.0,length); glVertex3f(0.0,0.0,0); glEnd(); // Y axis glColor3f(0,0,1); glBegin(GL_LINES); glVertex3f(0.0,length,0.0); glVertex3f(0.0,0.0,0.0); glEnd(); // X axis glColor3f(1,0,0); glBegin(GL_LINES); glVertex3f(length,0.0,0.0); glVertex3f(0.0,0.0,0.0); glEnd(); // swap the buffers to display, since double buffering is used. glutSwapBuffers(); } void idle() { // when idle, change the viewing position orbiting the origin: move += 1.1f; if( move>1000) move=0; camerarotationx = sin(rad * move); camerarotationy = cos(rad * move); glutPostRedisplay(); }
Additional Resources: