顯示具有 09160562_黃富驛 標籤的文章。 顯示所有文章
顯示具有 09160562_黃富驛 標籤的文章。 顯示所有文章

2022年6月7日 星期二

OuO圖學筆記Week15

 (1)

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


#include <windows.h>

#include <stdio.h>

int main()

{

    printf("PlaySound()\n");

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

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

}

如果錯誤會有錯誤音效


1-2.開 CodeBlocks/empty file-->week15_SND_SNTC_ASNYC.cpp

 直接播

#include <windows.h>

#include <stdio.h>

int main()

{

    PlaySound("do.wav",NULL,SND_SYNC);///ASYNC不等待

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

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

}


等待

#include <windows.h>

#include <stdio.h>

int main()

{

    PlaySound("07042111.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);

    }

}


(2)

2-1.開 CodeBlocks/empty file-->week15_MP3.cpp

2-2.moodle 下載CMP3_MCI.h

2-3.


#include <stdio.h>

#include "CMP3_MCI.h"

CMP3_MCI mp3;


int main()

{

    mp3.Load("07042111.mp3");

    mp3.Play();


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

    int N;///為了程式不要結束

    scanf("%d",&N);

}


2-4.開新專案-->week15_angle_TRT_again

      可以讀動作,長按r可播錄的動作


2-5.改上週程式


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

    glutCreateWindow("week15 angle TRT again");

    glutKeyboardFunc(keyboard);

    glutMouseFunc(mouse);

    glutMotionFunc(motion);

    glutDisplayFunc(display);

    glutMainLoop();

}

 按s可以存動作


找txt複製動作可以做很多次r



(3)HW

3-1.開新專案week15_HW

3-2.用notepad++改目錄-->"."

3-3.把freeglut/bin/freeglut.dll複製到cbp資料夾

3-4.把 glm.h & glm.cpp & gundam的data目錄

OuO圖學筆記Week16

alpha内插公式:alpha:

angle

alpha*新. + (1-a1pha)*舊

ex.

alpha: 0=>舊

alpha: 0.5半新半舊

alpha: 1=>新

使用 Excel or Google Spreadsheet 做練習


1-2.開glut ,用上週TRT_again程式改

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

}

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

        glutPostRedisplay();

        t++;

    }

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

    glutCreateWindow("week15 angle TRT again");

    glutKeyboardFunc(keyboard);

    glutMouseFunc(mouse);

    glutMotionFunc(motion);

    glutDisplayFunc(display);

    glutMainLoop();

}

長按P會有動畫


1-3.

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

}

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)

{

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

        ///glutPostRedisplay();

        ///t++;

    }

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

    glutCreateWindow("week15 angle TRT again");

    glutKeyboardFunc(keyboard);

    glutMouseFunc(mouse);

    glutMotionFunc(motion);

    glutDisplayFunc(display);

    glutMainLoop();

}


(2)

2-1.開windows/Projection



觀察運鏡效果

2-2.aspect ratio 長寬比 ex.1920 x1080 , 1280 x 720, 16 : 9 , 4 : 3


(3)

3-1.用十行程式改

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

}

int main(int argc,char**argv)

{

    glutInit(&argc,argv);

    glutInitDisplayMode(GLUT_DOUBLE|GLUT_DEPTH);

    glutCreateWindow("Week16 camara");


    glutDisplayFunc(display);

    glutReshapeFunc(reshape);///範例用resize

    glutMainLoop();

}


3-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);///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 camara");

    glutMotionFunc(motion);

    glutDisplayFunc(display);

    glutReshapeFunc(reshape);

    glutMainLoop();

}

2022年5月24日 星期二

OuO圖學筆記Week14

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

1-2.寫程式

#include<stdio.h>

int main(){指標fout開檔案(檔名,write模式)

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

        printf("Hello World\n");

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

    fclose(fout);關檔案

}

1-3.

#include<stdio.h>

int main(){

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

    fprintf(fout,"3.1415926\n");

    fclose(fout);


    float angle=0;

    FILE*fin=fopen("file.txt","r");

    fscanf(fin,"%f",&angle);

    printf("讀到角度:%f",angle);

    fclose(fin);

}

1-4.開上週程式改


#include <GL/glut.h>

#include <stdio.h>


float angle[20], oldX=0;

int angleID=0;

FILE * fout =NULL;

void myWrite(){

    if(fout==NULL)

        fout=fopen("file.txt,","w+");


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

        printf("%.1f",angle[i]);

    }

}

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

{

    if(key=='0') angleID=0;

    if(key=='1') angleID=1;

    if(key=='2') angleID=2;

    if(key=='3') angleID=3;

}

void mouse(int button,int state,int x,int y)

{

    oldX=x;

}

void motion(int x,int y)

{

    angle[angleID]+=(x-oldX);

    myWrite();

    oldX=x;

    glutPostRedisplay();

}


