210 likes | 422 Views
CS 3388: OpenGL Basics. [Hill § 2.2.1—2.3.2 § 5.6—5.6.2]. What is OpenGL?. “Application Programming Interface ”. Industry-standard API for 3D graphics Cross-platform ( ) Cross-vendor ( ) Cross-language ( C/C++, Python , Java... )
E N D
CS 3388: OpenGL Basics [Hill §2.2.1—2.3.2 §5.6—5.6.2]
What is OpenGL? “Application Programming Interface” • Industry-standard API for 3D graphics • Cross-platform ( ) • Cross-vendor ( ) • Cross-language ( C/C++, Python, Java... ) • Used in games, CAD, medical, .... ...
What is OpenGL? • Interface specifying hundreds of functions
What is OpenGL? • Vendors implement OpenGL drivers User-mode applications / drivers Kernel-mode drivers video cards application.exe osmesa32.dll (pure software) API opengl32.dll or... atikmdag.sys atiogl32.dll or... nvlddmkm.sys nvoglv32.dll drivers take entire teams of programmers years to develop!
OpenGL Functions • Simple example... #include <windows.h> // Windows API (for gl.h) #include <gl/gl.h> // OpenGL API void redraw() { glClear(GL_COLOR_BUFFER_BIT); glBegin(GL_LINE_STRIP);// begin primitive assembly glColor3f(1,0,0); glVertex2f( 0, .5f); // red vertex glColor3f(0,1,0); glVertex2f(-.5f,-.5f); // green vertex glColor3f(0,0,1); glVertex2f( .5f,-.5f); // blue vertex glColor3f(1,0,0); glVertex2f( 0, .5f); // repeat first vertex glEnd(); // end primitive assembly glFlush(); // kick OpenGL in the butt } ...
OpenGL 1.1 State (Actual) (DO NOT MEMORIZE)
OpenGL 1.1 State (Simplified) • OpenGL maintains many global variables (“state”) application.exe glWhatever(...) primitive assembly drawing modes glBegin clear colour z-buffer test? alpha-blending? shading? (none/flat /smooth) backface culling? opengl32.dll type (lines? triangles?) norm[0] norm[1] norm[2] ... vertex[0] vertex[1] vertex[2] ... transformations video card modelview matrix projection matrix viewport matrix texture matrix glEnd materials geometry lights “display lists” texture state unshaded color ambient color diffuse coefficients specular coefficients shininess enabled? positions colours falloffs current textures enabled? filtering? mipmapping? current normal current texcoord current point size current line width current raster point [0] primitives/state [1] primitives/state ... Example: glNormal sets current normal, and glVertex “emits” a point that uses current normal Not a complete list of state!
OpenGL 3.0 State (Simplified) • Matrices, materials, lighting, glBegin/End, all deprecated application.exe glWhatever(...) But OpenGL 1.1 will still be around for like 10 more years! no primitive assembly no glBegin/glEnd; must build your own vertex data structures drawing modes clear colour z-buffer test? alpha-blending? shading? (none/flat /smooth) backface culling? opengl32.dll transformations video card viewport matrix vertex arrays IDs vertex structures cached on video card framebuffer control shader control geometry texture state current framebuffer current renderbuffers vertex program fragment program uniform parameters current textures enabled? filtering? mipmapping? current point size current line width vertex array id [0] vertex array id [1] ... Not a complete list of state! OpenGL 3.0 is harder to program for beginners
OpenGL 1.1 API in C/C++ • On Windows, gl.h is standard: • It declares all OpenGL 1.1 functions/defs #include <windows.h> // Windows API (for gl.h) #include <gl/gl.h> // OpenGL API #define GL_POINTS 0x0000 #define GL_LINES 0x0001 #define GL_LINE_LOOP 0x0002 #define GL_LINE_STRIP 0x0003 #define GL_TRIANGLES 0x0004 #define GL_TRIANGLE_STRIP 0x0005 #define GL_BLEND_DST 0x0BE0 #define GL_BLEND_SRC 0x0BE1 #define GL_BLEND 0x0BE2 // ... WINGDIAPI void APIENTRY glVertex3f (GLfloat x, GLfloat y, GLfloat z); WINGDIAPI void APIENTRY glVertex3fv (const GLfloat *v); WINGDIAPI void APIENTRY glViewport (GLint x, GLint y, GLsizei width, GLsizei height); // ... gl.h
OpenGL 1.1 Matrices • Set OpenGL’s projection matrix: • why transpose? stop asking questions! (no need in DirectX) • Set OpenGL’s modelview matrix: • Set OpenGL’s viewport matrix: mat4x4 P = ...; // perspective or orthographic matrix glMatrixMode(GL_PROJECTION); // ready to change projection... glLoadMatrixf(transpose(P).ptr()); // ...use our P matrix please! mat4x4 M = ...; // transforms all into eye coordinates glMatrixMode(GL_MODELVIEW); // ready to change modelview... glLoadMatrixf(transpose(M).ptr()); // ...use our M matrix please! glViewport(left,bottom,width,height); // units, in pixels
OpenGL 1.1 Matrices • Instead of glLoadMatrix can use • Be aware that glFrustum, glOrtho, glLoadMatrix, and glLoadMatrix are all deprecated in OpenGL 3.0 • best to just use mat4x4 like on previous slide glMatrixMode(GL_PROJECTION); glLoadIdentity(); glFrustum(l,r,b,t,-n,-f); // same as perspective(l,r,b,t,n,f) or ... glMatrixMode(GL_PROJECTION); glLoadIdentity(); glOrtho(l,r,b,t,-n,-f); // same as orthographic(l,r,b,t,n,f)
OpenGL 1.1 Primitive Assembly • To define primitive, require: • type specifier (GL_POINTS? GL_LINES? GL_TRIANGLES?) • list of vertices (2D or 3D coordinates) • attributes for each vertex (color, normal, texcoord,...) • Assemble a primitive by glBegin(type), at least one glVertex, then glEnd() • error to call glVertex outside glBegin/glEnd! • Interpretation of points depends on type
OpenGL 1.1 Primitive Assembly • Attributes of vertex determined by current state variables • colour, material, normal, texcoord • Example: glBegin(GL_TRIANGLES); // begin primitive assembly (triangle list) glColor3f(1,0,0); // current solid colour = RED glVertex2f(0.8f,-0.8f); // emit bottom-right vertex glColor3f(0,1,0); // current solid colour = GREEN glVertex2f(0,0.8f); // emit top vertex glColor3f(0,0,1); // current solid colour = BLUE glVertex2f(-0.8f,-0.8f); // emit bottom-left vertex glEnd();
OpenGL 1.1 Materials • If lighting disabled, solid colour used • Otherwise, shading colour is computed • Configure shading with glMaterial • Smooth vs flat shading: glColor3f(1,0,0); // set current solid colour to RED glEnable(GL_LIGHTING); // enable shading calculations vec4 diffuse(1,0,0,1); // red diffuse colour vec4 specular(1,1,1,1); // white specular reflection float shininess = 20; glMaterialfv(GL_FRONT,GL_DIFFUSE,diffuse.ptr()); // set it glMaterialfv(GL_FRONT,GL_SPECULAR,specular.ptr()); glMaterialfv(GL_FRONT,GL_SHININESS,&shininess); glShadeModel(GL_SMOOTH); glShadeModel(GL_FLAT);
OpenGL 1.1 Lights • Up to 8 lights for any particular primitive • Each light must be configured using glLight, and explicitly enabled/disabled • Directional light has position.w=0 vec4 position(1,1,-3,1); // in front of eye, up and to right vec4 color(.5f,.5f,.5f,1); // weak white light // set up point light and enable it (used if GL_LIGHTING enabled) glLightfv(GL_LIGHT0,GL_POSITION,position.ptr()); glLightfv(GL_LIGHT0,GL_DIFFUSE,color.ptr()); glLightfv(GL_LIGHT0,GL_SPECULAR,color.ptr()); glEnable(GL_LIGHT0); // now do same for GL_LIGHT1, etc...
OpenGL 1.1 Primitives • GL_POINTS: – draw individual dots • GL_LINES: • GL_LINE_STRIP: GL_LINE_LOOP: vertex list: v0,v1,v2,v3,v4,v5,... (v0) (v1) (v2) (v3) (v4) (v5) glPointSize(diameter); (v0,v1) (v2,v3) (v4,v5) ... (v0,v1,v2,v3,v4,...) (v0,v1,v2,v3,...,v0)
OpenGL 1.1 Primitives • GL_TRIANGLES: • GL_TRIANGLE_STRIP: GL_TRIANGLE_FAN vertex list: v0,v1,v2,v3,v4,v5,... 0 2 3 (v0,v1,v2) (v3,v4,v5) ... 5 4 1 (v0,v1,v2) (v2,v1,v3) (v2,v3,v4) (v4,v3,v5) ... 4 0 2 0 (v0,v1,v2) (v0,v2,v3) (v0,v3,v4) ... 4 5 1 3 3 1 2
OpenGL 1.1 Primitives • GL_QUADS: • GL_QUAD_STRIP • GL_POLYGON • builds polygon out of triangle fan! vertex list: v0,v1,v2,v3,v4,v5,... 0 3 (v0,v1,v2,v3) ... 1 2 THESE THREE NOW DEPRECATED
OpenGL Hidden-Surface Removal • Depth test (z-buffer test) • “Back-face culling” (cull = filter out) • don’t rasterize triangle if “facing away” from eye glClear(GL_DEPTH_BUFFER_BIT); // reset depth buffer to 1 (far) ... glEnable(GL_DEPTH_TEST); // enable per-pixel z-buffer test glEnable(GL_CULL_FACE); // enable front/back facing test front-facing = counter-clockwise winding! backface_demo 0 0 2 2 back-facing front-facing 1 1
OpenGL 2.0/3.0 • Introduce programmable hardware! • Mini-programs called “shaders” • Write code in GL Shading Language (GLSL) • Discuss this soon... send my_code... application.exe opengl32.dll char* my_code = "void main() {" "gl_FragColor = vec4(1,0,0,1);" "}"; GLuint red_shader = ... glUseProgram(red_shader); void main() { gl_FragColor = vec4(1,0,0,1); } my_code runs inside video card!!