2022年3月8日 星期二

(OwO)電腦圖學week03_3/8

1.去老師的網址: https://jsyeh.org/3dcg10/ 

下載範例: 


windows.zip ~> 解壓縮至windows裡 \ transformation.exe

data.zip ~> 解壓縮到windows裡 \ data \ 模型






2.把模型打開(右鍵會出現選單)





跑上周程式



安裝好freeglut , lib裡最長的檔名要改成 libglut32.a

codeblocks裡打開GLUT專案 , 要先按 . . . 選擇桌面 , 第二個要選放freeglut

跑出這個後,刪掉原程式







把上週茶壺的程式碼拿來用
這周教的是移動

#include <GL/glut.h>///簡化程式

void display()

{



    glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );///畫圖前先清理畫面

    glPushMatrix( ); ///備份矩陣(移動會累積,它會修改矩陣)

    glTranslatef(0.5,0.5,0);///右上


    glColor3f(1,1,0);///設定色彩

    glutSolidTeapot(0.3);

    glPopMatrix( );///還原矩陣

    glutSwapBuffers();///畫好後換出來



}



int main (int argc, char** argv)

{


    glutInit( &argc, argv);///送給glutInit 初始化

    glutInitDisplayMode( GLUT_DOUBLE | GLUT_DEPTH );

    glutCreateWindow("第2周程式!");



    glutDisplayFunc(display);



    glutMainLoop();



}



茶壺的影分身

#include <GL/glut.h>///簡化程式
void myTeapot (float x,float y)
{
     glPushMatrix();///備份矩陣(舊的位置)移動會累積,它會修改矩陣

    //glTranslatef(0.5,0.5,0);///右上

    glTranslatef(x,y,0);

    glColor3f(1,1,0);///設定色彩-黃色

    glutSolidTeapot(0.3);///茶壺大小

    glPopMatrix();///還原矩陣
}

void display()

{
    glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );///畫圖前先清理畫面

    myTeapot(0.5,0.5);
    myTeapot(0.5,-0.5);
    myTeapot(-0.5,-0.5);
    myTeapot(-0.5,0.5);

    glutSwapBuffers();///畫好後換出來



}



int main (int argc, char** argv)

{


    glutInit( &argc, argv);///送給glutInit 初始化

    glutInitDisplayMode( GLUT_DOUBLE | GLUT_DEPTH );

    glutCreateWindow("第3周程式!");



    glutDisplayFunc(display);



    glutMainLoop();



}


滑鼠事件(以滑鼠點擊讓它移動)

#include <GL/glut.h>///簡化程式
float mouseX=0,mouseY=0;///
void myTeapot (float x,float y)
{
     glPushMatrix();///備份矩陣(舊的位置)移動會累積,它會修改矩陣

    //glTranslatef(0.5,0.5,0);///右上

    glTranslatef(x,y,0);

    glColor3f(1,1,0);///設定色彩-黃色

    glutSolidTeapot(0.3);///茶壺大小

    glPopMatrix();///還原矩陣
}

void display()

{
    glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );///畫圖前先清理畫面

    myTeapot( (mouseX-150)/150.0 , -(mouseY-150)/150.0);///

    glutSwapBuffers();///畫好後換出來



}

void mouse (int button, int state, int x, int y)
{
   mouseX=x,mouseY=y;///
}

int main (int argc, char** argv)

{


    glutInit( &argc, argv);///送給glutInit 初始化

    glutInitDisplayMode( GLUT_DOUBLE | GLUT_DEPTH );

    glutCreateWindow("第3周程式!");



    glutDisplayFunc(display);

    glutMouseFunc(mouse);///滑鼠事件

    glutMainLoop();


}

點擊滑鼠顯示座標


#include <GL/glut.h>///簡化程式
#include<stdio.h>///printf()印東西用

float mouseX=0,mouseY=0;
void myTeapot (float x,float y)
{
     glPushMatrix();///備份矩陣(舊的位置)移動會累積,它會修改矩陣

    glTranslatef(x,y,0);

    glColor3f(1,1,0);///設定色彩-黃色

    glutSolidTeapot(0.1);///茶壺大小

    glPopMatrix();///還原矩陣
}

void display()

{
    glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );///畫圖前先清理畫面

    myTeapot( (mouseX-150)/150.0 ,-(mouseY-150)/150.0);///座標換算可把mouse轉換成3D世界

    glutSwapBuffers();///畫好後換出來



}

void mouse (int button, int state, int x, int y)
{
   printf("%d %d %d %d\n",button,state,x,y); 
   mouseX=x,mouseY=y;
}

int main (int argc, char** argv)

{


    glutInit( &argc, argv);///送給glutInit 初始化

    glutInitDisplayMode( GLUT_DOUBLE | GLUT_DEPTH );

    glutCreateWindow("第3周程式!");



    glutDisplayFunc(display);

    glutMouseFunc(mouse);///滑鼠事件

    glutMainLoop();


}






沒有留言:

張貼留言

VERY BEAUTIFUL, VERY POWERFUL

一.     一樣先安裝且設定好freeglut,OpecCV, 開啟CodeBlocks建立新專案 week11_gundam,                 把 MyGundam.zip下載解壓縮後的data資料夾放到freeglut/bin裡面 把week09_openc...