(2)

2-1.

#include <GL/glut.h>

#include <stdio.h>


float angle[20], oldX=0;

int angleID=0;

FILE * fout =NULL;

void myWrite(){

    if(fout==NULL)

        fout=fopen("file.txt,","w+");


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

        printf( " %.1f ",angle[i]);

        fprintf(fout,"  %.1f  ",angle[i]);

    }

    printf("\n");

    fprintf(fout,"\n");

}

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

{

    if(key=='0') angleID=0;

    if(key=='1') angleID=1;

    if(key=='2') angleID=2;

    if(key=='3') angleID=3;

}

void mouse(int button,int state,int x,int y)

{

    oldX=x;

}

void motion(int x,int y)

{

    angle[angleID]+=(x-oldX);

    myWrite();

    oldX=x;

    glutPostRedisplay();

}





2-2.

#include <GL/glut.h>

#include <stdio.h>


float angle[20], oldX=0;

int angleID=0;///關節ID

FILE * fout =NULL,*fin=NULL;

void myWrite(){

    if(fout==NULL)

        fout=fopen("file.txt,","w+");


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

        printf("%.1f",angle[i]);

        fprintf(fout," %.1f ",angle[i]);

    }f後面要加空格

    printf("\n");

    fprintf(fout,"\n");

}


void myRead(){

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

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

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

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

    }

    glutPostRedisplay();

}


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

{

    if(key=='r') myRead();

    if(key=='0') angleID=0;

    if(key=='1') angleID=1;

    if(key=='2') angleID=2;

    if(key=='3') angleID=3;

}

void mouse(int button,int state,int x,int y)

{

    oldX=x;

}

void motion(int x,int y)

{

    angle[angleID]+=(x-oldX);

    myWrite();

    oldX=x;

    glutPostRedisplay();

}

長按r就會顯示錄到的動作




2-3.


    (1)用notepad++ 開 cbp檔 ---> 把working_dir 裡的路徑改成"."

    (2)把freeglut裡的freeglut.dll複製到專案中


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


(3)

3-1.開新專案week14_timer

3-2.

#include <GL/glut.h>

#include <stdio.h>


void timer(int t){

    printf("起床,現在時間 : %d\n",t);

}

void display(){

}

int main(int argc,char**argv)

{

    glutInit(&argc,argv);

    glutInitDisplayMode(GLUT_DOUBLE|GLUT_DEPTH);

    glutCreateWindow("Week14 timer");


    glutTimerFunc(1000,timer,1);

    glutTimerFunc(2000,timer,2);

    glutTimerFunc(3000,timer,3);

    glutTimerFunc(4000,timer,4);

    glutTimerFunc(5000,timer,5);


    glutDisplayFunc(display);

    glutMainLoop();

}



3-3.

#include <GL/glut.h>

#include <stdio.h>


void timer(int t){

    printf("起床,現在時間 : %d\n",t);

    glutTimerFunc(1000,timer,t+1);

}

void display(){

}

int main(int argc,char**argv)

{

    glutInit(&argc,argv);

    glutInitDisplayMode(GLUT_DOUBLE|GLUT_DEPTH);

    glutCreateWindow("Week14 timer");

    

     glutTimerFunc(1000,timer,1);

    glutTimerFunc(2000,timer,2);

    glutTimerFunc(3000,timer,3);

    glutTimerFunc(4000,timer,4);

    glutTimerFunc(5000,timer,5);


    glutTimerFunc(5000,timer,0);

    glutDisplayFunc(display);

    glutMainLoop();

}



3-4.

#include <GL/glut.h>

#include <stdio.h>

#include <mmsystem.h>


void timer(int t){

    printf("起床,現在時間 : %d\n",t);

    PlaySound("do.wav",NULL,SND_ASYNC);

    glutTimerFunc(1000,timer,t+1);

}

void display(){

}

int main(int argc,char**argv)

{

    glutInit(&argc,argv);

    glutInitDisplayMode(GLUT_DOUBLE|GLUT_DEPTH);

    glutCreateWindow("Week14 timer");


    glutTimerFunc(5000,timer,0);

    glutDisplayFunc(display);

    glutMainLoop();

}

會有噹噹聲



2022年5月17日 星期二

OuO圖學筆記Week13

step1-1 做一個長方形

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



step1-2 做一個手臂

#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(x, y, z);///3手臂位置
        ///glRotatef( angle, 0, 0, 1);///2旋轉,對z軸
        ///glTranslatef(x2, y2, z2);///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();
}



step
#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(x, y, z);///3手臂位置
        ///glRotatef( angle, 0, 0, 1);///2旋轉,對z軸
        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();
}

step
#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(x, y, z);///3手臂位置
        glRotatef( angle, 0, 0, 1);///2旋轉,對z軸
        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();
}


step
#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旋轉,對z軸
        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();
}


