OpenGL Introduction 
Bruce Tsai
State machine! 
Primitives! 
Buffers! 
Shaders! 
Attributes and uniforms! 
Drawing
Port Crane 
Crates inside each container are our instances! 
Containers are OpenGL’s objects ! 
textures, shaders, meshes! 
Port crane is OpenGL API 
4 
State Machine
Primitives 
GL_POINTS! 
GL_LINES! 
GL_LINE_STRIP! 
GL_LINE_LOOP! 
GL_TRIANGLES! 
GL_TRIANGLE_STRIP! 
GL_TRIANGLE_FAN 
5
Rendering Settings 
void glPolygonMode(GLenum face, GLenum mode);! 
GL_FRONT_AND_BACK GL_POINT 
! 
void glFrontFace(GLenum mode);! 
! 
void glCullFace(GLenum mode); 
GL_LINE 
GL_FILL 
GL_CW 
GL_CCW 
GL_FRONT 
GL_BACK 
GL_FRONT_AND_BACK 
8
Buffers 
Temporary optimized storage! 
Render buffers! 
Frame buffers! 
Buffer objects 
9
Render Buffer 
Color! 
Store final colored (RGB) image! 
Depth! 
Store final Z depth information! 
Stencil! 
Store visible part of object 
10
Frame Buffer 
Store final image! 
Combination of 
render buffers 
11
Buffer Object 
Hold informations about 3D objects! 
Structures! 
Indices 
12
Structures vs. Indices 
{v1, v2, v3}, {v1, v3, v4}, …! 
! 
{v1, v2, v3, v4, …} 
(0, 1, 2, 0, 2, 3, …) 
13
One Buffer at a Time 
OpenGL 
GL_ARRAY_BUFFER 
GL_ELEMENT_ARRAY_BUFFER 
GL_TEXTURE_BUFFER 
Buffer1 
Buffer2 
Buffer3 
Buffer4
Create Buffer 
void glGenBuffers(GLsizei n, GLuint 
*buffers);! 
! 
void glBindBuffers(GLenum target, GLuint 
buffer); GL_ARRAY_BUFFER 
GL_ELEMENT_ARRAY_BUFFER 
15
Set Buffer Data 
void glBufferData(GLenum target, 
GLsizeiptr size, const GLvoid *data, 
GLenum usage);! 
! 
_STATIC_ 
_DYNAMIC_ 
_STREAM_ 
_DRAW 
_READ 
_COPY 
GL_STATIC_DRAW 
! 
void glBufferSubData(GLenum target, 
GLintptr offset, GLsizeiptr size, const 
GLvoid *data); 
16
Copy Buffer Data 
void glCopyBufferSubData(GLenum 
readtarget, GLenum writetarget, GLintptr 
readoffset, GLintptr writeoffset, 
GLsizeiptr size); 
17
Get Buffer Data 
void glGetBufferSubData(GLenum target, 
GLintptr offset, GLsizeiptr size, GLvoid 
*data); 
18
Access Content Buffer 
void* glMapBuffer(GLenum target, GLenum 
access);! 
GL_READ_ONLY 
GL_WRITE_ONLY 
GL_READ_WRITE 
! 
GLboolean glUnmapBuffer(GLenum target) 
19
Create Shader 
GLuint glCreateShader(GLenum type);! 
! 
void glShaderSource(GLuint shader, 
GLsizei count, const GLchar **string, 
const GLint *length);! 
void glCompileShader(GLuint shader); 
21 
GL_VERTEX_SHADER 
GL_FRAGMENT_SHADER
Create Program 
GLuint glCreateProgram(void);! 
void glAttachShader(GLuint program, 
GLuint shader);! 
void glLinkProgram(GLuint program);! 
void glUseProgram(GLuint program); 
22
Attributes and Uniforms
Attribute Location 
1. Defined fixed location in shader! 
2. Bind attribute location before linking shader code! 
void glBindAttribLocation(GLuint program, 
GLuint index, const GLchar *name);! 
3. Get attribute location after linking shader code! 
GLint glGetAttribLocation(GLuint program, 
const GLchar *name); 
24
Uniform Location 
Get uniform location after linking shader code! 
GLint glGetUniformLocation(GLuint 
program, const GLchar *name); 
25
Define Attribute Values 
void glVertexAttrib{1234}f(GLuint index, 
GLfloat value[N]);! 
void glVertexAttrib{1234}fv(GLuint index, 
const GLfloat *values); 
26
Attribute Pointer 
void glVertexAttribPointer(GLuint index, 
GLint size, GLenum type, GLboolean 
normalized, GLsizei stride, const GLvoid 
*ptr);! 
! 
Required while using glVertexAttribPointer 
void glGenVertexArrays(GLsizei n, GLuint 
*arrays);! 
void glBindVertexArrays(GLuint array); 
27
Using Attribute Pointer 
void glEnableVertexAttribArray(GLuint 
index);! 
void glDisableVertexAttribArray(GLuint 
index); 
28
Define Uniform Values 
void glUniform{1234}{if}(GLint index, T 
value[N]);! 
void glUniform{1234}{if}v(GLint index, 
GLsizei count, const T *value);! 
void glUniformMatrix{234}fv(GLint index, 
GLsizei count, GLboolean transpose, const 
GLfloat *value); 
29
Clear 
void glClearColor(GLflozt red, GLfloat 
green, GLfloat blue, GLfloat alpha);! 
void glClear(GLbitfield mask); 
30 
GL_COLOR_BUFFER_BIT 
GL_DEPTH_BUFFER_BIT 
GL_STENCIL_BUFFER_BIT
Drawing 
void glDrawArrays(GLenum mode, GLint 
first, GLsizei count);! 
! 
! 
void glDrawElements(GLenum mode, GLsizei 
count, GLenum type, const GLvoid 
*indices); 
31 
GL_POINTS 
GL_LINES 
GL_LINE_STRIP 
GL_LINE_LOOP 
GL_TRIANGLES 
GL_TRIANGLE_STRIP 
GL_TRIANGLE_FAN 
GL_UNSIGNED_BYTE 
GL_UNSIGNED_SHORT 
GL_UNSIGNED_INT
Restarting Index 
void glPrimitiveRestartIndex(GLuint 
index); 
32
Sample 
v0! 
p(-1,-1,0,1)! 
c(1,1,1,1) 
v1! 
p(1,-1,0,1)! 
c(1,1,0,1) 
v2! 
p(-1,1,0,1)! 
c(1,0,1,1) 
v3! 
p(1,1,0,1)! 
c(0,1,1,1)
References 
http://blog.db-in.com/all-about-opengl-es-2-x-part-1/! 
http://www.dreamstime.com/stock-images-port-cranes-image25107404 
! 
http://blog.db-in.com/all-about-opengl-es-2-x-part-2/! 
OpenGL Programming Guide

