Vertex Buffer Objects

Similar documents
Vertex Buffer Objects. Vertex Buffer Objects: The Big Idea

OpenCL / OpenGL Vertex Buffer Interoperability: A Particle System Case Study

OpenCL / OpenGL Vertex Buffer Interoperability: A Particle System Case Study

Building Models. Prof. George Wolberg Dept. of Computer Science City College of New York

CS 450: COMPUTER GRAPHICS REVIEW: STATE, ATTRIBUTES, AND OBJECTS SPRING 2015 DR. MICHAEL J. REALE

8 Three-Dimensional Object Representations. Chapter 8. Three-Dimensional Object Representations. Department of Computer Science and Engineering 8-1

Computer Graphics CS 543 Lecture 4 (Part 2) Building 3D Models (Part 2)

CENG 477 Introduction to Computer Graphics. Graphics Hardware and OpenGL

CSE 4431/ M Advanced Topics in 3D Computer Graphics. TA: Margarita Vinnikov

A Review of OpenGL Compute Shaders

Building Models. CS 537 Interactive Computer Graphics Prof. David E. Breen Department of Computer Science

GLSL Overview: Creating a Program

CS 432 Interactive Computer Graphics

Starting out with OpenGL ES 3.0. Jon Kirkham, Senior Software Engineer, ARM

CS 543 Lecture 1 (Part 3) Prof Emmanuel Agu. Computer Science Dept. Worcester Polytechnic Institute (WPI)

CS452/552; EE465/505. Review & Examples

CS475/CS675 - Computer Graphics. OpenGL Drawing

Building Models. Objectives Introduce simple data structures for building polygonal models. Vertex lists Edge lists

Computer Graphics (CS 543) Lecture 4a: Linear Algebra for Graphics (Points, Scalars, Vectors)

GEOMETRIC OBJECTS AND TRANSFORMATIONS I

Comp 410/510 Computer Graphics Spring Programming with OpenGL Part 2: First Program

Building Models. Objectives. Introduce simple data structures for building polygonal models. OpenGL vertex arrays. Vertex lists Edge lists

Tutorial 1: Your First Triangle!

Display Lists. Conceptually similar to a graphics file. In client-server environment, display list is placed on server

Building Models. Ed Angel Professor of Computer Science, Electrical and Computer Engineering, and Media Arts University of New Mexico

OpenGL pipeline Evolution and OpenGL Shading Language (GLSL) Part 2/3 Vertex and Fragment Shaders

COMP3421. Modeling, Bezier Curves, L-Systems, VBOs

COMP371 COMPUTER GRAPHICS

Information Coding / Computer Graphics, ISY, LiTH. OpenGL! ! where it fits!! what it contains!! how you work with it 11(40)

Drawing with OpenGL. Chapter 3. Chapter Objectives

OpenGL Compute Shaders

3 Meshes. Strips, Fans, Indexed Face Sets. Display Lists, Vertex Buffer Objects, Vertex Cache

UNIT 4 GEOMETRIC OBJECTS AND TRANSFORMATIONS-1

3d Programming I. Dr Anton Gerdelan

Texture Mapping. Mike Bailey.

WebGL A quick introduction. J. Madeira V. 0.2 September 2017

Vertex Buffers. Mike Bailey. What is a Vertex Buffer?

Vertex Buffers. Mike Bailey. Computer Graphics VertexBuffers.pptx

CS Computer Graphics: OpenGL, Continued

CS Computer Graphics: OpenGL, Continued

Mali & OpenGL ES 3.0. Dave Shreiner Jon Kirkham ARM. Game Developers Conference 27 March 2013

CS452/552; EE465/505. Image Processing Frame Buffer Objects

CS 548: COMPUTER GRAPHICS PORTRAIT OF AN OPENGL PROGRAM SPRING 2015 DR. MICHAEL J. REALE

OpenGL Compute Shaders

Tutorial 04. Harshavardhan Kode. September 14, 2015

Performance OpenGL Programming (for whatever reason)

How OpenGL Works. Retained Mode. Immediate Mode. Introduction To OpenGL

Programming with OpenGL Complete Programs Objectives Build a complete first program

