CS 380 LAB I Open GL Donghyuk Kim
CS 380 LAB I Open. GL Donghyuk Kim Reference 1. [Open. GL course slides by Rasmus Stenholt] Reference 2. [http: //nehe. gamedev. net/]
Goal Introduce Open. GL programming n Help you do CS 380 homework by yourself n 2
Outline n Set up Open. GL development environments on Visual Studio ¨ Download n and dll/libs setting Start with the Open. GL framework ¨ Draw a Open. GL primitive ¨ Address keyboard and mouse input 3
Starting with an empty project n Create an empty Win 32 Console Application 4
Starting with an empty project n Create an empty Win 32 Console Application 5
Starting with an empty project n Download PA 1 source files & move into project directory 6
Starting with an empty project n Add to the current project 7
Visual Studio Functions n n Press F 7 to build solution Press Ctrl + F 5 to run program 8
Download GLUT n http: //user. xmission. com/~nate/glut. html 9
Troubleshooting : LIB Error n Solution 1 ¨ Copy glut 32. lib to $(Solution. Dir) or $(Solution. Dir)$(Configuration) e. g. , C: CS 380Open. GL_TutorialRelease 10
Troubleshooting : LIB Error n Solution 2 ¨ Set project specific lib path 11
Troubleshooting : DLL Error n Solution 1 ¨ Copy glut 32. dll to C: WindowsSystem 32 or to $(Solution. Dir)$(Configuration) e. g. , C: CS 380Open. GL_TutorialRelease 12
Troubleshooting : DLL Error n Solution 2 ¨ Set PATH=%PATH%; path-to-dll-files e. g. , C: Downloadsglut-3. 7. 6 -bin project specific PATH 13
Done! 14
Creating an empty window n n n n #include <GL/glut. h> #include <GL/glu. h> void display() { } int main( int argc, char* argv[] ) { glut. Init(&argc, argv); glut. Init. Display. Mode(GLUT_SINGLE | GLUT_RGBA | GLUT_DEPTH); glut. Init. Window. Size(512, 512); glut. Create. Window("CS 380 LAB"); glut. Display. Func( display ); glut. Main. Loop(); return 0; } 28
Draw your first polygon n void display() ¨ Main display function where we can do all the drawing 29
Draw your first polygon n Clear your screen void display() { ¨ gl. Clear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); ¨ gl. Flush(); ¨ } ¨ n n gl. Clear(parameters) // clear input buffers gl. Flush() // forces all pending commands to be executed 30
Draw your first polygon n n void display() { gl. Clear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); gl. Load. Identity(); // Reset our view gl. Begin(GL_TRIANGLES); // Draw a triangle gl. Vertex 3 f( 0. 0 f, 1. 0 f, 0. 0 f); gl. Vertex 3 f(-1. 0 f, 0. 0 f); gl. Vertex 3 f( 1. 0 f, -1. 0 f, 0. 0 f); gl. End(); gl. Flush(); (0, 0, 0) } 31
Draw your first polygon n In Open. GL, geometry is specified by vertices Vertices must be specified between gl. Begin(primitive type) and gl. End() function calls The primitive type represents how vertices are to be connected gl. Begin(GL_TRIANGLES); gl. Vertex 3 f( 0. 0 f, 1. 0 f, 0. 0 f); gl. Vertex 3 f(-1. 0 f, 0. 0 f); gl. Vertex 3 f( 1. 0 f, -1. 0 f, 0. 0 f); gl. End(); 32
Open. GL Primitives n Triangles ¨ There are 3 ways of making triangles with Open. GL n Individual triangles ¨ ¨ ¨ n Type is GL_TRIANGLES Each triangle requires 3 explicit vertices Sets of unconnected triangles are often called polygon soups Strips of connected triangles ¨ ¨ ¨ n GL_TRIANGLES Type is GL_TRIANGLE_STRIP The first triangle requires 3 vertices, the rest use 1 new vertex and the 2 most recently defined vertices GL_TRIANGLE_STRIP Complex objects are often built from Fans of connected triangles ¨ ¨ ¨ Type is GL_TRIANGLE_FAN Every triangle use the first, the previous, and a new vertex Useful for creating polygons or approximating circles/ellipses GL_TRIANGLE_FAN 33
Open. GL Primitives n Quadrilaterals (quads) ¨ Individual quads n n Type is GL_QUADS A quad is defined by 4 vertices Quads can be decomposed into two triangles Quads are not necessarily plane or convex ¨ ¨ GL_QUADS Be careful with the vertex sequence Strips of connected quads n n Type is GL_QUAD_STRIP Uses the most recent 2 vertices and 2 new vertices GL_QUAD_STRIP 34
Open. GL Primitives n Polygons ¨ Type is GL_POLYGON ¨ Polygons need 3 or more vertices n I. e. can be used for any polygon ¨ Polygons are divided into triangles by the graphics card GL_POLYGON 35
Open. GL Primitives n Points ¨ ¨ ¨ Type is GL_POINTS Points are 0 -D Points represent the simplest drawable primitive 1 vertex is used per point Points are rendered as small, unconnected dots on the screen Theoretically points have no area GL_POINTS 36
Open. GL Primitives n Lines ¨ Type is GL_LINES ¨ Lines are 1 -D ¨ Each line needs 2 vertices ¨ Lines have no area n Open series of lines ¨ Type n GL_LINES GL_LINE_STRIP is GL_LINE_STRIP Closed series of lines ¨ Type is GL_LINE_LOOP 37
Open. GL functions n Open. GL functions all follow the same naming conventions ¨ Function names have gl, glu, or glut as prefix depending on their package of origin ¨ The name of the function follows the prefix ¨ The parameter type of the function is placed as a postfix 38
Open. GL functions gl. Vertex 3 fv( v ) Number of components 2 3 4 - (x, y) (x, y, z, w) Data Type b ub s us i ui f d - byte unsigned byte short unsigned short int unsigned int float double Vector omit “v” for scalar form gl. Vertex 2 f( x, y ) 39
Tutorial n Draw this rectangle 40
Tutorial n n n n Drawing a red rectangle gl. Begin(GL_QUADS); gl. Vertex 3 f( -0. 5 f, 0. 0 f); gl. Vertex 3 f(-0. 5 f, 0. 0 f); gl. Vertex 3 f( 0. 5 f, -0. 5 f, 0. 0 f); gl. End(); 41
Adding colours n gl. Color 3 f(red, green, blue) n Drawing a red rectangle n n n n gl. Begin(GL_QUADS); gl. Color 3 f(1. 0 f, 0. 0 f); gl. Vertex 3 f( -0. 5 f, 0. 0 f); gl. Vertex 3 f(-0. 5 f, 0. 0 f); gl. Vertex 3 f( 0. 5 f, -0. 5 f, 0. 0 f); gl. End(); 42
Colours in Open. GL n Colours are modelled using the red-greenblue (RGB) system 43
Colours in Open. GL n There are several ways of representing colour in Open. GL ¨ Directly as RGB-tuples ¨ Extended RGBA-tuples ¨ Indexed mode n The RGBA mode has an extra component, alpha, which does not affect the colour directly ¨ Alpha is used when blending n E. g. transparency effects colours 44
Colours in Open. GL Colours are specified by the gl. Color*() family of functions n Example: gl. Color 3 f() n ¨ Specifies a colour by three floating point values in the range [0. 0; 1. 0] ¨ The parameters represent R, G, and B, respectively 45
Keyboard input n n n int main( int argc, char* argv[] ) { glut. Init(&argc, argv); glut. Init. Display. Mode(GLUT_SINGLE | GLUT_RGBA | GLUT_DEPTH); glut. Init. Window. Size( width, height ); glut. Create. Window("CS 380"); glut. Display. Func( display ); glut. Mouse. Func( mouse ); glut. Keyboard. Func( keyboard ); glut. Reshape. Func( reshape ); glut. Main. Loop(); return 0; } 46
Keyboard input n n void keyboard(unsigned char key, int x, int y) { if (key == 'r') { // todo } n glut. Post. Redisplay(); n n } 47
Tutorial n Change the color of your rectangle ¨ Press ‘r’: change it to a red rectangle ¨ Press ‘g’: change it to a green rectangle ¨ Press ‘b’: change it to a blue rectangle 48
Tutorial n n n void display() { … gl. Color 3 f(r, g, b); … } void keyboard(unsigned char key, int x, int y){ if (key == 'r') { r = 1. 0, g = 0. 0, b = 0. 0; } else if (key == 'g') { r = 0. 0, g = 1. 0, b = 0. 0; } else if (key == 'b') { r = 0. 0, g = 0. 0, b = 1. 0; } glut. Post. Redisplay(); } 49
Mouse input n n n int main( int argc, char* argv[] ) { glut. Init(&argc, argv); glut. Init. Display. Mode(GLUT_SINGLE | GLUT_RGBA | GLUT_DEPTH); glut. Init. Window. Size( width, height ); glut. Create. Window("CS 380"); glut. Display. Func( display ); glut. Mouse. Func( mouse ); glut. Keyboard. Func( keyboard ); glut. Reshape. Func( reshape ); glut. Main. Loop(); return 0; } 50
Mouse input n n void mouse( int button, int state, int mx, int my ) button GLUT_LEFT_BUTTON ¨ GLUT_MIDDLE_BUTTON ¨ GLUT_RIGHT_BUTTON ¨ n state GLUT_DOWN ¨ GLUT_UP ¨ n mx, my ¨ positions 51
Mouse input n n n void mouse( int button, int state, int mx, int my ) { if((button == GLUT_LEFT_BUTTON) && (state == GLUT_DOWN) ) { r = 1. 0, g = 1. 0, b = 1. 0; display(); } } 52
Next time n Transformation in Open. GL 53
- Slides: 40