INF250-VisIntro-P02-OpenGL_1of1.pptx

Download Report

Transcript INF250-VisIntro-P02-OpenGL_1of1.pptx

Introduction to OpenGL
(INF 250)
Veronika Solteszova et al.,
UiB Dept. of Informatics,
2016-03-17
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
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)
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 …
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…
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
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.)
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){..}
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)
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
Matrix Stacks - Example
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
Specify Vertices – Immediate Mode
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
The Depth Buffer
Every fragment has a depth value (Z-coordinate in screenspace)
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, …
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)
Vertex Color
Vertex Color cont.
Vertex Color cont.
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 nonzero ID is generated)
- Image data is then uploaded to the texture memory
(GPU) with the respective ID
- Mapping textures to vertices (texture coordinates)
Generate and Delete Textures
Generate texture ID and upload data
Delete texture
m
Creating an OpenGL Window
NeHe OpenGL tutorial 1
http://nehe.gamedev.net/tutorial/creating_an_opengl_win
dow_(win32)/13001/
GLUT library (quite simple)
https://www.opengl.org/resources/libraries/glut/
Qt OpenGL canvas (QGL Widget as a part of the user
interface)
Useful Resources
“The Red Book”
OpenGL Programming Guide
http://www.ics.uci.edu/~gopi/CS211B/opengl_programmi
ng_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.