2022年6月25日 星期六

電腦圖學week16課堂筆記

 課堂內容 : 讓動畫自動動作、運鏡
1.第一個程式碼 : (複製之前的程式碼)
#include <GL/glut.h>
#include <stdio.h>
float angle[20],oldX=0;
float newangle[20],oldangle[20];
int angleid=0;
FILE * fout = NULL , *fin =NULL;
void write()
{
    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 read()
{
    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 interpolate(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) read();
    interpolate( (t%50)/50.0 );
    glutPostRedisplay();
    glutTimerFunc(5,timer,t+1);
}

void key(unsigned char key,int x,int y)
{
    if(key=='p')
    {
        read();
        glutTimerFunc(0,timer,0);
    }
    if(key=='s') write();
    if(key=='r') read();
    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);
    glutCreateWindow("week16");
    glutMouseFunc(mouse);
    glutKeyboardFunc(key);
    glutMotionFunc(motion);
    glutDisplayFunc(display);
    glutMainLoop();
}
按 P 後動畫會自己動作

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


電腦圖學week15課堂筆記

 1.今日課堂內容 : 用程式撥放聲音
第一個程式 : 
#include <windows.h>
#include <stdio.h>
int main()
{
    printf("播放音樂前\n");
    PlaySound("07071064.wav", NULL, SND_SYNC);
    printf("播放音樂後\n");
}
播放後會有錯誤的音效,只要把音檔跟 CodeBlocks 放在同一個資料夾就可以解決了


電腦圖學week14課堂筆記

 課堂內容 : fprintf()、fscanf() 寫檔和讀檔;fprintf()、fscanf() + project

1.寫檔
#include <stdio.h>
int main()
{
    FILE * fout = fopen("file.txt", "w+");
    printf("Hello World\n");
    fprintf(fout,"Hello World\n");
    fclose(fout);
}
2.讀檔
#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);
}
3.加入上週的程式
#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\n", 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();
}
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("week14");
    glutMouseFunc(mouse);
    glutMotionFunc(motion);
    glutKeyboardFunc(keyboard);
    glutDisplayFunc(display);
    glutMainLoop();
}
最後可以做成動畫 : 
#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 );
    glutCreateWindow("week14");
    glutKeyboardFunc(keyboard);
    glutMouseFunc(mouse);
    glutMotionFunc(motion);
    glutDisplayFunc(display);
    glutMainLoop();
}
按 r 可以重複剛剛的動作








電腦圖學week13課堂筆記

今日課堂內容 : (1) glRectf()函式 和 glRectf()做的手臂
1. glRectf() 四個函數分別為 (1)右上X,右上Y,左上X,左上Y
第一個程式 : 
#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();
}
2. glRectf()加上手臂
第二個程式 :
#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( x, y, z);
            glRotatef(angle, 0,0,1);
            glTranslatef(x2,y2,z2);
            glColor3f(0,1,0);
            glRectf( 0.7, 0.5, 1.0, 0.3);
        glPopMatrix();
    glPopMatrix();
    glutSwapBuffers();
}
 最後的程式碼 : 
#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);
    glutCreateWindow("week13");
    glutMouseFunc(mouse);
    glutMotionFunc(motion);
    glutKeyboardFunc(keyboard);
    glutDisplayFunc(display);
    glutMainLoop();
}







2022年6月23日 星期四

 WEEK16 天氣晴

1. alpha 內插公式: 0.0 ~ 1.0

➡ angle=alpha*新角度+(1-alpha)*舊角度

利用excel來練習



2-1.複製上週 week15-angle-TRT-again 程式碼 

檔名: week16-interpolation

改善按r之後動作不連續的狀況,利用alpha內插法

程式碼:


4.改視角,aspect ratio:長寬比(寬:長)

#include <GL/glut.h>

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, ///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);///3D Model+View

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


    glutDisplayFunc(display);

    glutMotionFunc(motion);

    glutReshapeFunc(reshape);///初始視角

    glutMainLoop();

}



2022年6月22日 星期三

Ru的電腦圖學筆記>

 

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年6月14日 星期二

電腦圖學筆記week17

 筆記

Step

程式環境 : week01 (freeglut,GLUT專案)

                   week10 (OpenCV的設定)

                   week16 (改CBP的working_dir)

點線面顏色 : 10行程式碼  display()

                        main()前3行設定,最後一行glutMainLoop()

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

打光 : 8行 + 10多行

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

讀入3D模型 : 模型在工作目錄,glm.h、glm.cpp加進去 #include "glm.h" 再 GLMmodel * body;     再...

貼圖 : OpenCV 及 myTexture範例,還有圖,而且畫glmDraw(pmodel , GLM_TEXTURE  GLM_SMOOTH);

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

攝影機與運鏡 : 透視投影法 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, ///eye
              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 來知道值在哪裡
           要經常註解程式,才能找到 T-R-T的值

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

VERY BEAUTIFUL, VERY POWERFUL

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