OpenGL Coordinate System

Source: Internet
Author: User

This article Reprinted from: http://anony3721.blog.163.com/blog/static/511974201132244451/

What are the basic steps to use OpenGL in a Windows program?

  • Getting a device context (DC) for the rendering location
  • Selecting and setting a pixel format for the device context
  • Creating a rendering context (RC) associated with the device context
  • Drawing Using OpenGL commands
  • Releasing the rendering context
  • Release the device context

What is a dc?

  • A dc is simply a data structure that keeps the state of the current settings and route callto the appropriate device.
What is an RC?
  • An RC is a data structure that keeps the state variables for OpenGL. It is also a portal through which OpenGL CALS are sent to the device.
  • You can think of both DC and RC as a data structure that keeps the state of the current settings and routes callto the proper device.

Brief Introduction to pixel format data structure

  • Pixel formats are the translation between OpenGL CILS (such as an OpenGL call to draw a pixel with an RGB triad value of [128,120,135]) and the rendering operation that Windows performs (the pixel might be drawn with a translated RGB value of [128,128,128]).
    The selected pixel format describes such things as how colors are displayed, the depth of field resolution, and what additional capabilities are supported by the rendering context you have created. the Windows OpenGL has four functions that handle the pixel
    Format.

      Pixel Format Function Description ------------------------------------------------------------------ChoosePixelFormat() Obtains a DC's pixel format that's the closest match to a pixel format you've provided.SetPixelFormat() Sets a DC's current pixel format to the pixel format index specified.GetPixelFormat() Returns the pixel format index of a DC's current pixel format.DescribePixelFormat() Given a DC and a pixel format index, fills a PIXELFORMATDESCRIPTOR data structure with the pixel format's properties.

    The main properties of pixel format include:

    • Single or double buffering: mainly for smooth animation.
    • Rgba or Color Indexing: A color can be directly specified by Red, green, and Blue values. some video display controllers have limited frame buffer memory. for example, EGA supports 16 colors. therefore, only 4 bits is used to represent a pixel. this pixel
      Value is used as the index into a color map to find the associated RGB values for display.
    • Drawing to a window or bitmap: When a drawing is sent to a window, it will be displayed in real-time on the screen. a bitmap is a canvas in the main memory. the content in a bitmap may be displayed later by being copied into a window.
    • Support of GDI (window's Graphics Device Interface) or OpenGL CILS.
    • Color depth: the number of bits per pixel in the frame buffer.
    • Z-axis depth: the number of bits per pixel in Z-buffer that is used for hidden surface removal.

 

The view model transformation process is a process of transforming vertex coordinates from the world coordinate system to the visual coordinate system. It is important to understand the two coordinate systems.

The world coordinate system, also known as the global coordinate system. It is a right-hand coordinate system, which can be considered to be fixed. In the initial stateXAxis is right along the screen horizontal,YAxis is vertical up along the screen,ZThe axis is the vertical screen that points out to the user. Of course, if you convert the viewpoint in the program, you can no longer think of it as this.

The visual coordinate system, also known as the local coordinate system. It is a left-hand coordinate system that can be active. In the initial state, the origin andX, Y
The Origin andX, Y
The axes overlap, whileZ
The axis is the opposite, that is, inside the vertical screen.

--------------------------------------------------------------------------------

OpenGL coordinate systems can be divided into the world coordinate system and the current drawing coordinate system. It should be clear that ANSYS has been used.

The world coordinate system uses the screen center as the origin (0, 0, 0 ). When you face the screen, the right side is the X axis, and the Y axis is on the top. The screen points to you as the Z axis. The length unit is determined as follows:
The window ranges from (-1,-1) ).

The current drawing coordinate system is the coordinate system when an object is drawn, that is, the model coordinate system. At the beginning of the program initialization, the world coordinate system and the current drawing coordinate system are coincident. After you use gltranslatef (), glscalef (), and glrotatef () to translate, scale, and rotate the current drawing coordinate system, the world coordinate system and the current drawing coordinate system do not overlap. After the changes, when drawing functions such as glvertex3f () are used, all the functions are drawn in the current drawing coordinate system.
For the current drawing coordinate system.

 

World coordinates are the coordinates used in OpenGL to describe the scene. The vertical screen of the Z + axis is outward, x + is left to right, and the Y + axis is bottom to top. It is the Cartesian coordinate system of the right hand. We use this coordinate system to describe the position of the object and the light source.

Moving an object to a scene also means moving the object to a specific position and rotating a certain angle. These operations are coordinate transformation. OpenGL provides three coordinate transformation commands: gltranslate */glrotate */glscale *. By using OpenGL matrix operation commands, any complex coordinate transformation can be implemented.

Important:OpenGL hasCoordinate TransformationIn the matrix stack (modelview), the top of the stack is the current coordinate transformation matrix. Every coordinate (homogeneous coordinate) that enters the OpenGL pipeline will first Multiply this matrix, and the result is the world coordinate of the corresponding point in the scenario. The coordinate transformations in OpenGL are all completed through matrix operations, which are exactly the same as those described in the graphic textbooks. It should be noted that the matrix multiplication in the transformation is left multiplication, and the matrix multiplication is different from the arithmetic multiplication, and does not conform to the exchange law (in case you do not understand the matrix algebra ).

Gltranslate * (x, y, z): translation. The parameter indicates the movement of each axial direction.
Glrotate (D, x, y, z): rotation. The first parameter is the degree of rotation. The last three parameters indicate whether to rotate around the axis. Generally, only one of X, Y, and Z is 1, and the other is 0. Complex rotation is completed using several consecutive rotation commands. Due to the left multiplication of matrix operations, the order of rotation commands is exactly the opposite of that of rotation operations.

The following code uses three transformations to draw vertices:

