OpenGL tutorial (2) Draw points in the window

Source: Internet
Author: User

Address: http://ogldev.atspace.co.uk/www/tutorial02/tutorial02.html

When writing OpenGL programs, we usually need the glew library, which encapsulates various OpenGL extensions for our convenience. We can call the glew initialization function in the main function, and then we can query whether OpenGL extensions can be used. For functions that can be used, we can dynamically load them.

In this tutorial, we will first understand the usage of vertex buffer objects (VBO, vertex buffer object. In the 3D world of computer graphics, 3D object objects are composed of a series of vertices, such as the samurai model and the castle model (note the following two figures). These vertices are connected, mesh (triangle ).

As the name suggests, the vertex buffer is the memory object for storing the vertex. Generally, VBO is an area in the GPU memory. Using VBO can accelerate the reading speed of the vertex.

 

In this tutorial and the next tutorial, we only draw a point and a triangle using the programming method of fixed pipelines (Programmable pipelines) in OpenGL. We didn't translate, scale, or rotate the rendered objects (actually a vertex or a triangle). In fact, we just arranged our objects in the normalized cropping space, we can regard the normalized crop space as a cube. The coordinate origins are in the center of the square. The ranges of X, Y, and zcoordinate are [-1.0, 1.0]. we map objects to the screen space (we can think of objects in the cube first projected onto the z =-1 surface, and then the Quadrilateral maps to the screen space ), for example, if the screen space is 1024*768, X coordinate-1 is mapped to 0, + 1 is mapped to 1023, and X and Y are equal to 0, it is mapped to the center of the screen, finally, based on the semantics specified in the draw function, the object is rendered in the screen space through a raster operation.

Let's take a look at some of the source code implementation of the Program:

#include <GL/glew.h>

First, we need to include the glew header file. Note that we should put this header file in front of freee_glu.h. Otherwise, the program compilation may fail.

#include "math_3d.h"

In this tutorial, we draw a vertex, which is defined in math_3d.h.

GLenum res = glewInit();
if (res != GLEW_OK)
{
fprintf(stderr, "Error: '%s'\n", glewGetErrorString(res));
return 1;
}

The above is the glew initialization code. If the initialization fails, an error message is output. Note: The glew initialization code must be after the glut initialization.

Vector3f Vertices[1];
Vertices[0] = Vector3f(0.0f, 0.0f, 0.0f);

We created a vertex array with an element whose coordinate XYZ is 0, which will be displayed in the center of the screen.

GLuint VBO;

We define a global variable vbo of the gluint type to represent the vertex buffer. Generally, OpenGL objects are represented by a gluint type variable.

glGenBuffers(1, &VBO);

In OpenGL, all functions of the glgen * type generate various objects. These functions have two parameters. The first parameter specifies the number of objects to be created, the second parameter is the address of a gluint array, which stores various object handles allocated to you by the driver. The driver will ensure that the same object handle will not be generated when this function is called in the future, unless you call the gldeletebuffers function to display the delete object handle.

glBindBuffer(GL_ARRAY_BUFFER, VBO);

In OpenGL, an object is usually bound to a target name. For example, a VBO object is bound to gl_array_buffer (indicating that the buffer is a vertex array, and another common target name is gl_element_array_buffer, index Array), and then execute commands on the target name. These commands will always affect the bound object unless we bind the target name to a new object, executing the command on target name will affect the newly bound objects.

glBufferData(GL_ARRAY_BUFFER, sizeof(Vertices), Vertices, GL_STATIC_DRAW);

After the object is bound, we start to prepare the vertex buffer data. In the above function, we fill in the vertex buffer with vertices data. gl_static_draw indicates that the vertex data will not change during rendering, the corresponding value is gl_dynamic_draw. The driver will optimize the program based on these statuses.

glEnableVertexAttribArray(0);

In the subsequent shader program, we will see more detailed introduction to vertex attributes. In this tutorial, we did not use shader, But we installed the vertex position in the vertex buffer, the position attribute is used as the index 0 of the vertex attribute (Note: Since there is no shader, we use a fixed pipeline rendering), you must open it; otherwise, it cannot be used, because all vertex attributes must be enabled before use.

glBindBuffer(GL_ARRAY_BUFFER, VBO);

We can bind the vertex buffer again, but we can also not bind it, because we only have one buffer, and we have already bound it to it. However, in large 3D programs, there may be a lot of vertex buffering, before rendering, switch to bind different vertex buffer objects as needed.

glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, 0);

This function tells the pipeline how to explain the data in the vertex buffer. The first parameter specifies the attribute index, the second parameter is the number of attributes (3, indicating, x, y, z), and the third parameter is the attribute data type, the fourth parameter is whether the attribute is normalized. The fifth parameter is stride, which indicates the number of bytes of the attribute. We only have one attribute, so the value is 0. If we have two attributes, A position and a normal direction. If they are all vectors in the 3-dimensional float format, this parameter must be set to (6*4) = 24 byte, And the last parameter is offset, if there are two attributes, this value is 0 for the first attribute, no offset, and 12 for the second attribute.

glDrawArrays(GL_POINTS, 0, 1);

Finally, we call the draw function, which is the starting point for the GPU to start working. The GPU binds the parameters and status of the draw function and then transmits the data to the GPU through the driver. OpenGL has several forms of draw functions, which can be divided into ordered draws and indexed draws. The ordered draw function draws the specified vertex in the semantic order. For example, if you specify gl_triangles, the 0-2 vertex is the first triangle, and the 3-5 vertex is the second triangle. Index draws indexes vertex data through index buffering (in fact, at the hardware level, there is no index-free draw implementation. If no index buffering is specified, the hardware will follow the vertex sequence, generate an index that corresponds to the vertex order), so that vertex data can be reused. For example, we can use four points to represent a quadrilateral (composed of two triangles ), two points are shared by two triangles, and the data in the index buffer is the vertex's position in the vertex buffer.

We call the drawarrays function to draw a vertex. The first parameter specifies the body meta semantics and draws a vertex. The second parameter is the index position of the first vertex, the third parameter is the number of vertices to be rendered.

glDisableVertexAttribArray(0);

When you do not use the vertex attribute, remember to disable it to avoid unknown errors.

 

After the program is executed, the interface is as follows. In the center of the window, there is a small almost invisible white point.

We can also add the following code to set the vertex size and set the vertex to a dot.

// The following four lines of code set the vertex size, start Alpha blend, and multi-sample, so that you can draw a circular Vertex
Glenable (gl_point_smooth );
Glable (gl_blend );
Glblendfunc (gl_src_alpha, gl_one_minus_src_alpha );
Glpointsize (8.0f );

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.