2018年3月19日 星期一

Week04

2018/03/19
主題:移動、旋轉、縮放
範例:Transformation


PART1

     到 http://www.cmlab.csie.ntu.edu.tw/~jsyeh/3dcg10/ 網站下載

     Window  data  glut32.dll






用右手安培去旋轉判斷

       右手定則 glRotatef( 角度, X軸, Y軸, Z軸);
        X 軸: 正值 :右手大拇指向右,轉動向內 負值 :右手大拇指向左,轉動向外 
        Y 軸: 正值 :右手大拇指向上,轉動向內 負值 :右手大拇指向下,轉動向外 
        Z 軸: 正值 :右手大拇指向內,逆時針轉動 負





重點:  期中考80%





PART2

複製上週程式碼
修改使茶壺可用滑鼠調整旋轉

#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();
}
#include <stdio.h>
void mouse(int button, int state, int x, int y)
{
   if(state==GLUT_DOWN) printf("glVertex2f(%f, %f);\n",(x-150)/150.0,-(y-150)/150.0);

}
void motion(int x, int y)
{
    angle=x;
    glutPostRedisplay();
}
int main(int argc, char**argv)
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
    glutCreateWindow("Week04");

    glutDisplayFunc(display);
    glutMouseFunc(mouse);
    glutMotionFunc(motion);
    glutMainLoop();

}




///////可用滑鼠左鍵中建右鍵坐旋轉軸

#include <stdio.h>
#include <GL/glut.h>
float angle=0, teapotX=0, teapotY=0;
int oldX=0, oldy=0, nowRotate=0;
void display()
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glPushMatrix();
        glRotatef(angle,0,0,1);
        glutSolidTeapot(0.3);
    glPopMatrix();
    glutSwapBuffers();
}
void mouse(int button, int state, int x, int y)
{
   if(button==GLUT_LEFFT_BOTTON && state==GLUT_DOWN) nowRotate=1;
   if(button==GLUT_LEFFT_BOTTON && state==GLUT_DOWN) nowRotate=0;
    oldX=x;
    oldY=y;
}
void motion(int x, int y)
{
    if(nowRotate==1) angle+=(x-oldx);
    else
    {
        teapotY+=(x-oldX)/150.0;
        teapotY+=(y-oldY)/150.0;
    }
    oldX=x;
    oldY=y;
    glutPostRedisplay();
}
int main(int argc, char**argv)
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
    glutCreateWindow("Week04");

    glutDisplayFunc(display);
    glutMouseFunc(mouse);
    glutMotionFunc(motion);
    glutMainLoop();
}


沒有留言:

張貼留言