Glmatrixmode (gl_modelview );

Glloadidentity ();

Glmultmatrixf (N);/* Apply Transformation
N
*/

Glmultmatrixf (M);/* Apply Transformation
M*/

Glmultmatrixf (L);/* Apply Transformation
L
*/

Glbegin (gl_points );

Glvertex3f (V);/* Draw transformed Vertex
V*/

Glend ();

In this process, the gl_modelview status is introduced one after another.I(Unit array ),N,M,LMatrix. The transformed vertex isNmlv. Therefore, the transformation of vertices isN(M(LV).LAnd then transformMAnd finallyN. Here, the actual transformation order of vertex v is exactly the opposite of the specified order.

 

  • View Transformation

View transformation changes the position and direction of the viewpoint, that is, changes the visual coordinate system. In the world coordinate system, the point of view and the object location are relative relations, and some translation and Rotation Transformations are performed on the object, you can certainly achieve the same visual effect by translating and rotating the viewpoint. You can use the following methods to complete view Transformation:

(1 ).

Use one or more styling transformation commands (gltranslate * () and glrotate *()). These commands are also executed in the gl_modelview state, so it is difficult to distinguish them from those for shape transformation. The transformation of moving viewpoint and the transformation of moving object are easy to confuse. To facilitate the establishment of clear object and scene models, it can be considered that only one of the transformations is at work. For example, if only the model transformation is used, then gltranslate * () and glrotate *() it is regarded as a transformation of an object.

(2) set the visual coordinate system using the library function glulookat. In practical programming applications, after modeling a scenario, you often need to select a proper perspective or constantly change the perspective to observe the scenario. The practical library function glulookat () provides such a function.

 

Void glulookat (gldouble eyex, gldouble eyey, gldouble Eyez,

Gldouble centerx, gldouble centery, gldouble centerz,

Gldouble UPX, gldouble upy, gldouble upz );

This function defines a view matrix and multiply it with the current matrix.

 

Eyex, eyey, Eyez

Point of View

Centerx, centery, centerz

Location of the specified reference point

UPX, upy, upz

Specifies the upward direction of the viewpoint.

(3) create a practical function that encapsulates the rotation and translation commands. Some applications need to specify the custom function of view transformation in a simple way. For example, you can specify the rolling, pitching, and heading rotation angles during an airplane flight, or specify a conversion using polar coordinates for a camera that moves the surrounding object.

An object coordinate is a "world coordinate" created from an object's point of origin. This coordinate system is only applicable to this object and is used to simplify the description of coordinates of each part of the object. When an object is placed in a scene, the coordinate transformation of each part is the same, and the relative position remains unchanged. Therefore, it can be viewed as a whole, consistent with human thinking habits.

The eye coordinate is the orientation in the coordinate system with the viewpoint as the origin and the line of sight as the orientation in the positive direction of the Z + axis. The OpenGL pipeline will first transform the world coordinates to the eye coordinates and then crop them. Only scenes within the line of sight (depending on the body) will enter the next stage of computing.

Likewise, the projection transformation matrix stack (projection) is the current projection transformation matrix, which is responsible for converting the coordinates of the scenario to eye coordinates, the result is the cropped Scene Section, which is called the cropping coordinate. As mentioned above, the view object setting is actually creating this matrix.

Device coordinates: One of the important functions of OpenGL is to convert and project the 3D world coordinates, and finally calculate the corresponding position on the display device, which is called the device coordinates. The coordinates on the screen, printer, and other devices are two-dimensional coordinates. It is worth mentioning that OpenGL can be used to draw only a part of the device, which is called the view area or the viewport ). The projected coordinates are the coordinates in the area of view (projection coordinates). The calculation process from the projection coordinates to the device coordinates is the transformation of the device.

Matrix stack switching: glmatrixmode (gl_modelviewing or gl_projection); after this command is executed, the matrix Stack referred to by the parameter becomes the current matrix stack, and subsequent matrix stack control commands will act on it.

Matrix stack operation command:
Glpushmatrix (); when the current matrix is added to the stack, the matrix stack pushes the top value of the stack to the stack.
Glpopmatrix (); the stack is usually used with the previous command.
Glloadidentity (); set the top of the stack to a constant matrix (that is, the diagonal line is all one and the other is 0 ).
Glmultmatrix (m); set the top t of the stack to m · t.

------------------------------------------------------------------

From a 3D object to a two-dimensional image, just like taking a photo with a camera, the following steps are usually taken:

1. Place the camera on a tripod to align it with a 3D scene. It is equivalent to adjusting the viewpoint position in OpenGL, that is, viewing transformation ).

2. Place a 3D object in a proper position in the scene. It is equivalent to modeling transformation in OpenGL, that is, rotating, translating, and scaling the model.

3. Select a camera lens and focus on it so that a 3D object is projected on a two-dimensional film. It is equivalent to the process in OpenGL to project a three-dimensional model to a two-dimensional screen, that is, projection transformation of OpenGL. There are two projection methods in OpenGL: normal projection and Perspective Projection. In order for the displayed object to be displayed in a proper position, size, and direction, projection is required. Sometimes, to highlight a part of the image, only a part of the image is displayed. In this case, you can define a three-dimensional visual object (Viewing Volume ). Normal projection is generally a cube visual body, while perspective projection is generally a pyramid-like visual body. Only objects in the visual view can be projected on the display plane. Other parts cannot.

4. Rinse the negative film to determine the size of the Two-Dimensional Photo. It is equivalent to the viewport transformation in OpenGL (a rectangle can be defined in the screen window, called the viewport ), specifies the range and size of the scene displayed on the screen.

Through the above steps, an object in a three-dimensional space can be represented by a corresponding two-dimensional plane object.

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.