2018年5月25日 星期五

week13_林辰桓

(1)打開codeblocks建立一個files ,存檔後進行 FIFE input/ouput 的fprintf 的練習(寫檔 獨檔)
--------------------------------------------------------------------------------------------------------------------------
延伸上訴的練習和之前的TRT練習進行專案練習
(2)將老師的專案檔打開後
  1. 先寫keyboard()函式
  2. display()函式 (身體+旋轉右臂)
  3. motion()函式 (mouse motion 控制關節)
  4. 加寫檔 fprintf
以下是程式碼:

#include <GL/glut.h>
#include <stdio.h>
FILE*fout=  NULL;
FILE*fin=NULL;
float angle=60, angle2=90,angle3=60;
void keyboard(unsigned char key,int x,int y)
{
    if(key=='s'|| key=='S'||key=='w'||key=='W')
    {
        if(fout==NULL)fout=fopen("output.txt","w+");
        printf("%.3f %.3f %.3f\n",angle,angle2,angle3);
        fprintf(fout,"%.3f %.3f %.3f\n",angle,angle2,angle3);
    }
    if(key=='r'||key||'R')
    {
        if(fin==NULL)fin=fopen("output.txt","r");
        fscanf(fin,"%f %f %f",&angle,&angle2,&angle3);
        glutPostRedisplay();
    }
}
const GLfloat light_ambient[]  = { 0.0f, 0.0f, 0.0f, 1.0f };
const GLfloat light_diffuse[]  = { 1.0f, 1.0f, 1.0f, 1.0f };
const GLfloat light_specular[] = { 1.0f, 1.0f, 1.0f, 1.0f };
const GLfloat light_position[] = { 2.0f, 5.0f, 5.0f, 0.0f };

const GLfloat mat_ambient[]    = { 0.7f, 0.7f, 0.7f, 1.0f };
const GLfloat mat_diffuse[]    = { 0.8f, 0.8f, 0.8f, 1.0f };
const GLfloat mat_specular[]   = { 1.0f, 1.0f, 1.0f, 1.0f };
const GLfloat high_shininess[] = { 100.0f };

/* Program entry point */
void display()
{
    glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
    glPushMatrix();
        glutSolidTeapot(0.3);

        glPushMatrix();
            glTranslatef(0.4,0,0);
            glRotatef(angle,0,0,1);glPopMatrix();
            glTranslatef(0.4,0,0);
            glutSolidTeapot(0.3);
        glPopMatrix();
    glPopMatrix();
    glutSwapBuffers();
}
void motion(int x,int y)
{glPopMatrix();
    angle=x;
    if(fout==NULL)fout=fopen("output.txt","w+");glPopMatrix();
    fprintf(fout,"%.3f %.3f %.3f\n",angle,angle2,angle3);
    printf("%.3f %.3f %.3f\n",angle,angle2,angle3);

    glutPostRedisplay();glPopMatrix();
}
int main(int argc, char *argv[])
{
    glutInit(&argc, argv);
    glutInitWindowSize(640,480);
    glutInitWindowPosition(10,10);
    glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);

    glutCreateWindow("GLUT Shapes");

    //glutReshapeFunc(resize);
    glutDisplayFunc(display);
    glutMotionFunc(motion);

    glutKeyboardFunc(keyboard);
    //glutIdleFunc(idle);

    glClearColor(1,1,1,1);
    //glEnable(GL_CULL_FACE);
    //glCullFace(GL_BACK);

    glEnable(GL_DEPTH_TEST);
    glDepthFunc(GL_LESS);

    glEnable(GL_LIGHT0);
    glEnable(GL_NORMALIZE);
    glEnable(GL_COLOR_MATERIAL);
    glEnable(GL_LIGHTING);

    glLightfv(GL_LIGHT0, GL_AMBIENT,  light_ambient);
    glLightfv(GL_LIGHT0, GL_DIFFUSE,  light_diffuse);
    glLightfv(GL_LIGHT0, GL_SPECULAR, light_specular);
    glLightfv(GL_LIGHT0, GL_POSITION, light_position);

    glMaterialfv(GL_FRONT, GL_AMBIENT,   mat_ambient);
    glMaterialfv(GL_FRONT, GL_DIFFUSE,   mat_diffuse);
    glMaterialfv(GL_FRONT, GL_SPECULAR,  mat_specular);
    glMaterialfv(GL_FRONT, GL_SHININESS, high_shininess);

    glutMainLoop();

    return EXIT_SUCCESS;
}
-------------------------------------------------------------------------------------------------------------------------
再依照上面的做延升做出20個關節

沒有留言:

張貼留言