Introduction of OpenGL Gary
Motivation
• WebGL is based on OpenGL ES 2.0 and provides an API for 3D graphics
• WebGL uses the HTML5 canvas element
• Moreover, OpenGL ES is a subset of OpenGL
What is OpenGL ?
• Open Graphics Library
• A library designed for cross platform 3D graphics
• Is the only competitor to Direct3D in the Direct library
• Hardware accelerated
• Main focus for graphics card performance
Why We Learn OpenGL ?
• Good support of hardware• Microsoft DX 10 and DX11 can only run on the platform upon Windows
Vista
• The same function can be implemented by OpenGL on Windows XP
• OpenGL provides extensions for the latest function of GPU vendors
• Cross-platform• OpenGL works on Windows/ Linux/ Mac
• OpenGL ES
• WebGL
OpenGL Libraries
• OpenGL core library• OpenGL32 on Windows
• GL on most unix/linux system
• OpenGL Utility Library (GLU)• Provides functionality in OpenGL core but avoids having to rewrite code
• Links with window system• GLX for X window systems
• WGL for Windows
What is GLUT ?
• OpenGL Utility Toolkit library
• A tool which allows the creation of windows and handling of input on multiple systems
• Making OpenGL cross-platform and extremely simple to set up
• Original GLUT is no longer being developed
• There is a remake of GLUT called FreeGLUT
What is GLEW
• OpenGL Extension Wrangler
• A cross-platform open-source C/C++ extension loading library
• Provides efficient run-time mechanisms for determining which OpenGL extensions are supported on the target platform
API Hierarchy
UNIX APPLICATION
Xlib GLX OpenGL
GLU
WINDOWS APPLICATION
GDU WGL OpenGL
GLU
OpenGL applications use the window system’s window, input, and event mechanism
OpenGL Pipeline
• A schematic diagram
• openGL 4.4
DrawProcessing
VertexProcessing
Tessellation
PrimitiveProcessing
Rasterization
TransformFeedback
FragmentProcessing
PixelProcessing
Draw Processing
DrawProcessing
VertexProcessing
Tessellation
PrimitiveProcessing
Rasterization
TransformFeedback
FragmentProcessing
PixelProcessing
Draw Processing
• Vertex Specification, Vertex Puller
• The process of setting up the necessary objects for rendering with a particular shader program
• Vertex Array Object (VAO)• An object which contains one or more VBOs • Designed to store information for a complete renderded object
• Vertex Buffer Object (VBO) • A memory buffer in the high speed memory of your video card• Designed to hold information about vertices
Vertex Processing
DrawProcessing
VertexProcessing
Tessellation
PrimitiveProcessing
Rasterization
TransformFeedback
FragmentProcessing
PixelProcessing
Vertex Processing
• Vertex Shader
• Handles the processing of individual vertices
• Vertex shaders are fed vertex attribute, and generates vertexes to the output vertex stream
• Must be a 1:1 mapping
Tessellation
DrawProcessing
VertexProcessing
Tessellation
PrimitiveProcessing
Rasterization
TransformFeedback
FragmentProcessing
PixelProcessing
Tessellation
• Patches of vertex data are subdivided into smaller Primitives• Note : Primitives are ways that OpenGL interprets vertex streams,
converting them from vertices into triangles, lines, points and so forth
Primitive Processing
DrawProcessing
VertexProcessing
Tessellation
PrimitiveProcessing
Rasterization
TransformFeedback
FragmentProcessing
PixelProcessing
Primitive Processing
• Geometry Shader
• Governs the processing of primitives
• Take a single primitive as input and may output zero or more primitives
Primitive Processing
The shader takes a triangle as input and outputs 3 lines that represent the normal for each vertex
Transform Feedback
DrawProcessing
VertexProcessing
Tessellation
PrimitiveProcessing
Rasterization
TransformFeedback
FragmentProcessing
PixelProcessing
Transform Feedback
• So far, we've used VBOs (Vertex Buffer Objects) to store vertices to be used for drawing operations.
• The transform feedback extension allows shaders to write vertices back to VBOs as well.
• You could for example build a vertex shader that simulates gravity and writes updated vertex positions back to the buffer.
• This way you don't have to transfer this data back and forth from graphics memory to main memory.
Rasterization
DrawProcessing
VertexProcessing
Tessellation
PrimitiveProcessing
Rasterization
TransformFeedback
FragmentProcessing
PixelProcessing
Rasterization
• Converts vector information (composed of shapes or primitives) into a raster image (composed of pixels) for the purpose of displaying real-time 3D graphics
Fragment Processing
DrawProcessing
VertexProcessing
Tessellation
PrimitiveProcessing
Rasterization
TransformFeedback
FragmentProcessing
PixelProcessing
Fragment Processing
• Fragment Shader, Pixel Shader
• Process a fragment into a set of colors and a single depth value
• Common fragment shader operations include texture mapping and lighting
• Since the fragment shader runs independently for every pixel drawn, it can perform the most sophisticated special effects
Pixel Processing
DrawProcessing
VertexProcessing
Tessellation
PrimitiveProcessing
Rasterization
TransformFeedback
FragmentProcessing
PixelProcessing
Pixel Processing
• Fragments output from a fragment shader are processed, and their resulting data are written to various buffers
• A framebuffer can have a depth buffer and stencil buffer, both of which optionally filter fragments before they are drawn to the framebuffer
Environment
• Visual Studio 2012
• FreeGLUT• http://freeglut.sourceforge.net/
• 2.8.1 version
• GLEW• http://glew.sourceforge.net/
• 1.10.0 version
Installation
1. Download a stable release of FreeGLUT
2. Unzip. Go to VisualStudio\2012 directory and open the freeglut.sln Visual Studio solution file
3. Solution Configuration as Release. In Solution Explorer, right-click on the freeglut project and choose Build.
Installation
4. Copy all the header files in include\GL of FreeGLUT to C:\Program Files (x86)\Microsoft Visual Studio 11.0\VC\include\GL
5. Copy freeglut.lib from lib\x86 to C:\Program Files (x86)\Microsoft Visual Studio 11.0\VC\lib
6. Copy freeglut.dll from lib\x86 to System32 or SysWOW64folder
Visual Studio Project
1. Start a “Win32 Console Application” new project
2. Go to your “project properties” -> “Configuration Properties” -> “Linker”, click on “input” and add two additional dependencies, “glew32.lib” and “freeglut.lib”
OpenGL API
• void glutInit (int *argcp, char **argv);• Initialized GLUT
• void glutInitDisplayMode (unsigned int mode);• Sets the display mode
• void glutInitWindowSize (int width, int height);
• void glutInitWindowPosition (int x, int y);• Set the size and the position on the screen for GLUT window
• int glutCreateWindow (char *name);• Create window and give it a title/caption
OpenGL API
• void glutDisplayFunc (void (*func) (void));• Sets the display callback for the current window
• void glutMainLoop (void);• Enters the GLUT event processing loop
• void glClearColor (GLfloat red, GLfloat green, GLfloat blue, GLfloat alpha);• Specify clear values for the color buffers
• void glClear (GLbitfield mask);• Clear buffers to preset values
Conclusion
• OpenGL pipeline is the process, from vertex buffers to framebuffer, that your data goes through when you make a single "draw" call in OpenGL.
• Rendering a scene usually involves multiple draw jobs, switching out textures, other uniform state, or shaders between passes and using the framebuffer's depth and stencil buffers to combine the results of each pass.
• Now that we've covered the general dataflow of 3d rendering, we can write a simple program to see how OpenGL makes it all happen.
Reference
• http://www.khronos.org/
• http://viml.nchc.org.tw/blog/
• http://www.opengl.org/wiki/Main_Page
• http://www.g-truc.net/doc/OpenGL%204.4%20Pipeline%20Map.pdf