2021年6月9日 星期三

week16

 


複製week15的程式碼
#include <stdio.h>///可割可棄
#include <GL/glut.h>
float angle[20]={},diff=2;
int angleID=0;
int oldX=0;
void timer(int t){
    ///glutTimerFunc(30,timer,t+1);
    
}
void mouse(int button, int state,int x,int y){
    oldX = x;
}
void motion(int x,int y){
    angle[angle ID] += x - oldX;
    oldX = x;
    glutPostRedisplay();
}
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;
}
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();

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

    glutMouseFunc(mouse);
    glutMotionFunc(motion);
    glutKeyboardFunc(keyboard);
    glutTimerFunc(0, timer ,0);
    glutDisplayFunc(display);
    glutMainLoop();
}


#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; 
}
void display()
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glPushMatrix();
        glutSolidTeapot( 0.3 );///身體
        glPushMatrix();///左半邊
            glTranslatef(-0.3,0,0);///(3)掛在正確的地方
            glRotatef(angle[0], 0,0,1);///(2)旋轉 ///Step05
            glTranslatef(-0.3,0,0);///(1)把關節旋轉中心放到畫面中心
            glutSolidTeapot( 0.3 );///左手臂(沒有重疊)
            glPushMatrix();
                glTranslatef(-0.3,0,0);///(3)掛在正確的地方
                glRotatef(angle[1], 0,0,1);///(2)旋轉 ///Step05
                glTranslatef(-0.3,0,0);///(1)把關節旋轉中心放到畫面中心
                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("week15 file");

    glutMouseFunc(mouse);
    glutMotionFunc(motion);
    glutKeyboardFunc(keyboard);
    glutTimerFunc(0, timer, 0);
    glutDisplayFunc(display);
    glutMainLoop();
}
#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'){
         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'){
        if( fin==NULL ) fin = fopen( "angle.txt", "r" );
        for(int i=0; i<20; i++) fscanf( fin, "%f", & angle[i] );
        glutPostRedisplay();
    }
    if(key=='p'){

    }
}
void display()
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glPushMatrix();
        glutSolidTeapot( 0.3 );///身體
        glPushMatrix();///左半邊
            glTranslatef(-0.3,0,0);///(3)掛在正確的地方
            glRotatef(angle[0], 0,0,1);///(2)旋轉 ///Step05
            glTranslatef(-0.3,0,0);///(1)把關節旋轉中心放到畫面中心
            glutSolidTeapot( 0.3 );///左手臂(沒有重疊)
            glPushMatrix();
                glTranslatef(-0.3,0,0);///(3)掛在正確的地方
                glRotatef(angle[1], 0,0,1);///(2)旋轉 ///Step05
                glTranslatef(-0.3,0,0);///(1)把關節旋轉中心放到畫面中心
                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("week15 file");

    glutMouseFunc(mouse);
    glutMotionFunc(motion);
    glutKeyboardFunc(keyboard);
    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...