2024年3月11日 星期一

軒week04

  1.MOODLE解壓縮 freeglut-MinGW-3.0.0-1.mp.zip

把解壓縮完的檔案移到桌面

把124KB的檔案改名成 libglut32.a

開新的專案命名week03_mouse

解壓縮到C:\Users\austi\OneDrive\桌面\freeglut  

2.從老師的網頁(https://jsyeh.org/3dcg10/)下載兩個檔案:1.data 2.win32➡先解壓縮win32的檔案 然後把data資料夾移到windows資料夾

打開 Transformation.exe


                                      

                      👇


                                      👇



                                     👇



縮放:glScalef(x,y,z)➡調整X軸變瘦變胖

                                     調整Y軸變高變矮

                                     調整Z軸變長變短


#include <GL/glut.h>

void display()

{

    glClearColor(1,1,0.9,1);///背景變淡黃色(R,G,B,A),Alpha的意思是半透明程度

    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);///清背景

    glColor3f(0,1,0);///茶壺變綠色

    glutSolidTeapot(0.3);///畫一個實心的茶壺,大小是0.3

    glutSwapBuffers();///請GLUT把畫面swap送到顯示的地方

}

int main(int argc, char *argv[])

{ ///上面是特別的main函式,有很多參數

glutInit(&argc, argv);///把GLUT開起來 (GLUT初始化)

glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);

        ///上面這行是把顯示的模式設定好

glutCreateWindow("week03 Mouse");///要開視窗

glutDisplayFunc(display);///要顯示的對應函式

glutMainLoop();///最後用main迴圈,壓在最後面

}


-----------------------------------------------------------------------------------------------------------------------------

#include <GL/glut.h>
float s = 1;///一開始是原本一倍的大小
void display()
{
    glClearColor(1,1,0.9,1);
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);///清背景
    glColor3f(0,1,0);
    glPushMatrix();
        glScalef(s,s,s);///縮放S倍
        glutSolidTeapot(0.3);///畫一個實心的茶壺,大小是0.3
    glPopMatrix();
glutSwapBuffers();///請GLUT把畫面swap送到顯示的地方
}
void motion(int x,int y)
{
    s = 1 + (x-150)/150.0;///會變大變小的0 ~ 1 ~ 2
    display();///重畫畫面
}
int main(int argc, char *argv[])
{ ///上面是特別的main函式,有很多參數
glutInit(&argc, argv);///把GLUT開起來 (GLUT初始化)
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
        ///上面這行是把顯示的模式設定好
glutCreateWindow("week03 Mouse");///要開視窗
glutDisplayFunc(display);///要顯示的對應函式
glutMotionFunc(motion);///拖動motion的時候
glutMainLoop();///最後用main迴圈,壓在最後面
}

可以通過滑鼠拉動讓茶壺放大縮小



----------------------------------------------------------------------------------------------------------------------------

交換Translatef / Rotatef

旋轉軸在人物正中間(人物自己在轉)

旋轉軸在方框的正中間(人物繞著一個柱子轉)





----------------------------------------------------------------------------------------------------------------------------

讓茶壺自轉

#include <GL/glut.h>
float teapotX = 0.5 , teapotY = 0;///放右邊
float angle = 0;///旋轉角度
float s = 0.3;///一開始是原本一倍的大小

void display()
{
    glClearColor(1,1,0.9,1);
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);///清背景
    glColor3f(0,1,0);
    glPushMatrix();
        glTranslatef(teapotX,teapotY,0);
        
glRotatef(angle++,0,0,1);

        glScalef(s,s,s);///縮放S倍
        glutSolidTeapot(0.3);///畫一個實心的茶壺,大小是0.3
    glPopMatrix();
glutSwapBuffers();///請GLUT把畫面swap送到顯示的地方
}
void motion(int x,int y)
{
    s = 1 + (x-150)/150.0;///會變大變小的0 ~ 1 ~ 2
    display();///重畫畫面
}
int main(int argc, char *argv[])
{ ///上面是特別的main函式,有很多參數
  glutInit(&argc, argv);///把GLUT開起來 (GLUT初始化)
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
        ///上面這行是把顯示的模式設定好
glutCreateWindow("week03 Mouse");///要開視窗
glutDisplayFunc(display);///要顯示的對應函式
glutMotionFunc(motion);///拖動motion的時候
glutIdleFunc(display);///為了讓12行的angle一直++,所以一直畫
glutMainLoop();///最後用main迴圈,壓在最後面
}



