2017年3月28日 星期二

week 06 翊帆上課日記

hw1 期中考練習

glPushMatrix();//備份矩陣
glRotatef(angle, x,y,z);//旋轉
glTranslatef(x,y,z);//移動
glScalef(x,y,z);//縮放
glBegin(GL_POLYGON);//開始畫
glNormal3f(nx,ny,nz);//打光的法向量
glTexcoord2f(tx,ty);//貼圖座標
glColor3f(r,g,b);//顏色
glVertex3f(x,y,z);//頂點
glEnd();//結束畫
glpopMatrix();//還原矩陣


理解旋轉


點開Transformation.exe


glRotatef(angle, x, y, z);//旋轉, 前面是轉動角度, 後面是旋轉軸
右手定則:拇指的方向是轉動軸, 其他手指的方向是轉動方向

實作


程式碼:
#include <GL/glut.h>
float mouseX=0;
void display()
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glPushMatrix();
        glRotatef(mouseX,0,0,1);
        glColor3f(1,0,0);
        glutSolidTeapot(0.3);
        glPushMatrix();
            //3.掛上去
            glTranslatef(0.5,0.14,0);//3.把整個樓下的東西,移到紅色茶壺嘴
            glRotatef(mouseX,0,0,1);//2.會轉動
            glTranslatef(0.5,0,0);//1.把手把移到轉盤的中心
            glColor3f(1,1,1);
            glutSolidTeapot(0.3);
        glPopMatrix();
    glPopMatrix();
    glutSwapBuffers();
}
void motion(int x,int y)
{
    mouseX=x;
    glutPostRedisplay();
}
int main(int argc,char **argv)
{
    glutInit(&argc,argv);
    glutInitDisplayMode(GLUT_RGB|GLUT_DOUBLE|GLUT_DEPTH);
    glutCreateWindow("TRT robot");
    glutDisplayFunc(display);
    glutMotionFunc(motion);
    glutMainLoop();
}

沒有留言:

張貼留言