顯示具有 09160393_黃暐豪 標籤的文章。 顯示所有文章
顯示具有 09160393_黃暐豪 標籤的文章。 顯示所有文章

2022年6月14日 星期二

豪哥的電腦圖學筆記week17

 豪哥的電腦圖學筆記week17

step02-1

1.working dir 路徑改成 "." 然後桌面freeglut資料夾複製freeglut.dll進專案資料夾

茶壺打光


#include <GL/glut.h>
#include <stdio.h>
void display(){
    glClearColor(1,1,0,0);
    glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);

    glColor3f(1,0,0);
    glutSolidTeapot(0.3);
    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 };
void myLight(){
    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);
}
int main(int argc,char**argv)
{
    glutInit(&argc,argv);
    glutInitDisplayMode(GLUT_DOUBLE|GLUT_DEPTH);
    glutCreateWindow("week17 demo");

    glutDisplayFunc(display);
    myLight();

    glutMainLoop();
}



2022年6月7日 星期二

豪哥圖學筆記week16

week16筆記

step01-1

alpha內插公式  alpha '0.0~1.0'

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

alpha:0 > 舊

alpha:0.5 > 半新半舊

alpha:1 > 新

可以用excel來實作


step01-2

用week15_angles_TRT_again 拿來改

1.File new project week16_interpolation 內插

2.按s存一行,按r動作不連續


///week16_interpolation

///改編自week15_angles_TRT_again

#include <GL/glut.h>

#include <stdio.h>

float angle[20],oldX=0;

int angleID=0;

FILE * fout=NULL, * fin = NULL;

void myWrite()

{///每呼叫一次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]);///檔案印出來

    }///這裡老師沒有fclose

    printf("\n");///小黑印出跳行

    fprintf(fout,"\n");///檔案跳行

}

float NewAngle[20],OldAngle[20];

void myRead()

{

    if(fout!=NULL){fclose(fout); fout=NULL;}

    if(fin==NULL) fin=fopen("file.txt","r");

    for(int i=0;i<20;i++)

    {

        OldAngle[i] = NewAngle[i];

        fscanf(fin, "%f", &NewAngle[i] );

        ///fscanf(fin,"%f",&angle[i]);

    }

    glutPostRedisplay();///重劃畫面

}

void myInterpolate(float alpha){

    for(int i=0;i<20;i++){

        angle[i]= alpha * NewAngle[i] + (1-alpha) * OldAngle[i];

    }

}

int t=0;

void keyboard(unsigned char key,int x,int y)

{

    if(key=='p'){

        if(t%30==0) myRead();

        myInterpolate( (t%30)/30.0 );///介於0.0~1.0

        glutPostRedisplay();

        t++;

    }

    if(key=='s') myWrite();///調好動作才save存檔

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

}

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

    glutCreateWindow("week15_angles_TRT_again");

    glutMouseFunc(mouse);

    glutMotionFunc(motion);

    glutKeyboardFunc(keyboard);

    glutDisplayFunc(display);


    glutMainLoop();

 }



step02-1


///week16_interpolation
///改編自week15_angles_TRT_again
#include <GL/glut.h>
#include <stdio.h>
float angle[20],oldX=0;
int angleID=0;
FILE * fout=NULL, * fin = NULL;
void myWrite()
{///每呼叫一次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]);///檔案印出來
    }///這裡老師沒有fclose
    printf("\n");///小黑印出跳行
    fprintf(fout,"\n");///檔案跳行
}
float NewAngle[20],OldAngle[20];
void myRead()
{
    if(fout!=NULL){fclose(fout); fout=NULL;}
    if(fin==NULL) fin=fopen("file.txt","r");
    for(int i=0;i<20;i++)
    {
        OldAngle[i] = NewAngle[i];
        fscanf(fin, "%f", &NewAngle[i] );
        ///fscanf(fin,"%f",&angle[i]);
    }
    glutPostRedisplay();///重劃畫面
}
void myInterpolate(float alpha){
    for(int i=0;i<20;i++){
        angle[i]= alpha * NewAngle[i] + (1-alpha) * OldAngle[i];
    }
}
///int t=0;
void timer(int t){
    if(t%50==0) myRead();
    myInterpolate((t%50)/50.0);
    glutPostRedisplay();
    glutTimerFunc(20,timer,t+1);
}
void keyboard(unsigned char key,int x,int y)
{
    if(key=='p'){
        myRead();
        glutTimerFunc(0,timer,0);
        ///if(t%30==0) myRead();
        ///myInterpolate( (t%30)/30.0 );///介於0.0~1.0
        ///glutPostRedisplay();
        ///t++;
    }
    if(key=='s') myWrite();///調好動作才save存檔
    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();
}
 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);
    glutCreateWindow("week15_angles_TRT_again");
    glutMouseFunc(mouse);
    glutMotionFunc(motion);
    glutKeyboardFunc(keyboard);
    glutDisplayFunc(display);

    glutMainLoop();
 }



