2017年10月5日 星期四

余瑞得 week 4

旋轉
1.先把預設project打開
2.重點code
   
   glPushMatrix();    //把現在的原點push進stack
        glTranslated(-2.4,1.2,-6);   //位移現在的原點,因為有resize所以範圍超過1
        glRotated(60,1,0,0);           // (角度,x軸,y軸,z軸)
        glRotated(a,0,0,1);
        glColor3f(0.88,0.5,1);       //改色
        glutSolidSphere(1,slices,stacks);   //畫出圓球 
        glutSolidTeapot(1);  //畫出茶壺
    glPopMatrix();   //把點pop回來

3.暫時的結果  (未修改rotate)
4.稍微改一下rotate然後讓三個茶壺分別根據x,y,z軸轉
5.rotate的參數可以一次改多個 (根據那幾個維度做旋轉 )

↑上面左邊的兩個茶壺是根據(1,1,0)跟(1,-1,0)去做旋轉 所以是對稱的
6.新增glScalef(float x ,float y,float z)函式 去做xyz軸做增大

↑可以看出上方左邊的茶壺明顯比其他的大
7.glPushMatrix跟glPopMatrix有在上面提到他的用法了
8.滑鼠移動的部分要寫兩個新的函式

9.寫完之後就可以利用滑鼠拖曳你的圖形了


↑把茶壺拖到左上角示意圖








//這次比較麻煩,紀錄一下code
#include <GL/glut.h>
#include <stdlib.h>

float dx=0,dy=0,dz=0;
int oldx=0,oldy=0,oldz=0;

static void display(void)
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    glPushMatrix();
        glTranslated(dx,dy,dz);
        glColor3f(0.2,0.5,0.8);
        glutSolidTeapot(0.3);
    glPopMatrix();
    glutSwapBuffers();
}
void motion (int x,int y)
{
    dx += (x-oldx)/200.0;  ///點要不斷更新所以不斷去加位移量
    dy += (oldy-y)/200.0;  ///y變成下面是1所以用舊的減
                           ///除150是因為xy的界線都是0~1 除的數字越小位移越大
    oldx = x,oldy = y;     ///更新點座標
    glutPostRedisplay();   ///電腦畫面重繪
}
void mouse(int button,int state,int x,int y)
{
    if(state==GLUT_DOWN){oldx=x,oldy=y;}///按滑鼠之後更新點座標
}
int main(int argc, char *argv[])
{
    glutInit(&argc, argv);
    glutInitWindowSize(640,480);
    glutInitWindowPosition(10,10);
    glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
    glutCreateWindow("hw2");

    glutDisplayFunc(display);
    glutMouseFunc(mouse); ///第七週
    glutMotionFunc(motion); ///7w
    glutMainLoop();

    return EXIT_SUCCESS;
}

沒有留言:

張貼留言