OpenGL. Jimmy Johansson Norrköping Visualization and Interaction Studio Linköping University

Computer graphics Labs: OpenGL (2/2) Vertex Shaders and Fragment Shader

Comp 410/510 Computer Graphics Spring Programming with OpenGL Part 3: Shaders

Lecture 2 2D transformations Introduction to OpenGL

CS 4731 Lecture 3: Introduction to OpenGL and GLUT: Part II. Emmanuel Agu

EECS 487: Interactive Computer Graphics

Geometry Shaders. And how to use them

Introduction to the OpenGL Shading Language (GLSL)

CSE 167. Discussion 03 ft. Glynn 10/16/2017

Lectures Display List

Image Rendering. Rendering can be divided into three sub-problems. Image Formation The hidden surface problem visibility determination steps

Computer Graphics. Prof. Feng Liu. Fall /14/2016

The Basic Computer Graphics Pipeline, OpenGL-style. Introduction to the OpenGL Shading Language (GLSL)

The GLSL API. Mike Bailey. Oregon State University. Geometry Shader. Program. The GLSL Shader-creation Process. create. compile. Vertex.

Shaders. Slide credit to Prof. Zwicker

Computer Graphics (CS 543) Lecture 3b: Shader Setup & GLSL Introduction

Coding OpenGL ES 3.0 for Better Graphics Quality

API Background. Prof. George Wolberg Dept. of Computer Science City College of New York

Programming with WebGL Part 1: Background. CS 432 Interactive Computer Graphics Prof. David E. Breen Department of Computer Science

6.S096 Lecture 9 Visualization

Objectives. Programming with WebGL Part 1: Background. Retained vs. Immediate Mode Graphics. Early History of APIs. PHIGS and X.

CS 432 Interactive Computer Graphics

Comp 410/510 Computer Graphics Spring Programming with OpenGL Part 4: Three Dimensions

Graphics Programming. Computer Graphics, VT 2016 Lecture 2, Chapter 2. Fredrik Nysjö Centre for Image analysis Uppsala University

Computer Graphics (CS 543) Lecture 1 (Part 2): Introduction to OpenGL/GLUT (Part 1)

to OpenGL Introduction Pipeline Graphics pipeline OpenGL pipeline OpenGL syntax Modeling Arrays Conclusion 1 Introduction Introduction to OpenGL

Introductory Seminar

Introduction to Computer Graphics with WebGL

Introduction to Computer Graphics with OpenGL/GLUT

Models and Architectures. Angel and Shreiner: Interactive Computer Graphics 7E Addison-Wesley 2015

CS452/552; EE465/505. Image Formation

the gamedesigninitiative at cornell university Lecture 12 Scene Graphs

We assume that you are familiar with the following:

CSE 167: Introduction to Computer Graphics Lecture #7: GLSL. Jürgen P. Schulze, Ph.D. University of California, San Diego Spring Quarter 2016

EECE 478. Learning Objectives. Learning Objectives. Rasterization & Scenes. Rasterization. Compositing

RECITATION - 1. Ceng477 Fall

Rendering. Part 1 An introduction to OpenGL

ERKELEY DAVIS IRVINE LOS ANGELES RIVERSIDE SAN DIEGO SAN FRANCISCO EECS 104. Fundamentals of Computer Graphics. OpenGL

Normalized Device Coordinate System (NDC) World Coordinate System. Example Coordinate Systems. Device Coordinate System

World Coordinate System

CS380: Computer Graphics Screen Space & World Space. Sung-Eui Yoon ( 윤성의 ) Course URL:

Tutorial 12: Real-Time Lighting B

GPU Programming EE Midterm Examination

GPU Programming EE Final Examination

Exercise 1 Introduction to OpenGL

CS 4204 Computer Graphics

The Transition from RenderMan to the OpenGL Shading Language (GLSL)

Introduction to OpenGL

COSC342: Computer Graphics

Programming with OpenGL Part 5: More GLSL. Ed Angel Professor Emeritus of Computer Science University of New Mexico

WebGL: Hands On. DevCon5 NYC Kenneth Russell Software Engineer, Google, Inc. Chair, WebGL Working Group

