2018年3月19日 星期一

Week04_+0筆記

下載: http://www.cmlab.csie.ntu.edu.tw/~jsyeh/3dcg10/
 1.data  2.win32  3.glut32.dll
調整旋轉角度 glRotatef
自動旋轉

#include <GL/glut.h>//使用GLUT外掛
float angle=0;
void display()
{
    glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);//2種模式
    glPushMatrix();//備份矩陣
        glRotatef(angle,0,0,1);//旋轉angle度
        glutSolidTeapot(0.3);
    glPopMatrix();//還原矩陣
    glutSwapBuffers();//交換畫面buffer便會顯示
    angle++;//每次角度加1度
}
int main(int argc, char**argv)//進階的main()參數(個數,字串)
{
    glutInit(&argc,argv);//進階參數,初始GLUT
    glutInitDisplayMode(GLUT_DOUBLE|GLUT_DEPTH);//開始2種模式
    glutCreateWindow("Week04");//盡量不要用中文
    glutIdleFunc(display);//很閒/Idle,去重畫
    glutDisplayFunc(display);//等一下樓上要準備 void display()
    glutMainLoop();//主要迴圈
}

手動旋轉
老師的

我的

#include <GL/glut.h>//使用GLUT外掛
float angle=0;
void display()
{
    glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);//2種模式
    glPushMatrix();//備份矩陣
        glRotatef(angle,0,0,1);//旋轉angle度
        glutSolidTeapot(0.3);
    glPopMatrix();//還原矩陣
    glutSwapBuffers();//交換畫面buffer便會顯示
    angle++;//每次角度加1度
    //Now:printf("%.1f\n",angle);///Now:angle++;//每次角度加1度
}
void mouse(int button,int state,int x,int y)
{
}
void motion(int x,int y)
{///Now:當mouse在drag motion時
    angle=x;//Now:去改變angle值
    glutPostRedisplay();//Now:請畫面重畫
}
int main(int argc, char**argv)//進階的main()參數(個數,字串)
{
    glutInit(&argc,argv);//進階參數,初始GLUT
    glutInitDisplayMode(GLUT_DOUBLE|GLUT_DEPTH);//開始2種模式
    glutCreateWindow("Week04");//盡量不要用中文
    glutMouseFunc(mouse);//Now:mouse
    glutMotionFunc(motion);//Now:motion
    ///Now:glutIdleFunc(disolay);///很閒/Idle,去重畫
    glutDisplayFunc(display);//等一下樓上要準備 void display()
    glutMainLoop();//主要迴圈
}

移動旋轉

#include <stdio.h>
#include <GL/glut.h>//使用GLUT外掛
float angle=0,teapotX=0,teapotY=0;
int nowRotate=0,oldY=0,oldX=0;
void display()
{
    glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);//2種模式
    glPushMatrix();//備份矩陣
        glTranslatef(teapotX,teapotY,0);
        glRotatef(angle,0,0,1);//旋轉angle度
        glutSolidTeapot(0.3);
    glPopMatrix();//還原矩陣
    glutSwapBuffers();//交換畫面buffer便會顯示
    //Now:printf("%.1f\n",angle);///Now:angle++;//每次角度加1度
}
void mouse(int button,int state,int x,int y)
{
    if(button==GLUT_LEFT_BUTTON && state==GLUT_DOWN) nowRotate=1;
    if(button==GLUT_RIGHT_BUTTON && state==GLUT_DOWN) nowRotate=0;
    oldX=x;oldY=y;
}
void motion(int x,int y)
{///Now:當mouse在drag motion時
    if(nowRotate==1) angle+=(x-oldX);
    else{
                 teapotX+=(x-oldX)/150.0;
                teapotY+=(oldY-y)/150.0;

    }
    oldX=x;oldY=y;
    glutPostRedisplay();//Now:請畫面重畫
}
int main(int argc, char**argv)//進階的main()參數(個數,字串)
{
    glutInit(&argc,argv);//進階參數,初始GLUT
    glutInitDisplayMode(GLUT_DOUBLE|GLUT_DEPTH);//開始2種模式
    glutCreateWindow("Week04");//盡量不要用中文
    glutMouseFunc(mouse);//Now:mouse
    glutMotionFunc(motion);//Now:motion
    ///Now:glutIdleFunc(disolay);///很閒/Idle,去重畫
    glutDisplayFunc(display);//等一下樓上要準備 void display()
    glutMainLoop();//主要迴圈
}

沒有留言:

張貼留言