step03-1
1.new project week16_camera_projection_gluLookAt


#include <GL/glut.h>
void reshape(int w,int h){
    float ar = (float) w/(float) h;
    glViewport(0,0,w,h);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluPerspective(60,ar,0.1,100);

    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
    gluLookAt(0,0,3, ///eye
              0,0,0, ///center看哪裡
              0,1,0);///up向量
}
void display()
{
    glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
    glutSolidTeapot(1);
    glutSwapBuffers();
}
int main(int argc,char**argv)
{
    glutInit(&argc,argv);
    glutInitDisplayMode(GLUT_DOUBLE|GLUT_DEPTH);
    glutCreateWindow("week16 camera");
    glutDisplayFunc(display);
    glutReshapeFunc(reshape);
    glutMainLoop();
}


steo03-2
滑鼠可拖曳
新加的程式碼





#include <GL/glut.h>
void reshape(int w,int h){
    float ar = (float) w/(float) h;
    glViewport(0,0,w,h);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluPerspective(60,ar,0.1,100);

    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
    gluLookAt(0,0,3, ///eye
              0,0,0, ///center看哪裡
              0,1,0);///up向量
}
void display()
{
    glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
    glutSolidTeapot(1);
    glutSwapBuffers();
}
void motion(int x,int y){
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
    gluLookAt((x-150)/150.0,(y-150)/150.0,3, ///eye
              0,0,0, ///center看哪裡
              0,1,0);///up向量
    glutPostRedisplay();
}
int main(int argc,char**argv)
{
    glutInit(&argc,argv);
    glutInitDisplayMode(GLUT_DOUBLE|GLUT_DEPTH);
    glutCreateWindow("week16 camera");
    glutMotionFunc(motion);
    glutDisplayFunc(display);
    glutReshapeFunc(reshape);
    glutMainLoop();
}




2022年5月31日 星期二

豪哥的電腦圖學作業week15

 電腦圖學筆記week15

step01-1

PlaySound() 更詳細解說

0.New Empty File,存檔成week15-1_PlaySound.cpp

1.#include<mmsystem.h>上週教的

2.#include<windows.h>這週教的

3.PlaySound( " 檔名.wav" , NULL,SND_ASYNC );//上週

4.PlaySound( "檔名.wav" , NULL,SND_SYNC );//這週

5.Setting-Complier Setting,Linker 加入 winmm

6.下載個wav檔放進程式碼目錄

#include <windows.h>
#include <stdio.h>
int main()
{
    printf("PlaySound()之前\n");
    PlaySound("badbad.wav",NULL,SND_SYNC);
    printf("PlaySound()之後\n");
}
可播放音樂

stoe01-2
0.New Empty File,存成week15-2_SND_SYNC_SND_ASYNC

#include <windows.h>
#include <stdio.h>
int main()
{
    PlaySound("badbad.wav",NULL,SND_ASYNC);///ASYNC不等待
    while(1){
        printf("請輸入數字: ");
        int N;
        scanf("%d",&N);
        if(N==1) PlaySound("do.wav",NULL,SND_ASYNC);
        if(N==2) PlaySound("re.wav",NULL,SND_ASYNC);
        if(N==3) PlaySound("mi.wav",NULL,SND_ASYNC);
    }
}///最後一行,就沒了

ASYNC互動效果比較好

step02-1

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

PlaySound只能用WAV檔

0. New Empty File ,week15-3_mp3.cpp

1.在moodle下載 CMP3_MCI.h

2.程式碼 #include "CMP3_MCI.h"

3.宣告 CMP3_MCI mp3;

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

5.mp3.Play();

#include <stdio.h>

#include "CMP3_MCI.h"

CMP3_MCI mp3;


int main()

{

    mp3.Load("badbad.mp3");

    mp3.Play();


    printf("隨便等你輸入數字,程式就卡住囉: ");

    int N;

    scanf("%d",&N);

}


