chapter 11: opengl extensions dl, va, vbo

47
Chapter 11: OpenGL Extensions DL, VA, VBO Graphics Programming, 11th Oct. Graphics and Media Lab. Seoul National University 2011 Fall

Upload: others

Post on 02-Nov-2021

14 views

Category:

Documents


0 download

TRANSCRIPT

Graphics and Media Lab.
2 Graphics and Media Lab. at Seoul National University
void init() {
...
GL_RGB, GL_UNSIGNED_BYTE, bitmap);
↓The details
Specify the image data for this texture object. Now the copy of this texture is existing in the device memory.
Image data residing in the host
3 Graphics and Media Lab. at Seoul National University
CPU
RAM
Vertex
Processor
Pixel
Processor
CPU
RAM
Vertex
Processor
Pixel
Processor
Off- Screen
CPU
RAM
Vertex
Processor
Pixel
Processor
CPU
RAM
Vertex
Processor
Pixel
Processor
Geometry Program
7 Graphics and Media Lab. at Seoul National University
• Probably the best opportunity to learn the essence of two books and more in three weeks.
• Will be example based – Example source codes will be given.
• – Practice it!
• Consortium for OpenGL specification – Formed in 1992.
– Voting members:
• 3Dlabs, Apple, ATI, Dell, IBM, Intel, NVidia, SGI, Sun, Microsoft (quit in 2003)
– Handed over the control of OpenGL specification to Khronos Group on July 31, 2006.
• What Khronos Group handles: – OpenGL, OpenCL, OpenGL ES, WebGL, WebCL, etc.
OpenGL Architecture Review Board
How to Find My OpenGL Version?
‏ Before getting started, you need to know the capability of your own GPU.
‏ Code Example:
What is OpenGL Extension?
– Ex)‏Microsoft‏Windows’‏OpenGL‏API‏=>‏OpenGL1.1‏
‏ For high-level, modern OpenGL functionality, graphics card vendors provide driver-level hidden functions.
– They also ship their cards with vendor-specific functions.
‏ OS vendors provides OS-specific OpenGL functions, and some of them are also hidden by default.
– Ex) wglSwapIntervalEXT for Windows
‏ To use it, we must unveil those hidden functions!
11 Graphics and Media Lab. at Seoul National University
• Doorway to cutting edge 3D rendering functionalities provided by HW/SW vendors
• Classified by: – Vendor specific extensions
– Multivendor extensions
– Extensions approved by OpenGL ARB
• Supports recent advances in the windows systems: – API for Microsoft windows
– API for X window system
What is OpenGL Extension?
‏ Example: Swap Interval for vertical sync.
‏ Using GLUT
glutExtensionSupported(“WGL_EXT_swap_control”);
‏ Microsoft Windows Example
typedef void (*PFNWGLSWAPINTERVALEXTPROC)(int);
– There are over hundreds of extension functions
– You have to perform the above procedure for each of those functions!
– Is there any easier way to use the extensions?
Using OpenGL Extension with Library
‏ There exist external libraries to simplify the process
– Determining the availability, and obtaining the function pointers.
‏ GLEW (OpenGL Extension Wrangler) Library
‏ GLEE (OpenGL Easy Extension) Library
This Class Uses GLEW
– http://glew.sourceforge.net/
• Simple!
• In compiling, use the library files – glew32.lib
– glew32.dll
Initializing GLEW
‏ #include <GL/glew.h>
‏ #include <GL/glut.h>
‏ glutCreateWindow(“...”);
• Example: Vertex buffer object
glewIsSupported(“GL_ARB_vertex_buffer_object”);
Name of the extension
19 Graphics and Media Lab. at Seoul National University
• Specifications of vendors: – ARB: Extensions officially approved by the OpenGL ARB – EXT: Extensions agreed on by multiple OpenGL vendors – NV: NVidia Corporation – ATI: ATI Technologies – SGI: Silicon Graphics – SGIS: Silicon Graphics (specialized) – SGIX: Silicon Graphics (experimental) – …
• Specifications of APIs: – GL: Extensions for OpenGL core – WGL: Extensions for Microsoft windows – GLX: Extensions for X window systems
• Name of token (magic number)
– GL_VENDOR_extension_name • ex. GL_ARB_multitexture
• Example: Vertex buffer object
21 Graphics and Media Lab. at Seoul National University
OpenGL 1.0 (Jan. 1992)
OpenGL 1.1 (Jan. 1997)
Extension Extension ID Functions
Polygon Offsets (depth biasing) EXT_polygon_offset glPolygonOffset
RGBA logical blending EXT_blend_logic_op glBlendFunc
Texture Copy and Sub-copy EXT_subtexture, EXT_copy_texture glTexSubImage1D/2D/3D
Texture Formats EXT_texture RGB, LUMINANCE, ALPHA, INTENSITY (in glT exImage2D)
Texture Objects EXT_texture_object glGenTextures, glBindTextures
OpenGL 1.2 (Mar. 1998)
Extension Extension ID Functions
BGRA Texture Format GL_EXT_bgra BGR_EXT, BGRA_EXT (in glTexImage2D)
Packed Pixels GL_EXT_packed_pixels
Normal Rescaling GL_EXT_rescale_normal
Texture LOD Control SGIS_texture_lod
Image Processing Subset EXT_color_table, EXT_convolution, SGI_color _matrix, EXT_histogram, EXT_blend_color, E XT_blend_minmax
Multi-Texturing SGIS_multitexture glActiveTextureARB, glClientActiveTextureAR B
OpenGL 1.0~1.2
OpenGL 1.3~1.5 OpenGL 1.3 (Aug. 2001)
Extension Extension ID Functions
Fog Coordinates GL_EXT_fog_coord
Texture LOD Bias GL_EXT_texture_lod_bias
Texture Mirrored Repeat GL_ARB_texture_mirrored_repeat
Window Raster Position GL_ARB_window_pos
OpenGL 1.5 (Jul. 2003)
Extension Extension ID Functions
Occlusion Queries GL_ARB_occlusion_query
OpenGL 2.0 (2004)
• OpenGL 2.0 officially added programmable shaders. – Vertex shader augments the previous fixed function transform
and lighting stage
– Fragment shader augments the fragment coloring stage
• GLSL(OpenGL Shading Language) was introduced. – GLSL 1.2 version is most popular.
24 Graphics and Media Lab. at Seoul National University
OpenGL 2.0~2.1
Extension Extension ID Functions
Vertex Shaders (VS) GL_ARB_vertex_shader
Fragment Shaders (FS) GL_ARB_fragment_shader
Non-power-of-two Texture GL_ARB_texture_non_power_of_two
Point Sprites GL_ARB_point_sprite
Multiple Render Targets GL_ARB_draw_buffers not truly "multiple" on ATI cards at that time (GL_MAX_DRAW_BUFFERS=1)
Vertex texture fetch not truly supported on ATI cards at that time (GL_MAX_VERTEX_TEXTURE_IMAGE_UNITS=0)
Separate Blend Equation GL_EXT_blend_equation_separate
Extension Extension ID Functions
Non-Square Matricies glUniformMatrix{}fv
25 Graphics and Media Lab. at Seoul National University
OpenGL 3.0 (2008)
• OpenGL 3.0 introduced the mechanisms for removing features from OpenGL, called the deprecation model. – Why ?
– To catch up the modern graphics technologies.
• The syntax of GLSL 1.3 or higher is somewhat different from GLSL 1.2 – Deprecated:
• gl_Vertex, gl_Normal, g_Color, gl_MultiTexCoord,...
OpenGL 4.0 (2010)
Geometry / Fragment
– Geometry shader can add the generated primitives
Geometry
Shader
Fragment
Shader
OpenGL 3.0~4.2
Extension Extension ID Functions
FBO Multisampling GL_EXT_framebuffer_multisample
Transform Feedback GL_EXT_transform_feedback glBeginTransformFeedback, glBeginTransform Feedback, glBindBufferRange, glTransformFee dbackVaryings
Geometry shader
Tessellation control shader & Tessellation evaluation shader
OpenGL 4.1 with GLSL 4.1 (Jul. 2010)
OpenGL 4.2 with GLSL 4.2 (Aug. 2011)
28 Graphics and Media Lab. at Seoul National University
Our Example Cube
• 24 vertex calls may be required.
Drawing the Cube
0 1
2 3
4 5
6 7
float v[8][3] = { {-1.f,-1.f,-1.f}, { 1.f,-1.f,-1.f}, { 1.f, 1.f,-1.f}, {-1.f, 1.f,-1.f}, // back {-1.f,-1.f, 1.f}, { 1.f,-1.f, 1.f}, { 1.f, 1.f, 1.f}, {-1.f, 1.f, 1.f} // front };
glBegin(GL_QUADS); for(i=0; i<24; i++) { glVertex3fv(vv[i]); } glEnd();
float vv[24][3] = { {-1.f,-1.f,-1.f}, {-1.f, 1.f,-1.f}, { 1.f, 1.f,-1.f}, { 1.f,-1.f,-1.f}, // back {-1.f,-1.f, 1.f}, { 1.f,-1.f, 1.f}, { 1.f, 1.f, 1.f}, {-1.f, 1.f, 1.f}, // front { 1.f, 1.f,-1.f}, {-1.f, 1.f,-1.f}, {-1.f, 1.f, 1.f}, { 1.f, 1.f, 1.f}, // up {-1.f,-1.f,-1.f}, { 1.f,-1.f,-1.f}, { 1.f,-1.f, 1.f}, {-1.f,-1.f, 1.f}, // down { 1.f,-1.f,-1.f}, { 1.f, 1.f,-1.f}, { 1.f, 1.f, 1.f}, { 1.f,-1.f, 1.f}, // right {-1.f, 1.f,-1.f}, {-1.f,-1.f,-1.f}, {-1.f,-1.f, 1.f}, {-1.f, 1.f, 1.f} // left };
30 Graphics and Media Lab. at Seoul National University
• 24 vertex calls may be required. – There can be extra calls for normal, color, and texture.
Drawing the Cube
glBegin(GL_QUADS); for(i=0; i<24; i++) { glVertex3fv(vv[i]); } glEnd(); 0 1
2 3
4 5
6 7
31 Graphics and Media Lab. at Seoul National University
• 24 vertex calls may be required. – There can be extra calls for normal, color, and texture.
– Too many GL command calls!
Drawing the Cube
glBegin(GL_QUADS); for(i=0; i<24; i++) { glColor3fv(cc[i]); glNormal3fv(nn[i]); glVertex3fv(vv[i]); } glEnd();
32 Graphics and Media Lab. at Seoul National University
• Every frame may have to process millions of graphics data: – Vertex coordinates, normals, colors, texture coordinates
– Textures
– OpenGL commands
• Are there more efficient ways? – Yes. OpenGL Extensions have been developed continuously
for this purpose.
• Display list
• Vertex array
33 Graphics and Media Lab. at Seoul National University
• Keeping a group of OpenGL commands in the device memory – Once created & compiled, all the command calls and associated
data are copied into the device memory (i.e.,‏GPU’s‏memory)
– Reduces CPU cycles taken for the transfer
• Usage
‏ glDeleteLists(dlist, 1);
34 Graphics and Media Lab. at Seoul National University
• Instead of sending individual GL commands to GPU, it sends only the data and lets the drawing be done at the GPU side.
• “Vertex‏Array”‏actually‏means‏“Per-Vertex‏Data‏Set” – We can send multiple arrays:
• Vertex array
• Normal array
• Color array
Usage of Vertex Array
With 24 (vertex + color)
Usage of Vertex Array
With 8 (vertex + color + normal) and 24 index
GLubyte ind[24] = { 0, 3, 2, 1, // back 4, 5, 6, 7, // front 2, 3, 7, 6, // up 0, 1, 5, 4, // down 1, 2, 6, 5, // right 3, 0, 4, 7 // left };
0 1
2 3
4 5
6 7
Usage of Vertex Array
Syntax of the Commands
glVertex tends to be deprecated
• OpenGL ES, another OpenGL family for embedded system, does not have glVertex command.
• Microsoft’s‏DirectX‏series‏also‏do‏not‏provide‏ such immediate vertex command.
39 Graphics and Media Lab. at Seoul National University
• Display List – id-based, thus once the display list is defined, no further transfer of
that list is needed. – Once a list is defined, however, it cannot be modified. – Summary: flexibility ↓,‏reusability‏↑
• Vertex Array – Reduces command calls & redundant transfer of shared data. – The context cannot be saved for further referencing.
• There‏isn’t‏any‏such‏thing‏as‏“vertex‏array‏id”. • When program switches among multiple contexts, the arrays must be
resent.
– Summary:‏flexibility‏,↑‏reusability‏↓
• Abstract Buffer Objects – They create buffer objects for vertex or pixel data on the device
memory – They provide functions to reference the data – They can be read and updated by mapping the buffer – Summary:‏flexibility‏↑, reusability ↑
Comparison
40 Graphics and Media Lab. at Seoul National University
• Vertex Buffer Object (VBO) – allows vertex array data to be stored in the
device memory.
– GL_ARB_vertex_buffer_object
• Pixel Buffer Object (PBO) – allows pixel data to be stored in the device
memory for further intra-GPU transfer
– GL_ARB_pixel_buffer_object
stencil) to be stored in non-displayable framebuffers (e.g., texture object, renderbuffer object)
– GL_EXT_framebuffer_object
Usage of Vertex Buffer Object
Similar to creating texture //‏ ;GLuint vboId‏ ;{...} = []float data‏ ;glGenBuffers(1, &vboId)‏ ;glBindBuffer(GL_ARRAY_BUFFER, vboId)‏ ;glBufferData(GL_ARRAY_BUFFER, sizeof(data), data, GL_STATIC_DRAW)‏ ..Drawing is similar to vertex array //‏ ;glEnableClientState(GL_VERTEX_ARRAY)‏ ..glEnableClientState(GL_COLOR_ARRAY); // can enable more arrays‏ glVertexPointer(3, GL_FLOAT, 0, 0); // Starting offset of the buffer‏ ;glColorPointer(3, GL_FLOAT, 0, (void *)(sizeof(data)/2))‏ glDrawArrays(GL_QUADS, 0, 24); // glDrawElements() can be used instead‏ ;glDisableClientState(GL_VERTEX_ARRAY)‏ ;glDisableClientState(GL_COLOR_ARRAY)‏ ..Unbinding vboId //‏ ;glBindBuffer(GL_ARRAY_BUFFER, 0)‏ ..deleting vboId //‏ ;glDeleteBuffers(1, &vboId)‏
With 24 (vertex + color)
Usage of Vertex Buffer Object
With 24 (vertex + color)
float data[48][3] = { {-1.f,-1.f,-1.f}, {-1.f, 1.f,-1.f}, { 1.f, 1.f,-1.f}, { 1.f,-1.f,-1.f}, // back {-1.f,-1.f, 1.f}, { 1.f,-1.f, 1.f}, { 1.f, 1.f, 1.f}, {-1.f, 1.f, 1.f}, // front { 1.f, 1.f,-1.f}, {-1.f, 1.f,-1.f}, {-1.f, 1.f, 1.f}, { 1.f, 1.f, 1.f}, // up {-1.f,-1.f,-1.f}, { 1.f,-1.f,-1.f}, { 1.f,-1.f, 1.f}, {-1.f,-1.f, 1.f}, // down { 1.f,-1.f,-1.f}, { 1.f, 1.f,-1.f}, { 1.f, 1.f, 1.f}, { 1.f,-1.f, 1.f}, // right {-1.f, 1.f,-1.f}, {-1.f,-1.f,-1.f}, {-1.f,-1.f, 1.f}, {-1.f, 1.f, 1.f}, // left { 0.f, 0.f, 0.f}, { 0.f, 0.f, 0.f}, { 0.f, 0.f, 0.f}, { 0.f, 0.f, 0.f}, // back 0, 3, 2, 1 { 0.f, 0.f, 1.f}, { 0.f, 1.f, 0.f}, { 1.f, 0.f, 0.f}, { 1.f, 1.f, 1.f}, // front 4, 5, 6, 7 { 0.f, 0.f, 0.f}, { 0.f, 0.f, 0.f}, { 1.f, 1.f, 1.f}, { 1.f, 0.f, 0.f}, // up 2, 3, 7, 6 { 0.f, 0.f, 0.f}, { 0.f, 0.f, 0.f}, { 0.f, 1.f, 0.f}, { 0.f, 0.f, 1.f}, // down 0, 1, 5, 4 { 0.f, 0.f, 0.f}, { 0.f, 0.f, 0.f}, { 1.f, 0.f, 0.f}, { 0.f, 1.f, 0.f}, // right 1, 2, 6, 5 { 0.f, 0.f, 0.f}, { 0.f, 0.f, 0.f}, { 0.f, 0.f, 1.f}, { 1.f, 1.f, 1.f} // left 3, 0, 4, 7 };
43 Graphics and Media Lab. at Seoul National University
Usage of Vertex Buffer Object
With 8 (vertex + color) and 24 index float v[8][3] = { {-1.f,-1.f,-1.f}, { 1.f,-1.f,-1.f}, { 1.f, 1.f,-1.f}, {-1.f, 1.f,-1.f}, {-1.f,-1.f, 1.f}, { 1.f,-1.f, 1.f}, { 1.f, 1.f, 1.f}, {-1.f, 1.f, 1.f} }; float c[8][3] = { { 0.f, 0.f, 0.f}, { 0.f, 0.f, 0.f}, { 0.f, 0.f, 0.f}, { 0.f, 0.f, 0.f}, { 0.f, 0.f, 1.f}, { 0.f, 1.f, 0.f}, { 1.f, 0.f, 0.f}, { 1.f, 1.f, 1.f} }; GLubyte ind[24] = { 0, 3, 2, 1, // back 4, 5, 6, 7, // front 2, 3, 7, 6, // up 0, 1, 5, 4, // down 1, 2, 6, 5, // right 3, 0, 4, 7 // left };
…Suppose that VBO for vertex, color, normal is already created //‏ ;GLuint vboElementId‏ ;glGenBuffers(1, &vboElementId)‏ ;glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, vboElementId)‏ glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(ind), ind, GL_STATIC_DRAW); ;glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0)‏ ...‏ ;glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, vboElementId)‏ glDrawElements(GL_QUADS, 24, GL_UNSIGNED_BYTE, 0); // Starting offset‏ ;glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0)‏
44 Graphics and Media Lab. at Seoul National University
Another Example Code
With 24 (vertex + normal + color) subdata sets
45 Graphics and Media Lab. at Seoul National University
• Two possible choices 1. Update vertices in the host side, and copy the result to the
buffer in the device memory. • Is not utilizing the VBO mechanism.
2. Map the buffer address to the host side, and change the data with the pointer. • Mapping allows the device memory space to be treated as the
host memory space.
• A better utilization of the VBO mechanism.
Updating VBO
Updated vertices
How to map buffer ?
‏ if (ptr) {
Any Questions ?