2019年3月14日 星期四

Hans杜芬舒斯的邪惡圖學_week04

1.首先到小葉老師的網站下載(http://www.cmlab.csie.ntu.edu.tw/~jsyeh/3dcg10/)
2.Examples: [data]、[win32]、glut32.dll>將[win32]解壓縮> 把[data]、glut32.dll、放進[win32]資料夾 > 打開範例Transformation.exe
<glut>
step
1.在瀏覽器搜尋freeglut windows 找到mingw下載
2.在lib內找到libfreeglut.a複製一個新的改成libglut32.a
3.打開codeblocks>file>new>project>glut project
4.在視窗輸入project title設定位置為C:\Users\user\Desktop\freeglut>finish
5.點擊management窗格的檔案main.cpp,執行build and run
<滑鼠移動茶壺範例程式碼>
#include <GL/glut.h>
float x=0, y=0;
void display()
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glPushMatrix();
        glTranslatef( (x-150)/150.0, -(y-150)/150.0, 0 );
        glutSolidTeapot(0.3);
    glPopMatrix();
    glutSwapBuffers();
}
void motion(int nowX, int nowY)
{
    x=nowX; y=nowY;
    display();
}
int main(int argc, char**argv)
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
    glutCreateWindow("week04");
    glutDisplayFunc(display);
    glutMotionFunc(motion);

    glutMainLoop();
}

2018年7月8日 星期日

week14 李宜謙

利用Projection來學會如何使用相機

(1)windows 資料夾內打開 Projection



2. gluLookAt 紅框框為相機的位置(x,y,z)橙框框出為看的中心點位置(x,y,z)黃框處為相機(上)的位置(x,y,z)


gluPerspetive 為透視投影法,參數依序為fovy為y方向視野視角的意思,aspect為畫面長寬比的意思,zNear為前面的透明藍方塊,zFar為後面的灰色背景,調整數值可以調整遠近



了解glut範例程式的每行程式在做什麼

code:
#ifdef __APPLE__
#include <GLUT/glut.h>
#else
#include <GL/glut.h>
#endif

#include <stdlib.h>

static int slices = 16;
static int stacks = 16;

/* GLUT callback Handlers */

static void resize(int width, int height)//今天教的 glutReshapeFunc(你的函式名字)
{
    const float ar = (float) width / (float) height;
    //ar=aspect ratio
    glViewport(0, 0, width, height);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    glFrustum(-ar, ar, -1.0, 1.0, 2.0, 100.0);

    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity() ;
}

static void display(void)
{
    const double t = glutGet(GLUT_ELAPSED_TIME) / 1000.0;
    const double a = t*90.0;

    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glColor3d(1,0,0);

    glPushMatrix();
        glTranslated(-2.4,1.2,-6);
        glRotated(60,1,0,0);
        glRotated(a,0,0,1);
        glutSolidSphere(1,slices,stacks);
    glPopMatrix();

    glPushMatrix();
        glTranslated(0,1.2,-6);
        glRotated(60,1,0,0);
        glRotated(a,0,0,1);
        glutSolidCone(1,1,slices,stacks);
    glPopMatrix();

    glPushMatrix();
        glTranslated(2.4,1.2,-6);
        glRotated(60,1,0,0);
        glRotated(a,0,0,1);
        glutSolidTorus(0.2,0.8,slices,stacks);
    glPopMatrix();

    glPushMatrix();
        glTranslated(-2.4,-1.2,-6);
        glRotated(60,1,0,0);
        glRotated(a,0,0,1);
        glutWireSphere(1,slices,stacks);
    glPopMatrix();

    glPushMatrix();
        glTranslated(0,-1.2,-6);
        glRotated(60,1,0,0);
        glRotated(a,0,0,1);
        glutWireCone(1,1,slices,stacks);
    glPopMatrix();

    glPushMatrix();
        glTranslated(2.4,-1.2,-6);
        glRotated(60,1,0,0);
        glRotated(a,0,0,1);
        glutWireTorus(0.2,0.8,slices,stacks);
    glPopMatrix();

    glutSwapBuffers();
}


static void key(unsigned char key, int x, int y)
{
    switch (key)
    {
        case 27 :
        case 'q':
            exit(0);
            break;

        case '+':
            slices++;
            stacks++;
            break;

        case '-':
            if (slices>3 && stacks>3)
            {
                slices--;
                stacks--;
            }
            break;
    }

    glutPostRedisplay();
}

static void idle(void)
{
    glutPostRedisplay();
}

const GLfloat light_ambient[]  = { 0.0f, 0.0f, 0.0f, 1.0f };
const GLfloat light_diffuse[]  = { 1.0f, 1.0f, 1.0f, 1.0f };
const GLfloat light_specular[] = { 1.0f, 1.0f, 1.0f, 1.0f };
const GLfloat light_position[] = { 2.0f, 5.0f, 5.0f, 0.0f };

