Top Banner
Introduction of OpenGL Gary
39
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Introduction  of openGL

Introduction of OpenGLGary

Page 2: Introduction  of openGL

Outline

• Overview of OpenGL

• Libraries

• OpenGL Pipeline

• OpenGL Programming

Page 3: Introduction  of openGL

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

Page 4: Introduction  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

Page 5: Introduction  of openGL

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

Page 6: Introduction  of openGL

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

Page 7: Introduction  of openGL

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

Page 8: Introduction  of openGL

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

Page 9: Introduction  of openGL

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

Page 10: Introduction  of openGL

OpenGL Pipeline

• A schematic diagram

• openGL 4.4

DrawProcessing

VertexProcessing

Tessellation

PrimitiveProcessing

Rasterization

TransformFeedback

FragmentProcessing

PixelProcessing

Page 11: Introduction  of openGL

Draw Processing

DrawProcessing

VertexProcessing

Tessellation

PrimitiveProcessing

Rasterization

TransformFeedback

FragmentProcessing

PixelProcessing

Page 12: Introduction  of openGL

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

Page 13: Introduction  of openGL

Vertex Processing

DrawProcessing

VertexProcessing

Tessellation

PrimitiveProcessing

Rasterization

TransformFeedback

FragmentProcessing

PixelProcessing

Page 14: Introduction  of openGL

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

Page 15: Introduction  of openGL

Tessellation

DrawProcessing

VertexProcessing

Tessellation

PrimitiveProcessing

Rasterization

TransformFeedback

FragmentProcessing

PixelProcessing

Page 16: Introduction  of openGL

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

Page 17: Introduction  of openGL

Tessellation

Tessellation invocations turn a rough model into a smooth model

Page 18: Introduction  of openGL

Primitive Processing

DrawProcessing

VertexProcessing

Tessellation

PrimitiveProcessing

Rasterization

TransformFeedback

FragmentProcessing

PixelProcessing

Page 19: Introduction  of openGL

Primitive Processing

• Geometry Shader

• Governs the processing of primitives

• Take a single primitive as input and may output zero or more primitives

Page 20: Introduction  of openGL

Primitive Processing

The shader takes a triangle as input and outputs 3 lines that represent the normal for each vertex

Page 21: Introduction  of openGL

Transform Feedback

DrawProcessing

VertexProcessing

Tessellation

PrimitiveProcessing

Rasterization

TransformFeedback

FragmentProcessing

PixelProcessing

Page 22: Introduction  of openGL

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.

Page 23: Introduction  of openGL

Rasterization

DrawProcessing

VertexProcessing

Tessellation

PrimitiveProcessing

Rasterization

TransformFeedback

FragmentProcessing

PixelProcessing

Page 24: Introduction  of openGL

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

Page 25: Introduction  of openGL

Fragment Processing

DrawProcessing

VertexProcessing

Tessellation

PrimitiveProcessing

Rasterization

TransformFeedback

FragmentProcessing

PixelProcessing

Page 26: Introduction  of openGL

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

Page 27: Introduction  of openGL

Fragment Processing

Vertex Shader Geometry Shader Pixel Shader

Page 28: Introduction  of openGL

Pixel Processing

DrawProcessing

VertexProcessing

Tessellation

PrimitiveProcessing

Rasterization

TransformFeedback

FragmentProcessing

PixelProcessing

Page 29: Introduction  of openGL

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

Page 30: Introduction  of openGL

OpenGL pipeline

Page 31: Introduction  of openGL

Environment

• Visual Studio 2012

• FreeGLUT• http://freeglut.sourceforge.net/

• 2.8.1 version

• GLEW• http://glew.sourceforge.net/

• 1.10.0 version

Page 32: Introduction  of openGL

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.

Page 33: Introduction  of openGL

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

Page 34: Introduction  of openGL

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”

Page 35: Introduction  of openGL

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

Page 36: Introduction  of openGL

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

Page 37: Introduction  of openGL

OpenGL API

Page 38: Introduction  of openGL

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.

Page 39: Introduction  of openGL

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