顯示具有 05161216_焦奕維 標籤的文章。 顯示所有文章
顯示具有 05161216_焦奕維 標籤的文章。 顯示所有文章

2018年6月4日 星期一

week15_焦焦焦焦焦焦焦

主題 : 複習之前教過的

week1 :

(1)webGL
(2)跑OpenGL專案

week2 :

(1)GLUT專案
(2)將177行程式碼縮減成17行程式碼
(3)新增一個茶壺

week3 :

移動、旋轉、縮放
跑Transformation.exe
glutSolidTeapot(半徑)
glutSolidSphere(半徑、切、切)
glutSolidCone(半徑、切、切)

week4 :

Rotate , 矩陣

week5 :

T-R-T 特定軸

week6 :

glm 3D模型
#include"glm.h"
GLM*pmodel = NULL;
畫圖 ;
if(pmodel==NULL)
{
     glmReadOBJ("  ")
glmDraw(pmodel) ;

*小心舊的strdup()會不相容

week7 :

小心CodeBlocks專案與Setting-Compiler要設對,glm.cpp才行Compile
working_dir 設成 "."

week8 :

打光、陣列

week10 :

貼圖 : openCV
(1)安裝OpenCV
(2)設目錄link
(3)秀圖

week11 :

keyboard 配 PlaySound() 變鍵盤鋼琴
mouse 配 PlaySound() 變射擊遊戲

CMP3_MCI.h 可播 MP3

2018年5月28日 星期一

week14_焦焦焦焦焦焦焦

主題 : 攝影機、投影矩陣

步驟(1) :

到 jsyeh/3dcg10 下載

windows.zip 下載 \ windows \ Projection.exe
data.zip 下載 \ windows \ data \ 一堆模型
glut32.dll 下載 \ windows \ glut32.dll



將data資料夾拉進windows資料夾


glut32.dll也拉進windows資料夾


點開Projection :


fovy : 張開的角度
aspect : 長寬比例
zNear : 近的裁一刀
zFar : 遠的裁一刀


#include <GL/glut.h>
#include <stdio.h>
FILE * fout=NULL;     ///(0)宣告檔案的指標,一開始是空的
float angle=60, angle2=90, angle3=60;      ///準備3個角度的值
void keyboard(unsigned char key, int x, int y)
{
    if(key=='s' || key=='S' || key=='w' || key=='W'){
        if(fout==NULL) fout=fopen("output.txt", "w+");
        ///上面這行,只有第一次按s鍵,才會還沒有值,才fopen
        ///因為第二次按s鍵,fout名花有主,不會再重覆開檔案

        printf("%.3f %.3f %.3f\n", angle,angle2,angle3);
        fprintf(fout, "%.3f %.3f %.3f\n", angle,angle2,angle3);
    }///3個角度的值,印到檔案去
}
///打光的陣列之前全部刪掉, 陣列不要刪
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 */
void display()
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    ///Now: 要畫東西
    glPushMatrix();///Now:像大括號一樣,把裡面的移動/旋轉,控制在裡面,不要影響到外面
        glutSolidTeapot(0.3);///Now:身體

        glPushMatrix();  ///Now:像大括號一樣,把裡面的移動/旋轉,控制在裡面,不要影響到外面
            glTranslatef(0.4, 0, 0);  ///Now:(3) 掛在右邊
            glRotatef(angle, 0,0,1);  ///Now:(2) angle的轉動角度,對Z軸
            glTranslatef(0.4, 0, 0);  ///Now:(1) 往右移,讓手把在中間
            glutSolidTeapot(0.3);  ///Now:右手臂
        glPopMatrix();  ///Now:像大括號一樣,把裡面的移動/旋轉,控制在裡面,不要影響到外面
    glPopMatrix();  ///Now:像大括號一樣,把裡面的移動/旋轉,控制在裡面,不要影響到外面
    glutSwapBuffers();
}
void motion(int x, int y)///Now:
{///Now:
    angle = x;///Now: 角度會隨mouse motion而改值
    glutPostRedisplay();///Now:
}///Now:

static void resize(int width, int height)
{
    const float ar = (float) width / (float) height;///aspect rotio 長寬比

    glViewport(0, 0, width, height);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();

    glFrustum(-ar, ar, -1.0, 1.0, 2.0, 100.0);

    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity() ;
    gluLookAt(0,3,1,  0,0,0,  0,1,0);
}

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);
    glutDisplayFunc(display);
    glutMotionFunc(motion);///Now: 加上motion事件

    glutKeyboardFunc(keyboard);
    ///glutKeyboardFunc(key);
    ///glutIdleFunc(idle);

    glClearColor(1,1,1,1);
    ///glEnable(GL_CULL_FACE);
    ///glCullFace(GL_BACK);

    glEnable(GL_DEPTH_TEST);
    glDepthFunc(GL_LESS);

    glEnable(GL_LIGHT0);
    glEnable(GL_NORMALIZE);
    glEnable(GL_COLOR_MATERIAL);
    glEnable(GL_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);
    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;
}