step02-2
0.開新GLUT專案 week15_angles_TRT_again
1.複製上週程式,試跑2.不像動畫 動作不自然
3.myWrite()會寫一行 myRead()會讀一行
4.不能把myWrite()放motion()
5.關節動作調好再存檔
6.所以擺完動作後再按s存檔

///week15修改自week14_angles_fprintf
#include <GL/glut.h>
#include <stdio.h>
float angle[20],oldX=0;
int angleID=0;
FILE * fout=NULL, * fin = NULL;
void myWrite()
{///每呼叫一次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]);///檔案印出來
    }///這裡老師沒有fclose
    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=='s') myWrite();///調好動作才save存檔
    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();
}
 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);
    glutCreateWindow("week15_angles_TRT_again");
    glutMouseFunc(mouse);
    glutMotionFunc(motion);
    glutKeyboardFunc(keyboard);
    glutDisplayFunc(display);

    glutMainLoop();
 }


step03-1
機器人擺動
1.開新GLUT專案 week15_hw_gundam_parts
2.工作執行目錄 working.dir 不太好, 要改到現在程式的目錄
3.用Notepad++編輯 cbp檔的路徑,working dir 路徑改成小數點,把freeglut.dll放進目錄



2022年5月24日 星期二

豪哥的電腦圖學筆記week14

 Week14筆記

step01-1

寫檔(為了記錄動畫的動作)

0.new empty file,存檔成week14-1_fprintf裡的week14.cpp

1.fopen() 開檔案

2.printf() ==> fprintf()

3.fclose() 關檔案

按build後再按build and run

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


step01-2
讀檔file input
0.new empty file,存檔成week14-3_fprintf_fscanf裡的week14-2.cpp
1.把剛剛week14-1.cpp拿來用
2.另外一組file*fin
3.scanf ()==> fscanf()
4.fclose()

///week14-2
#include <stdio.h>
int main()
{///檔案指標 fout 開啟檔案(檔名,write模式)
    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);
}



step01-3
把上週的程式 week13_rect_many_TRT 拿來改成 week14
0.File New Project,GLUT專案 week14_angles_fprintf

///week14_angles_fprintf 改自 week13_rect_many_TRT
#include <GL/glut.h>
#include <stdio.h>
float angle[20],oldX=0;
int angleID=0;///0號關節,1號關節...
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]);///檔案印出來 
    }
}
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();///請GLUT重劃畫面
}
 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);///對z軸旋轉
        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);///對z軸旋轉
        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[])//main()主函式 進階版
 {
    glutInit(&argc,argv);//把參數送給glutInit初始化
    glutInitDisplayMode(GLUT_DOUBLE|GLUT_DEPTH);//雙緩衝區+3D深度功能
    ///glutInitWindowSize(600,600);
    glutCreateWindow("week13_rect_TRT");//開GLUT視窗
    glutKeyboardFunc(keyboard);
    glutMouseFunc(mouse);
    glutMotionFunc(motion);
    glutDisplayFunc(display);//顯示用的函式

    glutMainLoop();
 }


step02-1
0.加新專案 week14_angles_fprintf_fscanf
1.複製前一個版本的程式
2.要寫void myRead()
3.鍵盤裡,按下"r"呼叫myRead()

///week14_angles_fprintf_fscanf
#include <GL/glut.h>
#include <stdio.h>
float angle[20],oldX=0;
int angleID=0;///0號關節,1號關節...
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]);///檔案印出來
    }
    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();///請GLUT重劃畫面
}
 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);///對z軸旋轉
        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);///對z軸旋轉
        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[])//main()主函式 進階版
 {
    glutInit(&argc,argv);//把參數送給glutInit初始化
    glutInitDisplayMode(GLUT_DOUBLE|GLUT_DEPTH);//雙緩衝區+3D深度功能
    ///glutInitWindowSize(600,600);
    glutCreateWindow("week13_rect_TRT");//開GLUT視窗
    glutKeyboardFunc(keyboard);
    glutMouseFunc(mouse);
    glutMotionFunc(motion);
    glutDisplayFunc(display);//顯示用的函式

    glutMainLoop();
 }