GLSL Geometry Shaders

Transcription:

1 Vertex Buffer Objects This work is licensed under a Creative Commons Attribution-NonCommercial- NoDerivatives 4.0 International License Mike Bailey mjb@cs.oregonstate.edu VertexBuffers.pptx

Vertex Buffer Objects: The Big Idea 2 Store vertex coordinates and vertex attributes on the graphics card. Optionally store the connections on the graphics card too. Every time you go to redraw, coordinates will be pulled from GPU memory, avoiding a potentially significant amount of bus latency.

The Cube Can Also Be Defined with Triangles 3 6 2 0 1 4 5 GLuint CubeQuadIndices[ ][4] = { { 0, 2, 3, 1 }, { 4, 5, 7, 6 }, { 1, 3, 7, 5 }, { 0, 4, 6, 2 }, { 2, 6, 7, 3 }, { 0, 1, 5, 4 } }; 7 3 GLuint CubeTriangleIndices[ ][3] = { { 0, 2, 3 }, { 0, 3, 1 }, { 4, 5, 7 }, { 4, 7, 6 }, { 1, 3, 7 }, { 1, 7, 5 }, { 0, 4, 6 }, { 0, 6, 2 }, { 2, 6, 7 }, { 2, 7, 3 }, { 0, 1, 5 } { 0, 5, 4 } };

Did any of you ever watch Star Trek: Deep Space Nine? 4 It was about life aboard a space station. Ships docked there to unload cargo and pick up supplies. When a ship was docked at docking port A, for instance, the supply-loaders didn t need to know what ship it was. They could just be told, send these supplies out docking port A, and pick up this cargo from docking port A. B A Surprisingly, this has something to do with computer graphics! D C

The OpenGL Rendering Context 5 The OpenGL Rendering Context (also called the state ) contains all the characteristic information necessary to produce an image from geometry. This includes the current transformation, color, lighting, textures, where to send the display, etc. Each window (e.g., glutcreatewindow) has its own rendering context. vertices, colors, normal, texture coordinates vertex connection indices Array Buffer Color Element Array Buffer Lighting Texture0 Texture1 Context Transf. Display Dest.

More Background Binding to the Context 6 The OpenGL term binding refers to attaching or docking (a metaphor which I find to be more visually pleasing) an OpenGL object to the Context. You can then assign characteristics, and they will flow in through the Context into the object. Vertex Buffer Object pointed to by bufa glbindbuffer( GL_ARRAY_BUFFER, bufa); glbufferdata( GL_ARRAY_BUFFER, numbytes, data, usage ); Array Buffer Color Element Texture0 Texture1 Array Buffer Context Lighting Transf. Ships docked there to unload cargo and pick up supplies. When a ship was docked at docking port A, for instance, the supply-loaders didn t need to know what ship it was. They could just be told, send these supplies out docking port A, and pick up this cargo from docking port A.

More Background Binding to the Context 7 When you want to use that Vertex Buffer Object, just bind it again. All of the characteristics will then be active, just as if you had specified them again. Its contents will flow out of the object into the Context. Vertex Buffer Object glbindbuffer( GL_ARRAY_BUFFER, bufa); gldrawarrays( GL_TRIANGLES, 0, numvertices ); Array Buffer Color Element Texture0 Texture1 Array Buffer Context Lighting Transf Color

If you were writing OpenGL in C, how would you implement the State? 8 You would probably make all of that information into a big C struct struct OpenGLState { float CurrentColor[3]; float CurrentLineWidth; float CurrentMatrix[4][4]; struct TextureObject *CurrentTexture0; struct ArrayBufferObject *CurrentArrayBuffer;... } TheState;

If you were writing OpenGL in C, how would you implement the State? 9 Then, you could create any number of Array Buffer Objects, each with its own data and parameters. When you want to make a particular Array Buffer current, you just need to bind it to the state: TheState.CurrentArrayBuffer = GpuAddressOf( bufa ); and, when you want to do something with it or to it, you just need to do an indirection: TheState.CurrentBuffer->data[0].x = 42.;

