Basic OpenGL functions

Source: Internet
Author: User

 

Glviewport ()Function

After OpenGL Initialization is complete, we should make some view settings. The first step is to set the visible area, that is, to tell OpenGL which part of the rendered image should be drawn in the form. When the visible area is the entire form, OpenGL will draw the rendering result to the entire window. We call the glviewport function to determine the region as shown in the following figure:

Procedure glviewport (X: glint; Y: glint; width: glsizei; Height: glsizei );

The parameters X and Y specify the position in the window in the lower-left corner of the visible area. Generally, they are (0, 0), and width and height specify the width and height of the visible area. Note that the window coordinates used by OpenGL are different from those used by windowsgdi. Figure 3.1-1 represents the window coordinates in windowsgdi, while Figure 3.1-2 represents the window coordinates defined by OpenGL.

For example, to set the visible area in 3.1-3, we should call the function:

Glviewport (100,100, width, height );

Glmatrixmode ()

Specifies which matrix is the current Matrix
Void glmatrixmode (glenum mode) in C Language)
The mode parameter specifies which matrix stack is the target of the next matrix operation. Optional values: gl_modelview, gl_projection, and gl_texture.

Description

Glmatrixmode:
Gl_modeview: applies subsequent matrix operations to the model visual matrix stack.
Gl_projection: Apply subsequent matrix operations to the projection matrix.
Gltexture applies subsequent matrix operations to the texture matrix stack.

Glloadidentity ()
This function is used to reset the current specified matrix as the unit matrix. After glloadidentity (), we set a perspective for the scenario. Glmatrixmode (gl_modelview) sets the current matrix to a model view matrix, which stores information about objects.

Gluortho2d ()
Gluortho2d (-5.0, 5.0,-5.0, 5.0 );
Parameters represent (the lower left corner X coordinate, the upper right corner X coordinate, the lower left corner y coordinate, and the upper right corner y coordinate) -- coordinates are all relative to the lower left corner of the window -- origin), near and far are-1 and 1 by default, this function determines a parallel cube, that is, view volume! The larger the view volume, the smaller the objects in it! For example, the coordinates of a point are (0, 0, 0) in the middle of the parallel cubes, that is, in the middle of the viewport. For example, the coordinates of a point are (-5.0,-5.0, 0) is in the lower left corner of the parallel cube, that is, in the lower left corner of vieport.
Note: viewport refers to the window size, just like our eyes! Viweport can be specified using the following function: glviewport (x coordinate in the lower left corner, Y coordinate in the lower left corner, wightth, height);, default is (0, 0, window width, window height) you can use the gluortho function to specify near, far, gluortho (xmin, xmax, ymin, Ymax, near, far );

Gltranslatef ()
Gltranslatef is used to move the coordinate origin. The three parameters correspond to three coordinate axes. If you call gltranslatef (1.0f, 0.0f, 0.0f) and draw a small ball, then call gltranslatef (0.0f, 1.0f, 0.0f) and draw another small ball. At this time, one of the two balls is on the right of the other. Therefore, if you want to place the two balls in the X and Y axes respectively, you need to call the glloadidentity () function before the second painting to restore the coordinate origin. In addition, the coordinate system here is the right-hand coordinate system.

Glpushmatrix () glpopmatrix ()
It is equivalent to the inbound and outbound stacks in the stack.
What many people do not understand is what they are and what they are. I also learned it after repeated tests. For example, the origin of your current coordinate system is at the top left of your computer screen. Now you call glPushMatrix, call a bunch of translation and rotation codes, and draw images. The translation and rotation changes based on the starting point of the SIT cursor. And it will change the coordinates. After these changes, your coordinates will definitely not be in the upper left corner. What should I do if I want to restore it? Then, call glPopMatrix to retrieve a "state" from the stack. This state is the one before you call glPushMatrix. As mentioned in many opengl books: Calling glPushMatrix is actually to make a copy of the current State into the stack.

Glrasterpos2i ()
GlRasterPos2i (200,200); change the Grating Position
Raster: a rectangular grid composed of pixels. The data to be displayed on the grating is stored in the frame cache.

Glbitmap ()
After the grating position is set, you can call the glBitmap () function to display the bitmap data. The function form is:
Void glBitmap (GLsizei width, GLsizei height, GLfloat xbo, GLfloat ybo, GLfloat xbi, GLfloat ybi, const GLubyte * bitmap );
Displays the bitmap specified by bitmap. bitmap is a pointer to a bitmap. The origin of the bitmap is placed on the recently defined position of the current grating. If the current Grating Position is invalid, this bitmap or part is not displayed, and the current grating position is still invalid. The width and height parameters are measured in pixels, indicating the width and height of the bitmap. The width is not necessarily a multiple of 8. The xbo and ybo parameters define the origin of the bitmap (when a positive value is reached, the origin is moved up; when a negative value is reached, the origin is moved down ). Increment of the grating position after the raster of the in-position graph where the xbi and ybi parameters are located.

