2021年6月7日 星期一

吉親摳ㄎㄎ-Week16

電腦圖學

Week16

首先延續上禮拜沒教完的,把Week15的程式下載下來

1.把關節變成20個,讓按下不同鍵可以轉動不同角度
程式碼:
#include <stdio.h>
#include <GL/glut.h>
float angle[20]={};
float diff=2;
int  angleID=0;
void timer (int t)
{
    glutTimerFunc(30,timer,t+1);  ///設定新的timer(等多久,timer ,參數)
    angle[angleID]+=diff;
    if(angle[angleID]>90) diff=-2;
    if(angle[angleID]<0) diff=+2;
    glutPostRedisplay();
}
void keyboard(unsigned char key,int x,int y){
    if(key=='0') angleID=0; ///之後timer會改變關節 angle[0]
    if(key=='1') angleID=1;
    if(key=='2') angleID=2;
    if(key=='3') angleID=3;
}
void display()
{
     glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
    glPushMatrix();
        glutSolidTeapot( 0.3 );///身體
        glPushMatrix();
            glTranslatef(-0.3,0,0);
            glRotatef(angle[0], 0,0,1);
            glTranslatef(-0.3,0,0);
            glutSolidTeapot( 0.3 );///左手臂, 但是,要用 T-R-T移位置
            glPushMatrix();
                glTranslatef(-0.3, 0,0);
                glRotatef(angle[1], 0,0,1);
                glTranslatef(-0.3, 0,0);
                glutSolidTeapot( 0.3 );///左手肘
            glPopMatrix();
        glPopMatrix();
        glPushMatrix();
            glTranslatef(+0.3,0,0);
            glRotatef(-angle[2], 0,0,1);
            glTranslatef(+0.3,0,0);
            glutSolidTeapot( 0.3 );///右手臂
            glPushMatrix();
                glTranslatef(+0.3,0,0);
                glRotatef(-angle[3], 0,0,1);
                glTranslatef(+0.3,0,0);
                glutSolidTeapot( 0.3 );///右手肘
            glPopMatrix();
        glPopMatrix();
    glPopMatrix();
    glutSwapBuffers();
}
int main(int argc,char**argv)
{
    glutInit(&argc,argv);
    glutInitDisplayMode(GLUT_DOUBLE |GLUT_DEPTH);
    glutCreateWindow("Week16 many angles");

    glutKeyboardFunc(keyboard);
    glutDisplayFunc(display);
    glutTimerFunc(0,timer,0);
    glutMainLoop();
}

2.那因為上面的程式碼是用時間去更新,實在太不先進,所以我們改用mouse和motion來控制

程式碼:
#include <stdio.h>
#include <GL/glut.h>
float angle[20]={};
float diff=2;
int  angleID=0;
void timer (int t)
{
    ///timer什麼都不做
}
///用mouse和motion來控制關節
int oldX=0,oldY=0;
void mouse(int button,int state,int x,int y)
{
    oldX = x;
    oldY = y;
}
void motion(int x,int y)
{
    angle[angleID] += x - oldX;
    oldX = x;
    glutPostRedisplay();
}
void keyboard(unsigned char key,int x,int y){
    if(key=='0') angleID=0; ///之後timer會改變關節 angle[0]
    if(key=='1') angleID=1;
    if(key=='2') angleID=2;
    if(key=='3') angleID=3;
}
void display()
{
     glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
    glPushMatrix();
        glutSolidTeapot( 0.3 ); ///身體
        glPushMatrix();
            glTranslatef(-0.3,0,0);
            glRotatef(angle[0], 0,0,1);
            glTranslatef(-0.3,0,0);
            glutSolidTeapot( 0.3 ); ///左手臂, 但是,要用 T-R-T移位置
            glPushMatrix();
                glTranslatef(-0.3, 0,0);
                glRotatef(angle[1], 0,0,1);
                glTranslatef(-0.3, 0,0);
                glutSolidTeapot( 0.3 ); ///左手肘
            glPopMatrix();
        glPopMatrix();
        glPushMatrix();
            glTranslatef(+0.3,0,0);
            glRotatef(-angle[2], 0,0,1);
            glTranslatef(+0.3,0,0);
            glutSolidTeapot( 0.3 ); ///右手臂
            glPushMatrix();
                glTranslatef(+0.3,0,0);
                glRotatef(-angle[3], 0,0,1);
                glTranslatef(+0.3,0,0);
                glutSolidTeapot( 0.3 ); ///右手肘
            glPopMatrix();
        glPopMatrix();
    glPopMatrix();
    glutSwapBuffers();
}
int main(int argc,char**argv)
{
    glutInit(&argc,argv);
    glutInitDisplayMode(GLUT_DOUBLE |GLUT_DEPTH);
    glutCreateWindow("Week16 many angles");

    glutMouseFunc(mouse);
    glutMotionFunc(motion);
    glutKeyboardFunc(keyboard);
    glutDisplayFunc(display);
    ///glutTimerFunc(0,timer,0);
    glutMainLoop();
}