More Background How do you Create an OpenGL Buffer Object? 10 When creating data structures in C++, objects are pointed to by their addresses. In OpenGL, objects are pointed to by an unsigned integer handle. You can assign a value for this handle yourself (not recommended), or have OpenGL generate one for you that is guaranteed to be unique. For example: how many handles to generate GLuint bufa; the array to put them in glgenbuffers( 1, &bufa ); OpenGL then uses these handles to determine the actual GPU memory addresses to use.

Loading data into the currently-bound Vertex Buffer Object 11 glbufferdata( type, numbytes, data, usage ); type is the type of buffer object this is: Use GL_ARRAY_BUFFER to store floating point vertices, normals, colors, and texture coordinates numbytes is the number of bytes to store all together. It s not the number of numbers, not the number of coordinates, not the number of vertices, but the number of bytes! data is the memory address of (i.e., pointer to) the data to be transferred from CPU memory to the graphics memory. (This is allowed to be NULL, indicating that you will transfer the data over later.)

Loading data into the currently-bound Vertex Buffer Object 12 glbufferdata( type, numbytes, data, usage ); usage is a hint as to how the data will be used: GL_xxx_yyy where xxx can be: STATIC DYNAMIC and yyy can be: DRAW READ this buffer will be re-written seldom this buffer will be re-written often this buffer will be used for drawing this buffer will be copied into For what we are doing in most classes, use GL_STATIC_DRAW Adding data into the current Vertex Buffer Object glbuffersubdata( type, offset, numbytes, data );

Step #1 Fill the C/C++ Arrays with Drawing Data (vertices, colors, ) 13 GLfloat Vertices[ ][3] = { { 1., 2., 3. }, { 4., 5., 6. },... }; Step #2 Transfer the Drawing Data glgenbuffers( 1, &bufa ); glbindbuffer( GL_ARRAY_BUFFER, bufa ); glbufferdata( GL_ARRAY_BUFFER, 3*sizeof(GLfloat)*numVertices, Vertices, GL_STATIC_DRAW );

Step #3 Activate the Drawing Data Types That You Are Using 14 glenableclientstate( type ) where type can be any of: GL_VERTEX_ARRAY GL_COLOR_ARRAY GL_NORMAL_ARRAY GL_TEXTURE_COORD_ARRAY Call this as many times as you need to enable all the drawing data types that you are using. To deactivate a type, call: gldisableclientstate( type )

Step #4 To start the drawing process, bind the Buffer that holds the Drawing Data 15 glbindbuffer( GL_ARRAY_BUFFER, bufa );

Step #5 Then, specify how to get at each Data Type within that Buffer 16 glvertexpointer( size, type, stride, offset); glcolorpointer( size, type, stride, offset); glnormalpointer( type, stride, offset); gltexcoordpointer( size, type, stride, offset); size is the how many numbers per vertex, and can be: 2, 3, or 4 type can be: GL_SHORT GL_INT GL_FLOAT GL_DOUBLE stride is the byte offset between consecutive entries in the buffer (0 means tightly packed) offset is the byte offset from the start of the data array buffer to where the first element of this part of the data lives. Vertex Data Color Data vs. Vertex Data Color Data Vertex Data Color Data Vertex Data Color Data

The Data Types in a vertex buffer object can be stored either as packed or interleaved 17 gl*pointer( size, type, stride, offset); Packed: glvertexpointer( 3, GL_FLOAT, 3*sizeof(GLfloat), 0 ); Vertex Data glcolorpointer( 3, GL_FLOAT, 3*sizeof(GLfloat), 3*numVertices*sizeof(GLfloat)); Color Data stride offset Interleaved: glvertexpointer( 3, GL_FLOAT, 6*sizeof(GLfloat), 0 ); glcolorpointer( 3, GL_FLOAT, 6*sizeof(GLfloat), 3*sizeof(GLfloat) ); stride offset Vertex Data Color Data Vertex Data Color Data Vertex Data Color Data

Step #6 Draw! 18 gldrawarrays( GL_TRIANGLES, first, numvertices ); 2 Example: 5 0 1 3 4 gldrawarrays( GL_TRIANGLES, 0, 6 ); This is how you do it if your vertices are to be drawn in consecutive order

