2018年3月19日 星期一

Week04 ㄩㄐ的流浪日常

Week03教學內容

(1)到jsyeh.org/3dcg10

下載

①windows.zip ➤ Transformation.exe
②data.zip ➤ 很多模型
③glut32.dll



噹啷~~~~~~~~~






(2)開啟隨意檔案,試 glRotatef (angle, x, y, z);


嘗試各種旋轉軸


以及各種模型


(3)轉轉茶壺




#include <stdio.h>
#include <GL/glut.h>

float angle=0;
void display()
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glPushMatrix(); /*備份矩陣*/
        glRotatef(angle, 0, 0, 1); /*旋轉角度*/
        glutSolidTeapot(0.3);
    glPopMatrix(); /*還原矩陣*/
    glutSwapBuffers();
    printf("%.1f\n", angle);
    angle++; /*每次角度+1*/
}

int main(int argc, char*argv[])
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
    glutCreateWindow("Week04_Transformation");

    glutIdleFunc(display); /*重畫*/
    glutDisplayFunc(display);
    glutMainLoop();
}

↑程式碼長這樣 

(4)加入拖曳茶壺的程式
int nowRotate=0/*0=translate, 1=rotate*/

void motion(int button, int state, int x, int y)
{
   /*當滑鼠在操作時*/
   angle=x;
   glutPostRedisplay();
}

這時會發現茶壺移動的方式很奇怪
於是更改motion並加入mouse程式碼及移動座標

float angle=0, teapotX=0, teapotY=0;
int oldX=0oldY=0, nowRotate=0;

void mouse(int button, int state, int x, int y)
{
   if(button == GLUT_LEFT_BUTTON && state == GLUT_DOWNnowRotate=1/*左鍵則旋轉*/
   if(button == GLUT_LEFT_BUTTON && state == GLUT_DOWNnowRotate=0/*右鍵則移動*/
   oldX=x; oldY=y;
}

void motion(int button, int state, int x, int y)
{
   if(nowRotate == 1) angle=x;
   else
   {
      teapotX += (x-150) / 150.0;
      teapotY += (150-y) / 150.0;;
   }
   oldX=xoldY=y;
   glutPostRedisplay();
}

沒有留言:

張貼留言