2023年5月18日 星期四

jason week14

安裝gitbash,開啟2023graphicsb

1-1

新增專案week14-timer,這次路徑存在2023graphicsb裡,就用上星期2023graphicsb裡的freeglut


設定時間讓茶壺在一定時間旋轉

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

}

1-2

新增專案week14-2_timer_play
增加keyboard函式,點擊鍵盤讓茶壺旋轉



 2-1

用Excel算出alpha值,(alpha)*新的+(1-alpha)*舊的

github上傳week14的兩個檔案

2-2

新專案week14-3_timer_play_interpolation
增加mouse motion函式,讓畫面執行的時候用滑鼠左右移動,再按空白鍵來重複剛才移動的軌跡


#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();
        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);
    //glutTimerFunc(3000,timer,0);
    glutDisplayFunc(display);
    glutMainLoop();
}

3-1


按下數字鍵可以改變關節的顏色
#include<stdio.h>
#include<GL/glut.h>
#include "glm.h"

GLMmodel * head = NULL;
GLMmodel * body = NULL;
GLMmodel * ruparmR = NULL;
GLMmodel * rlowarmR = 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;//show[0]=! show[0];
    if(key=='1')ID=1;//show[1]=! show[1];
    if(key=='2')ID=2;//show[2]=! show[2];
    if(key=='3')ID=3;//show[3]=! show[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");
        ruparmR =glmReadOBJ("model/ruparm.obj");
        rlowarmR =glmReadOBJ("model/rlowarm.obj");
        ///glmUnitize(head);
    }
    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(ruparmR,GLM_MATERIAL);

        if (ID==3)glColor3f(1,0,0);
        else glColor3f(1,1,1);
        if (show[3])glmDraw(rlowarmR,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("week12");
    glutMotionFunc(motion);
    glutDisplayFunc(display);
    glutKeyboardFunc(keyboard);
    glutMouseFunc(mouse);

    glutMainLoop();
}

3-2

增加程式讓移動關節的時候可以出現移動座標







沒有留言:

張貼留言