2024年3月14日 星期四

魚鱗☆゚.*・。電腦圖學 Week04

 魚鱗☆゚.*・。電腦圖學 Week04

1. week04_mouse_glScalef ~滑鼠可以縮放茶壺~☆゚.*・。

(1)在桌面安裝freeglut☆゚.*・。

(2)新增glut專案---> week04_mouse_glScalef ☆゚.*・。

(3)手動輸入10行程式☆゚.*・。

#include <GL/glut.h>
void display()
{
    glutSolidTeapot(0.3);
    glutSwapBuffers();
}
int main(int argc, char*argv[])
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_DOUBLE|GLUT_DEPTH);
    glutCreateWindow("week04 mouse glScalef");
    glutDisplayFunc(display);
    
    glutMainLoop();
}

(4)加上清背景的程式☆゚.*・。

#include <GL/glut.h>
void display()
{
    glClearColor(1.0,1.0,0.9,1.0); ///清背景的淡黃色
    glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT); ///清背景
    glutSolidTeapot(0.3);
    glutSwapBuffers();
}
int main(int argc, char*argv[])
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_DOUBLE|GLUT_DEPTH);
    glutCreateWindow("week04 mouse glScalef");
    glutDisplayFunc(display);

    glutMainLoop();
}

(4)執行程式☆゚.*・。





















(5)加上旋轉角度的程式☆゚.*・。

#include <GL/glut.h>
float angle=0; ///旋轉角度
void display()
{
    glClearColor(1.0,1.0,0.9,1.0); ///清背景的紫色
    glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT); ///清背景
    glutSolidTeapot(0.3);
    glutSwapBuffers();
}
void motion(int x,int y)
{
    angle = x;
    display();
}

int main(int argc, char*argv[])
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_DOUBLE|GLUT_DEPTH);
    glutCreateWindow("week04 mouse glScalef");
    glutDisplayFunc(display);
    glutMotionFunc(motion);
    glutMainLoop();
}


(6)把旋轉角度的程式註解調,加上縮放的程式☆゚.*・。

#include <GL/glut.h>
float angle = 0; ///旋轉角度
float s =1;
void display()
{
    glClearColor(1.0, 0.0, 0.9, 1.0); ///清背景的紫色
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); ///清背景
    glPushMatrix();
        ///glRotatef(angle, 0, 0, 1);
        glScalef(s, s, s); ///縮放
        glutSolidTeapot( 0.3 );
    glPopMatrix();
    glutSwapBuffers();
}
void motion(int x,int y)
{
    ///angle = x;
    s=1+(x-150)/150.0;
    display();
}
int main(int argc, char*argv[])
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_DOUBLE|GLUT_DEPTH);
    glutCreateWindow("week04 mouse glScalef");
    glutDisplayFunc(display);
    glutMotionFunc(motion);
    glutMainLoop();
}














2. week04_keyboard_mouse ~按鍵盤得出滑鼠座標~☆゚.*・。

(1)複製貼上11行程式(包含glClear)☆゚.*・。

#include <GL/glut.h>
void display()
{
   
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glutSolidTeapot(0.3);
    glutSwapBuffers();
}
int main(int argc, char*argv[])
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_DOUBLE|GLUT_DEPTH);
    glutCreateWindow("week04 mouse glScalef");
    glutDisplayFunc(display);
    
    glutMainLoop();
}

(2)加上keyboard程式☆゚.*・。

#include <GL/glut.h>
#include <stdio.h>
void display()
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glutSolidTeapot(0.3);
    glutSwapBuffers();
}
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[])
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_DOUBLE|GLUT_DEPTH);
    glutCreateWindow("week04 keyboard mouse");
    glutDisplayFunc(display);
    glutKeyboardFunc(keyboard);

    glutMainLoop();
}

(3)執行程式☆゚.*・。

按下鍵盤上的任意按鍵,會印出你按下的鍵以及滑鼠所在的座標












(4)再加上旋轉茶壺的程式☆゚.*・。

#include <GL/glut.h>
#include <stdio.h>
float angle = 0, oldX = 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){
    oldX = x; ///當mouse按下時,記錄它的位置
}
void motion(int x, int y)
{
    angle += (x-oldX);
    oldX = x;
    display();
}
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[])
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_DOUBLE|GLUT_DEPTH);
    glutCreateWindow("week04 keyboard mouse");
    glutDisplayFunc(display);
    glutKeyboardFunc(keyboard);
    glutMouseFunc(mouse);
    glutMotionFunc(motion);
    glutMainLoop();
}