2018年5月21日 星期一

week13_焦焦焦焦焦焦焦

主題(1) : 寫檔、讀檔

步驟 (A) :

(1) File-New-Empty File (Ctrl-Shift-N)
     另存新檔(File-SaveAs) file.cpp



(2)

#include<stdio.h>
int main()
{
    FILE *  fout=fopen("output.txt", "w+");
    printf("Hello world\n");
   fprintf(fout,"Hello world\n");
}


步驟 (B) :

#include<stdio.h>
float angle=0, angle2=10, angle3=90;
int main()
{
    FILE *  fout=fopen("output.txt", "w+");
   fprintf(fout, "%.3f %.3f %.3f\n", angle, angle2, angle3); 
}



主題(2) : 控制關節

程式碼

#include <GL/glut.h>
#include <stdio.h>
FILE * fout=NULL;
float angle=0;
int oldX=0;
void mouse(int button, int state, int x, int y)
{
    oldX=x;
}
void motion(int x, int y)
{
    angle += x-oldX;
    oldX=x;
    if(fout==NULL){
        fout = fopen("output.txt", "w+");
    }
    fprintf(fout, "%3f\n", angle);
    printf("%.3f\n", angle);
    glutPostRedisplay();
}
void display()
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glPushMatrix();
        glutSolidTeapot(0.3);
        glPushMatrix();
            glTranslatef(0.4, 0, 0);
            glRotatef(angle, 0, 0, 1);
            glTranslatef(0.4, 0, 0);
            glutSolidTeapot(0.3);
        glPopMatrix();
    glPopMatrix();
    glutSwapBuffers();
}



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");

    glutMouseFunc(mouse);
    glutMotionFunc(motion);

    ///glutReshapeFunc(resize);
    glutDisplayFunc(display);
    ///glutKeyboardFunc(key);
    ///glutIdleFunc(idle);

    glClearColor(1,1,1,1);
    ///glEnable(GL_CULL_FACE);
    ///glCullFace(GL_BACK);

    glEnable(GL_DEPTH_TEST);
    glDepthFunc(GL_LESS);

    glEnable(GL_LIGHT0);
    glEnable(GL_NORMALIZE);
    glEnable(GL_COLOR_MATERIAL);
    glEnable(GL_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);
    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;
}

加入一個mouse

#include <GL/glut.h>
#include <stdio.h>
FILE * fout=NULL;
float angle=0;
int oldX=0;
void mouse(int button, int state, int x, int y)
{
    oldX=x;
}
void motion(int x, int y)
{
    angle += x-oldX;
    oldX=x;
    if(fout==NULL){
        fout = fopen("output.txt", "w+");
    }
    fprintf(fout, "%3f\n", angle);
    printf("%.3f\n", angle);
    glutPostRedisplay();
}

程式碼解說(1) :



程式碼解說(2) :

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");

    glutMouseFunc(mouse);
    glutMotionFunc(motion);

    ///glutReshapeFunc(resize);
    glutDisplayFunc(display);
    ///glutKeyboardFunc(key);
    ///glutIdleFunc(idle);

    glClearColor(1,1,1,1);
    ///glEnable(GL_CULL_FACE);
    ///glCullFace(GL_BACK);



執行 :



加入一個keyboard

再加程式碼

#include <GL/glut.h>
#include <stdio.h>
FILE * fout=NULL;
FILE * fin=NULL;
float angle=0;
int oldX=0;
void keyboard(unsigned char key, int x,int y)
{
    if(fin==NULL){
        fin=fopen("output.txt", "r");
    }
    fscanf(fin, "%f", &angle);
    glutPostRedisplay();
}
void mouse(int button, int state, int x, int y)
{
    oldX=x;
}
void motion(int x, int y)
{
    angle += x-oldX;
    oldX=x;
    if(fout==NULL){
        fout = fopen("output.txt", "w+");
    }
    fprintf(fout, "%3f\n", angle);
    printf("%.3f\n", angle);
    glutPostRedisplay();
}

程式碼解說(1) :



程式碼解說(2) :

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");

    glutMouseFunc(mouse);
    glutMotionFunc(motion);
    glutKeyboardFunc(keyboard);

    ///glutReshapeFunc(resize);
    glutDisplayFunc(display);
    ///glutKeyboardFunc(key);
    ///glutIdleFunc(idle);

    glClearColor(1,1,1,1);
    ///glEnable(GL_CULL_FACE);
    ///glCullFace(GL_BACK);

