以文本方式查看主题 - 中文XML论坛 - 专业的XML技术讨论区 (http://bbs.xml.org.cn/index.asp) -- 『 C/C++编程思想 』 (http://bbs.xml.org.cn/list.asp?boardid=61) ---- [推荐]NeHe OpenGL教程(中英文版附带VC++源码)Lesson 03-lesson 04 (http://bbs.xml.org.cn/dispbbs.asp?boardid=61&rootid=&id=53682) |
-- 作者:一分之千 -- 发布时间:10/12/2007 12:09:00 PM -- Lesson 03 In the last tutorial I taught you how to display Triangles and Quads on the screen. In this tutorial I will teach you how to add 2 different types of coloring to the triangle and quad. Flat coloring will make the quad one solid color. Smooth coloring will blend the 3 colors specified at each point (vertex) of the triangle together, creating a nice blend of colors. Using the code from the last tutorial, we will be adding to the DrawGLScene procedure. I will rewrite the entire procedure below, so if you plan to modify the last lesson, you can replace the DrawGLScene procedure with the code below, or just add code to the DrawGLScene procedure that is not already in the last tutorial. int DrawGLScene(GLvoid) // Here's Where We Do All The Drawing glTranslatef(-1.5f,0.0f,-6.0f); // Left 1.5 Then Into Screen Six Units glBegin(GL_TRIANGLES); // Begin Drawing Triangles We are setting the color to red (full red intensity, no green, no blue). The line of code right after that is the first vertex (the top of the triangle), and will be drawn using the current color which is red. Anything we draw from now on will be red until we change the color to something other than red. glColor3f(1.0f,0.0f,0.0f); // Set The Color To Red glColor3f(0.0f,1.0f,0.0f); // Set The Color To Green glColor3f(0.0f,0.0f,1.0f); // Set The Color To Blue glTranslatef(3.0f,0.0f,0.0f); // From Right Point Move 3 Units Right So to draw our square all one color, all we have to do is set the color once to a color we like (blue in this example), then draw the square. The color blue will be used for each vertex because we're not telling OpenGL to change the color at each vertex. The final result... a solid blue square. Again, the square (quad) is drawn in a clockwise order meaning we start off looking at the back of the quad. glColor3f(0.5f,0.5f,1.0f); // Set The Color To Blue One Time Only if (keys[VK_F1]) // Is F1 Being Pressed? Jeff Molofee (NeHe) |
-- 作者:一分之千 -- 发布时间:10/12/2007 12:11:00 PM -- 第四课 在这一课里,我将教会你如何旋转三角形和四边形。左图中的三角形沿Y轴旋转,四边形沿着X轴旋转。 GLfloat rtri; // 用于三角形的角度 int DrawGLScene(GLvoid) // 此过程中包括所有的绘制代码 Y轴-假设您正处于一个巨大的龙卷风中心,龙卷风的中心从地面指向天空(就像OpenGL中的Y轴)。垃圾和碎片围着Y轴从左向右或是从右向左狂转不止。这与我们在OpenGL中绕着Y轴旋转什么的情形是一样的。 Z轴-您从正前方看着一台风扇。风扇的中心正好朝着您(就像OpenGL中的Z轴)。风扇的叶片绕着Z轴顺时针或逆时针狂转。这与我们在OpenGL中绕着Z轴旋转什么的情形是一样的。 下面的一行代码中,如果rtri等于7,我们将三角形绕着Y轴从左向右旋转7 。您也可以改变参数的值,让三角形绕着X和Y轴同时旋转。 glRotatef(rtri,0.0f,1.0f,0.0f); // 绕Y轴旋转三角形 glBegin(GL_TRIANGLES); // 绘制三角形 glLoadIdentity(); // 重置模型观察矩阵 glColor3f(0.5f,0.5f,1.0f); // 一次性将当前色设置为蓝色 rtri+=0.2f; // 增加三角形的旋转变量 // 重建 OpenGL 窗口 |
-- 作者:一分之千 -- 发布时间:10/12/2007 12:11:00 PM -- Lesson 04 In the last tutorial I taught you how to add color to triangles and quads. In this tutorial I will teach you how to rotate these colored objects around an axis. Using the code from the last tutorial, we will be adding to a few places in the code. I will rewrite the entire section of code below so it's easy for you to figure out what's been added, and what needs to be replaced. We'll start off by adding the two variables to keep track of the rotation for each object. We do this at the top of our program, underneath the other variables. You will notice two new lines after 'bool fullscreen=TRUE;'. These lines set up two floating point variables that we can use to spin the objects with very fine accuracy. Floating point allows decimal numbers. Meaning we're not stuck using 1, 2, 3 for the angle, we can use 1.1, 1.7, 2.3, or even 1.015 for fine accuracy. You will find that floating point numbers are essential to OpenGL programming. The new variables are called rtri which will rotate the triangle and rquad which will rotate the quad. #include <windows.h> // Header File For Windows HDC hDC=NULL; // Private GDI Device Context bool keys[256]; // Array Used For The Keyboard Routine GLfloat rtri; // Angle For The Triangle ( NEW ) int DrawGLScene(GLvoid) // Here's Where We Do All The Drawing D. Michael Traub: has supplied the above explanation of the Xvector, Yvector and Zvector parameters. To better understand X, Y and Z rotation I'll explain using examples... X Axis - You're working on a table saw. The bar going through the center of the blade runs left to right (just like the x axis in OpenGL). The sharp teeth spin around the x axis (bar running through the center of the blade), and appear to be cutting towards or away from you depending on which way the blade is being spun. When we spin something on the x axis in OpenGL it will spin the same way. Y Axis - Imagine that you are standing in the middle of a field. There is a huge tornado coming straight at you. The center of a tornado runs from the sky to the ground (up and down, just like the y axis in OpenGL). The dirt and debris in the tornado spins around the y axis (center of the tornado) from left to right or right to left. When you spin something on the y axis in OpenGL it will spin the same way. Z Axis - You are looking at the front of a fan. The center of the fan points towards you and away from you (just like the z axis in OpenGL). The blades of the fan spin around the z axis (center of the fan) in a clockwise or counterclockwise direction. When You spin something on the z axis in OpenGL it will spin the same way. So in the following line of code, if rtri was equal to 7, we would spin 7 on the Y axis (left to right). You can try experimenting with the code. Change the 0.0f's to 1.0f's, and the 1.0f to a 0.0f to spin the triangle on the X and Y axes at the same time. It's important to note that rotations are done in degrees. If rtri had a value of 10, we would be rotating 10 degrees on the y-axis. glRotatef(rtri,0.0f,1.0f,0.0f); // Rotate The Triangle On The Y axis ( NEW ) glBegin(GL_TRIANGLES); // Start Drawing A Triangle Once the scene has been reset, so X is running left to right, Y up and down, and Z in and out, we translate. You'll notice we're only moving 1.5 to the right instead of 3.0 like we did in the last lesson. When we reset the screen, our focus moves to the center of the screen. meaning we're no longer 1.5 units to the left, we're back at 0.0. So to get to 1.5 on the right side of zero we dont have to move 1.5 from left to center then 1.5 to the right (total of 3.0) we only have to move from center to the right which is just 1.5 units. After we have moved to our new location on the right side of the screen, we rotate the quad, on the X axis. This will cause the square to spin up and down. glLoadIdentity(); // Reset The Current Modelview Matrix glColor3f(0.5f,0.5f,1.0f); // Set The Color To A Nice Blue Shade Try chaning the + to a - in the line below see how the object spins the other direction. Try changing the values from 0.2 to 1.0. The higher the number, the faster the object will spin. The lower the number, the slower it will spin. rtri+=0.2f; // Increase The Rotation Variable For The Triangle ( NEW ) if (keys[VK_F1]) // Is F1 Being Pressed? Jeff Molofee (NeHe) |
-- 作者:卷积内核 -- 发布时间:10/12/2007 3:22:00 PM -- step by step method,wonderful!!! |
-- 作者:hwfchina -- 发布时间:3/18/2008 10:34:00 PM -- 顶~~~~~~~~~~~~~~~` |
-- 作者:gaoxingt -- 发布时间:10/24/2008 5:18:00 PM -- Thanks for your tutorial! |
-- 作者:sh_chenjian -- 发布时间:3/1/2010 9:57:00 AM -- very good! That's really help me to understand openGL |
-- 作者:wanchao -- 发布时间:4/18/2010 6:26:00 PM -- ??第四课在哪啊? |
-- 作者:lovelylcj -- 发布时间:2/17/2012 10:14:00 AM -- DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD |
W 3 C h i n a ( since 2003 ) 旗 下 站 点 苏ICP备05006046号《全国人大常委会关于维护互联网安全的决定》《计算机信息网络国际联网安全保护管理办法》 |
125.000ms |