Win32 OpenGL programming (6) entering the 3D world

Source: Internet
Author: User
Tags mercurial truncated
Win32 OpenGL programming (6) entering the 3D world

Write by nine days Yan Ling (jtianling) -- blog.csdn.net/vagrxie


Discuss newsgroups and documents

Technorati label: OpenGL
, 3D
, Graphic
, Programming Abstract

OpenGL is designed for high-performance 3D image processing. It is also used in VR, 3D games, and other fields. This section officially begins to engage in the 3D field, it is no longer mixed in the small world of 2D. (In fact, 2D is also a big field. Didn't it all be 2D in the past? -_-!) This article mainly uses the triangle cone as an example to explain what we need to understand in 3D world when drawing a 3D coordinate axis, a line-frame model of the triangle cone, and a solid model of the triangle cone. In fact, it is very easy to simply consider 3D graphics, not just an extra zcoordinate.

 

3D Coordinate Axis

Different general procedures, we start from a 3D coordinate axis into the 3D world, mainly for future comparison with 3D box models.

The biggest difference between 3D and previously learned 2D is that we need to consider the coordinate of the Z axis of an object, instead of assuming that the Z axis of any object is 0.0, first, let's see how we plot the coordinate axis of a 3D space. For the sake of image, I rotate it to a certain angle. The source code is as follows:

Void drawcoordinate ()
{
Static glfloat fcoordatas [] = {0.0, 0.0, 0.0, // Origin
1.0, 0.0, 0.0, // X dir
0.0, 1.0, 0.0, // y dir
0.0, 0.0, 1.0}; // Z dir

Static glubyte ubyindices [] = {0, 1, // X axis
0, 2, // y axis
0, 3}; // Z axis

Glenableclientstate (gl_vertex_array );
Glvertexpointer (3, gl_float, 0, fcoordatas );

Gldrawelements (gl_lines, 2, gl_unsigned_byte, ubyindices );
Gldrawelements (gl_lines, 2, gl_unsigned_byte, ubyindices + 2 );
Gldrawelements (gl_lines, 2, gl_unsigned_byte, ubyindices + 4 );
}

// Do all the plotting work here
Void sceneshow (glvoid)
{
Glclear (gl_color_buffer_bit); // clear the color buffer.
Glcolor3f (1.0, 0.0, 0.0 );

Glpushmatrix ();
Glrotatef (30, 1.0,-1.0, 0.0 );
Drawcoordinate ();
Glpopmatrix ();

Glflush ();
}
To save space, only key clips are posted. For the complete source code, see the 2009-10-21/gl3dcoordinate/directory of the source code of my blog. For details about how to obtain the complete source code of the blog, see the article. The main difference from the previous Code is that we started to consider the Z axis coordinates. Here we use (0.0, 0.0, 1.0) to indicate the direction of the Z axis. If we do not rotate, the Z axis is facing us. It is a point projected onto the screen and we cannot see it. So here we make a little rotation and adjust the Z axis to the lower left, this angle is in line with the 3D coordinates we normally draw. The specific implementation project utilizes the vertex array. For more information about the vertex array, see article 5 of this OpenGL series (xo5). The results are as follows:

In this 3D coordinate chart, we can see that the Z axis does not actually extend out of the window, but is truncated. The specific reason will be explained later when the visual object is cropped, but now it can be understood that in OpenGL, the Z axis of the entire 3D space is not infinite, but the size of the window is the same, forming a 3D cube space, as in a 2D plane, the part that exceeds the window is invisible, and the part that exceeds the Z axis space is invisible. In this case, the Z axis is truncated. In addition, we can see through the source code to locate the positive direction of the Z axis (0.0, 0.0, 1.0) and the reality in the graphics, In the OpenGL 3D coordinate system, the positive direction of the Z axis points to the outside of the screen, which is described in xo3. Here, we can use the axis chart to understand it again. After understanding the 3D coordinate system, all 3D images are just fixed coordinates like 2D images.

Wiremap model of a triangle cone (that is, a triangle or pyramid)

Next, we will start to draw a triangle cone in the above 3D coordinate system. Here we will use the box model to draw it. In fact, there is no 3D knowledge. Just by imagining it out of thin air, we can determine the spatial coordinates of such a simple triangle. If so, there is actually no technical difficulty. We only need to consider the Z axis, the following procedures rotate the triangle cone around its own Z axis without stopping. For the sake of image, the three-dimensional coordinate axes described above are also drawn. The format is a bit messy, because the live writer's code snippet automatic format is not easy to use -_-! The layout of the complete source code is more aligned with indentation.

Void drawwirepyramid (glfloat adsize)
{
Static glfloat fpyramiddatas [] = {0.0, 1.0, 0.0, // Vertex on the triangle cone
-1.0, 0.0, 1.0, // left front vertex of the bottom
1.0, 0.0, 1.0, // bottom right bottom Vertex
0.0, 0.0,-1.0}; // bottom vertex of the bottom surface
Glfloat fpyramidsizedatas [sizeof (fpyramiddatas)/sizeof (glfloat)] = {0 };

// Calculate the size
For (INT I = 0; I <12; ++ I)
{
Fpyramidsizedatas [I] = fpyramiddatas [I] * adsize;
}

Static glubyte ubyindices [] = {0, 1, // The following three sides
0, 2,
0, 3,
1, 2, // The following three sides
2, 3,
3, 1 };

Glenableclientstate (gl_vertex_array );
Glvertexpointer (3, gl_float, 0, fpyramidsizedatas );

For (INT I = 0; I <6; ++ I)
{
Gldrawelements (gl_lines, 2, gl_unsigned_byte, ubyindices + I * 2 );
}
}

// Do all the plotting work here
Void sceneshow (glvoid)
{
Glclear (gl_color_buffer_bit );
Glcolor3f (1.0, 0.0, 0.0 );

Glpushmatrix ();
Drawcoordinate ();
Drawwirepyramid (0.5 );
Glpopmatrix ();

Glrotatef (1.0, 0.0, 1.0, 0.0 );

Glflush ();
}

In order to save space, only key clips are posted. For the complete source code, see the 2009-10-21/glwirepyramid directory of the source code of my blog. For the obtaining method, see the description of obtaining the complete source code of the blog at the end of this article.

Shows the display effect.

Triangle cone

In the above section, 3D images are all demonstrated in a box model, mainly to allow you to consider the issue of 3D coordinates purely. Next we will add colors, it's easy to add colors to the preceding example because vertex arrays are already used, we only need to enable the color array.

For example:

Void drawsmoothcolorpyramid (glfloat adsize)
{
Static glfloat fpyramiddatas [] = {0.0, 1.0, 0.0, // Vertex on the triangle cone
-1.0, 0.0, 1.0, // left front vertex of the bottom
1.0, 0.0, 1.0, // bottom right bottom Vertex
0.0, 0.0,-1.0}; // bottom vertex of the bottom surface

Glfloat fpyramidsizedatas [sizeof (fpyramiddatas)/sizeof (glfloat)] = {0 };

// Calculate the size
For (INT I = 0; I <12; ++ I)
{
Fpyramidsizedatas [I] = fpyramiddatas [I] * adsize;
}

Static glfloat fpyramidcolors [] = {0.0, 0.0, 0.0,
1.0, 0.0, 0.0,
0.0, 1.0, 0.0,
0.0, 0.0, 1.0 };

Static glubyte ubyindices [] = {0, 1, 2, // front
0, 1, 3, // left side
0, 2, 3, // right side
1, 2, 3}; // bottom

Glenableclientstate (gl_vertex_array );
Glableclientstate (gl_color_array );

Glvertexpointer (3, gl_float, 0, fpyramidsizedatas );
Glcolorpointer (3, gl_float, 0, fpyramidcolors );

For (INT I = 0; I <4; ++ I)
{
Gldrawelements (gl_triangles, 3, gl_unsigned_byte, ubyindices + I * 3 );
}
}

// Do all the plotting work here
Void sceneshow (glvoid)
{
Glclear (gl_color_buffer_bit );
Glcolor3f (1.0, 0.0, 0.0 );

Glpushmatrix ();
Drawsmoothcolorpyramid (0.5 );
Glpopmatrix ();

Glrotatef (1.0, 0.0, 1.0, 0.0 );

Glflush ();
}

To save space, only key clips are posted. For the complete source code, see the 2009-10-21/glsmoothcolorpyramid/directory of the source code of my blog. For more information, see the description of obtaining the complete source code of the blog.

Note the difference between this example and the previous example. to display the complete three-dimensional model, you cannot draw lines with gl_lines. We need to use gl_triangles to draw the Triangle Plane, A triangle cone is actually just a triangle. Draw four sides, everything is OK. The running effect is as follows:

Summary

In fact, after reading the full text, we will find that there is no difficulty in the 3D world. It is nothing more than an extra Z axis to consider. Think of the whole screen as a front space, the cube in the back space, and then imagine the coordinates of the 3D model, everything is simple. This article mainly talks about the triangle cone, because it is the simplest, you can try the cube to increase the difficulty, experience 3D coordinate system.

 

References

1. OpenGL Reference Manual
, OpenGL Reference Manual

2. OpenGL
OpenGL programming guide
), Dave shreiner, Mason Woo, Jackie neider, Tom Davis
Xu Bo, Mechanical Industry Press

