Lecture 3 Understanding of OPenGL programming
What is OpenGL GL: stands for Graphic Library Software interface for rendering purposes for 2D or 3D geometric data objects.
Various Pieces gl: The basic libraries. glu: Advanced implementation of OpenGL. Higher level function calls/commands. i.e. glusphere. glut: Generic platform independent library.
Reference/Source Code Nehe s OpenGL Tutorials http://nehe.gamedev.net/ GLUT Programming Interface: The OpenGL Utility ToolKit (GLUT) Programming Interface GL Programming Interface http://www.glprogramming.com/blue/ Blue Book
Hello world.cpp #include <GL\glew.h> #include <GL\freeglut.h> #include <GL\glut.h> #include <math.h> void display( ) // Clear the background before drawing glclear(gl_color_buffer_bit); // Actual code to do the drawing glbegin(gl_polygon); glvertex2f(-0.5, -0.5); glvertex2f(-0.5, 0.5); glvertex2f(0.5, 0.5); glvertex2f(0.5, -0.5); glend(); int main(int argc, char* argv[]) glutinit(&argc, argv); glutinitdisplaymode(glut_single GLUT_RGB); glutinitwindowposition(50, 50); glutinitwindowsize(200, 200); glutcreatewindow("hello World"); glutdisplayfunc(display); glutmainloop(); return 0; // Ensure all drawing commands are executed glflush();
int main(int argc, char* argv[]) glutinit(&argc, argv); glutinitdisplaymode(glut_single GLUT_RGB); glutinitwindowposition(50, 50); glutinitwindowsize(200, 200); glutcreatewindow("hello World"); glutdisplayfunc(display); glutmainloop(); glutinitwindowposition(50, 50); Window is created 50 pixel for left of screen and 50 pixel for top of the screen glutcreatewindow("hello World"); The created windows is named as Hello World return 0; glutcreatewindow("hello World"); The size of the Hello World windows is 200 pixel x 200 pixel Task: Change the position of the windows and the size of the windows. Observe the changes.
void display( ) // Clear the background before drawing glclear(gl_color_buffer_bit); glbegin(gl_polygon); The rectangle is called as GL_POLYGON // Actual code to do the drawing glbegin(gl_polygon); glvertex2f(-0.5, -0.5); glvertex2f(-0.5, 0.5); glvertex2f(0.5, 0.5); glvertex2f(0.5, -0.5); glend(); // Ensure all drawing commands are executed glflush(); glvertex2f(-0.5, -0.5); glvertex2f(-0.5, 0.5); glvertex2f(0.5, 0.5); glvertex2f(0.5, -0.5); Task: By changing the size of the GL_POLYGON, find the size of the windows in terms Cartesian Coordinate? These are the coordinates of the GL_POLYGON. The center of the Windows is reference point (0,0)
Color Setting void display( ) // Clear the background before drawing glclear(gl_color_buffer_bit); // Actual code to do the drawing glbegin(gl_polygon); glcolor3f(1.0, 1.0, 1.0); glvertex2f(-0.5, -0.5); glcolor3f(1.0, 0.0, 0.0); glvertex2f(-0.5, 0.5); glcolor3f(0.0, 1.0, 0.0); glvertex2f(0.5, 0.5); glcolor3f(0.0, 0.0, 1.0); glvertex2f(0.5, -0.5);glEnd(); // Ensure all drawing commands are executed glflush();
void glcolor3 (TYPE red, TYPE green, TYPE blue); glcolor3f(1.0, 0.0, 0.0); glvertex2f(-0.5, 0.5); (red : 1, green: 0, blue : 0) red glcolor3f(0.0, 1.0, 0.0); glvertex2f(0.5, 0.5); (red : 0, green: 1, blue : 0) green glcolor3f(1.0, 1.0, 1.0); glvertex2f(-0.5, -0.5); (red : 1, green: 1, blue : 1) white glcolor3f(0.0, 0.0, 1.0); glvertex2f(0.5, -0.5); (red : 0, green: 0, blue : 1) blue Task: Observe the square by just by setting the colored once before glbegin
Shape of different sizes void display( ) // Clear the background before drawing glclear(gl_color_buffer_bit); glmatrixmode(gl_projection); glloadidentity(); gluortho2d(-5.0, 5.0, -5.0, 5.0); glmatrixmode(gl_modelview); // Actual code to do the drawing glcolor3f(0.0, 1.0, 0.0); glbegin(gl_polygon); glvertex2f(-3, -3); glvertex2f(-3, 3); glvertex2f(3, 3); glvertex2f(3, -3); glend(); glflush();
Animation Concept Changing the one or more variables that will change the position of the entity or entities. By displaying each frame with different coordinates, animation can be formed. Frame 1 Frame 2 Frame 3 Frame 4 Therefore, shape must be drawn using variable/s and by changing the value of variable/s, animation can be created.
Animation: Development of the equations X1 = R * cos (pi/2 + q) Y1 = R * sin (pi/2 + q) X2 = R * cos (pi + q) Y2 = R * sin (pi + q) X0 = R * cos (0 + q) Y0= R * sin (0 + q) X3 = R * cos (1.5*pi + q) Y3 = R * sin (1.5*pi + q) Additional Codes #include <GL\glew.h> #include <GL\freeglut.h> #include <math.h> #define pi 4 * atan(1.0) GLfloat theta =0 ; glbegin(gl_polygon); glvertex2f(3*cos(0+theta), 3*sin(0+theta)); glvertex2f(3 * cos(0.5*pi + theta), 3 * sin(0.5*pi + theta)); glvertex2f(3 * cos(pi + theta), 3 * sin(pi + theta)); glvertex2f(3 * cos(1.5*pi + theta), 3 * sin(1.5*pi + theta)); glend();
Animation: Modification of the programs int main(int argc, char* argv[]) glutinit(&argc, argv); glutinitdisplaymode(glut_double GLUT_RGB); glutinitwindowposition(100, 100); glutinitwindowsize(600, 600); glutcreatewindow("hello World"); glutdisplayfunc(display); glutidlefunc(myidle); glutmainloop(); glvertex2f(3 * cos(0.5*pi + theta), 3 * sin(0.5*pi + theta)); glvertex2f(3 * cos(pi + theta), 3 * sin(pi + theta)); glvertex2f(3 * cos(1.5*pi + theta), 3 * sin(1.5*pi + theta)); glend(); // Ensure all drawing commands are executed glutswapbuffers(); //replacing glflush() return 0; void myidle() theta += 0.01 * pi / 180.0;; if (theta > 2 *pi) theta -= -2*pi; glutpostredisplay();
Animation: two or more variables Two or more entities are animated at the same time - In myidle function, more than one variables can be used. Therefore, var1, var2 varn can be used. void myidle() theta1 += 0.01 * pi / 180.0;; if (theta1 > 2 *pi) theta -= -2*pi; theta2 += 1 * pi / 180.0;; if (theta2 > 2 *pi) theta -= -2*pi thetan += 10 * pi / 180.0;; if (thetan > 2 *pi) theta -= -2*pi glutpostredisplay();
Animation: in sequence The task is to make the rectangle travels along three designated routes. Therefore, the rectangle will travel along path 1, then follows with Path 2 and finally Path 3. Soln: a flag that indicates which path to follow should be used here. Common practice is that the value flag will be either 0 or 1. void myidle() if ( flag_path1 == 0 && flag_path2 == 0 && flag_path3 = 0) var1 += 1; if ( point.x == P1.x && point.y == P1.y) flag_path1 = 1 else if (flag_path2 == 0 && flag_path3 = 0) then glutpostredisplay();