顯示具有 09160943_黃昌勝 標籤的文章。 顯示所有文章
顯示具有 09160943_黃昌勝 標籤的文章。 顯示所有文章

2022年6月14日 星期二

電腦圖學作業_week_17

 1.講解期末報告評分、該如何製作

2.程式碼:
#include <GL/glut.h>
 void display()
 {
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glColor3f(1,1,0);
    glutSolidTeapot(0.3);

    glutSwapBuffers();
 }
 int main(int argc, char *argv[])//main()主函式 進階版
 {
    glutInit(&argc,argv);//把參數送給glutInit初始化
    glutInitDisplayMode(GLUT_DOUBLE|GLUT_DEPTH);//雙緩衝區+3D深度功能
    glutCreateWindow("第02週的程式喔!!");//開GLUT視窗

    glutDisplayFunc(display);//顯示用的函式

    glutMainLoop();
 }


打光:

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 };
函示宣告
 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);

2022年6月7日 星期二

電腦圖學作業_week16

 Step01

    1.利用Excel來把alpha內插公式實作出來, 看到新的、舊的資料可以做內插

        -alpha 內插公式:alpha: 0.0~1.0

        -angle = alpha*新 + (1-alpha)*舊

    2.拿 week15_angles_TRT_again 來改

        -建立新的 GLUT 檔案,檔名:week16_interpolation

Step02

    1.講解上一週作業

    2.利用課本的範例,講解gluLookAt(眼睛座標、看哪裡、up向量)

        -開啟在老師網頁下載的 windows/data/Projection.exe



Step03

    1.建立新的 GLUT 檔案,檔名:week16_camera_projection_GlutLookat

2022年5月31日 星期二

電腦圖學作業week_15

 Step01

    1.PlaySound() 解釋

        -新增檔案,檔名:week15-1_playsound.cpp

        -上週使用 #include<mmsystem.h>

        -這週使用#include<windows.h>

        -上週playsound("檔名.wav",NULL,SND_ASYNC);//不等待,不同步

        -這週PlaySound("檔名.wav",NULL,SND_SYNC);//等待,同步

        -setting-compiler>>setting-linker加入winmm(可以撥聲音)

        -將下載下來的聲音檔放在 week15_playsound.cpp 存放的位置(wav檔)

    2.PlaySound() 更多參數

        -新增檔案,檔名:week15-2_SND_SYNC_SND_ASYNC

        -PlaySound("檔名.wav",NULL,SND_SYNC);=>加上變ASYNC就會不播音樂直接結束

        -加入變數 N,輸入多少就有相對應的聲音

Step02

    1.MP3(小/有壓縮) vs WAV(大/原始檔)

        -新增檔案,檔名:week15-3_CMP3.cpp

        -Moodle 下載 CMP3_MCI.h,加入week15-3 的檔案目錄中

        -程式碼 #include<CMP3_MCI.h>

        -宣告 CMP3_MCI mp3

        -mp3.Load("檔名.mp3");

        -mp3.play();

    2.接續上周的week14_angles_TRT_write_and_read

        -新增 GLUT 專案,檔名:week15_angles_TRT_again

        -複製上週程式碼,並試跑看看能否正確執行

        -發現問題:不像動畫,只會動一個關節,而且很慢

        -解決後可以在 freeglut/bin 裡面看到一個 file.txt 裡面紀錄手臂的動作過程

Step03

    1.讓機器人搖擺

        -需要3D Model (glm.h,glm.cpp .obj....)

        -需要切割模型分別讀入

        -使用TRT程式轉動分割後的模型

        -keboard() 切換關節, mouse motion() 用滑鼠轉動關節

        -新增 GLUT 專案,檔名:week15_hw_gundam_parts

        -用 Notepad++ 將工作目錄的路徑改成 "."

        -將 freeglut 資料夾內的 freeglut.dill 檔按複製到 week15_hw_gundam_parts內

        -把glm.h跟glm.c複製到week15_hw_gundam_parts內

2022年5月24日 星期二

電腦圖學作業_week14

 1-1.開 CodeBlocks/empty file-->week14-1.cpp

