2016年3月21日 星期一

陳昱宏03161122,week05

開啟transformation後的視窗:


1.glRotatef(a,b,c,d);
a為視野的旋轉角度  可由-360.0至360.0
b,c,d能旋轉成三軸所對稱的方向,範圍是-1.00到1.00
相同數值得正副剛好與所對的軸承對稱(ex:b=-1.00與b=1.00的x軸為對稱圖)
也就是攝影機拍攝的角度 類似mayaE鍵的選轉功能
2.glTranslatef(x,y,z);
三值範圍皆為-5到5
為視野的三軸位置
也就是攝影機所在的位置 類似mayaW鍵的移動功能
3.glScalef(x,y,z);
為三軸的變形
值的範圍也為-5至5 改變值可延軸做圖的拉長或壓縮
也就是所攝物件的變形 類似maya程式R鍵的變形功能


旋轉茶壺:
如之前的方式開啟GLUT,一樣把選轉物件的程式刪掉
改成這禮拜的程式碼(如下)
下圖為只有X軸轉向的程式碼  匯入茶壺後在X軸配合mouse的左右轉動而改變x的值
#include <GL/glut.h>
float rotX=0;
void display()
{
    glClearColor(1,1,1,1);
    glClear(GL_COLOR_BUFFER_BIT);
    glPushMatrix();    ///備份Matrix (Matrix裡有translate,rotate,scale的量值)
        glRotatef(rotX, 1,0,0);   ///圖2     /////(rotX,0,1,0)圖1
        glColor3f(1,0,0);
        glutSolidTeapot(0.3);
    glPopMatrix();     ///還原Matrix(回到剛剛push時的樣子)
    glutSwapBuffers();
}
void motion(int x,int y)
{
    rotX=y;    ///圖2    ///rotX=x;圖1
    display();    ///可以用glutPostRedisplay();一樣的意思
}


int main(int argc, char**argv)
{
    glutInit(&argc,argv);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);    ////double buffers有兩倍的記憶體,一                                                                                                                                                            個屬於螢幕上,一個是內部記憶體
    glutCreateWindow("hello3D");
    glutDisplayFunc(display);
    glutMotionFunc(motion);
    glutMainLoop();
}
之後可加入y軸的程式碼  便可利用滑鼠的上下來做垂直的旋轉
下為程式碼(加註解)
#include <GL/glut.h>
float rotX=0,rotY=0;
void display()
{
    glClearColor(1,1,1,1);
    glClear(GL_COLOR_BUFFER_BIT);
    glPushMatrix();///備份Matrix(Matrix裡有translate,rotate,scale的量值)
        glRotatef(rotX,0,1,0);///X的移動量,讓他對Y軸轉
        glRotatef(rotY,1,0,0);///Y的移動量,讓他對X軸轉
        glColor3f(1,0,0);
        glutSolidTeapot(0.3);
    glPopMatrix();///還原Matrix:回到push時的樣子
    glutSwapBuffers();
}
void motion(int x,int y)
{
    rotX=x;
    rotY=y;///同時拿來用
    display();///glutPostRedisplay();
}
int main(int argc,char**argv)
{
    glutInit(&argc,argv);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);///double buffers兩倍的記憶體
    glutCreateWindow("hello3D");
    glutDisplayFunc(display);
    glutMotionFunc(motion);

    glutMainLoop();

}

沒有留言:

張貼留言