2023年5月18日 星期四

happy

 WEEK14

STEP01-1

先把程式備分起來 安裝GIT 開GIT BASH
cd desktop 到桌面
git clone  https://github.com/Claudia0729/2023graphicsb
cd 2023graphicsb
start . 開檔案總管



開新專案 week14-timer 專案



新專案 開在桌面2023graphicsb裡 freeglut用2023graphicsb的
Final_project 的 freeglut

原本的10行程式碼,在加上3行timer讓他轉,
使用float angle=0 一開始是0度 之後每次加90度 然後重畫畫面


程式碼:
#include <GL/glut.h>
float angle=0;///step01-1
void timer(int t)///step01-1
{
    glutTimerFunc(500,timer,t+1);///step01-1
    angle += 90;///角度+90度
    glutPostRedisplay();///step01-1 重劃畫面

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

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

    glutTimerFunc(3000,timer,0);///step01-1
    glutDisplayFunc(display);

    glutMainLoop();

}

STEP 01-2

 按鍵後 在開始播放Play動畫 week14_2_time_play


程式碼:
#include <GL/glut.h>
float angle=0;///step01-1
void timer(int t)///step01-1
{
    glutTimerFunc(33,timer,t+1);///step01-1
    angle += 3;///角度+90度
    glutPostRedisplay();///step01-1 重劃畫面

}
void keyboard(unsigned char key, int x, int y)///step01-2
{
    glutTimerFunc(0,timer,0);///step01-2
}
void display()
{
    glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
    glPushMatrix();///step01-1
        glRotatef(angle,0,0,1);///step01-1
        glutSolidTeapot(0.3);
    glPopMatrix();///step01-1
    glutSwapBuffers();

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

    glutKeyboardFunc(keyboard);///step01-2
    ///glutTimerFunc(3000,timer,0);///step01-1
    glutDisplayFunc(display);

    glutMainLoop();

}

step02-1

關於動作內插 可以順利把中間的值變出來 使用的攻勢 有個alpha值 介於0.0~1.0之間
使用(alpha)*(新的)+(1-alpha)*(舊的) 能把中間的值都推算出來


把程式備分起來 

git config --global user.email claudialin729@gmail.com
git config --global user.name Claudia0729
git add .
git commit -m week14
git push

STEP02-2




程式碼; 
#include <GL/glut.h>
float angle=0, newAngle = 0 , oldAngle = 0;///step02-2 宣告 新角度 舊角度
float oldX = 0;///STEP02-2 舊的X座標
void timer(int t)///step01-1
{
    if ( t<100 ) glutTimerFunc(33,timer,t+1);///step01-1
    float alpha = t/100.0;///step02-2 所以alpha 介於0.0~1.0 間
    angle = alpha * newAngle + (1-alpha)*oldAngle;///step02-2
    ///angle += 3;///角度+90度
    glutPostRedisplay();///step01-1 重劃畫面

}
void keyboard(unsigned char key, int x, int y)///step01-2
{
    glutTimerFunc(0,timer,0);///step01-2
}
void mouse ( int button, int state , int x , int y )
{
    if( state==GLUT_DOWN) oldAngle = angle;///step02-2
    if( state==GLUT_UP ) newAngle = angle;///step02-2
    oldX = x;///step02-2
    glutPostRedisplay();///step02-2 更新畫面

}
void motion( int x, int y )
{
    angle += x-oldX;///STEP02-2
    oldX= x;///STEP02-2
    glutPostRedisplay();///step02-2更新畫面

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

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

    glutMouseFunc(mouse);///step02-2
    glutMotionFunc(motion);///step02-2
    glutKeyboardFunc(keyboard);///step01-2
    ///glutTimerFunc(3000,timer,0);///step01-1
    glutDisplayFunc(display);

    glutMainLoop();

}

STEP03-1



沒有留言:

張貼留言