新书推介:《语义网技术体系》
作者:瞿裕忠,胡伟,程龚
   XML论坛     W3CHINA.ORG讨论区     >>计算机科学论坛<<     SOAChina论坛     Blog     开放翻译计划     新浪微博  
 
  • 首页
  • 登录
  • 注册
  • 软件下载
  • 资料下载
  • 核心成员
  • 帮助
  •   Add to Google

    >> 本版讨论高级C/C++编程、代码重构(Refactoring)、极限编程(XP)、泛型编程等话题
    [返回] 计算机科学论坛计算机技术与应用『 C/C++编程思想 』 → [推荐]NeHe OpenGL教程(中英文版附带VC++源码)Lesson 03-lesson 04 查看新帖用户列表

      发表一个新主题  发表一个新投票  回复主题  (订阅本版) 您是本帖的第 36643 个阅读者浏览上一篇主题  刷新本主题   树形显示贴子 浏览下一篇主题
     * 贴子主题: [推荐]NeHe OpenGL教程(中英文版附带VC++源码)Lesson 03-lesson 04 举报  打印  推荐  IE收藏夹 
       本主题类别:     
     一分之千 帅哥哟,离线,有人找我吗?射手座1984-11-30
      
      
      威望:1
      等级:研一(随老板参加了WWW大会还和Tim Berners-Lee合了影^_^)
      文章:632
      积分:4379
      门派:XML.ORG.CN
      注册:2006/12/31

    姓名:(无权查看)
    城市:(无权查看)
    院校:(无权查看)
    给一分之千发送一个短消息 把一分之千加入好友 查看一分之千的个人资料 搜索一分之千在『 C/C++编程思想 』的所有贴子 引用回复这个贴子 回复这个贴子 查看一分之千的博客楼主
    发贴心情 [推荐]NeHe OpenGL教程(中英文版附带VC++源码)Lesson 03-lesson 04

    第三课第四课源码下载


    第三课 中文


    按此在新窗口浏览图片添加颜色:

    作为第二课的扩展,我将叫你如何使用颜色。你将理解两种着色模式,在左图中,三角形用的是光滑着色,四边形用的是平面着色。

      
       
       
    上一课中我教给您三角形和四边形的绘制方法。这一课我将教您给三角形和四边形添加2种不同类型的着色方法。使用Flat coloring(单调着色)给四边形涂上固定的一种颜色。使用Smooth coloring(平滑着色)将三角形的三个顶点的不同颜色混合在一起,创建漂亮的色彩混合。
    继续在上节课的DrawGLScene例程上修改。下面将整个例程重写了一遍。如果您计划修改上节课的代码,只需用下面的代码覆盖原来的DrawGLScene()就可以了。
      
       

    int DrawGLScene(GLvoid)       // 此过程中包括所有的绘制代码
    {
     glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);  // 清除屏幕及深度缓存
     glLoadIdentity();      // 重置模型观察矩阵
     glTranslatef(-1.5f,0.0f,-6.0f);     // 左移 1.5 单位,并移入屏幕 6.0

     glBegin(GL_TRIANGLES);      // 绘制三角形

       
    如果您还记得上节课的内容,这段代码在屏幕的左半部分绘制三角形。下一行代码是我们第一次使用命令glColor3f(r,g,b)。括号中的三个参数依次是红、绿、蓝三色分量。取值范围可以从0,0f到1.0f。类似于以前所讲的清除屏幕背景命令。
    我们将颜色设为红色(纯红色,无绿色,无蓝色)。接下来的一行代码设置三角形的第一个顶点(三角形的上顶点),并使用当前颜色(红色)来绘制。从现在开始所有的绘制的对象的颜色都是红色,直到我们将红色改变成别的什么颜色。
      
       

      glColor3f(1.0f,0.0f,0.0f);    // 设置当前色为红色
      glVertex3f( 0.0f, 1.0f, 0.0f);    // 上顶点

       
    第一个红色顶点已经设置完毕。接下来我们设置第二个绿色顶点。三角形的左下顶点被设为绿色。  
       

      glColor3f(0.0f,1.0f,0.0f);    // 设置当前色为绿色
      glVertex3f(-1.0f,-1.0f, 0.0f);    // 左下

       
    现在设置第三个也就是最后一个顶点。开始绘制之前将颜色设为蓝色。这将是三角形的右下顶点。glEnd()出现后,三角形将被填充。但是因为每个顶点有不同的颜色,因此看起来颜色从每个角喷出,并刚好在三角形的中心汇合,三种颜色相互混合。这就是平滑着色。  
       

      glColor3f(0.0f,0.0f,1.0f);    // 设置当前色为蓝色
      glVertex3f( 1.0f,-1.0f, 0.0f);    // 右下
     glEnd();       // 三角形绘制结束

     glTranslatef(3.0f,0.0f,0.0f);     // 右移3单位

       
    现在我们绘制一个单调着色-蓝色的正方形。最重要的是要记住,设置当前色之后绘制的所有东东都是当前色的。以后您所创建的每个工程都要使用颜色。即便是在完全采用纹理贴图的时候,glColor3f仍旧可以用来调节纹理的色调。等等....,以后再说吧。
    我们必须要做的事只需将颜色一次性的设为我们想采用的颜色(本例采用蓝色),然后绘制场景。每个顶点都是蓝色的,因为我们没有告诉OpenGL要改变顶点的颜色。最后的结果是.....全蓝色的正方形。再说一遍,顺时针绘制的正方形意味着我们所看见的是四边形的背面。
      
       

     glColor3f(0.5f,0.5f,1.0f);     // 一次性将当前色设置为蓝色
     glBegin(GL_QUADS);      // 绘制正方形
      glVertex3f(-1.0f, 1.0f, 0.0f);    // 左上
      glVertex3f( 1.0f, 1.0f, 0.0f);    // 右上
      glVertex3f( 1.0f,-1.0f, 0.0f);    // 左下
      glVertex3f(-1.0f,-1.0f, 0.0f);    // 右下
     glEnd();       // 正方形绘制结束
     return TRUE;       // 继续运行
    }

       
    最后换掉窗口模式下的标题内容  
       

      // 重建 OpenGL 窗口
      if (!CreateGLWindow("NeHe's颜色实例",640,480,16,fullscreen))

       
    在这一课中,我试着尽量详细的解释如何为您的OpenGL多边形添加单调和平滑的着色效果的步骤。改改代码中的红绿蓝分量值,看看最后y有什么样的结果。如果您有什么意见或建议请给我EMAIL。如果您认为有什么不对或可以改进,请告诉我。我想做最好的OpenGL教程并对您的反馈感兴趣。


       收藏   分享  
    顶(0)
      




    ----------------------------------------------
    越学越无知

    点击查看用户来源及管理<br>发贴IP:*.*.*.* 2007/10/12 12:07:00
     
     一分之千 帅哥哟,离线,有人找我吗?射手座1984-11-30
      
      
      威望:1
      等级:研一(随老板参加了WWW大会还和Tim Berners-Lee合了影^_^)
      文章:632
      积分:4379
      门派:XML.ORG.CN
      注册:2006/12/31

    姓名:(无权查看)
    城市:(无权查看)
    院校:(无权查看)
    给一分之千发送一个短消息 把一分之千加入好友 查看一分之千的个人资料 搜索一分之千在『 C/C++编程思想 』的所有贴子 引用回复这个贴子 回复这个贴子 查看一分之千的博客2
    发贴心情 

    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
    {
     glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Clear The Screen And The Depth Buffer
     glLoadIdentity();     // Reset The Current Modelview Matrix

     glTranslatef(-1.5f,0.0f,-6.0f);    // Left 1.5 Then Into Screen Six Units

     glBegin(GL_TRIANGLES);     // Begin Drawing Triangles

       
    If you remember from the last tutorial, this is the section of code to draw the triangle on the left half of the screen. The next line of code will be the first time we use the command glColor3f(r,g,b). The three parameters in the brackets are red, green and blue intensity values. The values can be from 0.0f to 1.0f. It works the same way as the color values we use to clear the background of the screen.

    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
      glVertex3f( 0.0f, 1.0f, 0.0f);   // Move Up One Unit From Center (Top Point)

       
    We've placed the first vertex on the screen, setting it's color to red. Now before we set the second vertex we'll change the color to green. That way the second vertex which is the left corner of the triangle will be set to green.   
       

      glColor3f(0.0f,1.0f,0.0f);   // Set The Color To Green
      glVertex3f(-1.0f,-1.0f, 0.0f);   // Left And Down One Unit (Bottom Left)

       
    Now we're on the third and final vertex. Just before we draw it, we set the color to blue. This will be the right corner of the triangle. As soon as the glEnd() command is issued, the polygon will be filled in. But because it has a different color at each vertex, rather than one solid color throughout, the color will spread out from each corner, eventually meeting in the middle, where the colors will blend together. This is smooth coloring.   
       

      glColor3f(0.0f,0.0f,1.0f);   // Set The Color To Blue
      glVertex3f( 1.0f,-1.0f, 0.0f);   // Right And Down One Unit (Bottom Right)
     glEnd();      // Done Drawing A Triangle

     glTranslatef(3.0f,0.0f,0.0f);    // From Right Point Move 3 Units Right

       
    Now we will draw a solid blue colored square. It's important to remember that anything drawn after the color has been set will be drawn in that color. Every project you create down the road will use coloring in one way or another. Even in scenes where everything is texture mapped, glColor3f can still be used to tint the color of textures, etc. More on that later.

    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
     glBegin(GL_QUADS);     // Start Drawing Quads
      glVertex3f(-1.0f, 1.0f, 0.0f);   // Left And Up 1 Unit (Top Left)
      glVertex3f( 1.0f, 1.0f, 0.0f);   // Right And Up 1 Unit (Top Right)
      glVertex3f( 1.0f,-1.0f, 0.0f);   // Right And Down One Unit (Bottom Right)
      glVertex3f(-1.0f,-1.0f, 0.0f);   // Left And Down One Unit (Bottom Left)
     glEnd();      // Done Drawing A Quad
     return TRUE;      // Keep Going
    }

       
    Finally change the code to toggle window / fullscreen mode so that the title at the top of the window is proper.   
       

       if (keys[VK_F1])   // Is F1 Being Pressed?
       {
        keys[VK_F1]=FALSE;  // If So Make Key FALSE
        KillGLWindow();   // Kill Our Current Window
        fullscreen=!fullscreen;  // Toggle Fullscreen / Windowed Mode
        // Recreate Our OpenGL Window ( Modified )
        if (!CreateGLWindow("NeHe's Color Tutorial",640,480,16,fullscreen))
        {
         return 0;  // Quit If Window Was Not Created
        }
       }

       
    In this tutorial I have tried to explain in as much detail, how to add flat and smooth coloring to your OpenGL polygons. Play around with the code, try changing the red, green and blue values to different numbers. See what colors you can come up with. If you have comments or questions please email me. If you feel I have incorrectly commented something or that the code could be done better in some sections, please let me know. I want to make the best OpenGL tutorials I can. I'm interested in hearing your feedback.

    Jeff Molofee (NeHe)

    ----------------------------------------------
    越学越无知

    点击查看用户来源及管理<br>发贴IP:*.*.*.* 2007/10/12 12:09:00
     
     一分之千 帅哥哟,离线,有人找我吗?射手座1984-11-30
      
      
      威望:1
      等级:研一(随老板参加了WWW大会还和Tim Berners-Lee合了影^_^)
      文章:632
      积分:4379
      门派:XML.ORG.CN
      注册:2006/12/31

    姓名:(无权查看)
    城市:(无权查看)
    院校:(无权查看)
    给一分之千发送一个短消息 把一分之千加入好友 查看一分之千的个人资料 搜索一分之千在『 C/C++编程思想 』的所有贴子 引用回复这个贴子 回复这个贴子 查看一分之千的博客3
    发贴心情 

    第四课

    按此在新窗口浏览图片旋转:

    在这一课里,我将教会你如何旋转三角形和四边形。左图中的三角形沿Y轴旋转,四边形沿着X轴旋转。

      
       
       
    上一课中我教给您三角形和四边形的着色。这一课我将教您如何将这些彩色对象绕着坐标轴旋转。
    其实只需在上节课的代码上增加几行就可以了。下面我将整个例程重写一遍。方便您知道增加了什么,修改了什么。
    我们增加两个变量来控制这两个对象的旋转。这两个变量加在程序的开始处其他变量的后面( bool fullscreen=TRUE;下面的两行)。它们是浮点类型的变量,使得我们能够非常精确地旋转对象。浮点数包含小数位置,这意味着我们无需使用1、2、3...的角度。你会发现浮点数是OpenGL编程的基础。新变量中叫做 rtri 的用来旋转三角形, rquad 旋转四边形。  
       

    GLfloat  rtri;      // 用于三角形的角度
    GLfloat  rquad;      // 用于四边形的角度

       
    接着我们修改DrawGLScene()的代码。
    下面这段代码与上一课的相同。  
       

    int DrawGLScene(GLvoid)      // 此过程中包括所有的绘制代码
    {
     glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // 清除屏幕及深度缓存
     glLoadIdentity();     // 重置模型观察矩阵
     glTranslatef(-1.5f,0.0f,-6.0f);    // 左移 1.5 单位,并移入屏幕 6.0

       
    下一行代码是新的。glRotatef(Angle,Xvector,Yvector,Zvector)负责让对象绕某个轴旋转。这个命令有很多用处。 Angle 通常是个变量代表对象转过的角度。 Xvector , Yvector 和 Zvector 三个参数则共同决定旋转轴的方向。比如(1,0,0)所描述的矢量经过X坐标轴的1个单位处并且方向向右。(-1,0,0)所描述的矢量经过X坐标轴的1个单位处,但方向向左。
    D. Michael Traub:提供了对 Xvector , Yvector 和 Zvector 的上述解释。
    为了更好的理解X, Y 和 Z的旋转,我举些例子...
    X轴-您正在使用一台台锯。锯片中心的轴从左至右摆放(就像OpenGL中的X轴)。尖利的锯齿绕着X轴狂转,看起来要么向上转,要么向下转。取决于锯片开始转时的方向。这与我们在OpenGL中绕着X轴旋转什么的情形是一样的。(译者注:这会儿您要把脸蛋凑向显示器的话,保准被锯开了花 ^-^。)

    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轴旋转三角形

       
    下面的代码没有变化。在屏幕的左面画了一个彩色渐变三角形,并绕着Y轴从左向右旋转。  
       

     glBegin(GL_TRIANGLES);     // 绘制三角形
      glColor3f(1.0f,0.0f,0.0f);   // 设置当前色为红色
      glVertex3f( 0.0f, 1.0f, 0.0f);   // 上顶点
      glColor3f(0.0f,1.0f,0.0f);   // 设置当前色为绿色
      glVertex3f(-1.0f,-1.0f, 0.0f);   // 左下
      glColor3f(0.0f,0.0f,1.0f);   // 设置当前色为蓝色
      glVertex3f( 1.0f,-1.0f, 0.0f);   // 右下
     glEnd();      // 三角形绘制结束

       
    您会注意下面的代码中我们增加了另一个glLoadIdentity()调用。目的是为了重置模型观察矩阵。如果我们没有重置,直接调用glTranslate的话,会出现意料之外的结果。因为坐标轴已经旋转了,很可能没有朝着您所希望的方向。所以我们本来想要左右移动对象的,就可能变成上下移动了,取决于您将坐标轴旋转了多少角度。试试将glLoadIdentity() 注释掉之后,会出现什么结果。
    重置模型观察矩阵之后,X,Y,Z轴都以复位,我们调用glTranslate。您会注意到这次我们只向右一了1.5单位,而不是上节课的3.0单位。因为我们重置场景的时候,焦点又回到了场景的中心(0.0处)。这样就只需向右移1.5单位就够了。
    当我们移到新位置后,绕X轴旋转四边形。正方形将上下转动。
      
       

     glLoadIdentity();     // 重置模型观察矩阵
     glTranslatef(1.5f,0.0f,-6.0f);    // 右移1.5单位,并移入屏幕 6.0
     glRotatef(rquad,1.0f,0.0f,0.0f);   //  绕X轴旋转四边形

       
    下一段代码保持不变。在屏幕的右侧画一个蓝色的正方形  
       

     glColor3f(0.5f,0.5f,1.0f);    // 一次性将当前色设置为蓝色
     glBegin(GL_QUADS);     // 绘制正方形
      glVertex3f(-1.0f, 1.0f, 0.0f);   // 左上
      glVertex3f( 1.0f, 1.0f, 0.0f);   // 右上
      glVertex3f( 1.0f,-1.0f, 0.0f);   // 左下
      glVertex3f(-1.0f,-1.0f, 0.0f);   // 右下
     glEnd();      // 正方形绘制结束

       
    下两行是新增的。倘若把 rtri 和 rquad 想象为容器,那么在程序的开始我们创建了容器( GLfloat rtri , 和 GLfloat rquad )。当容器创建之后,里面是空的。下面的第一行代码是向容器中添加0.2。因此每次当我们运行完前面的代码后,都会在这里使 rtri 容器中的值增长0.2。后面一行将 rquad 容器中的值减少0.15。同样每次当我们运行完前面的代码后,都会在这里使 rquad 容器中的值下跌0.15。下跌最终会导致对象旋转的方向和增长的方向相反。
    尝试改变下面代码中的+和-,来体会对象旋转的方向是如何改变的。并试着将0.2改成1.0。这个数字越大,物体就转的越快,这个数字越小,物体转的就越慢。
      
       

     rtri+=0.2f;      // 增加三角形的旋转变量
     rquad-=0.15f;      // 减少四边形的旋转变量
     return TRUE;      // 继续运行
    }

       
    最后换掉窗口模式下的标题内容  
       

      // 重建 OpenGL 窗口
      if (!CreateGLWindow("NeHe's 旋转实例",640,480,16,fullscreen))

       
    在这一课中,我试着尽量详细的解释如何让对象绕某个轴转动。改改代码,试着让对象绕着Z轴、X+Y轴或者所有三个轴来转动:)。如果您有什么意见或建议请给我EMAIL。如果您认为有什么不对或可以改进,请告诉我。我想做最好的OpenGL教程并对您的反馈感兴趣。

    ----------------------------------------------
    越学越无知

    点击查看用户来源及管理<br>发贴IP:*.*.*.* 2007/10/12 12:11:00
     
     一分之千 帅哥哟,离线,有人找我吗?射手座1984-11-30
      
      
      威望:1
      等级:研一(随老板参加了WWW大会还和Tim Berners-Lee合了影^_^)
      文章:632
      积分:4379
      门派:XML.ORG.CN
      注册:2006/12/31

    姓名:(无权查看)
    城市:(无权查看)
    院校:(无权查看)
    给一分之千发送一个短消息 把一分之千加入好友 查看一分之千的个人资料 搜索一分之千在『 C/C++编程思想 』的所有贴子 引用回复这个贴子 回复这个贴子 查看一分之千的博客4
    发贴心情 
    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
    #include <gl\gl.h>     // Header File For The OpenGL32 Library
    #include <gl\glu.h>     // Header File For The GLu32 Library
    #include <gl\glaux.h>     // Header File For The GLaux Library

    HDC  hDC=NULL;     // Private GDI Device Context
    HGLRC  hRC=NULL;     // Permanent Rendering Context
    HWND  hWnd=NULL;     // Holds Our Window Handle
    HINSTANCE hInstance;     // Holds The Instance Of The Application

    bool  keys[256];     // Array Used For The Keyboard Routine
    bool  active=TRUE;     // Window Active Flag
    bool  fullscreen=TRUE;    // Fullscreen Flag Set To TRUE By Default

    GLfloat  rtri;      // Angle For The Triangle ( NEW )
    GLfloat  rquad;      // Angle For The Quad     ( NEW )

       
    Now we need to modify the DrawGLScene() code. I will rewrite the entire procedure. This should make it easier for you to see what changes I have made to the original code. I'll explain why lines have been modified, and what exactly it is that the new lines do. The next section of code is exactly the same as in the last tutorial.   
       

    int DrawGLScene(GLvoid)      // Here's Where We Do All The Drawing
    {
     glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Clear The Screen And The Depth Buffer
     glLoadIdentity();     // Reset The View
     glTranslatef(-1.5f,0.0f,-6.0f);    // Move Into The Screen And Left

       
    The next line of code is new. glRotatef(Angle,Xvector,Yvector,Zvector) is responsible for rotating the object around an axis. You will get alot of use out of this command. Angle is some number (usually stored in a variable) that represents how much you would like to spin the object. Xvector, Yvector and Zvector parameters together represent the vector about which the rotation will occur. If you use values (1,0,0), you are describing a vector which travels in a direction of 1 unit along the x axis towards the right. Values (-1,0,0) describes a vector that travels in a direction of 1 unit along the x axis, but this time towards the left.

    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 )

       
    The next section of code has not changed. It draws a colorful smooth blended triangle. The triangle will be drawn on the left side of the screen, and will be rotated on it's Y axis causing it to spin left to right.   
       

     glBegin(GL_TRIANGLES);     // Start Drawing A Triangle
      glColor3f(1.0f,0.0f,0.0f);   // Set Top Point Of Triangle To Red
      glVertex3f( 0.0f, 1.0f, 0.0f);   // First Point Of The Triangle
      glColor3f(0.0f,1.0f,0.0f);   // Set Left Point Of Triangle To Green
      glVertex3f(-1.0f,-1.0f, 0.0f);   // Second Point Of The Triangle
      glColor3f(0.0f,0.0f,1.0f);   // Set Right Point Of Triangle To Blue
      glVertex3f( 1.0f,-1.0f, 0.0f);   // Third Point Of The Triangle
     glEnd();      // Done Drawing The Triangle

       
    You'll notice in the code below, that we've added another glLoadIdentity(). We do this to reset the view. If we didn't reset the view. If we translated after the object had been rotated, you would get very unexpected results. Because the axis has been rotated, it may not be pointing in the direction you think. So if we translate left on the X axis, we may end up moving up or down instead, depending on how much we've rotated on each axis. Try taking the glLoadIdentity() line out to see what I mean.

    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
     glTranslatef(1.5f,0.0f,-6.0f);    // Move Right 1.5 Units And Into The Screen 6.0
     glRotatef(rquad,1.0f,0.0f,0.0f);   // Rotate The Quad On The X axis ( NEW )

       
    This section of code remains the same. It draws a blue square made from one quad. It will draw the square on the right side of the screen in it's rotated position.   
       

     glColor3f(0.5f,0.5f,1.0f);    // Set The Color To A Nice Blue Shade
     glBegin(GL_QUADS);     // Start Drawing A Quad
      glVertex3f(-1.0f, 1.0f, 0.0f);   // Top Left Of The Quad
      glVertex3f( 1.0f, 1.0f, 0.0f);   // Top Right Of The Quad
      glVertex3f( 1.0f,-1.0f, 0.0f);   // Bottom Right Of The Quad
      glVertex3f(-1.0f,-1.0f, 0.0f);   // Bottom Left Of The Quad
     glEnd();      // Done Drawing The Quad

       
    The next two lines are new. Think of rtri, and rquad as containers. At the top of our program we made the containers (GLfloat rtri, and GLfloat rquad). When we built the containers they had nothing in them. The first line below ADDS 0.2 to that container. So each time we check the value in the rtri container after this section of code, it will have gone up by 0.2. The rquad container decreases by 0.15. So every time we check the rquad container, it will have gone down by 0.15. Going down will cause the object to spin the opposite direction it would spin if you were going up.

    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 )
     rquad-=0.15f;      // Decrease The Rotation Variable For The Quad     ( NEW )
     return TRUE;      // Keep Going
    }

       
    Finally change the code to toggle window / fullscreen mode so that the title at the top of the window is proper.   
       

       if (keys[VK_F1])   // Is F1 Being Pressed?
       {
        keys[VK_F1]=FALSE;  // If So Make Key FALSE
        KillGLWindow();   // Kill Our Current Window
        fullscreen=!fullscreen;  // Toggle Fullscreen / Windowed Mode
        // Recreate Our OpenGL Window ( Modified )
        if (!CreateGLWindow("NeHe's Rotation Tutorial",640,480,16,fullscreen))
        {
         return 0;  // Quit If Window Was Not Created
        }
       }

       
    In this tutorial I have tried to explain in as much detail as possible, how to rotate objects around an axis. Play around with the code, try spinning the objects, on the Z axis, the X & Y, or all three :) If you have comments or questions please email me. If you feel I have incorrectly commented something or that the code could be done better in some sections, please let me know. I want to make the best OpenGL tutorials I can. I'm interested in hearing your feedback.

    Jeff Molofee (NeHe)

    ----------------------------------------------
    越学越无知

    点击查看用户来源及管理<br>发贴IP:*.*.*.* 2007/10/12 12:11:00
     
     卷积内核 帅哥哟,离线,有人找我吗?
      
      
      威望:8
      头衔:总统
      等级:博士二年级(版主)
      文章:3942
      积分:27590
      门派:XML.ORG.CN
      注册:2004/7/21

    姓名:(无权查看)
    城市:(无权查看)
    院校:(无权查看)
    给卷积内核发送一个短消息 把卷积内核加入好友 查看卷积内核的个人资料 搜索卷积内核在『 C/C++编程思想 』的所有贴子 访问卷积内核的主页 引用回复这个贴子 回复这个贴子 查看卷积内核的博客5
    发贴心情 
    step by step method,wonderful!!!

    ----------------------------------------------
    事业是国家的,荣誉是单位的,成绩是领导的,工资是老婆的,财产是孩子的,错误是自己的。

    点击查看用户来源及管理<br>发贴IP:*.*.*.* 2007/10/12 15:22:00
     
     hwfchina 帅哥哟,离线,有人找我吗?
      
      
      等级:大一新生
      文章:4
      积分:68
      门派:XML.ORG.CN
      注册:2008/3/17

    姓名:(无权查看)
    城市:(无权查看)
    院校:(无权查看)
    给hwfchina发送一个短消息 把hwfchina加入好友 查看hwfchina的个人资料 搜索hwfchina在『 C/C++编程思想 』的所有贴子 引用回复这个贴子 回复这个贴子 查看hwfchina的博客6
    发贴心情 
    顶~~~~~~~~~~~~~~~`
    点击查看用户来源及管理<br>发贴IP:*.*.*.* 2008/3/18 22:34:00
     
     gaoxingt 帅哥哟,离线,有人找我吗?
      
      
      等级:大一新生
      文章:1
      积分:55
      门派:XML.ORG.CN
      注册:2008/10/24

    姓名:(无权查看)
    城市:(无权查看)
    院校:(无权查看)
    给gaoxingt发送一个短消息 把gaoxingt加入好友 查看gaoxingt的个人资料 搜索gaoxingt在『 C/C++编程思想 』的所有贴子 引用回复这个贴子 回复这个贴子 查看gaoxingt的博客7
    发贴心情 
    Thanks for your tutorial!
    点击查看用户来源及管理<br>发贴IP:*.*.*.* 2008/10/24 17:18:00
     
     sh_chenjian 帅哥哟,离线,有人找我吗?
      
      
      等级:大一新生
      文章:1
      积分:54
      门派:XML.ORG.CN
      注册:2010/2/25

    姓名:(无权查看)
    城市:(无权查看)
    院校:(无权查看)
    给sh_chenjian发送一个短消息 把sh_chenjian加入好友 查看sh_chenjian的个人资料 搜索sh_chenjian在『 C/C++编程思想 』的所有贴子 引用回复这个贴子 回复这个贴子 查看sh_chenjian的博客8
    发贴心情 
    very good!
    That's really help me to understand openGL
    点击查看用户来源及管理<br>发贴IP:*.*.*.* 2010/3/1 9:57:00
     
     wanchao 帅哥哟,离线,有人找我吗?
      
      
      等级:大一新生
      文章:1
      积分:55
      门派:XML.ORG.CN
      注册:2010/4/7

    姓名:(无权查看)
    城市:(无权查看)
    院校:(无权查看)
    给wanchao发送一个短消息 把wanchao加入好友 查看wanchao的个人资料 搜索wanchao在『 C/C++编程思想 』的所有贴子 引用回复这个贴子 回复这个贴子 查看wanchao的博客9
    发贴心情 
    ??第四课在哪啊?
    点击查看用户来源及管理<br>发贴IP:*.*.*.* 2010/4/18 18:26:00
     
     lovelylcj 帅哥哟,离线,有人找我吗?
      
      
      等级:大一新生
      文章:2
      积分:59
      门派:XML.ORG.CN
      注册:2012/2/16

    姓名:(无权查看)
    城市:(无权查看)
    院校:(无权查看)
    给lovelylcj发送一个短消息 把lovelylcj加入好友 查看lovelylcj的个人资料 搜索lovelylcj在『 C/C++编程思想 』的所有贴子 引用回复这个贴子 回复这个贴子 查看lovelylcj的博客10
    发贴心情 
    DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD
    点击查看用户来源及管理<br>发贴IP:*.*.*.* 2012/2/17 10:14:00
     
     GoogleAdSense
      
      
      等级:大一新生
      文章:1
      积分:50
      门派:无门无派
      院校:未填写
      注册:2007-01-01
    给Google AdSense发送一个短消息 把Google AdSense加入好友 查看Google AdSense的个人资料 搜索Google AdSense在『 C/C++编程思想 』的所有贴子 访问Google AdSense的主页 引用回复这个贴子 回复这个贴子 查看Google AdSense的博客广告
    2024/6/22 20:13:27

    本主题贴数10,分页: [1]

    管理选项修改tag | 锁定 | 解锁 | 提升 | 删除 | 移动 | 固顶 | 总固顶 | 奖励 | 惩罚 | 发布公告
    W3C Contributing Supporter! W 3 C h i n a ( since 2003 ) 旗 下 站 点
    苏ICP备05006046号《全国人大常委会关于维护互联网安全的决定》《计算机信息网络国际联网安全保护管理办法》
    164.063ms