2018年3月23日 星期五

week04_黃惠嘉

*點線面顏色、移動/旋轉/縮放與矩陣(Matrix)
1.複製上周茶壺的程式碼,並旋轉

#include <stdio.h>
#include <GL/glut.h>
float teapotX=0,teapotY=0,angle=0;///rotate旋轉要得angle
int oldX=0,oldY=0,nowRotate=0;///oldX和oldY記下按了哪些地方
void display()
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glPushMatrix();
        glTranslatef(teapotX,teapotY,0);
        glRotatef(angle,0,0,1);///茶壺對著(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;///右鍵移動
    oldX=x; oldY=y;///新的座標
}
void motion(int x,int y)
{
    if(nowRotate==1) angle += (x-oldX);///滑鼠的座標就是轉的角度
    else
    {
        teapotX += (x-oldX)/150.0;///依照motion時的x來改teapot的座標
        teapotY += (oldY-y)/150.0;///依照motion時的y來改teapot的座標
    }
    oldX=x; oldY=y; ///新的座標
    glutPostRedisplay();///重畫畫面
}
int main (int argc,char ** argv)
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
    glutCreateWindow("05160080");
    glutDisplayFunc(display);
    glutMouseFunc(mouse);
    glutMotionFunc(motion);
    glutMainLoop();
}




2.

沒有留言:

張貼留言