step02-2
0.file.txt放在奇怪的目錄
1.GLUT 專案需要freeglut.dll 所以working dir被設到 freeglut/bin裡 
2.在cbp project檔案裡 有working dir的設定 工作執行的目錄
3.使用Notepad++把.cbp的 working_dir 路徑改成.後存檔
4.再把桌面freeglut/bin裡的freeflut.dll移動到專案資料夾裡


step03-1
glutTimerFunc()計時器
0.viod timer(int t)寫timer函式
1.glutTimerFunc(等多久,timer,t參數):
2.開新專案 week14_timer
3.其他GLUT 10行程式

#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();
}




step03-2
五秒後開始計時
播放聲音PlaySound(),先下載 do.wav
1.繼續改 week14_timer_one_by_one
2.#include <mmsystem.h>
3.PlaySound("do.wav",NULL,SND_ASYNC);


#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(5000,timer,0);///設定五秒後第0個timer
    glutDisplayFunc(display);
    glutMainLoop();
}

 

#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(2000,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);///設定五秒後第0個timer
    glutDisplayFunc(display);
    glutMainLoop();
}

會有聲音

2022年5月17日 星期二

豪哥電腦圖學筆記 week13

 week13

step01-1

1.新增專案

2.加入新教的程式碼(rect),glutInitWindowSize(600,600);可調整視窗大小

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


    glutDisplayFunc(display);


    glutMainLoop();

}


step01-2
1.加點色彩
2.準備好TRT程式碼
3.加上紅方塊
#include <GL/glut.h>
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();///把手臂掛回身體
        ///glRotatef(angle,0,0,1);///旋轉
        ///glTranslatef(x2,y2,z2);///把旋轉中心放中心
        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 TRT");

    glutDisplayFunc(display);

    glutMainLoop();
}

step01-3
1.把旋轉中心放在正中心
把(0.3,0.4)移動到(0,0)
glTranslatef(-0.3,-0.4,0);

2.移動45度,加角度float angle=45;,刪掉rotatef註解


3.移回正確位置
程式碼
#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 TRT");

    glutDisplayFunc(display);

    glutMainLoop();
}


step2-1
1.要用mouse motion旋轉角度
程式碼
#include <GL/glut.h>
float angle=45,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();///請GLUT重劃畫面
}
 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);///對z軸旋轉
        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[])//main()主函式 進階版
 {
    glutInit(&argc,argv);//把參數送給glutInit初始化
    glutInitDisplayMode(GLUT_DOUBLE|GLUT_DEPTH);//雙緩衝區+3D深度功能
    ///glutInitWindowSize(600,600);
    glutCreateWindow("week13_rect_TRT");//開GLUT視窗
    glutMouseFunc(mouse);
    glutMotionFunc(motion);
    glutDisplayFunc(display);//顯示用的函式

    glutMainLoop();
 }


step2-2
1.新開專案
2.新增第二個關節

///week13_rect_TRT_TRT
#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();///請GLUT重劃畫面
}
 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);///對z軸旋轉
        glTranslatef(-0.3,-0.4,0);///挑整手臂的旋轉中心
        glColor3f(1,0,0);
        glRectf(0.3,0.5,0.7,0.3);

        glPushMatrix();
            ///glTranslatef(x,y,z);
            ///glRotatef(angle,0,0,1);
            ///glTranslatef(0,1,10);
            glColor3f(0,1,0);///綠色的
            glRectf(0.7,0.5,1.0,0.3);///下手肘
        glPopMatrix();

    glPopMatrix();
    glutSwapBuffers();
 }
 int main(int argc, char *argv[])//main()主函式 進階版
 {
    glutInit(&argc,argv);//把參數送給glutInit初始化
    glutInitDisplayMode(GLUT_DOUBLE|GLUT_DEPTH);//雙緩衝區+3D深度功能
    ///glutInitWindowSize(600,600);
    glutCreateWindow("week13_rect_TRT");//開GLUT視窗
    glutMouseFunc(mouse);
    glutMotionFunc(motion);
    glutDisplayFunc(display);//顯示用的函式

    glutMainLoop();
 }



step2-3
程式碼

///week13_rect_TRT_TRT
#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();///請GLUT重劃畫面
}
 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);///對z軸旋轉
        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[])//main()主函式 進階版
 {
    glutInit(&argc,argv);//把參數送給glutInit初始化
    glutInitDisplayMode(GLUT_DOUBLE|GLUT_DEPTH);//雙緩衝區+3D深度功能
    ///glutInitWindowSize(600,600);
    glutCreateWindow("week13_rect_TRT");//開GLUT視窗
    glutMouseFunc(mouse);
    glutMotionFunc(motion);
    glutDisplayFunc(display);//顯示用的函式

    glutMainLoop();
 }