step 滑鼠可以旋轉手臂
#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();
}
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旋轉,對z軸
        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();
}


///Week13 rect 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();
}
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旋轉,對z軸
        glTranslatef(-0.3, -0.4, 0);///1旋轉軸中心
        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();
}
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();
}


///Week13 rect 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();
}
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旋轉,對z軸
        glTranslatef(-0.3, -0.4, 0);///1旋轉軸中心
        glColor3f(1,0,0);///紅色
        glRectf(0.3, 0.5, 0.7, 0.3);///上手臂

        glPushMatrix();
            ///glTranslatef(x, y, z);///3把手臂位置掛回去
            glRotatef( angle, 0, 0, 1);///2旋轉,對z軸
            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();
}


把Rotate註解用掉

///Week13 rect 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();
}
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旋轉,對z軸
        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旋轉,對z軸
            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旋轉,對z軸
        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旋轉,對z軸
            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();
}


2022年5月10日 星期二

OuO圖學筆記Week12

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

 step:1-2.解壓縮,把data放到windows裡

 step:1-3.開Transformation.exe


step:2-1.打 120.125.80.50/GL 練習

step:2-2.點 ToDraw,可在左邊黑色方塊畫圖(如下圖)

step:2-3.ctrl+R Reload可以清空

step:2-4.畫身體跟手臂,改程式碼順序,按angle 再按空白建,會自動旋轉



step:2-5.點 glTranslatef ,可以移動左邊的東西


step:3-1.開 CodeBlocks/GLUT/GLUT
step:3-2.貼十行程式 ///茶壺會一直轉

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

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

        glutSolidTeapot(0.3);

    glPopMatrix();

    glutSwapBuffers();

    angle+=0.02;///速度太快再加一個0

}

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();
        glTranslatef(0.4, 0, 0);
        glRotatef( angle, 0, 0, 1);
        glTranslatef(0.2, 0, 0);
        glColor3f(1,0,0);
        glutSolidTeapot( 0.2 );
        glPushMatrix();
            glTranslatef(0.2, 0, 0);
            glRotatef( angle, 0, 0, 1);
            glTranslatef(0.2, 0, 0);
            glColor3f(1,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();
}

#include <GL/glut.h> ///左手合體
float angle=0;
void display()
{
    glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);

    glColor3f(1,1,1);///白
    glutSolidTeapot(0.3);///身體茶壺

    glPushMatrix();///右手臂
        glTranslatef(0.4,0,0);///把茶壺掛右邊
        glRotatef(angle,0,0,1);///旋轉
        glTranslatef(0.2,0,0);///軸心放中間
        glColor3f(1,0,0);///紅
        glutSolidTeapot(0.2);///手臂茶壺
        glPushMatrix();
            glTranslatef(0.4,0,0);///把茶壺掛右邊
            glRotatef(angle,0,0,1);///旋轉
            glTranslatef(0.2,0,0);///軸心放中間
            glColor3f(1,0,0);///紅
            glutSolidTeapot(0.2);///手臂茶壺
        glPopMatrix();
    glPopMatrix();

    glPushMatrix();///左手臂
        glTranslatef(-0.4,0,0);///把茶壺掛右邊
        glRotatef(-angle,0,0,1);///旋轉
        glTranslatef(-0.2,0,0);///軸心放中間
        glColor3f(1,0,0);///紅
        glutSolidTeapot(0.2);///手臂茶壺
        glPushMatrix();
            glTranslatef(-0.4,0,0);///把茶壺掛右邊
            glRotatef(-angle,0,0,1);///旋轉
            glTranslatef(-0.2,0,0);///軸心放中間
            glColor3f(1,0,0);///紅
            glutSolidTeapot(0.2);///手臂茶壺
        glPopMatrix();
    glPopMatrix();

    glutSwapBuffers();
    angle+=0.05;
}
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日 星期二

OuO圖學筆記Week11

step01載myGundam.zip,data(file)放到工作目錄freeglut\bin\data


#include <opencv/highgui.h>

int main(){

    IplImage * img=cvLoadImage("data/Diffuse.jpg");

    cvShowImage("week11",img);

    cvWaitKey();

}


如下圖:


step02-1 偷week09或是week10的程式
把鋼蛋變成茶壺材質


  把這行* myTexture("gwawa.jpg");*  改成  myTexture("data/Diffuse.jpg");





step2-2把 glm.c & glm.h & transformation.c 加到 week11_gundam ,
把glm.c改附檔名-->glm.cpp
step2-3.加程式碼 codeblocks / week11_gundam 按右鍵 add file 加glm.cpp
step2-4.寫程式碼
#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;
}
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) ; ///重新計算模型頂點的法向量
    }
    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();
}


step2-5圖片方向不對,用小畫家把Diffuse.jpg垂直翻轉
step2-6加3D,旋轉
#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();
}



VERY BEAUTIFUL, VERY POWERFUL

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