3.做完上禮拜沒教完的部分,加上fopen fprintf,存檔
程式碼:
#include <stdio.h>
#include <GL/glut.h>
float angle[20]={};
float diff=2;
int  angleID=0;
void timer (int t)
{
    ///timer什麼都不做
}
///用mouse和motion來控制關節
int oldX=0,oldY=0;
void mouse(int button,int state,int x,int y)
{
    oldX = x;
    oldY = y;
}
void motion(int x,int y)
{
    angle[angleID] += x - oldX;
    oldX = x;
    glutPostRedisplay();
}
#include <stdio.h> ///才能用 fopen
FILE * fout =NULL;
void keyboard(unsigned char key,int x,int y){
    if(key=='0') angleID=0; ///之後timer會改變關節 angle[0]
    if(key=='1') angleID=1;
    if(key=='2') angleID=2;
    if(key=='3') angleID=3;
    if(key=='s'){ ///想要存檔
        if( fout == NULL) fout=fopen("angle.txt","w+");
        for(int i=0;i<20;i++)printf("%.2f ",angle[i]);
        printf("\n");
        for(int i=0;i<20;i++)fprintf(fout,"%.2f",angle[i]);
        fprintf(fout,"\n");
    }
}
void display()
{
     glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
    glPushMatrix();
        glutSolidTeapot( 0.3 );///身體
        glPushMatrix();
            glTranslatef(-0.3,0,0);
            glRotatef(angle[0], 0,0,1);
            glTranslatef(-0.3,0,0);
            glutSolidTeapot( 0.3 );///左手臂, 但是,要用 T-R-T移位置
            glPushMatrix();
                glTranslatef(-0.3, 0,0);
                glRotatef(angle[1], 0,0,1);
                glTranslatef(-0.3, 0,0);
                glutSolidTeapot( 0.3 );///左手肘
            glPopMatrix();
        glPopMatrix();
        glPushMatrix();
            glTranslatef(+0.3,0,0);
            glRotatef(-angle[2], 0,0,1);
            glTranslatef(+0.3,0,0);
            glutSolidTeapot( 0.3 );///右手臂
            glPushMatrix();
                glTranslatef(+0.3,0,0);
                glRotatef(-angle[3], 0,0,1);
                glTranslatef(+0.3,0,0);
                glutSolidTeapot( 0.3 );///右手肘
            glPopMatrix();
        glPopMatrix();
    glPopMatrix();
    glutSwapBuffers();
}
int main(int argc,char**argv)
{
    glutInit(&argc,argv);
    glutInitDisplayMode(GLUT_DOUBLE |GLUT_DEPTH);
    glutCreateWindow("Week16 many angles");

    glutMouseFunc(mouse);
    glutMotionFunc(motion);
    glutKeyboardFunc(keyboard);
    glutDisplayFunc(display);
    ///glutTimerFunc(0,timer,0);
    glutMainLoop();
}