step03-1
1.開新專案 week13_rect_many_TRT
複製中間的程式碼
修改正負值
程式碼
///week13_rect_many_TRT
#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();///請GLUT重劃畫面
}
 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);///對z軸旋轉
        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);///對z軸旋轉
        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[])//main()主函式 進階版
 {
    glutInit(&argc,argv);//把參數送給glutInit初始化
    glutInitDisplayMode(GLUT_DOUBLE|GLUT_DEPTH);//雙緩衝區+3D深度功能
    ///glutInitWindowSize(600,600);
    glutCreateWindow("week13_rect_TRT");//開GLUT視窗
    glutMouseFunc(mouse);
    glutMotionFunc(motion);
    glutDisplayFunc(display);//顯示用的函式

    glutMainLoop();
 }

step03-2
1.要多個角度float angle=0;
改成angle[20];
全部的angle都要改
很多關節要用keyboardfunc
後面要加glutKeyboardFunc(keyboard);
程式碼
///week13_rect_many_TRT
#include <GL/glut.h>
float angle[20],oldX=0;
int angleID=0;///0號關節,1號關節...
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();///請GLUT重劃畫面
}
 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);///對z軸旋轉
        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);///對z軸旋轉
        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[])//main()主函式 進階版
 {
    glutInit(&argc,argv);//把參數送給glutInit初始化
    glutInitDisplayMode(GLUT_DOUBLE|GLUT_DEPTH);//雙緩衝區+3D深度功能
    ///glutInitWindowSize(600,600);
    glutCreateWindow("week13_rect_TRT");//開GLUT視窗
    glutKeyboardFunc(keyboard);
    glutMouseFunc(mouse);
    glutMotionFunc(motion);
    glutDisplayFunc(display);//顯示用的函式

    glutMainLoop();
 }


2022年5月10日 星期二

豪哥電腦圖學筆記week12

step1-1

1.主題TRT(移動-旋轉-移動)對特定軸轉動

2.下周考試TRT



step01-2

1.用程式來理解對特定軸轉動120.125.80.50/GL 

2.右下角點ToDraw 左邊黑色畫圖

3.ctrl-r reload可以清空

4.可以畫身體 手臂 叫做 myDrawObject(0)和myDrawObject(1)

5.改程式碼的順序

6.可以按angle= 再按空白鍵 會自動改變動畫


step2-1

1.把glRotatef()那一行變紅色,就可以移動左邊的東西

```cpp

glPushMatrix();

myDrawObject(0);//畫身體

glRotatef(angle,0,0,1);//這個旋轉,會轉下面整個東西

glTranslatef(-0.3,-0.19,0);//往左下方移(讓軸心 放世界的中心)

myDrawObject(1);//畫手臂(右上方)

glPopMatrix();

```


step2-2

講解下周考試題目

glPushMatrix();

glTranslatef(-0.5,-0.9,0);

glRotatef(-45,0,0,1);

glTranslatef(-0.8,0.9,0);

drawHand();

glPopMatrix();

會換圖


step2-3

1.開新GLUT專案 week12_TRT

2.把之前的10行筆記貼上來

3.茶壺旋轉

#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.2,0,0);

        glRotatef(angle,0,0,1);

        glTranslatef(0.2,0,0);

        glColor3f(1,0,0);

        glutSolidTeapot(0.2);

    glPopMatrix();

    glutSwapBuffers();

    angle+=0.1; ///每次執行 display() 加1度

}

int main(int argc,char**argv)

{

    glutInit(&argc,argv);

    glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);

    glutCreateWindow("week12 TRT");


    glutIdleFunc(display);

    glutDisplayFunc(display);

    glutMainLoop();

}



TRT2