const GLfloat mat_ambient[]    = { 0.7f, 0.7f, 0.7f, 1.0f };
const GLfloat mat_diffuse[]    = { 0.8f, 0.8f, 0.8f, 1.0f };
const GLfloat mat_specular[]   = { 1.0f, 1.0f, 1.0f, 1.0f };
const GLfloat high_shininess[] = { 100.0f };

/* Program entry point */

int main(int argc, char *argv[])
{
    glutInit(&argc, argv);
    glutInitWindowSize(640,480);
    glutInitWindowPosition(10,10);
    glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);

    glutCreateWindow("GLUT Shapes");

    glutReshapeFunc(resize);//如果要改攝影機    投影矩陣的設定,在這裡寫 resize(int w,  int h)
    glutDisplayFunc(display);
    glutKeyboardFunc(key);
    glutIdleFunc(idle);

    glClearColor(1,1,1,1);//被景色是白色
    glEnable(GL_CULL_FACE);//可以讓有些面提早決定不要畫
    glCullFace(GL_BACK);//小心容易設定錯誤,可能是 GL_BACK 也可能是 GL_FRONT,要看面的頂點順序

    glEnable(GL_DEPTH_TEST);//3D的深度測試
    glDepthFunc(GL_LESS);

    glEnable(GL_LIGHT0);//打光,Light0 開起來
    glEnable(GL_NORMALIZE);//打光,裡面的法向量 長度 要讓電腦OpenGL重算成1
    glEnable(GL_COLOR_MATERIAL);//打光,material 開起來
    glEnable(GL_LIGHTING);//打光,lighting 開起來

    glLightfv(GL_LIGHT0, GL_AMBIENT,  light_ambient);//打光,把光設定好
    glLightfv(GL_LIGHT0, GL_DIFFUSE,  light_diffuse);
    glLightfv(GL_LIGHT0, GL_SPECULAR, light_specular);
    glLightfv(GL_LIGHT0, GL_POSITION, light_position);

    glMaterialfv(GL_FRONT, GL_AMBIENT,   mat_ambient);//打光,把material設定好
    glMaterialfv(GL_FRONT, GL_DIFFUSE,   mat_diffuse);
    glMaterialfv(GL_FRONT, GL_SPECULAR,  mat_specular);
    glMaterialfv(GL_FRONT, GL_SHININESS, high_shininess);

    glutMainLoop(); //主要迴圈

    return EXIT_SUCCESS;

}

用滑鼠來調整運鏡的角度

1. 新增一個 glut 專案
2. 開頭的地方要記得 #include<math.h> ,因為在後面的motion函式有用到 sin()、cos() 函式,然後在主程式要記得加上 glutMotionFunc(motion);
code:
void motion(int x,int y)
{
    float angle=x*3.1415926/180.0;
    float cameraY=(y-300)/100.0;

    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity() ;
    gluLookAt(5*cos(angle),cameraY,5*sin(angle)-6,     0,0,-6,     0,1,0);
    glutPostRedisplay();//從畫畫面
}

  學會運鏡,我們就可以自己當導演!!



2018年7月6日 星期五

week12 李宜謙

時間暫停!?

如何使用計時器?


函式:glutTimerFunc(1000,timer,t+1); 

第一個參數為幾毫秒,第二個為呼叫哪個函式,第三個為參數

記得要放在 glutCreateWindow("GLUT Shapes"); 之後!!


Code:

void timer(int t)
{
    glutTimerFunc(1000,timer,t+1); //等多久、呼叫誰、參數
    PlaySound("Do.wav",NULL,SND_ASYNC);
}
int main(int argc, char *argv[])
{
    //PlaySound("Do.wav",NULL,SND_ASYNC | SND_LOOP);
    //mymp3.Load("Spring.mp3"); //讀MP3
   // mymp3.Play();// 播放
    glutInit(&argc, argv);
    glutInitWindowSize(640,480);
    glutInitWindowPosition(10,10);
    glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);

    glutCreateWindow("GLUT Shapes");


    glutTimerFunc(5000,timer,0);

利用線性內差讓茶壺每隔一段時間就轉動

Code:

#include<GL/glut.h>
#include<stdio.h>
float angle = 0, oldAngle=0, newAngle=90;//  角度,就角度,新角度
void display()
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glPushMatrix();
        glRotatef(angle,0,0,1); ///會照角度去旋轉
        glutSolidTeapot(0.3);
    glPopMatrix();
    glutSwapBuffers();
}
void timer(int t)
{
    glutTimerFunc(1000,timer,t+1);//等多久,呼叫誰,參數
    float alpha = (t)/30.0;// 算出一個內插的alpha值
    angle = newAngle*alpha+oldAngle*(1-alpha);//角度去內插
    glutPostRedisplay();//照著上面的角度重畫
}
int main(int argc, char *argv[])
{
    //PlaySound("Do.wav",NULL,SND_ASYNC | SND_LOOP);
    //mymp3.Load("Spring.mp3"); //讀MP3
   // mymp3.Play();// 播放
    glutInit(&argc, argv);
    glutInitWindowSize(640,480);
    glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);

    glutCreateWindow("GLUT Shapes");

    glutTimerFunc(0,timer,0);//有個計時器 timer
    glutDisplayFunc(display);
    glutMainLoop();
}



