顯示具有 09160934_陳嘉徽 標籤的文章。 顯示所有文章
顯示具有 09160934_陳嘉徽 標籤的文章。 顯示所有文章

2022年6月14日 星期二

(OwO)電腦圖學week17_6/14

如果想貼一張以上貼圖

貼圖盡量2n*2n會比較好  (EX:256*256)

1.先宣告兩個變數 GLuint tex1,tex2;

在main()裡

tex1=myTexture("data/Diffuse.jpg");
tex2=myTexture("data/Wood.png");

2.在void display()裡

     glBindTexture(GL_TEXTURE_2D,tex1);
    glBindTexture(GL_TEXTURE_2D,tex2);



模型被壓扁要開3D前後深度測試

攝影機與運鏡 : 透視投影法 glyPerspective,配上glutReshapeFunc()再上gluLookAt()
void reshape(int w,int h){///不能 整數除
    float ar = (float) w/(float) h;
    glViewport(0,0,w,h);
    glMatrixMode(GL_PROJECTION);///3D變2D
    glLoadIdentity();
    gluPerspective(60,ar,0.1,100);
    glMatrixMode(GL_MODELVIEW);///3D Model+view
    glLoadIdentity();
    gluLookAt(0,0,3, 
              0,0,0, ///center看哪裡
              0,1,0);///up向量
}
兩張貼圖 : Gundam(鋼彈)、木紋、宣告2個整數 GLuint tex1、text2; 在main()裡面
                   text1 = myTexture("data/Diffuse.jpg");
                   text2 = myTexture("data/wood.png");
在 display() 裡
glBindTexture(GL_TEXTUTE_2D, text1) 及 glBindTexture(GL_TEXTURE_2D, text2) 來切換
把全部的模型讀入 : 改寫 myReadGundam() 改寫 myReadOne() 及 myReadAll();


TRT : 

必須有中心點
可以使用 mouse motion 來知道值在哪裡
        
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]);///檔案印出來
    }///這裡老師沒有fclose
}
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();
} 


2022年6月7日 星期二

(OwO)電腦圖學week16_6/7

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

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


ex:

  • alpha: 0     =>舊
  • alpha:0.5   =>半新半舊
  • alpha: 1     =>新
可使用EXCEL & GOOGLE SPREADSHEET來做練習


運鏡

eye:眼睛位置,center:眼睛往哪看,up:正看斜看

eye=>相機移動

center=>物體移動

up=>相機、物體不動,畫面旋轉

動作內插

STEP
開啟新專案檔~>使用15週程式~>新增功能~>按下"p"能做動作~>myinterpolate函式功能:內插公式應用~>myRead函式功能:陣列值座新舊交換~>只按一次"p"動作連續

2022年5月31日 星期二

(OwO)電腦圖學week15_5/31

 更詳細解說PlaySound()

#include<mmsystem.h>上週教的

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


本周教#include<windows.h>
PlaySound("107042111.wav",NULL, SND_SYNC);

開一個empty file存成week15-1.cpp

#include <windows.h>

#include <stdio.h>

int main()

{

    printf("PlaySound()之前\n");

    ///檔案不存在會有錯誤聲音

   PlaySound("107042111.wav",NULL, SND_SYNC);

   printf("PlaySound()之後\n");

}

Setting ->Compiler Setting->Linker加入winmm