1-2.寫程式
#include<stdio.h>
int main(){指標fout開檔案(檔名,write模式)
    FILE*fout=fopen("file.txt,","w+");
        printf("Hello World\n");
    fprintf(fout,"Hello World\n");
    fclose(fout);關檔案
}
1-3.
#include<stdio.h>
int main(){
    FILE*fout=fopen("file.txt,","w+");
    fprintf(fout,"3.1415926\n");
    fclose(fout);

    float angle=0;
    FILE*fin=fopen("file.txt","r");
    fscanf(fin,"%f",&angle);
    printf("讀到角度:%f",angle);
    fclose(fin);
}
1-4.開上週程式改

#include <GL/glut.h>
#include <stdio.h>

float angle[20], oldX=0;
int angleID=0;
FILE * fout =NULL;
void myWrite(){
    if(fout==NULL)
        fout=fopen("file.txt,","w+");

    for(int i=0;i<20;i++){
        printf("%.1f",angle[i]);
    }
}
void keyboard(unsigned char key,int x,int y)
{
    if(key=='0') angleID=0;
    if(key=='1') angleID=1;
    if(key=='2') angleID=2;
    if(key=='3') angleID=3;
}
void mouse(int button,int state,int x,int y)
{
    oldX=x;
}
void motion(int x,int y)
{
    angle[angleID]+=(x-oldX);
    myWrite();
    oldX=x;
    glutPostRedisplay();
}



2

2-1.
#include <GL/glut.h>
#include <stdio.h>

float angle[20], oldX=0;
int angleID=0;
FILE * fout =NULL;
void myWrite(){
    if(fout==NULL)
        fout=fopen("file.txt,","w+");

    for(int i=0;i<20;i++){
        printf( " %.1f ",angle[i]);
        fprintf(fout,"  %.1f  ",angle[i]);
    }
    printf("\n");
    fprintf(fout,"\n");
}
void keyboard(unsigned char key,int x,int y)
{
    if(key=='0') angleID=0;
    if(key=='1') angleID=1;
    if(key=='2') angleID=2;
    if(key=='3') angleID=3;
}
void mouse(int button,int state,int x,int y)
{
    oldX=x;
}
void motion(int x,int y)
{
    angle[angleID]+=(x-oldX);
    myWrite();
    oldX=x;
    glutPostRedisplay();
}



2-2.
#include <GL/glut.h>
#include <stdio.h>

float angle[20], oldX=0;
int angleID=0;///關節ID
FILE * fout =NULL,*fin=NULL;
void myWrite(){
    if(fout==NULL)
        fout=fopen("file.txt,","w+");

    for(int i=0;i<20;i++){
        printf("%.1f",angle[i]);
        fprintf(fout," %.1f ",angle[i]);
    }f後面要加空格
    printf("\n");
    fprintf(fout,"\n");
}

void myRead(){
    if(fout!=NULL){fclose(fout);fout=NULL;}
    if(fin==NULL)fin=fopen("file.txt,","r");
    for(int i=0;i<20;i++){
        fscanf(fin,"%f",&angle[i]);
    }
    glutPostRedisplay();
}

void keyboard(unsigned char key,int x,int y)
{
    if(key=='r') myRead();
    if(key=='0') angleID=0;
    if(key=='1') angleID=1;
    if(key=='2') angleID=2;
    if(key=='3') angleID=3;
}
void mouse(int button,int state,int x,int y)
{
    oldX=x;
}
void motion(int x,int y)
{
    angle[angleID]+=(x-oldX);
    myWrite();
    oldX=x;
    glutPostRedisplay();
}
長按r就會顯示錄到的動作


2-3.

    (1)用notepad++ 開 cbp檔 ---> 把working_dir 裡的路徑改成"."
    (2)把freeglut裡的freeglut.dll複製到專案中

這樣txt就會放到程式專案目錄了

3

3-1.開新專案week14_timer
3-2.
#include <GL/glut.h>
#include <stdio.h>

void timer(int t){
    printf("起床,現在時間 : %d\n",t);
}
void display(){
}
int main(int argc,char**argv)
{
    glutInit(&argc,argv);
    glutInitDisplayMode(GLUT_DOUBLE|GLUT_DEPTH);
    glutCreateWindow("Week14 timer");

    glutTimerFunc(1000,timer,1);
    glutTimerFunc(2000,timer,2);
    glutTimerFunc(3000,timer,3);
    glutTimerFunc(4000,timer,4);
    glutTimerFunc(5000,timer,5);

    glutDisplayFunc(display);
    glutMainLoop();
}