4.加入讀檔,把剛才存下的檔印出來

程式碼:
#include <stdio.h>
#include <GL/glut.h>
float angle[20]={};
float diff=2;
int  angleID=0;
void timer (int t)
{
    ///timer什麼都不做
}
///用mouse和motion來控制關節
int oldX=0,oldY=0;
void mouse(int button,int state,int x,int y)
{
    oldX = x;
    oldY = y;
}
void motion(int x,int y)
{
    angle[angleID] += x - oldX;
    oldX = x;
    glutPostRedisplay();
}
#include <stdio.h> ///才能用 fopen
FILE * fout =NULL;
FILE * fin =NULL;
void keyboard(unsigned char key,int x,int y){
    if(key=='0') angleID=0; ///之後timer會改變關節 angle[0]
    if(key=='1') angleID=1;
    if(key=='2') angleID=2;
    if(key=='3') angleID=3;
    if(key=='s'){ ///想要存檔
        if( fout == NULL) fout=fopen("angle.txt","w+");
        for(int i=0;i<20;i++)printf("%.2f ",angle[i]);
        printf("\n");
        for(int i=0;i<20;i++)fprintf(fout,"%.2f",angle[i]);
        fprintf(fout,"\n");
    } ///必須把程式關閉,才會更新angle.txt
    if(key=='r'){
        if(fin == NULL) fin=fopen("angle.txt","r");
        for(int i=0;i<20;i++)fscanf(fin,"%f",&angle[i]);
        glutPostRedisplay();
    }
}
void display()
{
     glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
    glPushMatrix();
        glutSolidTeapot( 0.3 );///身體
        glPushMatrix();
            glTranslatef(-0.3,0,0);
            glRotatef(angle[0], 0,0,1);
            glTranslatef(-0.3,0,0);
            glutSolidTeapot( 0.3 );///左手臂, 但是,要用 T-R-T移位置
            glPushMatrix();
                glTranslatef(-0.3, 0,0);
                glRotatef(angle[1], 0,0,1);
                glTranslatef(-0.3, 0,0);
                glutSolidTeapot( 0.3 );///左手肘
            glPopMatrix();
        glPopMatrix();
        glPushMatrix();
            glTranslatef(+0.3,0,0);
            glRotatef(-angle[2], 0,0,1);
            glTranslatef(+0.3,0,0);
            glutSolidTeapot( 0.3 );///右手臂
            glPushMatrix();
                glTranslatef(+0.3,0,0);
                glRotatef(-angle[3], 0,0,1);
                glTranslatef(+0.3,0,0);
                glutSolidTeapot( 0.3 );///右手肘
            glPopMatrix();
        glPopMatrix();
    glPopMatrix();
    glutSwapBuffers();
}
int main(int argc,char**argv)
{
    glutInit(&argc,argv);
    glutInitDisplayMode(GLUT_DOUBLE |GLUT_DEPTH);
    glutCreateWindow("Week16 many angles");

    glutMouseFunc(mouse);
    glutMotionFunc(motion);
    glutKeyboardFunc(keyboard);
    glutDisplayFunc(display);
    ///glutTimerFunc(0,timer,0);
    glutMainLoop();
}

5.把產生的txt檔的位置,改成當前專案下,這個做過很多次了,只要打專案的cbp檔裡的working_dir的位置改成".",並把freeglut.dll複製過來即可。

6.因為每一次按存檔太麻煩了,所以把存檔的程式碼改放到motion 裡,使每做一次動作記錄一次,記得也要把宣告的往上移



程式碼:


下禮拜端午節記得上課喔QQ







沒有留言:

張貼留言

Week18期末作業(橘貓的跳舞熊熊)

 期末作業(橘貓的跳舞熊熊) 影片: https://youtu.be/R89tptMaQZw 程式碼: #include <opencv/highgui.h> #include <opencv/cv.h> #include <GL/glut.h...