注意工作執行目錄working_dir(in C:\........


#include<GL/glut.h>

#include<stdio.h>

float angle[20] ,oldX=0;

int angleID=0;

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]);///檔案印出來



    ///1f要空格



   }



    printf("\n");///每呼叫一次小黑會跳行



    fprintf(fout,"\n");///每呼叫一次檔案會跳行







}///這邊沒有fclose



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

    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);///(3)整體移位



        glRotatef(angle[0],0,0,1);///(2)



        glTranslatef(-0.3,-0.4,0);///(1)把軸心放在正中心



        glColor3f(1,0,0);///紅



        glRectf(0.3,0.5,0.7,0.3);///上手臂







            glPushMatrix();



                glTranslatef(0.7,0.4,0);///(3)整體移位



                glRotatef(angle[1],0,0,1);///(2)



                glTranslatef(-0.7,-0.4,0);///(1)把軸心放在正中心







                glColor3f(0,1,0);///綠



                glRectf(0.7,0.5,1.0,0.3);///下手臂



            glPopMatrix();



    glPopMatrix();











     glPushMatrix();///左邊



        glTranslatef(-0.3,0.4,0);///(3)整體移位



        glRotatef(angle[2],0,0,1);///(2)



        glTranslatef(0.3,-0.4,0);///(1)把軸心放在正中心



        glColor3f(1,0,0);///紅



        glRectf(-0.3,0.5,-0.7,0.3);///上手臂







            glPushMatrix();



                glTranslatef(-0.7,0.4,0);///(3)整體移位



                glRotatef(angle[3],0,0,1);///(2)



                glTranslatef(0.7,-0.4,0);///(1)把軸心放在正中心







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







    glutMouseFunc(mouse);



    glutMotionFunc(motion);



    glutKeyboardFunc(keyboard);



    glutDisplayFunc(display);







    glutMainLoop();



}



2022年5月24日 星期二

(OwO)電腦圖學week14_5/24

開一個empty file(不是專案)


#include<stdio.h>

int main()

{

///檔案指標fout 開啟檔案(檔名,write模式)


    FILE * fout = fopen("write.txt","w+");

    printf("Hello world\n");

    fprintf(fout,"Hello world\n");

    fclose(fout);


}




再另外開一個empty file


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


}



拿上禮拜的程式碼再加點東西

#include<GL/glut.h>

#include<stdio.h>

float angle[20] ,oldX=0;

int angleID=0;

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]);///檔案印出來


    ///1f要空格


   }


    printf("\n");///每呼叫一次小黑會跳行


    fprintf(fout,"\n");///每呼叫一次檔案會跳行




}///這邊沒有fclose


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);///(3)整體移位


        glRotatef(angle[0],0,0,1);///(2)


        glTranslatef(-0.3,-0.4,0);///(1)把軸心放在正中心


        glColor3f(1,0,0);///紅


        glRectf(0.3,0.5,0.7,0.3);///上手臂




            glPushMatrix();


                glTranslatef(0.7,0.4,0);///(3)整體移位


                glRotatef(angle[1],0,0,1);///(2)


                glTranslatef(-0.7,-0.4,0);///(1)把軸心放在正中心




                glColor3f(0,1,0);///綠


                glRectf(0.7,0.5,1.0,0.3);///下手臂


            glPopMatrix();


    glPopMatrix();






     glPushMatrix();///左邊


        glTranslatef(-0.3,0.4,0);///(3)整體移位


        glRotatef(angle[2],0,0,1);///(2)


        glTranslatef(0.3,-0.4,0);///(1)把軸心放在正中心


        glColor3f(1,0,0);///紅


        glRectf(-0.3,0.5,-0.7,0.3);///上手臂




            glPushMatrix();


                glTranslatef(-0.7,0.4,0);///(3)整體移位


                glRotatef(angle[3],0,0,1);///(2)


                glTranslatef(0.7,-0.4,0);///(1)把軸心放在正中心




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




    glutMouseFunc(mouse);


    glutMotionFunc(motion);


    glutKeyboardFunc(keyboard);


    glutDisplayFunc(display);




    glutMainLoop();


}


產生的檔案file.txt跑到 C:\Users\user\Desktop\freeglut\gin ;想要放在程式專案的那個目錄裡

需要 freeglut.dll ( working_dir被設到 freeglut\bin 裡面)

在 .cbp CodeBlocks Project 檔裡, 有 working_dir的設定 工作執行的目錄

使用 Notepad++ 把 .cbp 的 working_dir="....." 改 working_dir="." (小數點)

存檔後, CodeBlocks Reload它

放到week14_angles_fprintf_fscanf 程式專案的同目錄, 再執行時,  mouse motion & 按r重播就ok

file.txt 放在你的程式專案目錄囉!!!



2022年5月17日 星期二

(OwO)電腦圖學week13_5/17

建個GLUT專案再貼上10 行程式碼外加一行新的 

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

}



多加個小方塊

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

        ///glTranslatef()

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

}



