2018年3月26日 星期一

Week04_老王

來源 : http://www.cmlab.csie.ntu.edu.tw/~jsyeh/3dcg10/
下載 data和win32 並解壓縮(
下載 glut32.dll 並複製到與win32同一個地方
解data壓縮至與win32同一個地方


啟動windows裡的Transformation.exe就可以移動旋轉縮放裡面的模型






把const double a = 0;//t*90.0;
//glRotated(60,1,0,0); 變成註解


旋轉茶壺



程式碼的茶壺
多家一行printf("%.1f\n", angle);


#include <stdio.h>
#include <GL/glut.h>
float angle=0, teapotX=0, teapotY=0;///加上移動的座標
int oldX=0,oldY=0, nowRotate=0; ///0: translate, 1: rotate
void display()
{
    glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
    glPushMatrix(); ///備份矩陣
        glTranslatef(teapotX, teapotY,0); ///我們把轉完的茶壺再移動
        glRotatef(angle,0,0,1); ///旋轉angle度
        glutSolidTeapot(0.3);
    glPopMatrix(); ///還原矩陣
    glutSwapBuffers();

}
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; ///如果是右鍵就移動
}
void motion(int x,int y)
 ///當mouse在drag motion時,
    if(nowRotate==1) angle=x; ///angle=x; ///去改變angle值
    else{
        teapotX+=(x-150)/150.0;  /// 加上改變量
        teapotY+=(150-y)/150.0;  /// 加上改變量
    }
    glutPostRedisplay(); ///請畫面重畫
}
int main(int argc, char *argv[])
{
    glutInit(&argc,argv);
    glutInitDisplayMode(GLUT_DOUBLE|GLUT_DEPTH);
    glutCreateWindow("Week04 transformation");
    glutMouseFunc(mouse);
    glutMotionFunc(motion);
    ///glutIdleFunc(display); ///很閒 /Idle,去重畫
    glutDisplayFunc(display);
    glutMainLoop();
}

沒有留言:

張貼留言