Glreadpixels ()Read pixel data, void glReadPixels (GLint x, GLint y, GLsizesi width, GLsizei height, GLenum format, GLenum type, GLvoid * pixel); function parameter (x, y) defines the coordinates of the points in the lower left corner of the image area. width and height indicate the height and width of the image respectively. * pixel is a pointer pointing to an array that stores image data. The format parameter specifies the format of the read pixel data element (index value or R, G, B, and A value, as shown in table 11-1 ), the parameter type indicates the Data type of each element (see table 11-2 ). Name pixel data type GL_INDEX single color index GL_RGB first red component, then green component, then there is the blue GL_RED, a single red GL_GREEN, a single green, GL_BLUE, a single blue, GL_ALPHA, a single Alpha, GL_LUMINANCE_ALPHA, the first brightness, then there is the Alpha value GL_STENCIL_INDEX single template index GL_DEPTH_COMPONENT single depth component Table 11-1 function glReadPixels () and glDrawPixels () pixel format Name Data Type GL_UNSIGNED_BYTE unsigned 8-bit integer GL_BYTE 8-bit integer GL_BITMAP unsigned 8-bit integer array of a single digit GL_UNSIGNED_SHORT unsigned_int unsigned 32-bit integer GL_INT 32-bit integer GL_FLOAT Single-precision floating point number table 11-2 function glReadPixels () and glDrawPixels () pixel data types
Gldrawpixels ()Write the pixel data void glDrawPixels (GLsizesi width, GLsizei height, GLenum format, GLenum type, GLvoid * pixel). The format and type parameters of the function are the same as those of glReadPixels, the array pointed to by pixel contains the pixel data to be drawn. Note: You must set the current Grating Position before calling this function. If the current Grating Position is invalid, no image is drawn when this function is provided, and the current grating position remains invalid.Glcopypixels ()The pixel copy function is void glCopyPixels (GLint x, GLint y, GLsizesi width, GLsizei height, GLenum type). This function is similar to calling glReadPixels () first () the function then calls glDrawPixels (), but it does not need to write data to the memory because it only writes data to framebuffer. The function is to copy the pixel data in the rectangle area (x, y) with the width and height in the lower left corner of framebuffer. Copy the data to a new position. The position in the lower left corner of the data source is in the current grating. The parameter type can be GL_COLOR, gl_stencer, or GL_DEPTH. During the copy process, the type parameter must be converted to format as follows:

1) if the type is GL_DEPTH or gl_stencel, the format should be GL_DEPTH_COMPONENT or GL_STENCIL_INDEX;
2) If the type is GL_COLOR, format uses GL_RGB or GL_COLOR_INDEX. This depends on whether the graphic system is in the RGBA mode or in the color table mode.

Glpixelzoom ()Generally, a pixel of an image is also a pixel when it is written to the screen, but sometimes the image needs to be zoomed in or out. OpenGL provides this function: Image Scaling void glPixelZoom (GLfloat zoomx, GLfloat zoomy); sets the zoom-in or zoom-out factor for Pixel write operations in the X and Y directions. By default, both zoomx and zoomy are 1.0. If they are all 2.0, each image pixel is painted on four screen pixels.Note:: Both the scaling factor and the negative factor in decimal form are acceptable.Gluswapbuffers ()The switching buffer is stored in the dual-buffer mode, and the game uses three image memory areas. One of them is "window memory ". The other two are "buffer memory 1" and "buffer memory 2 ". After glBegin () is used for rendering, glswapbuffers is used. The video card is responsible for copying the "buffered video memory 1" image to the "window memory. Then use glBegin () for rendering, and the screen is first rendered to "buffer memory 2. At this time, the video card is still busy copying the content on "buffer video memory 1" to "window memory. Therefore, the "rendering process" and "display process" are both correct!
Glupostredisplay ()If it is a single thread, only using postredisplay without returning it cannot go through display. Split the loop and place the loop body in idle.Glfrustum ()GlFrustum (left, right, bottom, top, near, far); parameters are used to determine the points of the angle of view boundary. Our goal is to determine the red area by calling glFrustum. First, the dot on the left side of the image is the coordinate origin (0, 0, 0), and the coordinate in the lower left corner of the yellow near plane (from the coordinate origin to the yellow plane). Use (left, bottom,-near) and the coordinates in the upper right corner of the near plane are specified using (right, top,-near. In order to determine the red area, we still lack coordinate points on the far plane. In fact, we only need to know the zcoordinate far of the far plane to determine this plane. Because the coordinates of the four corners of the far plane can be determined by the intersection of the origin point and the four corners of the near plane and the far plane. Therefore, we only need to set (left, bottom,-near), (right, top,-near), and-far to determine the visible area of red, this constitutes several parameters of the glFrustum function.Glbegin ()OpenGL abstracts an object into a set of points, line segments, and polygon in the Cartesian coordinate system, and then abstracts points, line segments, and polygon in the glbegin () and glend () functions () and draw a graph to restore objects. OpenGL uses the glbegin () and glend () functions to draw the glbegin (glenum) number of points, lines, triangles, quadrilateral and polygon. glenum has the following 10 parameters: gl_points: gl_lines: indicates that the function starts to draw the line gl_line_strip: indicates that the function starts to draw the line gl_line_loop: indicates that the function starts to draw the closed curve gl_trianles: indicates that the function starts to draw the triangle gl_trianle_strip: indicates that the function will start to draw the triangle slice gl_trianle_fan: indicates that the function will start to draw the triangle slice gl_quads: indicates that the function starts to draw the Quadrilateral gl_quad_strip: indicates that the function starts to draw the polygon.
Glrotatef ()An object rotates around a specified vector. The specified angle glrotatef (glfloat angle, glfloat X, glfloat y, and glfloat Z) angle is undoubtedly the rotation angle, while x, y, the three parameters Z should be regarded as a whole to represent a vector, indicating that the object rotates around the vector [x, y, z.

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.