TRT

把手的軸心放在正中心(1)

#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()
        glTranslatef(-0.3,-0.4,0);///(1)把軸心放在中心
    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();
}


旋轉(2)

#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()
        glRotatef(angle,0,0,1);///(2)
        glTranslatef(-0.3,-0.4,0);///(1)把軸心放在中心
    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();
}

改整體位置(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);///(3)整體移位
        glRotatef(angle,0,0,1);///(2)
        glTranslatef(-0.3,-0.4,0);///(1)把軸心放在中心
    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();
}

藉由滑鼠旋轉手

#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);///(3)整體移位
        glRotatef(angle,0,0,1);///(2)
        glTranslatef(-0.3,-0.4,0);///(1)把軸心放在中心
    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");

    glutMouseFunc(mouse);
    glutMotionFunc(motion);
    glutDisplayFunc(display);

    glutMainLoop();
}

再加一個手臂也可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);///(3)整體移位
        glRotatef(angle,0,0,1);///(2)
        glTranslatef(-0.3,-0.4,0);///(1)把軸心放在正中心
        glColor3f(1,0,0);///紅
        glRectf(0.3,0.5,0.7,0.3);///上手臂

            glPushMatrix();

                glTranslatef(0.7,0.4,0);///(3)整體移位
                glRotatef(angle,0,0,1);///(2)
                glTranslatef(-0.7,-0.4,0);///(1)把軸心放在正中心

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

    glutMouseFunc(mouse);
    glutMotionFunc(motion);
    glutDisplayFunc(display);

    glutMainLoop();
}

兩隻手都可旋轉

#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);///(3)整體移位
        glRotatef(angle,0,0,1);///(2)
        glTranslatef(-0.3,-0.4,0);///(1)把軸心放在正中心
        glColor3f(1,0,0);///紅
        glRectf(0.3,0.5,0.7,0.3);///上手臂

            glPushMatrix();
                glTranslatef(0.7,0.4,0);///(3)整體移位
                glRotatef(angle,0,0,1);///(2)
                glTranslatef(-0.7,-0.4,0);///(1)把軸心放在正中心

                glColor3f(0,1,0);///綠
                glRectf(0.7,0.5,1.0,0.3);///下手臂
            glPopMatrix();
    glPopMatrix();


     glPushMatrix();///左邊
        glTranslatef(-0.3,0.4,0);///(3)整體移位
        glRotatef(angle,0,0,1);///(2)
        glTranslatef(0.3,-0.4,0);///(1)把軸心放在正中心
        glColor3f(1,0,0);///紅
        glRectf(-0.3,0.5,-0.7,0.3);///下手臂

            glPushMatrix();
                glTranslatef(-0.7,0.4,0);///(3)整體移位
                glRotatef(angle,0,0,1);///(2)
                glTranslatef(0.7,-0.4,0);///(1)把軸心放在正中心

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

    glutMouseFunc(mouse);
    glutMotionFunc(motion);
    glutDisplayFunc(display);

    glutMainLoop();
}



用鍵盤按鍵指定要旋轉哪一部分

#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;///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);///(3)整體移位
        glRotatef(angle[0],0,0,1);///(2)
        glTranslatef(-0.3,-0.4,0);///(1)把軸心放在正中心
        glColor3f(1,0,0);///紅
        glRectf(0.3,0.5,0.7,0.3);///上手臂

            glPushMatrix();
                glTranslatef(0.7,0.4,0);///(3)整體移位
                glRotatef(angle[1],0,0,1);///(2)
                glTranslatef(-0.7,-0.4,0);///(1)把軸心放在正中心

                glColor3f(0,1,0);///綠
                glRectf(0.7,0.5,1.0,0.3);///下手臂
            glPopMatrix();
    glPopMatrix();


     glPushMatrix();///左邊
        glTranslatef(-0.3,0.4,0);///(3)整體移位
        glRotatef(angle[2],0,0,1);///(2)
        glTranslatef(0.3,-0.4,0);///(1)把軸心放在正中心
        glColor3f(1,0,0);///紅
        glRectf(-0.3,0.5,-0.7,0.3);///上手臂

            glPushMatrix();
                glTranslatef(-0.7,0.4,0);///(3)整體移位
                glRotatef(angle[3],0,0,1);///(2)
                glTranslatef(0.7,-0.4,0);///(1)把軸心放在正中心

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

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

    glutMainLoop();
}









