2018年3月19日 星期一

week04 阮致峰

1.

解壓縮在同一目錄
(jsyeh.org/3dcg10 <<由此下載)

2.

開啟transformmation
rotation第一個欄位為旋轉(v)
後三個是x,y,z軸的旋轉軌跡(即旋轉軸)

!!!
期中考題
英文(旋轉放大位置)
角度
參數

3.

(上課截圖)

(上課截圖)

(上課截圖)

(上課截圖)

4.
(茶壺旋轉)
#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();
    angle++;
}
int main(int argc,char*argv[])
{
    glutInit(&argc,argv);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
    glutCreateWindow("Week04 transformation");


    glutIdleFunc(display);
    glutDisplayFunc(display);
    glutMainLoop();
}

5.
(用滑鼠旋轉,只有x軸)
#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();
}
void mouse(int button, int state, int x, int y)
{

}
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 transformation");
    glutMouseFunc(mouse);
    glutMotionFunc(motion);
    ///glutIdleFunc(display);
    glutDisplayFunc(display);
    glutMainLoop();
}

6.
(5.的進階,拉角度時不會重製)
#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();
        glTranslatef(teapotX,teapotY,0);
        glRotatef(angle,0,0,1);
        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;
        teapotY += (oldY-y)/150.0;
    }
    oldX=x;
    oldY=y;
    glutPostRedisplay();
}
int main(int argc,char*argv[])
{
    glutInit(&argc,argv);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
    glutCreateWindow("Week04 transformation");
    glutMouseFunc(mouse);
    glutMotionFunc(motion);
    ///glutIdleFunc(display);
    glutDisplayFunc(display);
    glutMainLoop();
}

沒有留言:

張貼留言