What if your vertices are to be accessed in random order? 19 2 3 6 7 0 1 4 5 GLfloat CubeVertices[ ][3] = { { -1., -1., -1. }, { 1., -1., -1. }, { -1., 1., -1. }, { 1., 1., -1. }, { -1., -1., 1. }, { 1., -1., 1. }, { -1., 1., 1. }, { 1., 1., 1. } }; GLfloat CubeColors[ ][3] = { { 0., 0., 0. }, { 1., 0., 0. }, { 0., 1., 0. }, { 1., 1., 0. }, { 0., 0., 1. }, { 1., 0., 1. }, { 0., 1., 1. }, { 1., 1., 1. }, }; GLuint CubeQuadIndices[ ][4] = { { 0, 2, 3, 1 }, { 4, 5, 7, 6 }, { 1, 3, 7, 5 }, { 0, 4, 6, 2 }, { 2, 6, 7, 3 }, { 0, 1, 5, 4 } };

Cube Example 20

glenableclientstate( GL_VERTEX_ARRAY ); glenableclientstate( GL_COLOR_ARRAY ); glvertexpointer( 3, GL_FLOAT, 0, (Gluchar*) 0 ); glcolorpointer( 3, GL_FLOAT, 0, (Gluchar*) (3*sizeof(GLfloat)*numVertices) ); glbegin( GL_QUADS ); glarrayelement( 0 ); glarrayelement( 2 ); glarrayelement( 3 ); glarrayelement( 1 ); glarrayelement( 4 ); glarrayelement( 5 ); glend( ); glarrayelement( 7 ); glarrayelement( 6 ); glarrayelement( 1 ); glarrayelement( 3 ); glarrayelement( 7 ); glarrayelement( 5 ); glarrayelement( 0 ); glarrayelement( 4 ); glarrayelement( 6 ); glarrayelement( 2 ); glarrayelement( 2 ); glarrayelement( 6 ); glarrayelement( 7 ); glarrayelement( 3 ); glarrayelement( 0 ); glarrayelement( 1 ); glarrayelement( 5 ); glarrayelement( 4 ); Vertex Data Color Data GLuint CubeQuadIndices[ ][4] = { { 0, 2, 3, 1 }, { 4, 5, 7, 6 }, { 1, 3, 7, 5 }, { 0, 4, 6, 2 }, { 2, 6, 7, 3 }, { 0, 1, 5, 4 } }; 21

But, it would be better if that index array was over on the GPU as well 22 glbindbuffer( GL_ARRAY_BUFFER, bufa ); glbufferdata( GL_ARRAY_BUFFER, 3*sizeof(GLfloat)*numVertices, Vertices, GL_STATIC_DRAW ); glbindbuffer( GL_ELEMENT_ARRAY_BUFFER, bufb ); glbufferdata( GL_ELEMENT_ARRAY_BUFFER, sizeof(gluint)*numindices, CubeIndices, GL_STATIC_DRAW );

The gldrawelements( ) call 23 glbindbuffer( GL_ARRAY_BUFFER, bufa ); glbindbuffer( GL_ELEMENT_ARRAY_BUFFER, bufb ); glenableclientstate( GL_VERTEX_ARRAY ); glenableclientstate( GL_COLOR_ARRAY ); glvertexpointer( 3, GL_FLOAT, 0, (Gluchar*) 0 ); glcolorpointer( 3, GL_FLOAT, 0, (Gluchar*) (3*sizeof(GLfloat)*numVertices) ); gldrawelements( GL_QUADS, 24, GL_UNSIGNED_INT, (Gluchar*) 0 );

Re-writing Data into a Buffer Object, Treating it as a C/C++ Array of Structures 24 float * vertexarray = glmapbuffer( GL_ARRAY_BUFFER, usage ); usage is how the data will be accessed: GL_READ_ONLY GL_WRITE_ONLY GL_READ_WRITE the vertex data will be read from, but not written to the vertex data will be written to, but not read from the vertex data will be read from and written to You can now use vertexarray[ ] like any other C/C++ floating-point array of structures. When you are done, be sure to call: glunmapbuffer( GL_ARRAY_BUFFER );