OpenGL Introduction

  • 1.
  • 2.
    State machine! Primitives! Buffers! Shaders! Attributes and uniforms! Drawing
  • 4.
    Port Crane Cratesinside each container are our instances! Containers are OpenGL’s objects ! textures, shaders, meshes! Port crane is OpenGL API 4 State Machine
  • 5.
    Primitives GL_POINTS! GL_LINES! GL_LINE_STRIP! GL_LINE_LOOP! GL_TRIANGLES! GL_TRIANGLE_STRIP! GL_TRIANGLE_FAN 5
  • 8.
    Rendering Settings voidglPolygonMode(GLenum face, GLenum mode);! GL_FRONT_AND_BACK GL_POINT ! void glFrontFace(GLenum mode);! ! void glCullFace(GLenum mode); GL_LINE GL_FILL GL_CW GL_CCW GL_FRONT GL_BACK GL_FRONT_AND_BACK 8
  • 9.
    Buffers Temporary optimizedstorage! Render buffers! Frame buffers! Buffer objects 9
  • 10.
    Render Buffer Color! Store final colored (RGB) image! Depth! Store final Z depth information! Stencil! Store visible part of object 10
  • 11.
    Frame Buffer Storefinal image! Combination of render buffers 11
  • 12.
    Buffer Object Holdinformations about 3D objects! Structures! Indices 12
  • 13.
    Structures vs. Indices {v1, v2, v3}, {v1, v3, v4}, …! ! {v1, v2, v3, v4, …} (0, 1, 2, 0, 2, 3, …) 13
  • 14.
    One Buffer ata Time OpenGL GL_ARRAY_BUFFER GL_ELEMENT_ARRAY_BUFFER GL_TEXTURE_BUFFER Buffer1 Buffer2 Buffer3 Buffer4
  • 15.
    Create Buffer voidglGenBuffers(GLsizei n, GLuint *buffers);! ! void glBindBuffers(GLenum target, GLuint buffer); GL_ARRAY_BUFFER GL_ELEMENT_ARRAY_BUFFER 15
  • 16.
    Set Buffer Data void glBufferData(GLenum target, GLsizeiptr size, const GLvoid *data, GLenum usage);! ! _STATIC_ _DYNAMIC_ _STREAM_ _DRAW _READ _COPY GL_STATIC_DRAW ! void glBufferSubData(GLenum target, GLintptr offset, GLsizeiptr size, const GLvoid *data); 16
  • 17.
    Copy Buffer Data void glCopyBufferSubData(GLenum readtarget, GLenum writetarget, GLintptr readoffset, GLintptr writeoffset, GLsizeiptr size); 17
  • 18.
    Get Buffer Data void glGetBufferSubData(GLenum target, GLintptr offset, GLsizeiptr size, GLvoid *data); 18
  • 19.
    Access Content Buffer void* glMapBuffer(GLenum target, GLenum access);! GL_READ_ONLY GL_WRITE_ONLY GL_READ_WRITE ! GLboolean glUnmapBuffer(GLenum target) 19
  • 21.
    Create Shader GLuintglCreateShader(GLenum type);! ! void glShaderSource(GLuint shader, GLsizei count, const GLchar **string, const GLint *length);! void glCompileShader(GLuint shader); 21 GL_VERTEX_SHADER GL_FRAGMENT_SHADER
  • 22.
    Create Program GLuintglCreateProgram(void);! void glAttachShader(GLuint program, GLuint shader);! void glLinkProgram(GLuint program);! void glUseProgram(GLuint program); 22
  • 23.
  • 24.
    Attribute Location 1.Defined fixed location in shader! 2. Bind attribute location before linking shader code! void glBindAttribLocation(GLuint program, GLuint index, const GLchar *name);! 3. Get attribute location after linking shader code! GLint glGetAttribLocation(GLuint program, const GLchar *name); 24
  • 25.
    Uniform Location Getuniform location after linking shader code! GLint glGetUniformLocation(GLuint program, const GLchar *name); 25
  • 26.
    Define Attribute Values void glVertexAttrib{1234}f(GLuint index, GLfloat value[N]);! void glVertexAttrib{1234}fv(GLuint index, const GLfloat *values); 26
  • 27.
    Attribute Pointer voidglVertexAttribPointer(GLuint index, GLint size, GLenum type, GLboolean normalized, GLsizei stride, const GLvoid *ptr);! ! Required while using glVertexAttribPointer void glGenVertexArrays(GLsizei n, GLuint *arrays);! void glBindVertexArrays(GLuint array); 27
  • 28.
    Using Attribute Pointer void glEnableVertexAttribArray(GLuint index);! void glDisableVertexAttribArray(GLuint index); 28
  • 29.
    Define Uniform Values void glUniform{1234}{if}(GLint index, T value[N]);! void glUniform{1234}{if}v(GLint index, GLsizei count, const T *value);! void glUniformMatrix{234}fv(GLint index, GLsizei count, GLboolean transpose, const GLfloat *value); 29
  • 30.
    Clear void glClearColor(GLfloztred, GLfloat green, GLfloat blue, GLfloat alpha);! void glClear(GLbitfield mask); 30 GL_COLOR_BUFFER_BIT GL_DEPTH_BUFFER_BIT GL_STENCIL_BUFFER_BIT
  • 31.
    Drawing void glDrawArrays(GLenummode, GLint first, GLsizei count);! ! ! void glDrawElements(GLenum mode, GLsizei count, GLenum type, const GLvoid *indices); 31 GL_POINTS GL_LINES GL_LINE_STRIP GL_LINE_LOOP GL_TRIANGLES GL_TRIANGLE_STRIP GL_TRIANGLE_FAN GL_UNSIGNED_BYTE GL_UNSIGNED_SHORT GL_UNSIGNED_INT
  • 32.
    Restarting Index voidglPrimitiveRestartIndex(GLuint index); 32
  • 33.
    Sample v0! p(-1,-1,0,1)! c(1,1,1,1) v1! p(1,-1,0,1)! c(1,1,0,1) v2! p(-1,1,0,1)! c(1,0,1,1) v3! p(1,1,0,1)! c(0,1,1,1)
  • 34.