2021年6月9日 星期三

08160625 陳君豪 week16

先將week15的程式碼貼到week16.cpp裡面

然後加入void mouse跟void motion


main()裡面也要加入

glutMouseFunc(mouse);

glutMotionFunc(motion);

程式碼:

#include <stdio.h>///可割可棄
#include <GL/glut.h>
float angle[20]={},diff=2;
int angleID=0;
int oldX=0;
void timer(int t){
///timer裡的內容刪掉
}
void mouse(int button, int state,int x,int y){///滑鼠位置
    oldX=x;
}
void motion(int x,int y){///動作控制
    angle[angleID]+=x-oldX;
    oldX=x;
    glutPostRedisplay();
}
void keyboard(unsigned char key,int x,int y){
    if(key=='4')angleID=4;
    if(key=='3')angleID=3;
    if(key=='2')angleID=2;
    if(key=='1')angleID=1;
    if(key=='0')angleID=0;
}
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 );///左手臂(重疊了)
            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 animation");
    glutMouseFunc(mouse);
    glutMotionFunc(motion);
    glutKeyboardFunc(keyboard);
    glutTimerFunc(0,timer,0);
    glutDisplayFunc(display);
    glutMainLoop();
}

然後再motion裡面加入

if(fout==NULL)fout=fopen("angle.txt","w+");
    for(int i=0;i<20;i++)fprintf(fout,"%.1f ",angle[i]);
    fprintf(fout,"\n");

第六行加入

FILE*fout=NULL;

可以在freeglut\bin裡面找到angle.txt


程式碼:
#include <stdio.h>
#include <GL/glut.h>
float angle[20]={}, diff=2;
int angleID=0;
int oldx=0;
FILE * fout = NULL;
FILE * fin = NULL;///播放動畫
void timer(int t)
{
//    glutTimerFunc(30,timer,t+1);
//    if(fin==NULL) fin=fopen("angle.txt", "r");
//    for(int i=0;i<20;i++) fscanf(fin, "%f",&angle[i]);
//    glutPostRedisplay();///重畫畫面
}
void mouse(int button, int state,int x, int y){
    oldx=x;
}
void motion(int x, int y)
{
    angle[angleID]+= x-oldx;
    oldx=x;
    glutPostRedisplay();

//    if(fout==NULL) fout=fopen("angle.txt", "w+");
//    for(int i=0; i<20;i++) fprintf(fout, "%.1f", angle[i]);
//    fprintf(fout, "\n");
//    for(int i=0;i<20;i++) printf("%.1f", angle[i]);
//    printf("\n");

}
void keyboard(unsigned char key, int x, int y )
{
    if(key=='0') angleID=0;
    if(key=='1') angleID=1;
    if(key=='2') angleID=2;
    if(key=='3') angleID=3;
    if(key=='s'){///save
        if(fout==NULL) fout=fopen("angle.txt", "w+");
        for(int i=0; i<20;i++) fprintf(fout, "%.1f", angle[i]);
        fprintf(fout, "\n");
        for(int i=0;i<20;i++) printf("%.1f", angle[i]);
        printf("\n");

    }
    if(key=='r'){///read
        if(fin==NULL) fin=fopen("angle.txt", "r");
        for(int i=0;i<20;i++) fscanf(fin, "%f",&angle[i]);
        glutPostRedisplay();///重畫畫面
    }
    if(key=='p'){///play利用timer播放

    }
}
void display()
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glPushMatrix();
        glutSolidTeapot(0.3);
        glPushMatrix();///左
            glTranslatef(-0.3,0,0);
            glRotated(angle[0],0,0,1);
            glTranslatef(-0.3,0,0);
            glutSolidTeapot(0.3);
            glPushMatrix();
                glTranslatef(-0.3,0,0);
                glRotated(angle[0],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[2],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("08160625");
    glutKeyboardFunc(keyboard);
    glutMouseFunc(mouse);
    glutMotionFunc(motion);
    glutTimerFunc(0,timer,0);
    glutDisplayFunc(display);
    glutMainLoop();
}






沒有留言:

張貼留言

距地表面160 Week11

 #include "glm.h" GLMmodel* pmodel = NULL; void drawmodel(void) {     if (!pmodel) { pmodel = glmReadOBJ("data/porsche.obj...