2018年3月23日 星期五

week 04 林郁珊

複習上週mouse移動

1. 將data.zip, windows.zip, glut32.dll下載至桌面

2. 解壓縮,並將data, glut32.dll放進windows

3. 打開transformation.exe













4. 測試x,y,z軸如何旋轉

做出可旋轉茶壺

1. 建立一個專案(glut)

2. 刪除全部程式碼,重新撰寫

#include <GL/glut.h>
#include <stdio.h>
float teapotX=0,teapotY=0,angle=0;//加一個angle
void display()
{
    glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
    glPushMatrix();
        glTranslatef(teapotX, teapotY, 0);
        glRotatef(angle,0,0,1);//增加旋轉角
        glutSolidTeapot(0.3);
    glPopMatrix();
    glutSwapBuffers();
}

void motion(int x, int y)
{
    angle =x;//將角度設為x
    glutPostRedisplay();
}

int main(int argc, char **argv)
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
    glutCreateWindow("week03 Mouse");

    glutDisplayFunc(display);
    glutMotionFunc(motion);

    glutMainLoop();
}

讓茶壺轉動及移動更順利

#include <GL/glut.h>
#include <stdio.h>
float teapotX=0,teapotY=0,angle=0;
int oldX=0, oldY=0, nowRotate=0;
void display()
{
    glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
    glPushMatrix();
        glTranslatef(teapotX, teapotY, 0);
        glRotatef(angle,0,0,1);
        glutSolidTeapot(0.3);
    glPopMatrix();
    glutSwapBuffers();
}

void motion(int x, int y)
{
    if(nowRotate==1)angle += (x-oldX);
    else
    {
        teapotX += (x-oldX)/150.0;//利用移動量再移動
        teapotY += (oldY-y)/150.0;//利用移動量再移動
    }
    oldX=x; oldY=y;
    glutPostRedisplay();
}
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;
}
int main(int argc, char **argv)
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
    glutCreateWindow("week03 Mouse");
    glutDisplayFunc(display);
    glutMouseFunc(mouse);//滑鼠事件
    glutMotionFunc(motion);
    glutMainLoop();
}



沒有留言:

張貼留言