2018年5月14日 星期一

week12_焦焦焦焦焦焦焦

主題 : 複習上週PlaySound()進度

(1) File-New-Project , GLUT專案(做法跟之前一樣)



(2)再來將lesson32的data資料夾裡的Shot.wav拉到freeglut裡的bin資料夾



(3)再去Week12資料夾更改week12_mp3.cbp


(4)將裡面程式碼"freeglut/bin"資料夾"改成"."




(5)再把freeglut/bin的freeglut.dll和Shot.wav拉到week12資料夾


(6)最後在main函式加上程式碼 :

#include <mmsystem.h>
int main(int argc, char *argv[])
{
    PlaySound("Shot.wav", NULL, SND_ASYNC);


2018年5月7日 星期一

week11_焦焦焦焦焦焦焦

課堂作業(1) :

(A)先去wav download下載聲音
(B)下載完放進freeglut\bin

(C)到CodeBlocks
開啟OpenGL新增一個專案
(做法跟之前一樣)

找到main函式
上面呼叫外掛,下面多一行程式碼

#include <mmsystem.h>
int main(int argc, char *argv[])
{
    PlaySoundA("聲音.wav", NULL, SND_ASYNC);


課堂作業(2) :

建立一個新專案
這次要用Console application(貝殼專案)

打上程式碼 :

到Link加上winmm :



課堂作業(3) :
做鍵盤鋼琴
先將week11_music變黑

然後找到Keyboard加上程式碼 :

2018年4月30日 星期一

week10_焦焦焦焦焦焦焦

課堂(1) :
到jsyeh.org/3dcg10下載
windows.zip 下載  \windows\Texture.exe
data.zip 下載  \windows\fishermen.sgi
glut32.dll 下載  \windows\glut32.dll

註解:
1.到jsyeh/3dcg10下載win32,data,glut32
之後將win32解壓縮後把data跟glut32放進去在開啟texture
2.觀察texcoord跟verTex對應的關係



簡單的讀圖秀圖 :


先呼叫Opencv的外掛#include <opencv/highgui.h>
接著用3行把圖檔讀進來

課堂(2) :
moodle下載myEarth.zip
把地球讀近來 先改檔案
接著把freeglut.dll 跟圖檔都放進去(注意地球的圖需平面)



2018年4月16日 星期一

week08_焦焦焦焦焦焦焦

課堂(1) :

到 jsyeh.org/3dcg10 下載 :

windows.zip 下載  \ windows \ Light Materials.exe
data.zip 下載  \ windows \ data \ 模型
glut32.dll 下載  \ windows \ glut32.dll
雸轉 :

source.zip 等一下要用glm.h (glm.c改glim.cpp)

讓3D模型旋轉 :


打光 :
(1)打光的範例  Light Material.exe
(2)CodeBlock. File - New Project - GLUT
(3)把上週3D Model . (glm.h(glm.c改glm.cpp)
放到 :
     (A)#include "glm.h"
     (B)GLMmodel * pmodel = NULL;
     (C)畫模型



(4)打光自己來 - 留下 :
     (A)陣列 ...[ ] = {...};
                   ...[ ] = {...};
                   ...[ ] = {...};
                   ...[ ] = {...};
                   ...[ ] = {...};
                   ...[ ] = {...};
                   ...[ ] = {...};
                   ...[ ] = {...};
                   ...[ ] = {...};
     (B)在main()打光設定


             


2018年4月9日 星期一

week07_焦焦焦焦焦焦焦

課堂(1) :
moodle下載 3D Exploration 探索/檔案總管
安裝執行


課堂(2) :
下載 jsyeh.org/3dcg10
data.zip 移到 桌面/data/一堆3D模型
(用3D Explor 開3D模型)


課堂(3) :
自己讀入3D模型
source.zip有程式碼
(1) glm.h
(2) glm.c(改.cpp)
(3) Transformation.c(改.cpp)
先去CodeBlocks開一個專案(做法跟之前一樣)
都要放進桌面上的week07_glm資料夾再改檔名


回到CodeBlocks加入檔案 :




再去Setting裡的Compiler把勾勾拿掉


再將data資料夾放入freeglut資料夾裡的bin資料夾 :


回到CodeBlocks執行即可 :


自己做出3D模型的車子 :

(1)在main函式寫程式

#include "glm.h"
GLMmodel * pmodel=NULL;

void display()
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    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);

    glutSwapBuffers();
}
int main(int argc, char**argv)
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
    glutCreateWindow("Week07 焦奕維做出的3D模型");

    glutDisplayFunc(display);
    glutMainLoop();
}



程式碼解說 :

(2)再到transformation函式把全部程式碼刪掉(因為已經拉去main函式了)

(3)最後執行即可 :