OpenGL learning notes (1): State management and drawing

Source: Internet
Author: User
Tags server memory
Document directory
  • 6.1. Enable Array
  • 6.2. Put data
  • 6.3. unreference and draw images
  • 6.4 dereference a list of array elements
  • 6.5 dereference a sequence of an array element
  • 7.1 create a cache object
  • 7.2 activate a buffer object
  • 7.3 assign values/initialize cache objects
  • 7.4 update cache objects
  • 7.5 copy data between cached objects
  • 7.6 clear cache objects
  • 7.7 use cached objects to store vertex Arrays
  • 9.1 name, create, and delete
  • 9.2 execution
  • 9.3 use the display list to manage status variables

By yurunsun@gmail.com
Sina Weibo @ sun yurun Sina Blog
Csdn blog

Date:

Content of this chapter:

  1. Clear the window in any color
  2. Draw geometric elements in two or three dimensional space: points, straight lines, and polygon
  3. Enable/disable/query status
  4. Control the display of geometric elements: Line and polygon
  5. Specify the normal vector at an appropriate position on the surface of a solid object
  6. Store and access geometric data using Vertex arrays and buffer objects
  7. Save and restore several state variables at the same time
  8. Display list
1. Drawing toolbox
  1. Clear window

    glClearColor(0.0, 0.0, 0.0, 0.0);glClearDepth(1.0);glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  2. Color

    glColor3f(0.0, 1.0, 0.0);
2. Draw points, straight lines, and polygon
  1. Specify Vertex

    glVertex3f(1.0, 1.0, 1.0);
  2. Geometric elements

    Place a set of vertices between glbegin () and glend () to draw geometric elements:

    glBegin(GL_POLYGON)    glVertex3f(-1.0, -1.0, 0.0);    glVertex3f(1.0, -1.0, 0.0);    glVertex3f(1.0, 1.0, 0.0);    glVertex3f(-1.0, 1.0, 0.0);glEnd();

    DivisionGL_POLYGONFor more information about the names of other elements, see the manual.

3. open/close/query status
    glEnable(GL_LIGHTING);    glDisable(GL_FOG);    glIsEnabled(GL_DEPTH_TEST);    glGetBooleanv(GL_BLEND, bBlend);    glGetIntegerv(GL_CURRENT_COLOR, colorArr);
4. Control the display of geometric elements
  1. Point Size

    glPointSize(2.0);
  2. Straight line width and vertices

    glLineWidth(3.0);glLineStipple(1, 0x3F07);glEnable(GL_LINE_STIPPLE);
  3. Point, line, fill mode of polygon, front and discard

    Glpolygonmode (gl_front, gl_fill); glpolygonmode (gl_back, gl_line); glfrontface (gl_ccw); // The Front glcullface (gl_back) in the counterclockwise vertex direction ); // discard the back of glable (gl_cull_face) Before converting to the screen coordinate );
5. Normal
  1. Concept

    • The normal is a direction vector perpendicular to a certain surface.
    • The vertical direction of each point on the surface is the same, and the normal of each point on the surface may be different.
    • In OpenGL, you can specify either a normal for each polygon or a normal for each vertex of the polygon.
    • Only the normal can be allocated at the vertex.
  2. Implementation

    glBegin (GL_POLYGON);    glNormal3fv(n0); glVertex3fv(v0);    glNormal3fv(n1); glVertex3fv(v1);    glNormal3fv(n3); glVertex3fv(v3);glEnd();

    Enable Automatic normal Normalization:

    glEnable(GL_NORMALIZE);
6. vertex array (VA)

Using the previous method to draw a polygon with 20 edges requires 22 function calls: Specify 20 vertices, a pairglBegin/glEndYou must specify three times for each vertex to draw a cube. By using the OpenGL vertex array, you only need to call it once. The procedure is as follows:

  • Enable arrays (up to 8 at the same time)
  • Put data
  • Drawing: Random/system/linear access
6.1. Enable Array
glEnableClientState(GL_NORMAL_ARRAY);glEnableClientState(GL_VERTEX_ARRAY);

For example, if the normal state is disabled after the light is turned off

Gldisableclientstate (gl_normal_array); the new function name is used instead of 'glenable' because the vertex array is stored on the client end and cannot be placed in the display list. The 'glenable' must be able to display the list on the server.
6.2. Put data
glColorPointer(3, GL_FLOAT, 0, colorArr);glVertexPointer(3, GL_FLOAT, 0, vertexArr);
6.3. unreference and draw images