2022年5月10日 星期二

(OwO)電腦圖學week12_5/10

去老師的網站:http://jsyeh.org/3dcg10

下載:windows.zip & data.zip

T移動R轉動(按右鍵可SwapTR) glTranslatef( 0.9 , 0.0 , 0.0 ) ; glRotatef( 角度, 0, 1, 0)


可進入老師做的網站練習: https://120.125.80.50/GL/opengl_TRT_demo.html

Ctrl-R 可清空(還原到初始)

按住旁邊的字可拖曳並改寫程式的順序


區別

TR:轉動軸在物件的中心

TRT:改軸心->轉角度->改位置

(1) 最下面的T:把旋轉中心放到正中心

(2) 轉到我們要的角度

(3) 最上面的T:把東西放在對的位置

RT:轉動軸在座標的中心


2-2下周考試

glPushMatrix();

glTranslatef(-0.5,-0.9,0);
glRotatef(angle,0,0,1);   //angle=45
glTranslatef(-0.8,0.9,0);

myDrawObject();///物件(手)

glPopMatrix();

2-3寫程式

file->new->project->GLUT專案,week12_TRT

把10行程式碼放上去(之前的)

把TRT的6行放上去(今天的)

讓他自動轉(float angle=0)配上glRotatef(angle,0,0,1)再加上angle++

glutIdleFunc(display);(上週筆記)


#include <GL/glut.h>

float angle=0;

void display()

{

    glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);

    glPushMatrix();

        ///glTranslatef(0, 0, 0);

        glRotatef( angle, 0, 0, 1);

        ///glTransaltef(0, 0, 0);

        glutSolidTeapot( 0.2 );///想像它是手臂

    glPopMatrix();

    glutSwapBuffers();

    angle++;

}

int main( int argc, char** argv )

{

    glutInit( &argc, argv );

    glutInitDisplayMode( GLUT_DOUBLE|GLUT_DEPTH );

    glutCreateWindow("week12 TRT");


    glutIdleFunc(display);

    glutDisplayFunc(display);

    glutMainLoop();

}


茶壺超人

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

        glColor3f(1,0,0);///紅色

        glTranslatef(0.2, 0, 0);///(3)把整個往右移動

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

        glTranslatef(0.2, 0, 0); ///(1)把旋轉中心放到中心

        glutSolidTeapot( 0.2 ); ///做個小茶壼然後想像是手臂~

    glPopMatrix();

    glutSwapBuffers();

    angle++;

}

int main( int argc, char** argv )

{

    glutInit( &argc, argv );

    glutInitDisplayMode( GLUT_DOUBLE|GLUT_DEPTH );

    glutCreateWindow("week12 TRT");


    glutIdleFunc(display);

    glutDisplayFunc(display);

    glutMainLoop();

}


茶壺超人(整隻手)

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

        glColor3f(1,0,0);///紅

        glTranslatef(0.5, 0.2, 0);///(3)把整個往右移動

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

        glTranslatef(0.2, 0, 0); ///(1)把旋轉中心,放到中心

        glutSolidTeapot( 0.2 );///上臂

        glPushMatrix();

            glColor3f(1,0,0);///紅

            glTranslatef(0.4, 0.1, 0);

            glRotatef( angle, 0, 0, 1);

            glTranslatef(0.2, 0, 0); 

            glutSolidTeapot( 0.2 );///下臂

        glPopMatrix();

    glPopMatrix();

     glutSwapBuffers();

     angle++;

}

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日 星期二

(OwO)電腦圖學week11_5/3

先建立個專案再從 teams 上把 myGundam.zip 下載!

1.把week09的texture程式放在上面;Glut的10行程式放下面

#include <opencv/highgui.h>
#include <opencv/cv.h>
#include <GL/glut.h>
int myTexture(char * filename)
{
    IplImage * img = cvLoadImage(filename);
    cvCvtColor(img,img, CV_BGR2RGB);
    glEnable(GL_TEXTURE_2D);
    GLuint id;
    glGenTextures(1, &id);
    glBindTexture(GL_TEXTURE_2D, id);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
    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();
}





