2023年5月18日 星期四

Fafa的奇妙冒險

 Week14

Week 14-1.1

Step 1:先下載並安裝Git

Step 2:打開Git Bash打下列的指令

指令:
cd desktop

git clone 你的網址

cd 剛剛下載的目錄

Step 3:打開CodeBlock,並打開GLUT project


Step 4:將下列程式碼複製進GLUT project中

程式碼:
#include<GL/glut.h>
float angle =0;
void timer(int t)
{
    glutTimerFunc(500,timer,t+1);
    angle += 90;
    glutPostRedisplay();
}

void display()
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glPushMatrix();
        glRotatef(angle,0,0,1);
        glutSolidTeapot(0.3);
    glPopMatrix();
    glutSwapBuffers();
}

int main(int argc,char *argv[])
{
    glutInit(&argc,argv);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
    glutCreateWindow("week14");

    glutTimerFunc(3000,timer,0);
    glutDisplayFunc(display);

    glutMainLoop();
}

Step 5:觀察圖片的變動情況,完成!








Week 14-1.2

將上個程式碼增加幾行程式片段

Step 1:使用 Week14-1.1的程式碼

Step 2:在 time()和display()之間新增一行board函式

函式:
void keyboard(unsigned char key,int x,int y)
{
    glutTimerFunc(0,timer,0);
}

Step 3:在main()中,將glutTimerFunc()修改為下列程式碼

程式碼:
glutKeyboardFunc(keyboard);

Step 4:觀察程式變動情況,完成!


Week 14-2.1


Step 1:先開啟一個Excel檔案

Step 2:輸入資料:

在A1輸入【timer】,並由上到下打上0~20

在B2格打上:
=A2/20
並點右下角的點點下拉至B22,可自動套用該公式並對照每格,做函式的自動套用及計算







把【舊的角度】打在C1,並從C2~C22打上74

在D1打上【新的角度】

在E1打上新的角度,並在E2打上:
=B2*D2+(1-B2)*C2
並按住右下角的方點,下拉至E22










完成後如下圖:





Step 3:將檔案儲存


將檔案使用Git指令備份到Github上

Git指令:
git add .

git config --gloabl user.email "你的gite mail"

git config --global user.name"你的git 名字"

git commit -m"欲輸入的文字"

git push

Week 14-2.2


Step 1:開啟CodeBlock,並開啟GLUT project

Step 2:將下列程式碼複製並貼上至GLUT project

程式碼:

#include<GL/glut.h>
float angle =0,newangle,oldangle;
float oldX=0;
void timer(int t)
{
    if(t<100)glutTimerFunc(33,timer,t+1);
    float alpha = t/100.0;
    angle = alpha * newangle+(1-alpha)*oldangle;
    glutPostRedisplay();
}

void keyboard(unsigned char key,int x,int y)
{
    glutTimerFunc(0,timer,0);
}
void mouse(int button,int state,int x,int y)
{
    if(state==GLUT_DOWN) oldangle=angle;
    if(state==GLUT_UP)newangle = angle;
    oldX = x;
    glutPostRedisplay();
}
void motion(int x,int y)
{
    angle += x - oldX;
    oldX = x;
    glutPostRedisplay();
}
void display()
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glPushMatrix();
        glRotatef(angle,0,0,1);
        glutSolidTeapot(0.3);
    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();

}

按下空白鍵茶壺就會慢慢轉動角度


Step 14-3


Step 1:開啟CodeBlock,開啟Final_project.cbp


Step 2:如此Final_project便會被匯入CodeBlock中


Step 3:將程式碼修改如下

程式碼:

#include <stdio.h>
#include <GL/glut.h>
#include "glm.h"