The data is always stored on the client before the resolution of the reference, and the content is easily modified. In this step, the array data is sent to the server for plotting.

glArrayElement(GLint ith);

Obtains the ith vertex data of all currently enabled arrays. Separate calls:

glEdgeFlag3fv();glTexCoord3fv();glColor3fv();glSecondaryColor3fv();glIndexfv();glNormal3fv();glFogCoordfv();glVertex3fv();  

For example:

glEnableClientState(GL_NORMAL_ARRAY);glEnableClientState(GL_VERTEX_ARRAY);glColorPointer(3, GL_FLOAT, 0, colorArr);glVertexPointer(3, GL_FLOAT, 0, vertexArr);glBegin(GL_TRIANGLES);    glArrayElement(2);    glArrayELement(3);    glArrayELement(5);glEnd();

The effect is the same as the following:

glBegin(GL_TRIANGLES);    glColor3fv(colorArr + 2*3);    glVertex3fv(vertexArr + 2*3);    glColor3fv(colorArr + 2*3);    glVertex3fv(vertexArr + 2*3);       glColor3fv(colorArr + 2*5);    glVertex3fv(vertexArr + 2*5);glEnd();
6.4 dereference a list of array elements

AndglArrayElement()Similar functions include:

glDrawElements();glMultiDrawElements();glDrawRangeElements();
  • Gldrawelements ()

    void glDrawElements(GLenum mode, GLsizei count, GLenum type, const GLvoid* indices);

    Equivalent

    glBegin(mode);for (int i = 0; i < count; ++i) {    glArrayElement(indices[i]);}

    Note: Do not setglDrawElements()Put inglBegin()/glEnd()Between

  • Glmultidrawelements ()

    void glMultiDrawElements(GLenum mode, GLsizei* count, GLenum type, const GLvoid** indices, GLsizei primcount);

    Equivalent

    for (int i = 0; i < premcount; ++i) {    if (count[i] > 0) {        glDrawElements(mode, count[i], type, indices[i]);    }}    
  • Gldrawrangelements ()

    InAdded Based on gldrawelements ()Start/end, and vertices outside of them will be discarded.

6.5 dereference a sequence of an array element

All the above functions can be stored randomly,glDrawArrays()Only access in order:

void glDrawArrays(GLenum mode, GLint first, GLsizei count);

Equivalent:

Glbegin (mode); For (INT I = 0; I <count; ++ I) {glarrayelement (first + I);} glend (); and 'glmultidrawarrays () 'provides a similar combined call function.

In the following two chapters, the content of vbo vao is not supported by OpenGL 1.1.

7. cache object (BO)

We send a large amount of vertex, vector, and other data to OpenGL. This transmission may be simple from memory copy to the video card. However, OpenGL is designed in C/S mode, when OpenGL requires data, it must send data from the client memory to the server graphics card at any time. If distributed rendering is used, the data transmission efficiency is very low. Therefore, buffer object is introduced to allow the program to display the specified data cached on the server.

  • V1.5 supports vertex data caching
  • V2.1 supports pixel data caching
  • V3.1 supports the uniform cache object (Uniform buffer object), and stores block-based unified variable data for the coloring tool.
7.1 create a cache object
void glGenBuffers(GLsizei n, GLuint* buffers);

Return n unused names in the buffer array, indicating the cached object.

7.2 activate a buffer object
void glBindBuffer(GLenum target, GLuint buffer);

To activate a cache object, bind it first, indicating which cache object will be affected by future operations. The optional values include:

GL_ARRAY_BUFFERGL_ELEMENT_ARRAY_BUFFERGL_PIXEL_PACK_BUFFERGL_PIXEL_UNPACK_BUFFERGL_COPY_READ_BUFFERGL_COPY_WRITE_BUFFERGL_TRANSFORM_FEEDBACK_BUFFERGL_UNIFORM_BUFFER
7.3 assign values/initialize cache objects
void glBufferData(GLenum target, GLsizeiptre size, const GLvoid* data, GLenum usage);

The server memory allocated in each byte is used to store vertex data or indexes. If data is null, the memory is allocated. If it is a pointer to the client memory, data is copied to the server.

7.4 update cache objects