3. nehe OpenGL tutorials, In the http://nehe.gamedev.net/
You can find the tutorials and related code to download. (The PDF version of the tutorials is available) nehe also developed an object-oriented framework. As a demo program, this framework is very suitable. There are also Chinese Versions
Take all the necessary information.

4. OpenGL getting started, by eastcowboy, this is a good tutorial I have found on the Internet. It is quite complete and popular. This is the first address: http://bbs.pfan.cn/post-184355.html

The next article in this series, Win32 OpenGL programming (7) 3D view transformation-the key to true 3D
"

Other articles in this OpenGL Series

1.
Win32 OpenGL programming (1) steps required for OpenGL programming under Win32
"

2. Win32 OpenGL programming (2) searching for missing OpenGL functions
"

3. Win32 OpenGL programming (3) Basic Elements (points, straight lines, polygon)
"

4. Win32 OpenGL programming (4) 2D graphics BASICS (advanced knowledge of color and coordinate systems)
"

5. Win32 OpenGL programming (5) vertices Array
"

 

Complete source code retrieval instructions

Due to space limitations, this article generally only posts the main focus of the Code, the full version of the Code with a project (or makefile) (if any) can be downloaded in Google code using mercurial. The article is stored in different directories on the date published by the blog post. Use mercurial to clone the following database:

Https://blog-sample-code.jtianling.googlecode.com/hg/

For how to use mercurial, see Introduction and brief introduction to the distributed and next-generation version control system mercurial.
"

If you only want to browse all the code, you can go to Google Code to view it. the following URL:

Http://code.google.com/p/jtianling/source/browse? Repo = blog-Sample-code

The author of the original article retains the copyright reprinted. Please indicate the original author and give a link

Write by nine days Yan Ling (jtianling) -- blog.csdn.net/vagrxie

 

 

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.