GLMmodel * head =NULL;
GLMmodel * body =NULL;
GLMmodel * Ruparm =NULL;
GLMmodel * Rhand =NULL;
int show[4] ={1,1,1,1};
int ID =2;
float teapotX=0,teapotY=0;
FILE * fout =NULL;
FILE * fin =NULL;
void keyboard(unsigned char key,int x,int y)
{
    if(key=='0') ID = 0;
    if(key=='1') ID = 1;
    if(key=='2') ID = 2;
    if(key=='3') ID = 3;
    glutPostRedisplay();
}
void display()
{
    glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
    if(head==NULL){
        head = glmReadOBJ("model/head.obj");
        body = glmReadOBJ("model/body.obj");
        Ruparm = glmReadOBJ("model/Ruparm.obj");
        Rhand = glmReadOBJ("model/Rhand.obj");
    }
    glPushMatrix();
     glScalef(0.3,0.3,0.3);
     glPushMatrix();
        glTranslatef(teapotX,teapotY,0);
    if(ID==0) glColor3f(1,0,0);
    else glColor3f(1,1,1);
    if(show[0]) glmDraw(head,GLM_MATERIAL);
    glPopMatrix();

    if(ID==1) glColor3f(1,0,0);
    else glColor3f(1,1,1);
    if(show[1])glmDraw(body,GLM_MATERIAL);

    if(ID==2) glColor3f(1,0,0);
    else glColor3f(1,1,1);
    if(show[2])glmDraw(Ruparm,GLM_MATERIAL);

    if(ID==3) glColor3f(1,0,0);
    else glColor3f(1,1,1);
    if(show[3])glmDraw(Rhand,GLM_MATERIAL);

     glPopMatrix();
    glutSwapBuffers();
}
int oldX=0,oldY=0;
void  mouse(int button,int state,int x,int y)
{

    if(state==GLUT_DOWN){
       oldX = x;
       oldY = y;
    }
}
void motion(int x,int y)
{
      teapotX+=(x-oldX)/150.0;
    teapotY-=(y-oldY)/150.0;
    oldX = x;
    oldY = y;
    glutPostRedisplay();
}

int main(int argc,char** argv)
{
    glutInit(&argc,argv);
    glutInitDisplayMode(GLUT_DOUBLE|GLUT_DEPTH);
    glutCreateWindow("week13");

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

    glutMainLoop();
}

如此,只要按某個部位,該部位就會變成紅色的


Week 14-3.2


Step 1:將Final_project的程式碼修改如下

程式碼:

#include <stdio.h>
#include <GL/glut.h>
#include "glm.h"

GLMmodel * head =NULL;
GLMmodel * body =NULL;
GLMmodel * Ruparm =NULL;
GLMmodel * Rhand =NULL;
int show[4] ={1,1,1,1};
int ID =2;
float teapotX=0,teapotY=0;
float angle=0;
FILE * fout =NULL;
FILE * fin =NULL;
void keyboard(unsigned char key,int x,int y)
{
    if(key=='0') ID = 0;
    if(key=='1') ID = 1;
    if(key=='2') ID = 2;
    if(key=='3') ID = 3;
    glutPostRedisplay();
}
void display()
{
    glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
    if(head==NULL){
        head = glmReadOBJ("model/head.obj");
        body = glmReadOBJ("model/body.obj");
        Ruparm = glmReadOBJ("model/Ruparm.obj");
        Rhand = glmReadOBJ("model/Rhand.obj");
    }
    glPushMatrix();
     glScalef(0.3,0.3,0.3);
     glPushMatrix();
        ///glTranslatef(teapotX,teapotY,0);
    if(ID==0) glColor3f(1,0,0);
    else glColor3f(1,1,1);
    if(show[0]) glmDraw(head,GLM_MATERIAL);
    glPopMatrix();

    if(ID==1) glColor3f(1,0,0);
    else glColor3f(1,1,1);
    if(show[1])glmDraw(body,GLM_MATERIAL);
    glPushMatrix();
        glTranslatef(-1.360000,+0.360000,0);
        glRotatef(angle,0,0,1);
        glTranslatef(1.360000,-0.360000,0);
    if(ID==2) glColor3f(1,0,0);
    else glColor3f(1,1,1);
    if(show[2])glmDraw(Ruparm,GLM_MATERIAL);
    glPushMatrix();
        glTranslatef(-1.959999,+0.080000,0);
        glRotatef(angle,0,0,1);
        glTranslatef(1.959999,-0.080000,0);

    if(ID==3) glColor3f(1,0,0);
    else glColor3f(1,1,1);
    if(show[3])glmDraw(Rhand,GLM_MATERIAL);
    glPopMatrix();
        glPopMatrix();

     glPopMatrix();
     glColor3f(0,1,0);
     glutSolidTeapot(0.02);
    glutSwapBuffers();
}
int oldX=0,oldY=0;
void  mouse(int button,int state,int x,int y)
{

    if(state==GLUT_DOWN){
       oldX = x;
       oldY = y;
    }
}
void motion(int x,int y)
{
      teapotX +=(x-oldX)/150.0*3;
    teapotY -=(y-oldY)/150.0*3;
    printf("glTranslate(%f,%f,0);\n",teapotX,teapotY);
    angle += x-oldX;
    oldX = x;
    oldY = y;
    glutPostRedisplay();
}

int main(int argc,char** argv)
{
    glutInit(&argc,argv);
    glutInitDisplayMode(GLUT_DOUBLE|GLUT_DEPTH);
    glutCreateWindow("week14");

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

    glutMainLoop();
}

如此,只要修改手臂的位置,便會把它的位置給顯示出來

沒有留言:

張貼留言