Course directory:
Course 1 blog address: http://blog.csdn.net/shuxieweilai/article/details/6587501 // give the approximate idea of designing the cube
Lesson 2 blog address: http://blog.csdn.net/shuxieweilai/article/details/6588164 // construct a cube model, but do not do any processing
Course 3 blog address: http://blog.csdn.net/shuxieweilai/article/details/6589543 // can achieve the Magic Cube zoom in and out and the overall Rotation
Lesson 4 blog address: http://blog.csdn.net/shuxieweilai/article/details/6592830 // you can achieve a twist cube key
Code and demos for this course can be downloaded from http://download.csdn.net/source/3423269
The final code is displayed in this lesson:
To manually change the viewing angle of the cube, you can go to the glrotatef (45.0f, 10f, 0.0f) commented out in the ccube class ccube: Draw function; remove the comment, you only need to change the value of the 45-degree corner to see other aspects of the cube.
Task of this lesson:
Initialize a cube and construct a cube model.
Now, let's get started. In the previous lesson, you should have learned about the basic framework of cube production. Now, the first step is to build a cube model.
If you have not studied OpenGL, read the first 12 courses of the nehe tutorial, including the configuration of the compiling environment, I directly used the original code of the nehe Lesson 12th as the magic main program: display list, but I did not use the display list. If I want to change my code to a cube that can be mapped, you should use the display list. Otherwise, your program will become a snail bait. You can refer to the Code provided by me to download this course and delete the nehe Code, specifically the function for deleting the display list and the code in drawglsence, and a small part of other code, I name the main program mian. CPP. The Code copied in the main program is not described, so it will not be complicated or lengthy. Learn the nehe tutorial consciously.
First, you need to construct a Global Object of cube by main. cpp. The cmagiccube magiccube is located in lresult callback wndproc (......) In the int initgl (glvoid) function, add the following code magiccube. initmagiccube (); to initialize the cube. Now let's put main. cpp aside. Now we need to add the following code to face. h. I will explain it one by one.
Static cpoint points [8] = {-0.5,-0.5,-0.5}, // 0 l d f {-0.5,-0.5, 0.5 }, // 1 l d B {-0.5, 0.5,-0.5}, // 2 L u f {-0.5, 0.5, 0.5}, // 3 L u B {0.5, -0.5,-0.5}, // 4 r d f {0.5,-0.5, 0.5}, // 5 r d B {0.5, 0.5,-0.5 }, // 6 r u f {0.5, 0.5, 0.5} // 7 r u B}; static int points_index [6] [4] = {// OpenGL modeling, d3d should have different {,}, // left {,}, // right {,}, // down }, // up {5, 7, 3, 1}, // front {0, 2, 6, 4} // back}; static ccolor init_colors [6] ={{ 1.0f, 1.0f, 1.0f }, {1.0f, 0.0f, 0.0f}, {0.0f, 1.0f, 0.0f}, {0.0f, 0.0f, 1.0f}, {0.0f, 1.0f, 1.0f}, {1.0f, 1.0f, 0.0f }};
First, look at init_colors [6], which is the simplest of which represents the six colors that initialize the cube.
Points [8] represents the coordinates of the eight vertices of a cube (when the center of the cube is (0, 0 ));
Index [6] [4] First, use the numbers 0 to 7 in the 8 vertices of the cube. l Represents lefe U represents up others.
Then, we will initialize a cube to obtain various information about the cube. We just talked about magiccube. initmagiccube,
Now we will jump to the magiccube. cpp file and write the initialization function. The Code is as follows:
Void cmagiccube: initmagiccube () {for (INT I =-1; I <2; I ++) for (Int J =-1; j <2; j ++) for (int z =-1; Z <2; Z ++) {m_cube [I + 1] [J + 1] [Z + 1]. setpoint (float) I, (float) J, (float) z ); // set the center position of each cube m_cube [I + 1] [J + 1] [Z + 1]. initcube (); // initialize each cube }}
The above Code uses a triple loop to initialize each corresponding cube, and the subscript of each loop starts from-1 to 1, so that the cube can be located in the center of the screen, so that the center of the cube center and (0, 0, 0) overlap, now we jump to ccube. CPP to see how Initialization is made. The above two functions are used: setpoint and initcube. The Code is as follows:
void CCube :: SetPoint(float a, float b, float c){m_pt.m_x = a;m_pt.m_y = b;m_pt.m_z = c;}
Void ccube: initcube () {for (INT I = 0; I <6; I ++) {for (Int J = 0; j <4; j ++) {m_face [I]. getpeak (j) = points [points_index [I] [J]; // sets the coordinates of each vertex of the cube} m_face [I]. getcolorindex () I; // set the initial color }}
The hard-to-understand estimation above is m_face [I]. getpeak (j) = points [points_index [I] [J]. If you do not understand the meaning of this sentence, you can understand the meaning of several static variables mentioned above. I hope I can think more through myself. If I still don't understand it, I can leave a message in my blog. I will elaborate on it for you.
Here, we call the getpeak and getcolorindex in cface. It doesn't mean much. We can see that the name has completely understood the intention. So far, our initialization work is finished. Now we can draw a cube.
Return to main. cpp and add magiccube. Draw () to the drawglscene (glvoid) function. Represents the cube of painting.
Now, you can jump into the cmagiccube class to implement painting. The Code is as follows:
Void cmagiccube: Draw () {glclear (gl_color_buffer_bit | gl_depth_buffer_bit); // clear depth cache and color cache for (INT I =-1; I <2; I ++) for (Int J =-1; j <2; j ++) for (int z =-1; Z <2; Z ++) {m_cube [I + 1] [J + 1] [Z + 1]. draw ();}}
OK the above Code glclear clears the deep cache and color cache. The following code is easy to understand, so continue to jump into the draw code in the ccube class.
Void ccube: Draw () {glloadidentity (); // reset the projection matrix gltranslatef (0.0f, 0.0f,-10.0f); // J moves the coordinate system to a place with a depth of 10. Allows you to view the entire cube! // Glrotatef (45.0f, 1.0f, 1.0f, 0.0f); // you can change the coordinate system angle to see gltranslatef (m_pt.m_x * 1.1, m_pt.m_y * 1.1, m_pt.m_z * 1.1); For (INT I = 0; I <6; I ++) {m_face [I]. draw (I );}}
I will not further analyze the meanings of those OpenGL functions. The key sentence is gltranslatef (m_pt.m_x * 1.1, m_pt.m_y * 1.1, m_pt.m_z * 1.1, if it is not multiplied by 1.1 times, the meaning of the sentence is
If the origin of the coordinate is moved to the center of the currently drawn cube, Why multiply it by 1.1 times? If this is not done, there will be no interval between each cube, so that the same images are mixed together. The two images below are one that does not multiply by 1.1 and one that does it.
After multiplication:
Okay, the code below is also very simple. Finally, let's continue to look at the draw function in cface. The Code is as follows:
Void cface: Draw (INT index) {glcolor3f (init_colors [Index]. m_r, init_colors [Index]. m_g, init_colors [Index]. m_ B); // set the current color glbegin (gl_quads); // draw a rectangle for (INT I = 0; I <4; I ++) {glvertex3f (m_peak [I]. m_x, m_peak [I]. m_y, m_peak [I]. m_z); // cyclically locate the coordinates of the corresponding surface vertex} glend ();}
It seems that I have written all the comments and I have nothing to talk about. If you have any questions, please leave a message on your blog. I will try my best to explain it to you. But there are some questions about Baidu. I hope you can ask questions after Baidu and Google.
This article is about to end. If you want to learn more, you can read another blog section 3 of Rubik's Cube version 1.0 (which will be updated tomorrow)
OK. Let's take a look at some of the C ++ knowledge used here.
We do not recommend that you define global variables in the header file. The reason is as follows:
# Include means to add the included. h file to this. c file and compile it together to form a compilation unit. For example,. C and B. c All include COM. h, and COM. h defines a part of int COM. a. OBJ contains a com variable, while B. OBJ also contains a com variable. So when we connect a. OBJ and B. OBJ into an executable file and find that com is defined in both intermediate files, the compiler will certainly report a duplicate definition error.
However, static variables can be defined, and initialization must be followed immediately after the definition. For example, in the above points [8] array, if you add a points [0] = **; the compiler reports an error.
In the cface class, there are two getpeak and getcolorindex obtaining functions. Why are they returning references?
You can find the two sentences m_face [I] In the ccube class. getpeak (j) = points [points_index [I] [J]; m_face [I]. getcolorindex () = I; it is obvious that the returned values need to be used as the left value. If a variable is returned, a temporary variable is returned. The temporary variable can only be used as the right value, therefore, a reference is returned.