Using a Vertex Buffer Object C++ Class Declaring: VertexBufferObject VB ; 25 Filling: VB.glBegin( GL_QUADS ); // can be any of the OpenGL topologies for( int i = 0; i < 6; i++ ) { for( int j = 0; j < 4; j++ ) { int k = CubeIndices[ i ][ j ]; VB.glColor3fv( CubeColors[ k ] ); VB.glVertex3fv( CubeVertices[ k ] ); } } VB.glEnd( ); Drawing: VB.Draw( );

Vertex Buffer Object Class Methods 26 void CollapseCommonVertices( bool ); true means to not replicate common vertices in the internal vertex table. This is good if all uses of a particular vertex will have the same normal, color, and texture coordinates, like this instead of like this. void Draw( ); Draw the primitive. If this is the first time Draw( ) is being called, it will setup all the proper buffer objects, etc. If it as a subsequent call, then it will just initiate the drawing. void glbegin( topology); Initiate the primitive. void glcolor3f( r, g, b); void glcolor3fv( rgb[ 3 ] ); Specify a vertex s color. void glend( ); Terminate the definition of a primitive. void glnormal3f( nx, ny, nz ); void glnormal3fv( nxyz[ 3] ); Specify a vertex s normal.

Vertex Buffer Object Class Methods 27 void gltexcoord2f( s, t ); void gltexcoord2fv( st[ 2 ] ); Specify a vertex s texture coordinates. void glvertex3f( x, y, z); void glvertex3fv( xyz[ 3 ] ); Specify a vertex s coordinates. void Print( char *text, FILE * ); Prints the vertex, normal, color, texture coordinate, and connection element information to a file, along with some preliminary text. If the file pointer is not given, standard error (i.e., the console) is used. void RestartPrimitive( ); Causes the primitive to be restarted. This is useful when doing triangle strips or quad strips and you want to start another one without getting out of the current one. By doing it this way, all of the strips vertices will end up in the same table, and you only need to have one VertexBufferObject class going.

Notes 28 If you want to print the contents of your data structure to a file (for debugging or curiosity), do this: FILE *fp = fopen( debuggingfile.txt, w ); if( fp == NULL ) { fprintf( stderr, Cannot create file debuggingfile.txt \n ); } else { VB.Print( My Vertex Buffer :, fp ); fclose( fp ); } You can call the glbegin method more than once. Each call will wipe out your original display information and start over from scratch. This is useful if you are interactively editing geometry, such as sculpting a curve. In many cases, using standard glbegin( ) glend( ) in a display list can be just about as fast as using vertex buffer objects if the vendor has written the drivers to create the display list on the graphics card. But, the vendors don t always do this. You re better off using vertex buffer objects because they are always fast.

A Caveat 29 Be judicious about collapsing common vertices! The good news is that it saves space and it might increase speed some (by having to transform fewer vertices). But, the bad news is that it takes much longer to create large meshes. Here s why. Say you have a 1,000 x 1,000 point triangle mesh, drawn as 999 triangle strips, all in the same VertexBufferObject class (which you can do using the RestartPrimitive method). When you draw the S th triangle strip, half of those points are coincident with points in the S-1 st strip. But, to find those 1,000 coincident points, it must search through 1000*S points first. There is no way to tell it to only look at the last 1,000 points. Even though the search is only O(log 2 N), where N is the number of points kept so far, it still adds up to a lot of time over the course of the entire mesh. It starts out fast, but slows down as the number of points being held increases. If you did have a 1,000 x 1,000 mesh, it might be better to not collapse vertices at all. Or, a compromise might be to collapse vertices, but break this mesh up into 50 VertexBufferObjects, each of size 20 x 1,000. Just a thought