2.main()的glutMainLoop()之前加入myTexture("data/Diffuse.jpg"); 

3.茶壺貼上鋼蛋的貼圖

4.source下載glm.c (改成glm.cpp)、glm.h、transformation.c)放入專案目錄中

5.右鍵add file加入glm.cpp再把鋼蛋模組匯入程式中

6.貼圖會顛倒(使用小畫家可反轉貼圖)

7.最後開啟3D功能與3D深度測試 -> glEnable(GL_DEPTH_TEST);

#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);
    cvCvtColor(img,img, CV_BGR2RGB);
    glEnable(GL_TEXTURE_2D);
    GLuint id;
    glGenTextures(1, &id);
    glBindTexture(GL_TEXTURE_2D, id);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
    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);
    if( pmodel == NULL){
        pmodel=glmReadOBJ("data/Gundam.obj");///讀模型
        glmUnitize(pmodel);///換成Unit單位大小
        glmFacetNormals(pmodel);///重新計算模型面的法向量
        glmVertexNormals(pmodel,90);///重新計算模型頂點的法向量
    }
    glPushMatrix();
        glRotatef(angle,0,1,0);
        glmDraw(pmodel,GLM_TEXTURE);///模型畫貼圖
    glPopMatrix();

    glutSwapBuffers();
    angle+=0.8;
}

int main(int argc,char** argv)
{

    glutInit(&argc,argv);
    glutInitDisplayMode(GLUT_DOUBLE|GLUT_DEPTH);
    glutCreateWindow("week11 gundam");

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



2022年4月26日 星期二

(OwO)電腦圖學week10_4/26

安裝freeglut (libglut32.a),OPenCV2.1(Add PATH)

File-New-Project-GLUT(重開CodeBlocks)

把上週&上上周的 my Texture.txt 和 10行GLUT程式碼拿來用

#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);///開貼圖功能
    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("week09 texture");

    glutDisplayFunc(display);
    myTexture("sky.png");

    glutMainLoop();
}


openCV設定:setting

  • Include:Compiler,Add C:\opencv2.1\include
  • Lib:Linker,Add C:\opencv2.1\lib
  • Linker settings,Add cv210、cxcore210、highgui210


earth.jpg圖檔放工作目錄freeglut\bin



2022年4月19日 星期二

(OwO)電腦圖學week09_4/19

Opencv

1.下載 OpenCV-2.1.0-win32-vs2008.exe

2.安裝時,Add PATH 選第二個,路徑用預設(亂改會不好處理

3.安裝後要將CodeBlocks重開

4.CodeBlocks內設定:Setting -> Compiler -> Search directories

  • Include:Compiler,Add,C:\opencv2.1\include
  • Lib:Linker,Add,C:\opencv2.1\lib
  • Linker settings,Add,cv210、cxcore210、highgui210



OpenCV讀圖、秀圖


#include<opencv/highgui.h>///使用opencv的外掛

int main()

{

    IplImage * img=cvLoadImage("檔名.jpg"); ///讀圖

    cvShowImage("week09", img); ///秀圖

    cvWaitKey(0);///等任意按鍵繼續

}




結合OpenCV、OpenGL,圖檔要放在 freeglut\bin


#include<GL/glut.h>

#include<opencv/highgui.h>

void myTexture()

{

    IplImage * img=cvLoadImage("檔案.png");///讀圖

    cvShowImage("opencv", img);

    ///cvWaitKey(0);///可以不用寫,因為會被glutMainLoop()卡住

}

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("week09 texture");

    glutDisplayFunc(display);

    myTexture();

    glutMainLoop();

}


貼圖


#include<GL/glut.h>

#include <opencv/highgui.h> ///使用 OpenCV 2.1 比較簡單, 只要用 High GUI 即可

#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("week09 texture");

    glutDisplayFunc(display);

    myTexture("檔案.jpg");

    glutMainLoop();

}

VERY BEAUTIFUL, VERY POWERFUL

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