OpenGL Primer: Drawing Graphics Primitives and Rendering Polygons

OpenGL Primer: Drawing Graphics Primitives and Rendering Polygons
Slide Note
Embed
Share

This content covers fundamental concepts of OpenGL, including drawing graphics primitives, rendering polygons as points, outlines, or solids, and working with buffers. It explains the use of OpenGL functions for defining points, lines, triangles, and polygons, along with techniques for adjusting rendering styles and orientations. Additionally, it discusses methods for culling certain polygons to optimize rendering performance.

  • OpenGL
  • Graphics Primitives
  • Rendering
  • Polygons
  • Buffers

Uploaded on Feb 25, 2025 | 0 Views


Download Presentation

Please find below an Image/Link to download the presentation.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author.If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.

You are allowed to download the files provided on this website for personal or commercial use, subject to the condition that they are used lawfully. All files are the property of their respective owners.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author.

E N D

Presentation Transcript


  1. EECS 700: Computer Modeling, Simulation, and Visualization Dr. Shontz Chapter 3: Drawing with OpenGL

  2. OpenGL Graphics Primitives Points Points use glPointSize Point Sprites Point Sprites ignore these for now, as they are used with fragment/texture shaders Lines, Strips, and Loops Lines, Strips, and Loops use glLineWidth glLineWidth(). whereas an open sequence of lines is a line strip. line strip. Triangles, Strips, and Fans Triangles, Strips, and Fans glPointSize(). (). (). A closed sequence of lines is a line loop line loop, ,

  3. OpenGL Primitive Mode Tokens Primitive Type Primitive Type OpenGL Token OpenGL Token Points Points GL_POINTS GL_POINTS Lines Lines GL_LINES GL_LINES Line Strips Line Strips GL_LINE_STRIP GL_LINE_STRIP Line Loops Line Loops GL_LINE_LOOP GL_LINE_LOOP Independent Triangles Independent Triangles GL_TRIANGLES GL_TRIANGLES Triangle Strips Triangle Strips GL_TRIANGLE_STRIP GL_TRIANGLE_STRIP Triangle TriangleFans Fans GL_TRIANGLE_FAN GL_TRIANGLE_FAN

  4. Rendering Polygons as Points, Outlines, or Solids A polygon has two sides A polygon has two sides front and back. front and back. By default, they are drawn filled. By default, they are drawn filled. To change how they are drawn, or to draw only outlines or vertices, To change how they are drawn, or to draw only outlines or vertices, use glPolygonMode glPolygonMode( (face,mode face,mode): ): use face face = = GL_FRONT_AND_BACK GL_FRONT_AND_BACK mode = mode = GL_POINT, GL_LINE, GL_POINT, GL_LINE, or or GL_FILL GL_FILL (how polygon is drawn). (how polygon is drawn). Convention: Convention: Polygons whose vertices appear in counterclockwise order are called front Polygons whose vertices appear in counterclockwise order are called front facing. facing. Call Call glFrontFace glFrontFace(mode) (mode) to swap front and back faces. Mode: to swap front and back faces. Mode: GL_CCW GL_CCW (default) (default) or or GL_CW GL_CW

  5. Rendering Polygons as Points, Outlines, or Solids (cont.) To avoid drawing certain polygons: To avoid drawing certain polygons: use culling. Example: Example: glEnable glEnable(GL_CULL_FACE); (GL_CULL_FACE); glCullFace glCullFace(mode) (mode), where mode = GL_FRONT, GL_BACK, or GL_FRONT_AND_BACK mode = GL_FRONT, GL_BACK, or GL_FRONT_AND_BACK. To disable culling: To disable culling: glDisable glDisable(GL_CULL_FACE). (GL_CULL_FACE).

  6. Data in OpenGL Buffers Creating and allocating buffers: Creating and allocating buffers: glGenBuffers glGenBuffers(n, buffers) (n, buffers). Yields array of buffer object names. . Yields array of buffer object names. Next step: bind name to buffer binding target from table below. Next step: bind name to buffer binding target from table below. Buffer Binding Target Buffer Binding Target Uses Uses GL_ARRAY_BUFFER GL_ARRAY_BUFFER Binding point used to set vertex array Binding point used to set vertex array data pointers using pointers using glVertexAttribPointer glVertexAttribPointer(). Will use most often. use most often. data (). Will GL_COPY_READ_BUFFER and GL_COPY_READ_BUFFER and GL_COPY_WRITE_BUFFER GL_COPY_WRITE_BUFFER Pair of binding points used to copy data Pair of binding points used to copy data between buffers without disturbing between buffers without disturbing OpenGL state. state. OpenGL GL_DRAW_INDIRECT_BUFFER GL_DRAW_INDIRECT_BUFFER Buffer target used to store parameters for Buffer target used to store parameters for indirect indirect drawing commands drawing commands GL_ELEMENT_ARRAY_BUFFER GL_ELEMENT_ARRAY_BUFFER Buffer Buffers bound to this target contain vertex s bound to this target contain vertex indices used by index draw commands indices used by index draw commands GL_PIXEL_BACK_BUFFER GL_PIXEL_BACK_BUFFER Target for OpenGL commands that read data Target for OpenGL commands that read data from image objects such as textures from image objects such as textures or framebuffer framebuffer. . or

  7. Data in OpenGL Buffers (cont.) Binding Buffer Targets (continued) Binding Buffer Targets (continued) Uses Uses GL_PIXEL_UNPACK_BUFFER GL_PIXEL_UNPACK_BUFFER The opposite of the pixel The opposite of the pixel pack buffer. pack buffer. GL_TEXTURE_BUFFER GL_TEXTURE_BUFFER Buffers Buffers that are bound to texture objects that are bound to texture objects so their data can be directly read inside so their data can be directly read inside shaders shaders. . GL_TRANSFORM_FEEDBACK_BUFFER GL_TRANSFORM_FEEDBACK_BUFFER Transformed Transformed vertices can be captured as vertices can be captured as they exist the vertex processing part of the they exist the vertex processing part of the pipeline (after the vertex or geometry pipeline (after the vertex or geometry shader shader) and some of their attributes ) and some of their attributes written into buffer objects. written into buffer objects. GL_UNIFORM_BUFFER GL_UNIFORM_BUFFER Binding target for uniform buffer Binding target for uniform buffer objects. objects. To bind name: To bind name: glBindBuffer first time the name buffer has been bound. first time the name buffer has been bound. glBindBuffer(target, buffer); (target, buffer); This creates a buffer object if this is the This creates a buffer object if this is the

  8. Getting Data into and out of Buffers The easiest way to get data into a buffer object is to load data into the buffer when it is The easiest way to get data into a buffer object is to load data into the buffer when it is allocated: allocated: glBufferData glBufferData(target, size, data, usage); (target, size, data, usage); This allocates This allocates size that space is initialized with contents of the memory addressed by data. that space is initialized with contents of the memory addressed by data. usage to allow the application to indicate the intended usage. to allow the application to indicate the intended usage. size bytes of storage for buffer object bound to bytes of storage for buffer object bound to target target. If . If data data is non usage is provided is non- -NULL, is provided NULL, Note: The buffer will resize itself based on the new data being stored (if called several times). Note: The buffer will resize itself based on the new data being stored (if called several times). usage = GL_STATIC_DRAW, GL_DYNAMIC_COPY, etc. usage = GL_STATIC_DRAW, GL_DYNAMIC_COPY, etc. First part of token = STREAM. STREAM. Second part of token = Second part of token = DRAW, READ, or COPY. DRAW, READ, or COPY. First part of token = STATIC, DYNAMIC, or STATIC, DYNAMIC, or See Table 3.3 for more info on tokens. See Table 3.3 for more info on tokens. Pay particular attention to Pay particular attention to DYNAMIC DYNAMIC vs. vs. STREAM STREAM. . Use Use DRAW DRAW with vertex data, with vertex data, READ information from OpenGL. Use information from OpenGL. Use COPY READ with pixel buffer objects and other buffers used to retrieve with pixel buffer objects and other buffers used to retrieve COPY with transform feedback buffers. with transform feedback buffers.

  9. Initializing Part of a Buffer Use Use glBufferSubData glBufferSubData(target, offset, size, data) (target, offset, size, data), , to initialize part of a buffer. to initialize part of a buffer. Can use a combination of Can use a combination of glBufferData buffer object and upload data into separate sections of it. buffer object and upload data into separate sections of it. glBufferData() () and and glBufferSubData glBufferSubData() () to allocate and initialize a to allocate and initialize a Example 3.1: Example 3.1: // Vertex positions // Vertex positions Static Static const const GLfloat { { - -1.0f, 1.0f, - -1.0f, 0.0f, 1.0f, 1.0f, 0.0f, 1.0f, 1.0f, 1.0f, - -1.0f, 0.0f, 1.0f, 1.0f, 0.0f, 1.0f, 1.0f, 1.0f, 0.0f, 1.0f, 1.0f, 1.0f, 0.0f, 1.0f, - -1.0f, 1.0f, 0.0f, 1.0f 1.0f, 1.0f, 0.0f, 1.0f }; }; GLfloat positions[] = positions[] =

  10. Example 3.1 (continued) // Vertex colors // Vertex colors static static const const GLfloat { { 1.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, }; }; GLfloat colors[] = colors[] = // Buffer object // Buffer object GLuint GLuint buffer; buffer; // Reserve a name for the buffer object. // Reserve a name for the buffer object. glGenBuffers glGenBuffers(1, &buffer); (1, &buffer);

  11. Example 3.1 (continued) // Bind it to the GL_ARRAY_BUFFER target. // Bind it to the GL_ARRAY_BUFFER target. glBindBuffer glBindBuffer(GL_ARRAY_BUFFER, buffer); (GL_ARRAY_BUFFER, buffer); // Allocate space for it ( // Allocate space for it (sizeof glBufferData glBufferData( (GL_ARRAY_BUFFER,sizeof GL_ARRAY_BUFFER,sizeof(positions) + sizeof(positions) + (positions) + sizeof sizeof(colors)). (colors)). (positions) + sizeof sizeof(colors), NULL, GL_STATIC_DRAW); (colors), NULL, GL_STATIC_DRAW); // Put positions at offset zero in the buffer. // Put positions at offset zero in the buffer. glBufferSubData glBufferSubData(GL_ARRAY_BUFFER, 0, (GL_ARRAY_BUFFER, 0, sizeof sizeof(positions), positions); (positions), positions); // Put colors at an offset in the buffer equal to the filled size of the buffer so far. // Put colors at an offset in the buffer equal to the filled size of the buffer so far. glBufferSubData glBufferSubData(GL_ARRAY_BUFFER, (GL_ARRAY_BUFFER, sizeof sizeof(positions), (positions), sizeof sizeof(colors), colors); (colors), colors);

  12. Initializing Part of a Buffer (continued) Use Use glClearBufferData glClearBufferData() () or glClearBufferData glClearBufferData(target, or glClearBufferSubData glClearBufferSubData() () to clear the buffer. to clear the buffer. (target, internalformat internalformat, format, type, data); , format, type, data); glClearBufferSubData glClearBufferSubData(target, (target, internalformat internalformat, offset, size, format, type, data); , offset, size, format, type, data); Note: The data is first converted into the format specified by Note: The data is first converted into the format specified by internalformat Then the data is used to fil the specified range of the buffer s data store. In other Then the data is used to fil the specified range of the buffer s data store. In other words, you can initialize the data store of a buffer object this way. words, you can initialize the data store of a buffer object this way. internalformat. . Data can also be copied between buffer objects using Data can also be copied between buffer objects using glCopyBufferSubData glCopyBufferSubData(). (). glCopyBufferSubData glCopyBufferSubData( (readtarget readtarget, , writetarget writetarget, , readoffset readoffset, , writeoffset writeoffset, size); , size);

  13. Accessing Contents of the Buffer All of the functions covered in this section so far, i.e., All of the functions covered in this section so far, i.e., glBufferData glBufferSubData glBufferSubData(), (), and and glGetBufferSubData glGetBufferSubData() () all cause OpenGL to make a copy of your data. a copy of your data. glBufferData() (), , all cause OpenGL to make Use Use glMapBuffer glMapBuffer(target, access) (target, access) to obtain access to memory owned by OpenGL. to obtain access to memory owned by OpenGL. Maps to the client s address space the entire data store of the buffer object currently Maps to the client s address space the entire data store of the buffer object currently bound to bound to target target. Data can then be directly read/written relative to the returned . Data can then be directly read/written relative to the returned pointer, depending on the pointer, depending on the access access policy. policy. Access modes for Access modes for glMapBuffer glMapBuffer() (): : GL_READ_ONLY, GL_WRITE_ONLY, GL_READ_WRITE GL_READ_ONLY, GL_WRITE_ONLY, GL_READ_WRITE. . When finished: When finished: glUnmapBuffer glUnmapBuffer(target); (target);

  14. Example 3.2: Initializing a Buffer with glMapBuffer() GLuint GLuint buffer; buffer; FILE * f; FILE * f; size_t size_t filesize filesize; ; // Open a file and find its size. // Open a file and find its size. f = f = fopen fopen( data.dat , ( data.dat , rb rb ); fseek fseek(f, 0, SEEK_END); (f, 0, SEEK_END); filesize filesize = = ftell ftell(f); (f); fseek fseek(f, 0, SEEK_SET); (f, 0, SEEK_SET); ); // Create a buffer by generating a name and binding it to a buffer binding point. Note the // Create a buffer by generating a name and binding it to a buffer binding point. Note the // binding means nothing in this example. // binding means nothing in this example. glGenBuffers glGenBuffers(1, &buffer); (1, &buffer); glBindBuffer glBindBuffer(GL_COPY_WRITE_BUFFER, buffer); (GL_COPY_WRITE_BUFFER, buffer);

  15. Example 3.2 (continued) // Allocate the data store for the buffer by passing NULL for the data parameter. // Allocate the data store for the buffer by passing NULL for the data parameter. glBufferData glBufferData(GL_COPY_WRITE_BUFFER, ( (GL_COPY_WRITE_BUFFER, (GLsizei GLsizei) ) filesize filesize, NULL, GL_STATIC_DRAW); , NULL, GL_STATIC_DRAW); // Map the buffer // Map the buffer void * data = void * data = glMapBuffer glMapBuffer(GL_COPY_WRITE_BUFFER, GL_WRITE_ONLY); (GL_COPY_WRITE_BUFFER, GL_WRITE_ONLY); // Read the file into the buffer // Read the file into the buffer fread fread(data, 1, (data, 1, filesize filesize, f); , f); // // Unmap Unmap buffer since we re done. buffer since we re done. glUnmapBuffer glUnmapBuffer(GL_COPY_WRITE_BUFFER); (GL_COPY_WRITE_BUFFER); // Close the file // Close the file fclose fclose(f); (f);

  16. Asynchronous and Explicit Mapping Use Use glMapBufferRange glMapBufferRange() to specify access more precisely: () to specify access more precisely: glMapBufferRange glMapBufferRange(target, offset, length, access); (target, offset, length, access); This maps all or part of a buffer object s data store into the application s address space. This maps all or part of a buffer object s data store into the application s address space. Here Here access access is a GL_MAP_WRITE_BIT GL_MAP_WRITE_BIT and one or more of the following flags: and one or more of the following flags: is a bitfield bitfield that must contain one or both of the that must contain one or both of the GL_MAP_READ_BIT GL_MAP_READ_BIT and the and the - - GL_MAP_INVALIDATE_RANGE_BIT GL_MAP_INVALIDATE_RANGE_BIT (data in specified range of the buffer may be (data in specified range of the buffer may be discarded and considered invalid), discarded and considered invalid), GL_MAP_INVALIDATE_BUFFER_BIT GL_MAP_INVALIDATE_BUFFER_BIT (entire contents of buffer may be discarded and considered invalid), buffer may be discarded and considered invalid), GL_MAP_FLUSH_EXPLICIT_BIT (determine which parts of the mapped range contain valid data; use wit (determine which parts of the mapped range contain valid data; use wit GL_MAP_WRITE_BIT), GL_MAP_WRITE_BIT), GL_MAP_UNSYNCHRONIZED_BIT GL_MAP_UNSYNCHRONIZED_BIT (if this bit is set, OpenGL will not attempt to synchronize buffer operations). attempt to synchronize buffer operations). See Table 3.5 for more information. See Table 3.5 for more information. (entire contents of GL_MAP_FLUSH_EXPLICIT_BIT (if this bit is set, OpenGL will not

  17. Asynchronous and Explicit Mapping Flush a portion of the buffer: glFlushMappedBufferRange glFlushMappedBufferRange(target, offset, length); Flush a portion of the buffer: (target, offset, length); Indicates to OpenGL the range specified by offset and length in the mapped buffer bound Indicates to OpenGL the range specified by offset and length in the mapped buffer bound to target has been modified and should be incorporated back into the buffer object s data to target has been modified and should be incorporated back into the buffer object s data store. store.

  18. Vertex Specification At this point, we know how to put data in buffers and how to write a basic vertex At this point, we know how to put data in buffers and how to write a basic vertex shader shader. . Next up: Hooking the data up to the vertex Next up: Hooking the data up to the vertex shader shader. . VertexAttribPointer VertexAttribPointer in Depth: in Depth: Recall Recall glVertexAttribPointer glVertexAttribPointer(index, size, type, normalized, stride, pointer) (index, size, type, normalized, stride, pointer) specified where the data values for the vertex attribute with location index can be accessed. the data values for the vertex attribute with location index can be accessed. specified where Values of type for Values of type for glVertexAttribPointer glVertexAttribPointer: : GL_BYTE, GL_UNSIGNED_BYTE, GL_SHORT, , GL_BYTE, GL_UNSIGNED_BYTE, GL_SHORT, , GL_DOUBLE, etc. GL_DOUBLE, etc. See Table 3.6 for more details. See Table 3.6 for more details.

  19. Vertex Specification (continued) Integer Vertex Attributes: Use Integer Vertex Attributes: Use glVertexAttribIPointer glVertexAttribIPointer(index, size, type, stride, pointer). (index, size, type, stride, pointer). Note the parameters are the same as they are for Note the parameters are the same as they are for glVertexAttribPointer exception of the omission of the normalize parameter which is not relevant here. exception of the omission of the normalize parameter which is not relevant here. GL_BYTE, GL_SHORT, and etc. GL_BYTE, GL_SHORT, and etc. may be used for the may be used for the type glVertexAttribPointer() () with the with the type parameter. parameter. Double Double- -Precision Vertex Attributes: Use Precision Vertex Attributes: Use glVertexAttribLPointer pointer). pointer). Note that here L stands for long. The vertex attributes must be declared as Note that here L stands for long. The vertex attributes must be declared as 64 64- -bit double precision floating bit double precision floating- -point types in the vertex point types in the vertex shader glVertexAttribLPointer(index, size, type, stride, (index, size, type, stride, shader. . Packed Data Formats for Vertex Attributes: Packed Data Formats for Vertex Attributes: BGRA RGBA RGBA- -packing: packing: W, Z, Y, and X. W, Z, Y, and X. BGRA- -packing: packing: W, X, Y, and Z. W, X, Y, and Z. Vertex data can only be specified in the Vertex data can only be specified in the W, X, Y, and Z format GL_INT_2_10_10_10_REV GL_INT_2_10_10_10_REV or or GL_UNSIGNED_INT_3_20_20_20_REV GL_UNSIGNED_INT_3_20_20_20_REV tokens. W, X, Y, and Z format by using by using tokens.

  20. Vertex Specification (continued) Enabling of vertex attribute arrays: Use Enabling of vertex attribute arrays: Use glEnableVertexAttribArray attribute arrays. attribute arrays. If you forget to enable a vertex attribute array, the static vertex attribute If you forget to enable a vertex attribute array, the static vertex attribute will be used. will be used. glEnableVertexAttribArray() () to enable vertex to enable vertex Static vertex attributes: Static vertex attributes: glVertexAttrib glVertexAttrib{1234}{ glVertexAttrib glVertexAttrib{1234}{ glVertexAttrib4{ glVertexAttrib4{bsifd {1234}{fds {1234}{fds bsifd ub fds}(index, values); }(index, values); // The static value is specified with index. // The static value is specified with index. fds}v(index, values); }v(index, values); // Can specify up to four values for x, y, z, // Can specify up to four values for x, y, z, // and w parameters. // and w parameters. ub us us ui ui}v(index, values); }v(index, values); All of these functions implicitly convert supplied parameters to floating All of these functions implicitly convert supplied parameters to floating- -point before passing them to the vertex passing them to the vertex shader shader. . point before

  21. Vertex Specification (continued) Related commands: Related commands: glVertexAttrib4Nub(index, x, y, z, w); glVertexAttrib4Nub(index, x, y, z, w); glVertexAttrib4N{ glVertexAttrib4N{bsi bsi ub // for use with single // for use with single- -precision }v(index, v); // floating // floating- -point data precision ub us us ui ui}v(index, v); point data glVertexAttribI glVertexAttribI{1234}{I glVertexAttribI glVertexAttribI{123}{I glVertexAttribI4{ glVertexAttribI4{bsi {1234}{I ui ui}(index, values); }(index, values); // for use with integer data {123}{I ui ui}v(index, values); }v(index, values); bsi ub ub us us ui ui}v(index, values); }v(index, values); // for use with integer data glVertexAttribL glVertexAttribL{1234}(index, values); {1234}(index, values); glVertexAttribL glVertexAttribL{1234}v(index, values); {1234}v(index, values); // for use with double // for use with double- -precision data precision data Use 1.0 as default for w, 0.0 as default for y and z. Use 1.0 as default for w, 0.0 as default for y and z.

  22. OpenGL Drawing Commands Two categories of drawing commands: indexed and Two categories of drawing commands: indexed and nonindexed nonindexed draws. draws. Nonindexed draws: Nonindexed draws: simply read vertex data sequentially. simply read vertex data sequentially. Indexed draws: Indexed draws: use array of indices stored in buffer object bound to the use array of indices stored in buffer object bound to the GL_ELEMENT_ARRAY_BUFFER binding used to indirectly index into the GL_ELEMENT_ARRAY_BUFFER binding used to indirectly index into the enabled vertex arrays enabled vertex arrays Simplest Simplest nonindexed nonindexed drawing: of geometric primitives using array elements starting at first and ending at first + count of geometric primitives using array elements starting at first and ending at first + count 1 1 of each enabled array. of each enabled array. mode = GL_TRIANGLES, GL_LINE_LOOP, GL_LINES, mode = GL_TRIANGLES, GL_LINE_LOOP, GL_LINES, or or GL_POINTS. drawing: glDrawArrays glDrawArrays(mode, first, count). (mode, first, count). Constructs a sequence Constructs a sequence GL_POINTS. Most basic indexed drawing: Most basic indexed drawing: glDrawElements sequence of geometric primitives using count elements whose indices are stored in the sequence of geometric primitives using count elements whose indices are stored in the buffer bound to buffer bound to GL_ELEMENT_ARRAY_BUFFER GL_ELEMENT_ARRAY_BUFFER buffer binding point. GL_TRIANGLES, GL_LINE_LOOP, GL_LINES, GL_TRIANGLES, GL_LINE_LOOP, GL_LINES, and and GL_POINTS. glDrawElements(mode, count, type, indices). (mode, count, type, indices). Defines Defines buffer binding point. mode = GL_POINTS. mode =

  23. OpenGL Drawing Commands (continued) glDrawElementsBaseVertex glDrawElementsBaseVertex(mode, count, type, indices, (mode, count, type, indices, basevertex basevertex) ) behaves identically to behaves identically to transferred by the corresponding draw element indices[i i]+ ]+basevertex basevertex of each enabled attribute array. of each enabled attribute array. glDrawElements glDrawElements() except that the () except that the i ith th element command will be taken from command will be taken from element indices[ Typical use: Animations. Typical use: Animations. element transferred by the corresponding draw glDrawRangeElements glDrawRangeElements(mode, start, end, count, type, indices) (mode, start, end, count, type, indices) is a restricted form of glDrawElements glDrawElements(). Guarantees that any index contained in the section of the element array (). Guarantees that any index contained in the section of the element array buffer referenced by buffer referenced by indices and count indices and count will fall will fall within the range specified by start and end. is a restricted form of within the range specified by start and end. glDrawRangeElementsBaseVertex glDrawRangeElementsBaseVertex(mode, start, end, count, type, indices, (mode, start, end, count, type, indices, basevertex stored in the element array buffer will fall between start and end before stored in the element array buffer will fall between start and end before basevertex added. added. basevertex). basevertex is is ). Values Values

  24. OpenGL Drawing Commands (continued) OpenGL drawing commands that do not start with draw: OpenGL drawing commands that do not start with draw: 1. 1. glMultiDrawArrays glMultiDrawArrays(mode, first, count, primitives with a single OpenGL function call. primitives with a single OpenGL function call. first parameters that would be valid for a call to parameters that would be valid for a call to glDrawArrays (mode, first, count, primcount primcount) ) Draws multiple sets of geometric Draws multiple sets of geometric first and and count count are arrays of are arrays of primcount glDrawArrays(). (). primcount Equivalent code: Equivalent code: for ( for (i i = 0; = 0; i i < < primcount glDrawArrays glDrawArrays(mode, first[ primcount; ; i i++){ (mode, first[i i], count[ ++){ ], count[i i]);} ]);} 2. 2. glMultiDrawElements glMultiDrawElements(mode, count, type, indices, (mode, count, type, indices, primcount geometric primitives with a single OpenGL function call. geometric primitives with a single OpenGL function call. primcount) ) Draws multiple sets of Draws multiple sets of Equivalent code: Equivalent code: for ( for (i i = 0; = 0; i i < < primcount glDrawElements glDrawElements(mode, count[ primcount; ; i i++){ (mode, count[i i], type, indices[ ++){ ], type, indices[i i]);} ]);}

  25. OpenGL Drawing Commands (continued) 3. 3. glMultiDrawElementsBaseVertex glMultiDrawElementsBaseVertex(mode, count, type, indices, Draws multiple sets of geometric primitives with a single OpenGL function call. Draws multiple sets of geometric primitives with a single OpenGL function call. (mode, count, type, indices, primcount primcount, , baseVertex baseVertex) ) Equivalent code: Equivalent code: for ( for (i i = 0; = 0; i i < < primcount { { glDrawElements glDrawElements(mode, count[ } } primcount; ; i i++) ++) (mode, count[i i], type, indices[ ], type, indices[i i], ], baseVertex baseVertex[ [i i]); ]);

  26. Example 3.5: Setting up for the Drawing Command static static const { { - -1.0f, 1.0f, - -1.0f, 0.0f, 1.0f, 1.0f, 0.0f, 1.0f, 1.0f, 1.0f, - -1.0f, 0.0f, 1.0f, 1.0f, 0.0f, 1.0f, - -1.0f, 1.0f, 0.0f, 1.0f, 1.0f, 1.0f, 0.0f, 1.0f, - -1.0f, 1.0f, - -1.0f, 0.0f, 1.0f, 1.0f, 0.0f, 1.0f, }; }; const GLfloat GLfloat vertex_positions vertex_positions[] = [] = // Four vertices // Four vertices static static const { { 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 0.0f, 1.0f, 1.0f, 1.0f, 0.0f, 1.0f, 1.0f, 0.0f, 1.0f, 1.0f, 1.0f, 0.0f, 1.0f, 1.0f, 0.0f, 1.0f, 1.0f, 1.0f 0.0f, 1.0f, 1.0f, 1.0f }; }; const GLfloat GLfloat vertex_colors vertex_colors[] = [] = // Color for each vertex. // Color for each vertex.

  27. Example 3.5 (continued) // Three indices (to draw one triangle at a time): // Three indices (to draw one triangle at a time): const GLushort GLushort vertex_indices vertex_indices[] = static static const { { 0, 1, 2 0, 1, 2 }; }; [] = // Set // Set- -up element array buffer up element array buffer glGenBuffers glGenBuffers(1, (1, ebo glBindBuffer glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, (GL_ELEMENT_ARRAY_BUFFER, ebo glBufferData glBufferData(GL_ELEMENT_ARRAY_BUFFER, (GL_ELEMENT_ARRAY_BUFFER, sizeof GL_STATIC_DRAW); GL_STATIC_DRAW); ebo); ); ebo[0]); [0]); sizeof( (vertex_indices vertex_indices), ), vertex_indices vertex_indices, , // Set // Set- -up vertex attributes up vertex attributes glGenVertexArrays glGenVertexArrays(1, glBindVertexArray glBindVertexArray( (vao (1, vao vao); ); vao[0]); [0]);

  28. Example 3.5 (continued) // Set // Set- -up vertex attributes (continued) up vertex attributes (continued) glGenBuffers glGenBuffers(1, (1, vbo vbo); ); glBindBuffer glBindBuffer(GL_ARRAY_BUFFER, (GL_ARRAY_BUFFER, vbo glBufferData glBufferData(GL_ARRAY_BUFFER, (GL_ARRAY_BUFFER, sizeof GL_STATIC_DRAW); GL_STATIC_DRAW); glBufferSubData glBufferSubData(GL_ARRAY_BUFFER, 0, (GL_ARRAY_BUFFER, 0, sizeof glBufferSubData glBufferSubData(GL_ARRAY_BUFFER, (GL_ARRAY_BUFFER, sizeof vertex_colors vertex_colors); ); vbo[0]); [0]); sizeof( (vertex_positions vertex_positions) + ) + sizeof sizeof( (vertex_colors vertex_colors), NULL, ), NULL, sizeof( (vertex_positions vertex_positions), ), vertex_positions sizeof( (vertex_positions vertex_positions), ), sizeof vertex_positions); ); sizeof( (vertex_colors vertex_colors), ),

  29. Example 3.6: Drawing Commands Example // // DrawArrays DrawArrays model_matrix model_matrix = = vmath glUniformMatrix4fv( glUniformMatrix4fv(render_model_matrix_loc render_model_matrix_loc, 4, GL_FALSE, glDrawArrays glDrawArrays(GL_TRIANGLES, 0, 3); (GL_TRIANGLES, 0, 3); vmath::translation( ::translation(- -3.0f, 0.0f, 3.0f, 0.0f, - -5.0f); 5.0f); , 4, GL_FALSE, model_matrix model_matrix); ); // // DrawElements DrawElements model_matrix model_matrix = = vmath glUniformMatrix4fv( glUniformMatrix4fv(render_model_matrix_loc render_model_matrix_loc, 4, GL_FALSE, glDrawElements glDrawElements(GL_TRIANGLES, 3, GL_UNSIGNED_SHORT, NULL); (GL_TRIANGLES, 3, GL_UNSIGNED_SHORT, NULL); vmath::translation( ::translation(- -1.0f, 0.0f, 1.0f, 0.0f, - -5.0f); 5.0f); , 4, GL_FALSE, model_matrix model_matrix); ); // // DrawElementsBaseVertex DrawElementsBaseVertex model_matrix model_matrix = = vmath glUniformMatrix4fv( glUniformMatrix4fv(render_model_matrix_loc render_model_matrix_loc, 4, GL_FALSE, glDrawElementsBaseVertex(GL_TRIANGLES, 3, GL_UNSIGNED_SHORT, NULL, 1); glDrawElementsBaseVertex(GL_TRIANGLES, 3, GL_UNSIGNED_SHORT, NULL, 1); vmath::translation(1.0f, 0.0f, ::translation(1.0f, 0.0f, - -5.0f); 5.0f); , 4, GL_FALSE, model_matrix model_matrix); ); This renders 3 similar This renders 3 similar traingles traingles each rendered using a different drawing command. each rendered using a different drawing command.

  30. Restarting Primitives glPrimitiveRestartIndex glPrimitiveRestartIndex(index) (index) Specifies the vertex array element index used to indicate a new Specifies the vertex array element index used to indicate a new primitive should be started during rendering. primitive should be started during rendering. Only watches for index specified by Only watches for index specified by glPrimitiveReseartIndex is enabled. Disable it with is enabled. Disable it with glDisable glDisable(GL_PRIMITIVE_RESTART). glPrimitiveReseartIndex() if (GL_PRIMITIVE_RESTART). () if glEnable glEnable(GL_PRIMITIVE_RESTART) (GL_PRIMITIVE_RESTART) Example 3.7: Initializing data for a cube made of two triangle strips Example 3.7: Initializing data for a cube made of two triangle strips // 8 corners of a cube, side length 2, centered at the origin // 8 corners of a cube, side length 2, centered at the origin static static const const GLfloat GLfloat cube_positions cube_positions[] = { { - -1.0f, 1.0f, - -1.0f, 1.0f, - -1.0f, 1.0f, 1.0f, 1.0f, - -1.0f, 1.0f, - -1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f 1.0f, 1.0f, 1.0f, 1.0f }; }; [] =

  31. Example 3.7 (continued) // Color for each vertex: // Color for each vertex: const GLfloat GLfloat cube_colors static static const { { 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 0.5f, 0.5f, 0.5f, 1.0f 0.5f, 0.5f, 0.5f, 1.0f }; }; cube_colors[] = [] = // Indices for the triangle strips // Indices for the triangle strips static static const const GLushort GLushort cube_indices { { 0, 1, 2, 3, 6, 7, 4, 5, 0, 1, 2, 3, 6, 7, 4, 5, 0xFFFF, 0xFFFF, 2, 6, 0, 4, 1, 5, 3, 7 2, 6, 0, 4, 1, 5, 3, 7 }; }; cube_indices[] = [] = // first strip // first strip // restart index // restart index // second strip // second strip

  32. Example 3.7 (continued) // Set up the element array buffer // Set up the element array buffer (1, ebo ebo); ); glGenBuffers glGenBuffers(1, glBindBuffer glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, (GL_ELEMENT_ARRAY_BUFFER, ebo glBufferData glBufferData(GL_ELEMENT_ARRAY_BUFFER, (GL_ELEMENT_ARRAY_BUFFER, sizeof GL_STATIC_DRAW); GL_STATIC_DRAW); ebo[0]); [0]); sizeof( (cube_indices cube_indices), ), cube_indices cube_indices, , // Set up the vertex attributes // Set up the vertex attributes glGenVertexArrays glGenVertexArrays(1, glBindVertexArray glBindVertexArray( (vao (1, vao vao); ); vao[0]); [0]); glGenBuffers glGenBuffers(1, glBindBuffer glBindBuffer(GL_ARRAY_BUFFER, (GL_ARRAY_BUFFER, vbo glBufferData glBufferData(GL_ARRAY_BUFFER, (GL_ARRAY_BUFFER, sizeof GL_STATIC_DRAW); GL_STATIC_DRAW); glBufferSubData glBufferSubData(GL_ARRAY_BUFFER, 0, (GL_ARRAY_BUFFER, 0, sizeof (1, vbo vbo); ); vbo[0]); [0]); sizeof( (cube_positions cube_positions) + ) + sizeof sizeof( (cube_colors cube_colors), NULL, ), NULL, sizeof( (cube_positions cube_positions), ), cube_positions cube_positions); );

  33. Example 3.7 (continued) glBufferSubData glBufferSubData(GL_ARRAY_BUFFER, (GL_ARRAY_BUFFER, sizeof sizeof( (cube_positions cube_positions), ), sizeof sizeof( (cube_colors cube_colors), ), cube_colors cube_colors); ); glVertexAttribPointer glVertexAttribPointer(0, 4, GL_FLOAT, GL_FALSE, 0, NULL); (0, 4, GL_FLOAT, GL_FALSE, 0, NULL); glVertexAttribPointer glVertexAttribPointer(1, 4, GL_FLOAT, FL_FALSE, 0, ( (1, 4, GL_FLOAT, FL_FALSE, 0, (const glEnableVertexAttribArray glEnableVertexAttribArray(0); (0); glEnableVertexAttribArray glEnableVertexAttribArray(1); (1); const Glvoid Glvoid *) *) sizeof sizeof( (cube_positions cube_positions)); ));

  34. Example 3.8: Drawing a Cube Made from Two Triangle Strips Using Primitive Restart // Set up needed for a // Set up needed for a glDrawElements glBindVertexArray glBindVertexArray( (vao vao[0]); glBindBuffer glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, (GL_ELEMENT_ARRAY_BUFFER, ebo glDrawElements call. [0]); call. ebo[0]); [0]); #if USE_PRIMITIVE_RESTART #if USE_PRIMITIVE_RESTART // When primitive restart is on, we can call one draw command // When primitive restart is on, we can call one draw command glEnable glEnable(GL_PRIMITIVE_RESTART); (GL_PRIMITIVE_RESTART); glPrimitiveRestartIndex glPrimitiveRestartIndex(0xFFFF); (0xFFFF); glDrawElements glDrawElements(GL_TRIANGLE_STRIP, 17, GL_UNSIGNED_SHORT, NULL); (GL_TRIANGLE_STRIP, 17, GL_UNSIGNED_SHORT, NULL); #else #else // Without primitive restart, need to call two draw commands. // Without primitive restart, need to call two draw commands. glDrawElements glDrawElements(GL_TRIANGLE_STRIP, 8, GL_UNSIGNED_SHORT, NULL); (GL_TRIANGLE_STRIP, 8, GL_UNSIGNED_SHORT, NULL); glDrawElements glDrawElements(GL_TRIANGLE_STRIP, 8, GL_UNSIGNED_SHORT, ( (GL_TRIANGLE_STRIP, 8, GL_UNSIGNED_SHORT, (const (9* (9*sizeof sizeof( (GLushort GLushort))); ))); # #endif endif const GLvoid GLvoid *) *)

More Related Content