(5)執行程式☆゚.*・。

現在就能旋轉茶壺了













3. 看課本範例☆゚.*・。

(1)到老師目錄:http://jsyeh.org/3dcg10 ☆゚.*・。

(2)下載 win32 和 data ☆゚.*・。

(3)解壓縮windows.zip和data.zip☆゚.*・。

(4)把data資料夾放入windows資料夾☆゚.*・。

(5)進入 Transformation.exe ☆゚.*・。

##下面視窗案右鍵可以改變Translate/Rotate 的順序☆゚.*・。













##下面視窗案數值順序意義☆゚.*・。













4. week04_keyboard_mouse ☆゚.*・。

(1)把 2. 的程式複製貼上

(2)刪減+修改程式

#include <GL/glut.h>
#include <stdio.h>
float angle = 0, oldX = 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){
    oldX = x; ///當mouse按下時,記錄它的位置
}
void motion(int x, int y)
{
    angle += (x-oldX);
    oldX = x;
    display();
}
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[])
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_DOUBLE|GLUT_DEPTH);
    glutCreateWindow("week04 keyboard mouse");
    glutDisplayFunc(display);
    glutKeyboardFunc(keyboard);
    glutMouseFunc(mouse);
    glutMotionFunc(motion);
    glutIdleFunc(display);
    glutMainLoop();
}

(3)執行程式

茶壺就會自動旋轉了











(4)增加程式

#include <GL/glut.h>
#include <stdio.h>
float angle = 0, oldX = 0;
void display()
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glPushMatrix();
        glTranslatef( 0.8, 0, 0); ///放到右邊去
        glRotatef(angle++, 0, 0, 1); ///轉動中的
        glScalef( 0.3, 0.3, 0.3); ///小小的
        glColor3f(0, 1, 0); ///綠色的
        glutSolidTeapot( 0.3 ); ///茶壺
    glPopMatrix();
    glutSwapBuffers();
}
int main(int argc, char*argv[])
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_DOUBLE|GLUT_DEPTH);
    glutCreateWindow("week04 translate rotate scale");
    glutDisplayFunc(display);
    glutIdleFunc(display);
    glutMainLoop();
}

(5)執行程式,放到右邊去,轉動中的,小小的,綠色的茶壺就出現了











(6)增加改變背景的程式

#include <GL/glut.h>
#include <stdio.h>
float angle = 0, oldX = 0;
void display()
{
    glClearColor( 1.0, 1.0, 0.9, 1.0);
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glPushMatrix();
        glTranslatef( 0.8, 0, 0); ///放到右邊去
        glRotatef(angle++, 0, 0, 1); ///轉動中的
        glScalef( 0.3, 0.3, 0.3); ///小小的
        glColor3f(0, 1, 0); ///綠色的
        glutSolidTeapot( 0.3 ); ///茶壺
    glPopMatrix();
    glutSwapBuffers();
}
int main(int argc, char*argv[])
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_DOUBLE|GLUT_DEPTH);
    glutCreateWindow("week04 translate rotate scale");
    glutDisplayFunc(display);
    glutIdleFunc(display);
    glutMainLoop();
}

背景的顏色就改變了





















5. Week04_rotate_translate_scale ☆゚.*・。

(1)回到課本教材











(2) new project

(3) 把上一個程式複製貼上

(4) 把 RotateTranslate 的程式交換

#include <GL/glut.h>
#include <stdio.h>
float angle = 0, oldX = 0;
void display()
{
    glClearColor( 1.0, 1.0, 0.9, 1.0);
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glPushMatrix();
        glRotatef(angle++, 0, 0, 1); ///轉動中的(交換)
        glTranslatef( 0.8, 0, 0); ///放到右邊去(交換)

        glScalef( 0.3, 0.3, 0.3); ///小小的
        glColor3f(0, 1, 0); ///綠色的
        glutSolidTeapot( 0.3 ); ///茶壺
    glPopMatrix();
    glutSwapBuffers();
}
int main(int argc, char*argv[])
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_DOUBLE|GLUT_DEPTH);
    glutCreateWindow("week04 translate rotate scale");
    glutDisplayFunc(display);
    glutIdleFunc(display);
    glutMainLoop();
}
 

(5)執行程式



6. 上傳到github ☆゚.*・。

##只需上傳. cpp .cbp

沒有留言:

張貼留言