2023年5月18日 星期四

這禮拜沒泡芙拉 Week14

 

WEEK14


Step01-1


安裝git 開啟GIT bash

cd desktop 進入桌面
git clone 複製你的檔案
cd 進入你的檔案
start . 開啟檔案資料夾






開啟CODEBLOCK


開專案







打專案名稱 選擇檔案存檔位置



選擇自己之前存的Final_Project裡面的freeglut 
不用再自己下載啦!!!!!!!!!!!!!!!!!


他跑出來就會生成三個神祕圖形




之後把程式碼全部刪掉,改成以下程式碼

#include <GL/glut.h>
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_RGB | GLUT_DOUBLE| GLUT_DEPTH);
    glutCreateWindow("GLUT Shapes");
    glutDisplayFunc(display);
    glutMainLoop();
    }
就會出現神奇白色茶壺喔






新增一些程式碼
把茶壺變成計時器
到時間會旋轉

#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();
    glRotated(angle,0,0,1);
    glutSolidTeapot(0.3);
    glPopMatrix();
    glutSwapBuffers();
}
int main(int argc,char *argv[])
    {
        glutInit(&argc,argv);
    glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE| GLUT_DEPTH);
    glutCreateWindow("week14");
    glutTimerFunc(3000,timer,0);
    glutDisplayFunc(display);
    glutMainLoop();
    }





















Step01-2




開一個新專案



設定按鍵來啟動


按越多次轉越快
師大巴士駛到陵墓

#include <GL/glut.h>
float angle =0;
void timer(int t)
{
    glutTimerFunc(33,timer,t+1);
    angle +=3;
    glutPostRedisplay();
}
void keyboard(unsigned char key,int x,int y)
{
    glutTimerFunc(0,timer,0);
}
void display()
{
    glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
    glPushMatrix();
    glRotated(angle,0,0,1);
    glutSolidTeapot(0.3);
    glPopMatrix();
    glutSwapBuffers();
}
int main(int argc,char **argv)
    {
        glutInit(&argc,argv);
    glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE| GLUT_DEPTH);
    glutCreateWindow("week14");
    ///glutTimerFunc(3000,timer,0);
    glutKeyboardFunc(keyboard);
    glutDisplayFunc(display);
    glutMainLoop();
    }




Step02-1



用Exel來計算角度


先用git備份



git add . 加入你的檔案
git status 確認你的檔案
git config --global user.email "你的Gmail"
git config --global user.name "你的GitHubID"
git commit -m "你的周次"
git push 上傳






Step02-2




開Codeblock的一個新的專案檔




更改成以下程式碼
#include <GL/glut.h>
float angle =0,newAngle=0,oldAngle=0;
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();
    glRotated(angle,0,0,1);
    glutSolidTeapot(0.3);
    glPopMatrix();
    glutSwapBuffers();
}
int main(int argc,char **argv)
    {
        glutInit(&argc,argv);
    glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE| GLUT_DEPTH);
    glutCreateWindow("week14");
    glutMouseFunc(mouse);
    glutMotionFunc(motion);
    glutKeyboardFunc(keyboard);
    glutDisplayFunc(display);
    glutMainLoop();
    }




這樣就可以讓他做出動畫
先設定一個角度 ,之後再旋轉出另一個新的角度
按空白鍵茶杯就會從第一個角度慢慢轉到第二個角度




Step03-1




用CodeBlock開啟Final_Project.cbp





修改一下程式碼

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



這樣只要按哪個部位
那個部位就會變成紅色



Step03-2





修程式碼

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






修改手臂的位置
然後把它的位置給顯示出來






最後上傳GITHUB✌
cd desktop 進入桌面
git clone 複製你的檔案
cd 進入你的檔案
git add . 加入你的檔案
git status 確認你的檔案
git config --global user.email "你的Gmail"
git config --global user.name "你的GitHubID"
git commit -m "你的周次"
git push 上傳



沒有留言:

張貼留言