3-3.
#include <GL/glut.h>
#include <stdio.h>

void timer(int t){
    printf("起床,現在時間 : %d\n",t);
    glutTimerFunc(1000,timer,t+1);
}
void display(){
}
int main(int argc,char**argv)
{
    glutInit(&argc,argv);
    glutInitDisplayMode(GLUT_DOUBLE|GLUT_DEPTH);
    glutCreateWindow("Week14 timer");
    
     glutTimerFunc(1000,timer,1);
    glutTimerFunc(2000,timer,2);
    glutTimerFunc(3000,timer,3);
    glutTimerFunc(4000,timer,4);
    glutTimerFunc(5000,timer,5);

    glutTimerFunc(5000,timer,0);
    glutDisplayFunc(display);
    glutMainLoop();
}

3-4.
#include <GL/glut.h>
#include <stdio.h>
#include <mmsystem.h>

void timer(int t){
    printf("起床,現在時間 : %d\n",t);
    PlaySound("do.wav",NULL,SND_ASYNC);
    glutTimerFunc(1000,timer,t+1);
}
void display(){
}
int main(int argc,char**argv)
{
    glutInit(&argc,argv);
    glutInitDisplayMode(GLUT_DOUBLE|GLUT_DEPTH);
    glutCreateWindow("Week14 timer");

    glutTimerFunc(5000,timer,0);
    glutDisplayFunc(display);
    glutMainLoop();
}
會有聲音

2022年5月17日 星期二

電腦圖學作業_week13

先畫長方形


用glRectf加上座標來畫出長方形

#include <GL/glut.h>
void display()
{
     glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
     glRectf(0.3,0.5,-0.3,-0.5);
     glutSwapBuffers();
}
int main(int argc,char**argv)
{
    glutInit(&argc,argv);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
    glutInitWindowSize(600,600);
    glutCreateWindow("week13 rect");
    glutDisplayFunc(display);
    glutMainLoop();
}

把手臂的旋轉中心放在正中心

手臂位置跑到中心點

加上rotate讓手臂旋轉

再加上Translatef讓手臂回到原來的位置
#include <GL/glut.h>
float angle=45;
void display()
{
     glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
     glColor3f(1,1,1);
     glRectf(0.3,0.5,-0.3,-0.5);
     glPushMatrix();
        glTranslatef(0.3,0.4,0);
        glRotatef(angle,0,0,1);
        glTranslatef(-0.3,-0.4,0);
        glColor3f(1,0,0);
        glRectf(0.3,0.5,0.7,0.3);
     glPopMatrix();
     glutSwapBuffers();
}
int main(int argc,char**argv)
{
    glutInit(&argc,argv);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
    ///glutInitWindowSize(600,600);
    glutCreateWindow("week13 rect");
    glutDisplayFunc(display);
    glutMainLoop();
}

我們加上mouse motion
讓手臂能在滑鼠按住時跟著轉動
然後我們要畫第二個關節


如果要畫左手臂把X座標的正負顛倒就可以了
#include <GL/glut.h>
float angle=0,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;
    glutPostRedisplay();
}
void display()
{
     glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
     glColor3f(1,1,1);
     glRectf(0.3,0.5,-0.3,-0.5);
     glPushMatrix();
        glTranslatef(0.3,0.4,0);
        glRotatef(angle,0,0,1);
        glTranslatef(-0.3,-0.4,0);
        glColor3f(1,0,0);
        glRectf(0.3,0.5,0.7,0.3);


        glPushMatrix();
            glTranslatef(0.7,0.4,0);
            glRotatef(angle,0,0,1);
            glTranslatef(-0.7,-0.4,0);
            glColor3f(0,1,0);
            glRectf(0.7,0.5,1.0,0.3);
        glPopMatrix();
     glPopMatrix();
     glPushMatrix();
        glTranslatef(-0.3,0.4,0);
        glRotatef(angle,0,0,1);
        glTranslatef(+0.3,-0.4,0);
        glColor3f(1,0,0);
        glRectf(-0.3,0.5,-0.7,0.3);


        glPushMatrix();
            glTranslatef(-0.7,0.4,0);
            glRotatef(angle,0,0,1);
            glTranslatef(+0.7,-0.4,0);
            glColor3f(0,1,0);
            glRectf(-0.7,0.5,-1.0,0.3);
        glPopMatrix();
     glPopMatrix();
     glutSwapBuffers();
}
int main(int argc,char**argv)
{
    glutInit(&argc,argv);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
    ///glutInitWindowSize(600,600);
    glutCreateWindow("week13 rect new");
    glutMouseFunc(mouse);
    glutMotionFunc(motion);
    glutDisplayFunc(display);
    glutMainLoop();
}

