Presentation is loading. Please wait.

Presentation is loading. Please wait.

Introduction to OpenGL (INF 250) Veronika Solteszova et al., UiB Dept. of Informatics, 2016-03-17.

Similar presentations


Presentation on theme: "Introduction to OpenGL (INF 250) Veronika Solteszova et al., UiB Dept. of Informatics, 2016-03-17."— Presentation transcript:

1 Introduction to OpenGL (INF 250) Veronika Solteszova et al., UiB Dept. of Informatics, 2016-03-17

2 Topics What is OpenGL? Basic principles Rendering Pipeline OpenGL as a state machine, OpenGL context Drawing polygons Vertex colors Textures Creating an OpenGL window Useful literature

3 What is OpenGL Software application interface (API) to graphics hardware (GPU) to achieve hardware-accelerated rendering Free of charge Developed by Silicon Graphics Inc., now managed by consortium Khronos Exists for several programming languages (C++, python, java, Delphi, …) For different platforms (therefore window management is not part of OpenGL, but there are helping libraries)

4 OpenGL commands Naming convention starts with gl and GL - commands start with gl* (no spaces between words, words start with a capital letter) Ex.: glMatrixMode(.), glDepthFunc(.) - constants/bitmasks start with GL_* (all capital letters, words separated by underscores, used as parameters of OpenGL commands) Ex.: GL_POLYGON, GL_FLAT, GL_MODELVIEW In OpenGL documentation, a command can be specified as glCommand, but there is a suffix specifying dimension and data type and an optional v (input is a pointer) Ex.: glVertex in documentation, but actual commands are glVertex2f, glVertex3f, glVertex3d, glVertex3fv …

5 Rendering Pipeline Blue boxes are programmable (programs are called shaders) 1 Vertex specification: Upload vertex coordinates to GPU 2 – 4: Vertex processing (blue boxes, optional) 3: Vertex post-processing: coordinate transformation (model, viewing space, perspective space, clipping, perspective division, viewport transformation) 4: Primitive assembly: GPU will determine (specified by programmer) which vertices will create primitives 5: Rasterization: Conversion of primitives into fragments 6: Fragment processing: optional, programmable 7: Pre-sample operations: scissor test, stencil test, depth test, blending…

6 The State Machine Concept OpenGL rendering pipeline operates in various modes States/modes are set and they stay as long as they are changed again by the programmer The design avoid the necessity to set many parameters in each function call (higher speed) Each state has a default value You can query the system for its current value glGet*(.) glEnable(…), glDisable(.) can be GL_DEPTH_TEST, GL_TEXTURE_3D

7 Structure of an OpenGL Program Depends on the library, but usually has the following callback methods: init (commands that can for ex. set modes) reshape or resize (what happens if the window is resized) display or draw (drawing functions) idle (what happens if no user input comes) close (what happens when the application closes, clearing memory etc.)

8 Errors GLenum glGetError(void) return the value of the error flag GL_NO_ERROR means no error since GL was initialized or since glGetError was last called Possible error check: If(myError == GL_INVALID_ENUM){..} If(myError == GL_INVALID_OPERATION){..} If(myError == GL_INVALID_VALUE){..} If(myError == GL_INVALID_FRAMEBUFFER_OPERATION){..} If(myError == GL_OUT_OF_MEMORY){..}

9 The Stack Concept The states are stored in an attribute stack Store and restore attributes via glPushAttrib(.), glPopAttrib() Matrix stacks glMatrixMode(.) for activating model-view, projection, texture, color NB! Stack has finite depth and can overflow (each push must have a corresponding pop)

10 Matrix stacks Transformation of vertices happens via matrix multiplication (revise linear algebra) Matrices in OpenGL are 4x4, column-major order OpenGL has a command for matrix right multiplication Ex.: glMultMatrixf(const GLfloat *m), m points to an array of 16 floats that store matrix elements in column-major order If the current matrix on the stack is C, the transformed vector will be C x v. After glMultMatrixf( &(M[0]), the transformation will be C x M x v and not M x C x v

11 Matrix Stacks - Example

12 Drawing glBegin(..); //vertices and their properties glEnd(); glBegin parameters define what primitives is going to be drawn (GL_POINTS, GL_LINES, GL_LINE_STRIP, GL_LINE_LOOP, GL_TRIANGLES, GL_TRIANGLE_STRIP, GL_TRIANGLE_FAN, GL_QUAD, GL_QUAD_STRIP, GL_POLYGON) Do not change OpenGL modes between glBegin and glEnd, only set vertex properties Vertex properties can be color, texture coordinates, normal vector

13 Specify Vertices – Immediate Mode

14 Specify Vertices – Other Options Vertices can be specified in a more efficient way: uploaded once to the GPU and re-used allows the same vertex to be part of several primitives How it works: initiate a buffer upload vertex data to the GPU upload vertex properties to the GPU upload a data structure that will allow GPU to generate primitives from vertex data More advanced, not part of this lecture

15 The Depth Buffer Every fragment has a depth value (Z-coordinate in screen- space) By default: depth 0.0 = depth of the viewing plane, 1.0 depth of the far plane Fragment color (color buffer) and depth (depth buffer) Depth test steers what happens if two polygons overlap (last stage of the rendering pipeline) glDepthFunc( glEnum param); Param can be: GL_NEVER, GL_ALWAYS, GL_LESS, GL_LEQUAL, GL_GREATER, GL_GEQUAL, …

16 Color in OpenGL In the beginning of the drawing routine, clear the color buffer (often called at the same time as the clear-of-depth – buffer command) For each vertex, color can be specified via glColor??(..) (glColor3f, glColor4f, …) If it is not, then the vertex will have the same color that was set the last time (NB! State machine)

17 Vertex Color

18 Vertex Color cont.

19

20 Textures Color of fragments can also be defined by bitmaps (images) that are called textures Textures can be 1D, 2D and 3D - A texture object must be first generated (a unique non- zero ID is generated) - Image data is then uploaded to the texture memory (GPU) with the respective ID - Mapping textures to vertices (texture coordinates)

21 Generate and Delete Textures Generate texture ID and upload data Delete texture

22 m

23 Creating an OpenGL Window NeHe OpenGL tutorial 1 http://nehe.gamedev.net/tutorial/creating_an_opengl_windo w_(win32)/13001/ http://nehe.gamedev.net/tutorial/creating_an_opengl_windo w_(win32)/13001/ GLUT library (quite simple) https://www.opengl.org/resources/libraries/glut/ https://www.opengl.org/resources/libraries/glut/ Qt OpenGL canvas (QGL Widget as a part of the user interface)

24 Useful Resources “The Red Book” OpenGL Programming Guide http://www.ics.uci.edu/~gopi/CS211B/opengl_programming _guide_8th_edition.pdf Older editions are good enough for this course NeHe tutorials http://nehe.gamedev.net/ Contain lots of useful tutorials from window creating and OpenGL basics up to more advanced lessons.http://nehe.gamedev.net/


Download ppt "Introduction to OpenGL (INF 250) Veronika Solteszova et al., UiB Dept. of Informatics, 2016-03-17."

Similar presentations


Ads by Google