#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.2,0,0);///(3)掛到右邊(整個往動)

        glRotatef(angle, 0,0,1);///(2)旋轉

        glTranslatef(0.2,0,0);

        glColor3f(1,0,0);

        glutSolidTeapot(0.2);///小茶壺,當作小手臂

        glPushMatrix();

            glTranslatef(0.2,0,0);///(3)掛到右邊(整個往動)

            glRotatef(angle, 0,0,1);///(2)旋轉

            glTranslatef(0.2,0,0);

            glColor3f(1,0,0);

            glutSolidTeapot(0.2);///小茶壺,當作小手肘

        glPopMatrix();

    glPopMatrix();


    glPushMatrix();///左邊的手臂手肘

        glTranslatef(-0.2,0,0);///(3)掛到左邊(整個往動)

        glRotatef(-angle, 0,0,1);///(2)旋轉

        glTranslatef(-0.2,0,0);

        glColor3f(1,0,0);

        glutSolidTeapot(0.2);///小茶壺,當作小手臂

        glPushMatrix();

            glTranslatef(-0.2,0,0);///(3)掛到左邊(整個往動)

            glRotatef(-angle, 0,0,1);///(2)旋轉

            glTranslatef(-0.2,0,0);

            glColor3f(1,0,0);

            glutSolidTeapot(0.2);///小茶壺,當作小手肘

        glPopMatrix();

    glPopMatrix();

    glutSwapBuffers();

    angle+=0.01; ///每次執行 display() 加1度

}

int main(int argc,char**argv)

{

    glutInit(&argc,argv);

    glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);

    glutCreateWindow("week12_TRT");


    glutIdleFunc(display);

    glutDisplayFunc(display);

    glutMainLoop();

}



2022年5月3日 星期二

豪哥圖學筆記week11

電腦圖學week11
繼續實作glm模型練習

步驟一
1.安裝好freeglut
2.opencv裝好,opencv裡面設定調好
setting-compiler-search directories   compiler include  C:\OpenCV2.1\include
setting-compiler-search directories   Linker lib C:\OpenCV2.1\lib
setting-compiler-Linker  cv210 cxcore210 highgui210
3.重開codeblocks 開新專案
4.myGumdam.zip裡的data放進freeglut的bin裡打以下程式碼

#include <opencv/highgui.h>
int main()
{
    IplImage * img = cvLoadImage("data/Diffuse.jpg");
    cvShowImage("week11",img);
    cvWaitKey();
}




步驟二
把茶壺貼上鋼彈的貼圖
1.上面放上週的myTexture放在程式
2.下面放GLUT 10行程式
3.main()的glutMainLoop()之前,加入myTexture("data/Diffuse.jpg");

打以下程式碼


#include <opencv/highgui.h>
#include <opencv/cv.h>
#include <GL/glut.h>
int myTexture(char * filename)
{
    IplImage * img = cvLoadImage(filename); ///OpenCV讀圖
    cvCvtColor(img,img, CV_BGR2RGB); ///OpenCV轉色彩 (需要cv.h)
    glEnable(GL_TEXTURE_2D); ///1. 開啟貼圖功能
    GLuint id; ///準備一個 unsigned int 整數, 叫 貼圖ID
    glGenTextures(1, &id); /// 產生Generate 貼圖ID
    glBindTexture(GL_TEXTURE_2D, id); ///綁定bind 貼圖ID
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); /// 貼圖參數, 超過包裝的範圖T, 就重覆貼圖
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); /// 貼圖參數, 超過包裝的範圖S, 就重覆貼圖
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); /// 貼圖參數, 放大時的內插, 用最近點
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); /// 貼圖參數, 縮小時的內插, 用最近點
    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, img->width, img->height, 0, GL_RGB, GL_UNSIGNED_BYTE, img->imageData);
    return id;
}
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("week11 gundam");

    glutDisplayFunc(display);
    myTexture("data/Diffuse.jpg");
    glutMainLoop();
}


步驟三
1.jsyeh.org/3dcg10下載source.zip  (glm.h和glm.c和transformation.c)
2.glm.c改成glm.cpp,glm.cpp和glm.h 放進week11專案裡
3.add files glm.cpp
4.加入程式碼

#include後加入

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


display()裡加入

void display()
{
    glClear(GL_COLOR_BUFFER_BIT || GL_DEPTH_BUFFER_BIT);
    //glutSolidTeapot(0.3);
    if(pmodel == NULL){
        pmodel = glmReadOBJ("data/Gundam.obj");
        glmUnitize(pmodel);
        glmFacetNormals(pmodel);
        glmVertexNormals(pmodel ,90);
    }
    glmDraw(pmodel ,GLM_TEXTURE);
    glutSwapBuffers();
}