week07 李宜謙

使用Light Material來看3D模型的打光

(1) 下載 data.zip, windows.zip, glut32.dll 三個,壓縮後將 glut32.dll 複製到windows資料夾中

(2)左上方 Screen-space view 按右鍵可以更改模型    左下方 World-space view  按右鍵可以更改模型的材質


右方一樣能調整數值,pos為光源位置的設置,參數代表光源種類,1是點光源,0是平行光,ka 的 a 代表 Ambient 無所不在的光,kd 的 d 代表 Diffuse 擴散  , s  代表 Specular 鏡面


阿兩又要買模型??

(1)開新專案,下載source.zip,將glm.c跟glm.h放到專案裡(glm.c記得改成cpp檔)


(2)將glm.cpp匯入專案內



記得要在外部 #include"glm.h" 還有宣告一個空指標 GLMmodel *pmodel=NULL;


接下來我們要利用 source 資料夾內的 transformation.c 檔案,我們要從裡面拿到讀取模型和畫模型的程式碼(搜尋第三個glm並複製程式碼,加到我們的程式碼中)


 if (!pmodel) {

pmodel = glmReadOBJ("data/porsche.obj");///模型的位置路徑

if (!pmodel) exit(0);

glmUnitize(pmodel);

glmFacetNormals(pmodel);

glmVertexNormals(pmodel, 90.0);

}

    glmDraw(pmodel, GLM_SMOOTH | GLM_MATERIAL);


freeglut中沒有porsche的檔案,所以我們要把data資料夾移到freeglut裡的bin資料夾中,這樣才找得到檔案!








2018年7月3日 星期二

week06 李宜謙

用 Code::Blocks 跑 Transformation 的原始碼

(1) 下載 source.zip (2) 用 notepad++ 打開 transformation.c 檔案 然後複製裡面全部的程式碼

(3) 將剛剛複製的程式碼全部貼上


(4) 將 source 裡的 glm.c 改名為 glm.cpp 讓他變成 c++ 檔案


(5) 將 glm.cpp 和 glm.h 複製到我們的 glut 專案的資料夾內


(6) 將 glm.cpp 匯入專案內




(7) 將之前下載過的 data 資料夾複製到 freeglut -> bin 資料夾內


(8) 成功跑起來跑起來!!




將所有的東西都彙整在同個專案裡

(1) 複製 freeglut -> bin 裡的 data 資料夾 和 freeglut.dll,並將他們在我們剛剛新增的 glut 專案資料夾內貼上

(2) 用 notepad++ 開啟 Code::Blocks 的專案檔(副檔名為 .cbp的),將 working_dir 改為 ".","." 代表現在專案的執行目錄


執行專案後,成功將執行目錄改在我的專案目錄裡了!!







week04 李宜謙

使用 Transformation

(1) 準備 data.zip, windows.zip, glut32.dll 三個檔案

(2) 將 glut32.dll、data資料夾複製到 windows 資料夾內


(3) 打開 Transformation,在 Screen-space view 按右鍵可以切換各種模型



(4) glTranslatef(x,y,z) 可以調整物件在x,y,z軸的位置(在數值上按住滑鼠左鍵)

(5) glRotatef(angle,x,y,z) 可以旋轉物件,第一個參數(angle)代表旋轉的角度後面三個(x,y,z)分別代表x,y,z軸的軸向量,選轉方向依照右手定則。軸線並用右手握住這條軸線,然後大母指指向的方向為由(0,0,0) 到 (x,y,z) 的方向,而四指緊握軸線代表旋轉的方向

(6) glScalef(x,y,z) 可以調整物件的大小


滑鼠移動和旋轉茶壺


將以下的程式碼貼入main.cpp,成果如下(滑鼠左鍵為旋轉,右鍵為移動位置)


#include <stdio.h>
#include <GL/glut.h> 
float teapotX=0, teapotY=0, angle=0; //茶壺的座標,rotate旋轉要的angle
int oldX=0, oldY=0, nowRotate=0; //紀錄mouse在哪裡按下去,oldX, oldY 了解你走多少地方
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(); 
}
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) //mouse motion 事件
{
if(nowRotate==1) angle += (x-oldX); //滑鼠的座標,就是璇轉的角度,加上新的增加量    
else
{        
teapotX += (x-oldX)/150.0;//依照motion時的x來改teapot的座標,加上新的增加量
teapotY += (oldY-y)/150.0;//依照motion時的y來改teapot的座標,加上新的增加量
}    
oldX = x;       oldY = y;  //紀錄移動後新的位置
glutPostRedisplay(); //重畫畫面
}
int main(int argc, char** argv)
{    
    glutInit(&argc,argv);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
    glutCreateWindow("Week04 Mouse");
    glutDisplayFunc(display);    
    glutMouseFunc(mouse);    
    glutMotionFunc(motion);             
    glutMainLoop();
}