顯示具有 08161035_游騰翔 標籤的文章。 顯示所有文章
顯示具有 08161035_游騰翔 標籤的文章。 顯示所有文章

2022年6月14日 星期二

圖學筆記week17

 今天一開始老師講解期末作業要如何繳交及評分

我們會用sli.do這個投票網站進行投票

每個人都可以給1~5分

期末作業的分數就以此而定


然後老師開始幫我們複習並整合期末作業要怎麼做

2022年6月7日 星期二

圖學筆記week16

 今天老師教我們alpha內插公式

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

這樣才能讓動畫跑得更加順暢

並且也用excel示範了一次


然後我們把上次的程式碼複製過來並進行修改

可以看到之前會卡卡的動作,現在變得非常流暢
#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] );///檔案印出來
    }///印出20個數字
    printf("\n");///每呼叫一次, 小黑跳行
    fprintf(fout, "\n");///每呼叫一次, 檔案也跳行
}///這裡老師沒有fclose
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] );
    }
    glutPostRedisplay();///重畫畫面
}
void myinterporlate(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();
        myinterporlate((t%30)/30.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(); ///���b��
        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("week16 interporlation");

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

    glutMainLoop();
}

不過這樣還是需要按住P才能讓他播放,我們要改成按一下就可以自動播放
所以我們要加上timer
#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] );///檔案印出來
    }///印出20個數字
    printf("\n");///每呼叫一次, 小黑跳行
    fprintf(fout, "\n");///每呼叫一次, 檔案也跳行
}///這裡老師沒有fclose
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] );
    }
    glutPostRedisplay();///重畫畫面
}
void myinterporlate(float alpha){
        for(int i=0;i<20;i++){
            angle[i]=alpha * Newangle[i] + (1-alpha) * Oldangle[i];
        }
}
void timer(int t){
    if(t%50==0) myRead();
    myinterporlate((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);
        ///myinterporlate((t%30)/30.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);
    ///glutInitWindowSize(600,600);
    glutCreateWindow("week16 interporlation");

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

    glutMainLoop();
}




由於我們接下來要學攝影機
所以老師要我們先打開projection.exe檔案來看看


我們把一開始的範例程式碼複製起來
然後全部刪掉開始修改
把基礎10行程式打好
再加上reshape函式



#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,
              0,0,0,
              0,1,0);
}
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");
    glutDisplayFunc(display);
    glutReshapeFunc(reshape);
    glutMainLoop();
}

最後我們再加上motion
可以使用滑鼠拖動來改變視角


#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,
              0, 0, 0,
              0, 1, 0);
}
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,
              0 , 0, 0,
              0, 1, 0);
    glutPostRedisplay();
}
int main(int argc,char ** argv)
{
    glutInit(&argc,argv);
    glutInitDisplayMode(GLUT_DOUBLE|GLUT_DEPTH);
    glutCreateWindow("week16");
    glutMotionFunc(motion);
    glutDisplayFunc(display);
    glutReshapeFunc(reshape);
    glutMainLoop();
}



2022年5月31日 星期二

圖學week15

 今天一開始我們先學播聲音

因為沒有音檔,所以一開始應該會播錯誤訊息的音效

然而可能程式會錯誤無法播放

就必須在compilier>linker裡設定winmm




然後是MP3檔 要先去moodle下載CMP3_MCI.h
才能播放

#include <stdio.h>
#include "CMP3_MCI.h"
CMP3_MCI mp3;
int main()
{
    mp3.Load("07042111.mp3");
    mp3.Play();
    printf("隨便輸入數字程式就卡住了: ");
    int N;
    scanf("%d",&N);

}

然後我們把上週教的動畫程式碼複製過來

因為上週教的程式會動得很慢,所以我們要改善它

把myWrite()註解掉,在keyboard裡新增save的程式


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

    }

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

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

    ///glutInitWindowSize(600,600);

    glutCreateWindow("week15");

    glutKeyboardFunc(keyboard);

    glutMouseFunc(mouse);

    glutMotionFunc(motion);

    glutDisplayFunc(display);

    glutMainLoop();

}


2022年5月24日 星期二

week14

 今天一開始老師教我們利用指標fout寫檔案


執行程式後到目標資料夾就會看到多了一個file.txt檔案

然後是用fscanf來讀檔


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

}

然後就可以搭配上周的程式碼
當手臂轉動時就會寫出座標


加上跳行讓他更好看

然後利用讀檔就可以讓他跑動畫
先手動讓他記錄好之後按住R就可以讓他跑出剛剛做的動作

#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]);
    }
    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();
}
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("week14");
    glutKeyboardFunc(keyboard);
    glutMouseFunc(mouse);
    glutMotionFunc(motion);
    glutDisplayFunc(display);
    glutMainLoop();
}
然後老師教我們timer


#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);
    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(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日 星期二

week11




這周一開始我們先複習讀圖檔
把compiler設定都設定好之後
把Mygundum.zip裡的data放到工作目錄
然後輸入程式碼

#include <opencv/highgui.h>
        int main()
        { /// Ipl 是 Intel performance library 的縮寫
    
         IplImage * img = cvLoadImage("data/Diffuse.jpg"); //讀圖
        cvShowImage("week11", img); //秀圖
        cvWaitKey(0); //等任意鍵繼續

        }



然後我們去複製前兩周教的程式碼
就可以讓茶壺套用gundum材質

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

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

    glutMainLoop();
}
然後使用pmodel秀出gundam模型

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

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

    glutMainLoop();
}






VERY BEAUTIFUL, VERY POWERFUL

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