鋼彈模型程式碼
#include <opencv/highgui.h>
#include <opencv/cv.h>
#include <GL/glut.h>
#include "glm.h"
GLMmodel * pmodel =NULL;
int myTexture(char * filename)
{
    IplImage * img = cvLoadImage(filename); ///OpenCV讀圖
    cvCvtColor(img,img, CV_BGR2RGB); ///OpenCV轉色彩 (需要cv.h)
    glEnable(GL_TEXTURE_2D); ///1. 開啟貼圖功能
    GLuint id; ///準備一個 unsigned int 整數, 叫 貼圖ID
    glGenTextures(1, &id); /// 產生Generate 貼圖ID
    glBindTexture(GL_TEXTURE_2D, id); ///綁定bind 貼圖ID
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); /// 貼圖參數, 超過包裝的範圖T, 就重覆貼圖
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); /// 貼圖參數, 超過包裝的範圖S, 就重覆貼圖
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); /// 貼圖參數, 放大時的內插, 用最近點
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); /// 貼圖參數, 縮小時的內插, 用最近點
    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, img->width, img->height, 0, GL_RGB, GL_UNSIGNED_BYTE, img->imageData);
    return id;
}
void display()
{
    glClear(GL_COLOR_BUFFER_BIT || GL_DEPTH_BUFFER_BIT);
    //glutSolidTeapot(0.3);
    if(pmodel == NULL){///如果是空指標,表示模型沒準備好
        pmodel = glmReadOBJ("data/Gundam.obj");///就讀模型
        glmUnitize(pmodel);///換算成Unit單位 -1...+1
        glmFacetNormals(pmodel);///重新計算模型的面的法向量
        glmVertexNormals(pmodel ,90);///重新計算模型的頂點的法向量
    }
    glmDraw(pmodel ,GLM_TEXTURE);///有模型後,畫面要記得畫貼圖
    glutSwapBuffers();
}
int main(int argc,char**argv)
{
    glutInit(&argc,argv);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
    glutCreateWindow("week11 gundam");

    glutDisplayFunc(display);
    myTexture("data/Diffuse.jpg");
    glutMainLoop();
}
因為貼圖上下顛倒
所以用小畫家垂直翻轉 Diffuse.jpg     

步驟四

鋼彈轉動

#include <GL/glut.h>
#include <opencv/highgui.h>
#include <opencv/cv.h>
#include "glm.h"
GLMmodel * pmodel=NULL;
///GLUquadric * sphere = NULL;///指標,指到2次曲面
int myTexture(char * filename)
{
    IplImage * img = cvLoadImage(filename); ///OpenCV讀圖
    cvCvtColor(img,img, CV_BGR2RGB); ///OpenCV轉色彩 (需要cv.h)
    glEnable(GL_TEXTURE_2D); ///1. 開啟貼圖功能
    GLuint id; ///準備一個 unsigned int 整數, 叫 貼圖ID
    glGenTextures(1, &id); /// 產生Generate 貼圖ID
    glBindTexture(GL_TEXTURE_2D, id); ///綁定bind 貼圖ID
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); /// 貼圖參數, 超過包裝的範圖T, 就重覆貼圖
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); /// 貼圖參數, 超過包裝的範圖S, 就重覆貼圖
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); /// 貼圖參數, 放大時的內插, 用最近點
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); /// 貼圖參數, 縮小時的內插, 用最近點
    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, img->width, img->height, 0, GL_RGB, GL_UNSIGNED_BYTE, img->imageData);
    return id;
}
float angle=0;///旋轉角度
 void display()
 {
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glPushMatrix();
        glRotatef(angle,0,1,0);
        if(pmodel==NULL)///如果是空指標 表示模型還沒準備好
        {
            pmodel=glmReadOBJ("data/Gundam.obj");///讀模型
            glmUnitize(pmodel);///換算成unit單位大小
            glmFacetNormals(pmodel);///重新計算模型的面的法向量
            glmVertexNormals(pmodel,90);///重新計算模型頂點的法向量
        }
        glmDraw(pmodel,GLM_TEXTURE);///有模型後畫面要記得畫貼圖
    glPopMatrix();

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

    glEnable(GL_DEPTH_TEST);
    glutDisplayFunc(display);///顯示用的函式
    glutIdleFunc(display);
    myTexture("data/Diffuse.jpg");
    ///myTexture("earth.jpg");
    ///sphere=gluNewQuadric();///準備好2次曲面
    glutMainLoop();
 }






VERY BEAUTIFUL, VERY POWERFUL

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