Two update Methods:

  1. Directly replace memory

    void glBufferSubData(GLenum target, GLintptr offset, GLsizeiptr size, const GLvoid *data);
  2. Obtain the memory pointer and modify it flexibly.

    GLvoid *glMapBuffer(GLenum target, GLenum access);GLboolean glUnmapBuffer(GLenum target);
7.5 copy data between cached objects

There are two steps before v3.1:

  1. Copy data from cache object to client memory
  2. Bind to a new cache object

V3.1 use new functions:

void glCopyBufferSubData(GLenum readbuffer, GLenum writebuffer, GLintptr readoffset, GLintptr writeoffset, GLsizeiptr size);
7.6 clear cache objects
void glDeleteBuffers(GLsizei n, const GLuint *buffers);
7.7 use cached objects to store vertex Arrays

The procedure is as follows:

  1. Generate cache Object ID
  2. Bind Cache object to determine whether to store vertex data or index
  3. Request data memory
  4. Offset relative to the starting position of the cache
  5. Bind Cache object for rendering
  6. Use vertex array rendering Function

Complete example:

#define VERTICES 0#define INDICES 1#define NUM_BUFFERS 2GLuint buffers[NUM_BUFFERS];GLfloat vertices[][3] = {...}GLubyte indices[][4] = {...};glGenBuffers(NUM_BUFFERS, buffers);                     // step 1glBindBuffer(GL_ARRAY_BUFFER, buffers[VERTICES]);       // step 2glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW); // step 3glVertexPointer(3, GL_FLOAT, 0, BUFFER_OFFSET(0));      // step 4glEnableClientState(GL_VERTEX_ARRAY);glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, buffers[INDICES]);// step 2glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW);// step3glDrawElements(GL_QUADS, 24, GL_UNSIGNED_BYTE, BUFFER_OFFSET(0)); // step6
8. vertex array object (VAO)

Large data volumes may require switching between multiple sets of vertex arrays in each frame,glVertexPointer()The number of such function calls increases accordingly. The vertex array object is bound to the called set to set the status of the vertex array.

void glGenVertexArrays(GLsizei n, GLuint *arrays);void gBindVertexArray(GLuint array);void glDeleteVertexArrays(GLsizei n, GLuint *arrays);
9. display the list

OpenGL uses CS mode. data such as vertices needs to be sent from the client to the server for each draw. Use the display list to save the drawing command on the server and avoid repeated computation.

However, the value in the display list cannot be modified later. You need to delete the display list and create a new list.

The display list is most effective in the following areas:

  1. Matrix Operations
  2. Raster bitmap and Image
  3. Light source, material property, and illumination model
9.1 name, create, and delete
Gluint glgenlists (glsizei range); void glnewlist (gluint list, glenum mode); void gldlist (void); glboolean glislist (gluint list); void gldeletelists (gluint list, glsizei range ); gl_compile; // do not execute gl_compile_and_execute immediately

Functions on the client side and functions used to extract Status values cannot be stored in the display list.

9.2 execution
  1. General Form

    void glCallList(GLuint list);
  2. Hierarchical display list

    glNewList(listIndex,GL_COMPILE);    glCallList(handlebars);    glCallList(frame);    glTranslatef(1.0, 0.0, 0.0);    glCallList(wheel);    glTranslatef(3.0, 0.0, 0.0);    glCallList(wheel);glEndList();
  3. Execute multiple

    void glListBase(GLuint base);   

    This function specifies an offset to add the index to the display list in the glcalllists () function to obtain the final display list index. The default value is 0.glCallList()/glNewList()Function ineffective

    void glCallLists(GLsizein, GLenum type, const GLvoid *lists);
9.3 use the display list to manage status variables

If the rendering command contains a state change command, the state will be modified and maintained during execution, but sometimes we want to restore the original state after the display list is executed. AvailableglPushAttrib()The function saves a group of state variables and usesglPopAttrib()The function restores these values as needed.

OpenGL groups related state variables, called attribute groups, suchGL_LINE_BITThe attribute includes five states: width, dot-painting, and anti-sawtooth.glPushAttrib()/glPopAttrib()You can save and restore all five statuses at the same time.

  • If this article is helpful to you, please go to the csdn blog to leave a message;
  • Reprinted Please note: Technical blog from yurun

    Http://blog.csdn.net/sunyurun

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.