----------------------------------------------------------------------------------------------------------------------------
讓茶壺繞中心轉

#include <GL/glut.h>
float teapotX = 0.5 , teapotY = 0;///放右邊
float angle = 0;///旋轉角度
float s = 0.3;///一開始是原本一倍的大小
void display()
{
    glClearColor(1,1,0.9,1);
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);///清背景
    glColor3f(0,1,0);
    glPushMatrix();
        glRotatef(angle++,0,0,1);
       
glTranslatef(teapotX,teapotY,0);

        glScalef(s,s,s);///縮放S倍
        glutSolidTeapot(0.3);///畫一個實心的茶壺,大小是0.3
    glPopMatrix();
glutSwapBuffers();///請GLUT把畫面swap送到顯示的地方
}
void motion(int x,int y)
{
    s = 1 + (x-150)/150.0;///會變大變小的0 ~ 1 ~ 2
    display();///重畫畫面
}
int main(int argc, char *argv[])
{ ///上面是特別的main函式,有很多參數
  glutInit(&argc, argv);///把GLUT開起來 (GLUT初始化)
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
        ///上面這行是把顯示的模式設定好
glutCreateWindow("week03 Mouse");///要開視窗
glutDisplayFunc(display);///要顯示的對應函式
glutMotionFunc(motion);///拖動motion的時候
glutIdleFunc(display);
glutMainLoop();///最後用main迴圈,壓在最後面
}





Keyboard

#include <GL/glut.h>

#include <stdio.h>

void display()
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);///清背景
    glutSolidTeapot(0.3);///畫一個實心的茶壺,大小是0.3
    glutSwapBuffers();///請GLUT把畫面swap送到顯示的地方
}
void keyboard(unsigned char key ,int x,int y)
{
    printf("key:%c x:%d y:%d\n",key,x,y);
}
int main(int argc, char *argv[])
{ ///上面是特別的main函式,有很多參數
    glutInit(&argc, argv);///把GLUT開起來 (GLUT初始化)
    glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
    ///上面這行是把顯示的模式設定好
    glutCreateWindow("week04 keyboard mouse motion");///要開視窗
    glutDisplayFunc(display);///要顯示的對應函式
    glutKeyboardFunc(keyboard);///鍵盤事件函式
    glutMainLoop();///最後用main迴圈,壓在最後面
}




Keyboard + Mouse
#include <GL/glut.h>
#include <stdio.h>
int N = 0;
float teapotX[1000],teapotY[1000];///存座標
void display()
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);///清背景
    for(int i=0;i<N;i++){
        glPushMatrix();
            glTranslatef(teapotX[i],teapotY[i],0);
            glutSolidTeapot(0.03);///畫一個實心的茶壺,大小是0.3
        glPopMatrix();
    }
    glutSwapBuffers();///請GLUT把畫面swap送到顯示的地方
}
void keyboard(unsigned char key ,int x,int y)
{
    printf("key:%c x:%d y:%d\n",key,x,y);
}
void mouse(int button,int state,int x,int y)
{
    teapotX[N] = (x-150)/150.0;
    teapotY[N] = -(y-150)/150.0;
    N++;
}
int main(int argc, char *argv[])
{ ///上面是特別的main函式,有很多參數
glutInit(&argc, argv);///把GLUT開起來 (GLUT初始化)
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
        ///上面這行是把顯示的模式設定好
glutCreateWindow("week04 keyboard mouse motion");///要開視窗
glutDisplayFunc(display);///要顯示的對應函式
glutKeyboardFunc(keyboard);///鍵盤事件函式
glutMouseFunc(mouse);///滑鼠事件的函式
glutMainLoop();///最後用main迴圈,壓在最後面
}
///點一下出現一個滑鼠






















































沒有留言:

張貼留言