Introduction to Computer Graphics Hardware Acceleration Review
OpenGL Project Setup Create a command-line style project in Xcode 4 Select the project file and click Build Phases tab Add OpenGL.framework and GLUT.framework to Link list
OpenGL Project Setup Create a command-line style project in target IDE (or not...) Install libglut3-dev and libglew-dev using a package manager When compiling reference OpenGL, GLUT, GLEW libraries eg. gcc main.c -o a.out -I/usr/include -L/usr/lib -lglut -lglew -lgl
OpenGL Project Setup Create a command-line style project in Visual Studio 2010 Download and install GLUT - http://user.xmission.com/~nate/glut.html glut.h: C:\Program Files\Microsoft Visual Studio 10.0\VC\include\GL\ glut32.lib: C:\Program Files\Microsoft Visual Studio 10.0\VC\lib\ glut32.dll: C:\Windows\System32\ (or C:\Windows\SysWOW64\ for 64-bit) Download and install GLEW - http://glew.sourceforge.net/ glew.h, glxew.h, wglew.h: C:\Program Files\Microsoft Visual Studio 10.0\VC\include \GL\ glew32.lib: C:\Program Files\Microsoft Visual Studio 10.0\VC\lib\ glew32.dll, glew32s.dll: C:\Windows\System32\ (or C:\Windows\SysWOW64\) Add OpenGL, GLUT, and GLEW library files to project link stage (opengl32.lib, glut32.lib, glew32.lib) Also see http://openglbook.com/setting-up-opengl-glew-and-freeglut-in-visual-c/
Hardware Acceleration
Hardware Acceleration
Hardware Acceleration 2 Processors
Hardware Acceleration
Hardware Acceleration 192 Processors
Data read from Scene and OBJ files Back-face Culling Homogeneous Divide Rasterizer Model View Matrix Projection Matrix Viewport Transform Fragments resulting from rasterization Raster Depth Test Texturing Lighting Blending
Data read from Scene and OBJ files Back-face Culling Homogeneous Divide Rasterizer Model View Matrix Projection Matrix Viewport Transform Fragments resulting from rasterization Raster Depth Test Texturing Lighting Blending
Data read from Scene and OBJ files OpenGL ES Primitive Processing Vertex Shader OpenGL ES Rasterizer Fragments resulting from rasterization Frame Buffer Fragment Shader OpenGL ES Fragment Processing
OpenGL Shading Language Defines a C-like language that can be compiled into GPU instructions Floating-point, Integer, and Boolean data types Vectors of these types in 2, 3, 4 sizes Matrices of floats in 2x2, 3x3, 4x4 1D-Arrays and Structures Special Texture types Operators on all these types
OpenGL Shading Language Variable storage qualifiers - attribute, uniform, varying Variable precision qualifiers - lowp, mediump, highp Control Statements - if, else Loops - for, while, do-while Jumps - break, continue, return Function Definition Pre-processor Directives
OpenGL Shading Language Built-in functions for basic mathematics, trigonometry, geometry, and texturing (eg. exp, tan, cross, texture2d) Built-in variables to represent inputs and outputs Vertex Shader gl_position (vec4, out) Fragment Shader gl_fragcoord (vec2, in) gl_fragcolor (vec4, out)
Host-GPU Data Transfer Global values are sent to the graphics processing hardware by changing the values of uniform variables defined in the shaders using glgetunifromlocation and gluniform* calls Geometry data is sent to the graphics processing hardware by using glvertexattribpointer Textures are sent to the graphics processing hardware by calling glbindtexture and glteximage2d Drawing is initiated by calling gldrawarrays
Parallelism Notes Be careful with the use of control structures, especially branches and loops, to preserve efficiency Code is executing in parallel, so processor groups must all terminate before starting a new batch of data Remember that this is a Single Instruction Multiple Data system (SIMD) with scattering & gathering restrictions Possible to do general computation using GLSL, but it is best to use parallel computation specific APIs like OpenCL or CUDA
Data read from Scene and OBJ files OpenGL ES Primitive Processing Vertex Shader OpenGL ES Rasterizer Fragments resulting from rasterization Frame Buffer Fragment Shader OpenGL ES Fragment Processing
Vertex Shader Receives a vertex from OpenGL after minimal processing Modifies incoming vertex in some way Outputs the vertex May also output additional data for the fragment shader to use
Vertex Shader attribute vec4 position; void main() { gl_position = position; }
Vertex Shader attribute vec4 position; uniform mat4 modelview; void main() { gl_position = modelview * position; }
Vertex Shader attribute vec4 position; uniform mat4 modelview; uniform mat4 projection; void main() { gl_position = projection * modelview * position; }
Vertex Shader attribute vec4 position; attribute vec2 texturecoordinate; uniform mat4 modelview; uniform mat4 projection; varying lowp vec2 texturecoordinatevarying; void main() { gl_position = projection * modelview * position; texturecoordinatevarying = texturecoordinate; }
Vertex Shader attribute vec4 position; attribute vec3 normal; attribute vec2 texturecoordinate; uniform mat4 modelview; uniform mat4 projection; varying lowp vec3 normalvarying; varying lowp vec2 texturecoordinatevarying; void main() { gl_position = projection * modelview * position; normalvarying = vec3(normalize(modelview * vec4(normal, 0.0))); texturecoordinatevarying = texturecoordinate; }
Vertex Shader attribute vec4 position; attribute vec3 normal; attribute vec2 texturecoordinate; uniform mat4 modelview; uniform mat4 projection; uniform vec4 lightposition; uniform vec4 lightambient; uniform vec4 lightdiffuse; varying lowp vec3 normalvarying; varying lowp vec2 texturecoordinatevarying; varying lowp vec4 diffusevarying; void main() { gl_position = projection * modelview * position; normalvarying = vec3(normalize(modelview * vec4(normal, 0.0))); texturecoordinatevarying = texturecoordinate; } vec4 lightvector = normalize(lightposition - gl_position); float lightincidence = dot(lightvector, vec4(normalvarying, 1.0)); diffusevarying = lightdiffuse * vec4(max(lightincidence, 0.0));
Data read from Scene and OBJ files OpenGL ES Primitive Processing Vertex Shader OpenGL ES Rasterizer Fragments resulting from rasterization Frame Buffer Fragment Shader OpenGL ES Fragment Processing
Fragment Shader Receives a fragment from OpenGL resulting from rasterizing a primitive Chooses a color for the fragment based on data given by vertex shader Outputs the fragment color
Fragment Shader void main() { gl_fragcolor = vec4(1.0, 1.0, 0.0, 1.0); }
Fragment Shader uniform vec4 color; void main() { gl_fragcolor = color; }
Fragment Shader varying vec4 color; void main() { gl_fragcolor = color; }
Fragment Shader uniform sampler2d textureunit; varying vec2 texturecoordinate; void main() { gl_fragcolor = texture2d(textureunit, texturecoordinate); }
Fragment Shader uniform vec4 color; uniform vec4 lightpositioneyecoords; varying vec4 positioneyecoords; varying vec3 normaleyecoords; void main() { vec3 incident = vec3(normalize(lightpositioneyecoords - positioneyecoords)); vec3 normal = normaleyecoords; float incidence = max(0.0, dot(incident, normal)); gl_fragcolor = color * incidence; }
Data read from Scene and OBJ files OpenGL ES Primitive Processing Vertex Shader OpenGL ES Rasterizer Fragments resulting from rasterization Frame Buffer Fragment Shader OpenGL ES Fragment Processing
Short Example Program #include <GL/glew.h> // Not needed on Mac #include <GL/glut.h> // <GLUT/glut.h> on Mac void draw(); int main(int argc, char** argv) {! glutinit(&argc, argv);! glutinitwindowsize(800, 600);! glutinitdisplaymode(glut_double GLUT_RGBA GLUT_DEPTH);! glutcreatewindow("opengl");! GLenum err = glewinit(); // Not needed on Mac! if (err!= GLEW_OK)!! ; // Output error message gluseprogram(0);! glutdisplayfunc(draw);! glutmainloop();! return 0; } void draw() {! glclearcolor(0.8f, 0.0f, 0.8f, 1.0f);! glclear(gl_color_buffer_bit);! glutswapbuffers(); }
Noise Procedural geometry needs smooth randomness Randomness should be repeatable based on inputs 1D, 2D, 3D, etc. versions of this are often called noise Ken Perlin created a good basis for noise functions http://mrl.nyu.edu/~perlin/doc/oscar.html http://mrl.nyu.edu/~perlin/noise/ http://www.dreamincode.net/forums/topic/66480-perlin-noise/ http://devmag.org.za/2009/04/25/perlin-noise/
Noise
Noise
OpenGL ES Reference Card http://www.khronos.org/ opengles/2_x/