Drawing the Cube With Collapsing Identical Vertices 30 Drawing 8 points X Y Z R G B -1.00-1.00-1.00 0.00 0.00 0.00-1.00 1.00-1.00 0.00 1.00 0.00 1.00 1.00-1.00 1.00 1.00 0.00 1.00-1.00-1.00 1.00 0.00 0.00-1.00-1.00 1.00 0.00 0.00 1.00 1.00-1.00 1.00 1.00 0.00 1.00 1.00 1.00 1.00 1.00 1.00 1.00-1.00 1.00 1.00 0.00 1.00 1.00 Drawing 24 array elements: 0 1 2 3 4 5 6 7 3 2 6 5 0 4 7 1 1 7 6 2 0 3 5 4

Drawing the Cube Without Collapsing Identical Vertices 31 Drawing 24 points X Y Z R G B -1.00-1.00-1.00 0.00 0.00 0.00-1.00 1.00-1.00 0.00 1.00 0.00 1.00 1.00-1.00 1.00 1.00 0.00 1.00-1.00-1.00 1.00 0.00 0.00-1.00-1.00 1.00 0.00 0.00 1.00 1.00-1.00 1.00 1.00 0.00 1.00 1.00 1.00 1.00 1.00 1.00 1.00-1.00 1.00 1.00 0.00 1.00 1.00 1.00-1.00-1.00 1.00 0.00 0.00 1.00 1.00-1.00 1.00 1.00 0.00 1.00 1.00 1.00 1.00 1.00 1.00 1.00-1.00 1.00 1.00 0.00 1.00-1.00-1.00-1.00 0.00 0.00 0.00-1.00-1.00 1.00 0.00 0.00 1.00-1.00 1.00 1.00 0.00 1.00 1.00-1.00 1.00-1.00 0.00 1.00 0.00-1.00 1.00-1.00 0.00 1.00 0.00-1.00 1.00 1.00 0.00 1.00 1.00 1.00 1.00 1.00 1.00 1.00 1.00 1.00 1.00-1.00 1.00 1.00 0.00-1.00-1.00-1.00 0.00 0.00 0.00 1.00-1.00-1.00 1.00 0.00 0.00 1.00-1.00 1.00 1.00 0.00 1.00-1.00-1.00 1.00 0.00 0.00 1.00 Drawing 24 array elements: 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23

A Comparison 32 Not Collapsing Identical Vertices Collapsing Identical Vertices

Using Vertex Buffers with Shaders 33 Let s say that we have the following vertex shader and we want to supply the vertices from a Buffer Object. in vec3 avertex; in vec3 acolor; out vec3 vcolor; void main( ) { } vcolor = acolor; gl_position = gl_modelviewprojectionmatrix * vec4( avertex, 1. ); Let s also say that, at some time, we want to supply the colors from a Buffer Object as well, but for right now, the color will be constant.

Using Vertex Buffers with Shaders 34 We re assuming here that we already have the shader program setup in program, and already have the vertices in the vertexbuffer. glbindbuffer( GL_ARRAY_BUFFER, vertexbuffer ); glenableclientstate( GL_VERTEX_ARRAY ); glenableclientstate( GL_COLOR_ARRAY ); GLuint vertexlocation = glgetattriblocation( program, avertex ); GLuint colorlocation = glgetattriblocation( program, acolor ); glvertexattribpointer( vertexlocation, 3, GL_FLOAT, GL_FALSE, 0, (GLuchar *)0 ); glenablevertexattribarray( vertexlocation ); // dynamic attribute glvertexattrib3f( colorlocation, r, g, b ); gldisablevertexattribarray( colorlocation ); // static attribute gldrawarrays( GL_TRIANGLES, 0, 3*NumTris );

Using Vertex Buffers with the Shaders C++ Class 35 We re assuming here that we already have the shader program setup in program, and already have the vertices in the vertexbuffer. glbindbuffer( GL_ARRAY_BUFFER, vertexbuffer ); glenableclientstate( GL_VERTEX_ARRAY ); glenableclientstate( GL_COLOR_ARRAY ); Pattern->SetVertexAttributePointer3fv( avertex, (GLfloat *)0 ); Pattern->EnableVertexAttribArray( avertex ); // dynamic attribute Pattern->SetVertexAttributeVariable( acolor, r, g, b ); Pattern->DisableVertexAttribArray( acolor ); // static attribute gldrawarrays( GL_TRIANGLES, 0, 3*NumTris );