[NEHE Couse] 04.Rotating objects

     没有什么新内容,注意glRotatef函数的使用就可以了,我在程序中为了效果,引入了glutIdleFunc函数的使用,具体用法大家可以google下。此外为了让三角面片旋转时不至于旋转出视线,我把glOrtho的参数也修改了。点击鼠标左键物体开始旋转,点击右键旋转停止。

程序如下:

  1/*
  2Introduction:Written by krisdy,finished at 2009.1.9
  3Apply to:NEHE Couse 04:Rotating objects
  4*/

  5#include <gl/glut.h>
  6#include <stdlib.h>
  7
  8#define WinWidth 500        //the width of the window
  9#define WinHeight 500        //the height of the window
 10
 11static GLint t;
 12static GLfloat spin_x,spin_y;    //control the rotate range
 13
 14void init(void)
 15{
 16    glShadeModel(GL_SMOOTH);
 17    glClearColor(0.0f,0.0f,0.0f,0.0f);
 18    glClearDepth(1.0f);
 19    glEnable(GL_DEPTH_TEST);
 20    glHint(GL_PERSPECTIVE_CORRECTION_HINT,GL_NICEST);
 21}

 22void display(void)
 23{
 24    glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
 25    glMatrixMode(GL_MODELVIEW);
 26    glLoadIdentity();
 27    glPushMatrix();
 28    glTranslatef(1.0f,1.0f,0.0f);
 29    glRotatef(spin_x,1.0f,0.0f,0.0f);
 30    //draw a triangle with smooth color
 31    glBegin(GL_TRIANGLES);
 32        glColor3f(1.0f,0.0f,0.0f);
 33        glVertex3f(0.0f,0.0f,0.0f);
 34        glColor3f(0.0f,1.0f,0.0f);
 35        glVertex3f(3.0f,0.0f,0.0f);
 36        glColor3f(0.0f,0.0f,1.0f);
 37        glVertex3f(1.5f,4.0f,0.0f);
 38    glEnd();
 39    glPopMatrix();
 40    glPushMatrix();
 41    glTranslatef(6.0f,1.0f,0.0f);
 42    glRotatef(spin_y,0.0f,1.0f,0.0f);
 43    //draw a rectangle,without the help of glBegin&&glEnd
 44    glColor3f(1.5f,0.5f,0.5f);
 45    glRectf(0.0f,0.0f,3.0f,3.0f);
 46    glPopMatrix();
 47    glutSwapBuffers();    //apply to the moving objects,pay attention to it
 48}

 49void reshape(int w,int h)
 50{
 51    GLfloat range = 10.0f;
 52    if(h == 0)
 53        h = 1;
 54    glViewport(0,0,w,h);
 55    glMatrixMode(GL_PROJECTION);
 56    glLoadIdentity();
 57    if(w < h)
 58        glOrtho(-range,range,-GLfloat(range*h/w),GLfloat(range*h/w),-range,range);
 59    else
 60        glOrtho(-GLfloat(range*w/h),GLfloat(range*w/h),-range,range,-range,range);
 61    glMatrixMode(GL_MODELVIEW);
 62    glLoadIdentity();
 63}

 64void keyboard(unsigned char key,int x,int y)
 65{
 66    switch(key)
 67    {
 68        //use the space key to decide whether the window will be full-screen displayed.
 69    case 32:                                            
 70        if(t%2 == 0)
 71                        //requests that the current window be made full screen
 72            glutFullScreen();                            
 73        else
 74                        //requests a change to the size of the current window
 75            glutReshapeWindow(WinWidth,WinHeight);        
 76        t++;
 77        break;
 78        //use the Esc key to quit the application
 79    case 27:                                            
 80        exit(0);
 81        break;
 82    default:
 83        break;
 84    }

 85}

 86void spinDisplay(void)
 87{
 88    spin_x = spin_x + 2.0f;
 89    spin_y = spin_y + 2.0f;
 90    if(spin_x > 360.0f)
 91        spin_x = spin_x - 360.0f;
 92    if(spin_y > 360.0f)
 93        spin_y = spin_y - 360.0f;
 94    glutPostRedisplay();
 95}

 96void mouse(int button,int state,int x,int y)
 97{
 98    switch(button)
 99    {
100        //click the left button to let the object move
101    case GLUT_LEFT_BUTTON:            
102        if(state == GLUT_DOWN)
103            glutIdleFunc(spinDisplay);
104        break;
105        //click the right button to let the object stop moving
106    case GLUT_RIGHT_BUTTON:            
107        if(state ==    GLUT_DOWN)
108            glutIdleFunc(NULL);
109        break;
110    default:
111        break;
112    }

113}

114int main(int argc,char *argv[])
115{
116    glutInit(&argc,argv);
117    glutInitDisplayMode(GLUT_DEPTH|GLUT_RGB|GLUT_DOUBLE);
118    glutInitWindowSize(WinWidth,WinHeight);
119    glutInitWindowPosition(100,100);
120    glutCreateWindow("Lesson 04");
121
122    init();
123    glutDisplayFunc(display);
124    glutReshapeFunc(reshape);
125    glutKeyboardFunc(keyboard);
126    glutMouseFunc(mouse);
127    glutMainLoop();
128    return 0;
129}

 

效果图如下:

posted on 2009-01-09 14:49  笔记  阅读(360)  评论(0编辑  收藏  举报

导航