2018年3月19日 星期一

week04

1.上周進度:MOVE移動
    
    TODO:到jsyeh.org/3dcg10
    下載windows.zip
            data.zip
            glut32.dll
     

2.主題:移動、旋轉、縮放
   

#include <GL/glut.h>
float angle=0;
void display()
{
    glClear(GL_COLOR_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();
}

用滑鼠手動旋轉
#include <GL/glut.h>
float angle=0;
void display()
{
    glClear(GL_COLOR_BUFFER_BIT);
    glPushMatrix();
        glRotatef(angle, 0,0,1);
        glutSolidTeapot(0.3);
    glPopMatrix();
    glutSwapBuffers();
    ///angle++;
}
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();
}

左鍵旋轉,右鍵移動
#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();
    ///angle++;
}
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();
}


3.範例:Transformation.exe


4.期中考題
   

沒有留言:

張貼留言