2018年3月23日 星期五

week04 葉政翰

(1)複習上週mouse移動


滑鼠移動程式碼:

#include <stdio.h>

#include <GL/glut.h>



///(1) 我們要使用比較高級的GLUT (OpengGL User Toolkit)

float teapotX=0, teapotY=0;

void display()

{

    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);///(8)清畫面

    glPushMatrix();

        glTranslatef(teapotX, teapotY, 0);



    glutSolidTeapot(0.3);///(9) 大小為0.3的茶壺

    glPopMatrix();

    glutSwapBuffers();///(10)交換double buffers 來顯示畫出來的畫面

}



void mouse(int button, int state, int x, int y)

{

    ///if(state==GLUT_DOWN) printf("  glVertex2f(%f, %f);\n", (x-150)/150.0, (150-y)/150.0);

    ///printf("%d %d %d %d\n", button, state, x, y);

}

void motion(int x, int y)

{

    teapotX=(x-150)/150.0;teapotY=(150-y)/150.0;

    glutPostRedisplay();

}



int main(int argc, char **argv)///(2)主要的函式 main

{ /// 這個參數的意思,是把作育系統的參數,塞進來



    glutInit(&argc, argv);///(3)初始Initizlize你的glut參數設定

    glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);///(4)顯示模式:double buffers (以後再教其他)

    glutCreateWindow("week03 Mouse");///(5)建立視窗

    glutDisplayFunc(display);///(6) 顯示函式display() 用來畫圖的

    glutMouseFunc(mouse);

    glutMotionFunc(motion);

    glutMainLoop();///(7)主要的迴圈,用來控制程式



}

(2)主題;移動

上週一動程式碼在上面

     主題:旋轉
旋轉程式碼:

可是茶壺會亂轉,無法很有效控制(壞的程式碼)

#include <stdio.h>

#include <GL/glut.h>



///(1) 我們要使用比較高級的GLUT (OpengGL User Toolkit)

float teapotX=0, teapotY=0, angle = 0;
void display()
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);///(8)清畫面
    glPushMatrix();

        glTranslatef(teapotX, teapotY, 0);
        glRotated(angle,0,0,1);
        glutSolidTeapot(0.3);///(9) 大小為0.3的茶壺
    glPopMatrix();
    glutSwapBuffers();///(10)交換double buffers 來顯示畫出來的畫面

}
void mouse(int button, int state, int x, int y)
{

    ///if(state==GLUT_DOWN) printf("  glVertex2f(%f, %f);\n", (x-150)/150.0, (150-y)/150.0);

    ///printf("%d %d %d %d\n", button, state, x, y);

}
void motion(int x, int y)
{
    ///teapotX=(x-150)/150.0;teapotY=(150-y)/150.0;
    angle = x;
    glutPostRedisplay();
}
int main(int argc, char **argv)///(2)主要的函式 main
{ /// 這個參數的意思,是把作育系統的參數,塞進來
    glutInit(&argc, argv);///(3)初始Initizlize你的glut參數設定

    glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);///(4)顯示模式:double buffers (以後再教其他)

    glutCreateWindow("week03 Mouse");///(5)建立視窗

    glutDisplayFunc(display);///(6) 顯示函式display() 用來畫圖的

    //glutMouseFunc(mouse);

    glutMotionFunc(motion);

    glutMainLoop();///(7)主要的迴圈,用來控制程式
}


     主題:移動縮放


移動旋轉程式碼:
可以持續選轉移動的程式碼(好的程式碼)

#include <stdio.h>
#include <GL/glut.h>
///(1) 我們要使用比較高級的GLUT (OpengGL User Toolkit)
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);///(8)清畫面
    glPushMatrix();
        glTranslatef(teapotX, teapotY, 0);
        glRotated(angle,0,0,1);
        glutSolidTeapot(0.3);///(9) 大小為0.3的茶壺
    glPopMatrix();
    glutSwapBuffers();///(10)交換double buffers 來顯示畫出來的畫面

}
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;
   /// if(state==GLUT_DOWN) printf("  glVertex2f(%f, %f);\n", (x-150)/150.0, (150-y)/150.0);
    ///printf("%d %d %d %d\n", button, state, x, y);

}
void motion(int x, int y)
{
    ///teapotX=(x-150)/150.0;teapotY=(150-y)/150.0;
    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)///(2)主要的函式 main
{ /// 這個參數的意思,是把作育系統的參數,塞進來
    glutInit(&argc, argv);///(3)初始Initizlize你的glut參數設定
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);///(4)顯示模式:double buffers (以後再教其他)
    glutCreateWindow("week03 Mouse");///(5)建立視窗
    glutDisplayFunc(display);///(6) 顯示函式display() 用來畫圖的
    glutMouseFunc(mouse);
    glutMotionFunc(motion);
    glutMainLoop();///(7)主要的迴圈,用來控制程式
}


(3)範例:Transformation.exe
-->下載 jsyeh.org/3dcg1Q
windows.zip-->桌面\windows\data\Transformation.exe
data.zip-->桌面\windows\data\許多模型
glut32.dll-->桌面\windows\glut32.dll

(4)期中考題


沒有留言:

張貼留言