我們可以用keyboard函式當按下鍵盤就可以旋轉對應的關節

#include <GL/glut.h>
float angle[20],oldX=0;
int angleID=0;
void keyboard(unsigned char key,int x, int y)
{
    if(key=='0')angleID=0;
    if(key=='1')angleID=1;
    if(key=='2')angleID=2;
    if(key=='3')angleID=3;
}
void mouse(int button,int state,int x,int y )
{
    oldX=x;
}
void motion(int x,int y)
{
    angle[angleID] += (x-oldX);
    oldX=x;
    glutPostRedisplay();
}
void display()
{
     glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
     glColor3f(1,1,1);
     glRectf(0.3,0.5,-0.3,-0.5);
     glPushMatrix();
        glTranslatef(0.3,0.4,0);
        glRotatef(angle[0],0,0,1);
        glTranslatef(-0.3,-0.4,0);
        glColor3f(1,0,0);
        glRectf(0.3,0.5,0.7,0.3);


        glPushMatrix();
            glTranslatef(0.7,0.4,0);
            glRotatef(angle[1],0,0,1);
            glTranslatef(-0.7,-0.4,0);
            glColor3f(0,1,0);
            glRectf(0.7,0.5,1.0,0.3);
        glPopMatrix();
     glPopMatrix();
     glPushMatrix();
        glTranslatef(-0.3,0.4,0);
        glRotatef(angle[2],0,0,1);
        glTranslatef(+0.3,-0.4,0);
        glColor3f(1,0,0);
        glRectf(-0.3,0.5,-0.7,0.3);


        glPushMatrix();
            glTranslatef(-0.7,0.4,0);
            glRotatef(angle[3],0,0,1);
            glTranslatef(+0.7,-0.4,0);
            glColor3f(0,1,0);
            glRectf(-0.7,0.5,-1.0,0.3);
        glPopMatrix();
     glPopMatrix();
     glutSwapBuffers();
}
int main(int argc,char**argv)
{
    glutInit(&argc,argv);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
    ///glutInitWindowSize(600,600);
    glutCreateWindow("week13 rect new");
    glutKeyboardFunc(keyboard);
    glutMouseFunc(mouse);
    glutMotionFunc(motion);
    glutDisplayFunc(display);
    glutMainLoop();
}

2022年5月10日 星期二

電腦圖學作業_week12

1. 在下方Command manipulation window視窗右鍵可以swap 移動(translate)跟轉動(rotate)。

2.自轉


3.公轉


1.在 https://jsyeh.org/3dcg10,下載data跟win32

2.解壓縮後,把data放到windows裡

3.開Transformation.exe



#include <GL/glut.h>
float angle=0; void display()
{
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glColor3f(1,1,1);
glutSolidTeapot( 0.3 );///白身體
glPushMatrix();
glTranslatef(0.4,0.15,0);///3.把它往右放
glRotatef(angle,0,0,1);///2.旋轉
glTranslatef(0.2,0,0);///1.旋轉中心放到世界中心,希望 手茶壺的中心旋轉軸 在把手位置,因大小是0.2,才往右0.2
glColor3f(1,0,0);
glutSolidTeapot( 0.2 );///紅身體
glPopMatrix();
glutSwapBuffers();
angle+=0.1;
} int main(int argc,char ** argv)
{
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_DOUBLE|GLUT_DEPTH);
glutCreateWindow("week12");
glutIdleFunc(display);
glutDisplayFunc(display);
glutMainLoop();
}

VERY BEAUTIFUL, VERY POWERFUL

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