2019年5月16日 星期四

Week13_Summer

今日目標:

1、主題:檔案

2、主題:關節、擺動作
3、主題:複習
4、作業:機器人擺動作




 01 至 CodeBlock 開啟新的 Project ─ Console



 02 寫檔案的程式碼,text檔寫入Hello World



 03 讀入另一個檔案



  程式碼: 

#include <stdio.h>
FILE * fout2 = NULL;   ///NOW2 寫另一個檔案
FILE * fin2 = NULL;   ///NOW2 讀另一個檔案
float angle[20];   ///NOW2 我們有20個關節的角度
int main()
{ ///檔案寫黨
    fout2 = fopen("motion.txt","w+"); ///NOW2 另一個有關節的檔案
    for(int i=0 ; i<20 ; i++){
        ///printf("Hello Worle\n");
        fprintf(fout2, "%.2f" ,angle[i]);   ///NOW2 另一個有關節的檔案
                        ///加空格,因為怕數自黏起來
                        ///不加空格,數字真的黏起來
    }

}





 04 先寫出一個茶壺可以移動的程式


  程式碼:

#include <GL/glut.h>
float angle[20];///NOW2
void display()
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glPushMatrix();
        glColor3f(1, 1, 1);///white
        glutSolidTeapot( 0.3 );///body
        glPushMatrix();
            glTranslatef(0.2, 0, 0);///NOW2 (3)掛上去
            glRotatef(angle[1], 0,0,1);///NOW2 ///(2)旋轉
            glTranslatef(0.15, 0, 0);///NOW2 (1)旋轉中心
            glColor3f(1, 0, 0);///red
            glutSolidTeapot( 0.2 );///right arm
        glPopMatrix();
    glPopMatrix();
    glutSwapBuffers();
}
int oldX=0;///NOW2
void mouse(int button, int state, int x, int y)///NOW2
{///NOW2
    oldX = x;///NOW2
}
void motion(int x, int y)///NOW2
{///NOW2
    angle[1] += x-oldX;///NOW2
    oldX = x;///NOW2
    display();///NOW2
}
int main(int argc, char**argv)
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
    ///glutInitWindowSize(600,600);///可以開大一點的window
    glutCreateWindow("Week 13 angle motion file");
    glutMouseFunc(mouse);///NOW2
    glutMotionFunc(motion);///NOW2
    glutDisplayFunc(display);

    glutMainLoop();


}



 05 做出茶壺的關節,利用小黑窗print出移動的部分


  程式碼:

#include <GL/glut.h>
float angle[20];///NOW2
int angleID=1;///NOW3
void display()
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glPushMatrix();
        glColor3f(1, 1, 1);///white
        glutSolidTeapot( 0.3 );///body

        glPushMatrix();///右邊
            glTranslatef(0.2, 0, 0);///NOW2 (3)掛上去
            glRotatef(angle[1], 0,0,1);///NOW2 ///(2)旋轉
            glTranslatef(0.15, 0, 0);///NOW2 (1)旋轉中心
            glColor3f(1, 0, 0);///red
            glutSolidTeapot( 0.2 );///right arm
            glPushMatrix();
                glTranslatef(0.2, 0, 0);///NOW2 (3)掛上去
                glRotatef(angle[2], 0,0,1);///NOW2 ///(2)旋轉
                glTranslatef(0.15, 0, 0);///NOW2 (1)旋轉中心
                glColor3f(1, 0, 0);///red
                glutSolidTeapot( 0.2 );///right lower arm
            glPopMatrix();
        glPopMatrix();

        glPushMatrix();///左邊
            glTranslatef(-0.2, 0, 0);///NOW2 (3)掛上去
            glRotatef(angle[3], 0,0,1);///NOW2 ///(2)旋轉
            glTranslatef(-0.15, 0, 0);///NOW2 (1)旋轉中心
            glColor3f(1, 0, 0);///red
            glutSolidTeapot( 0.2 );///right arm
            glPushMatrix();
                glTranslatef(-0.2, 0, 0);///NOW2 (3)掛上去
                glRotatef(angle[4], 0,0,1);///NOW2 ///(2)旋轉
                glTranslatef(-0.15, 0, 0);///NOW2 (1)旋轉中心
                glColor3f(1, 0, 0);///red
                glutSolidTeapot( 0.2 );///right lower arm
            glPopMatrix();
        glPopMatrix();
    glPopMatrix();
    glutSwapBuffers();
}
int oldX=0;///NOW2
void mouse(int button, int state, int x, int y)///NOW2
{///NOW2
    oldX = x;///NOW2
}
#include <stdio.h>///NOW3
void motion(int x, int y)///NOW2
{///NOW2
    angle[angleID] += x-oldX;///NOW3
    oldX = x;///NOW2
    for(int i=0;i<20;i++){///NOW3
        printf(" %.2f ", angle[i]);///NOW3
    }
    printf("\n");///NOW3
    display();///NOW2
}
void keyboard(unsigned char key, int x, int y)///NOW3
{///NOW3
    if(key=='1') angleID=1;///NOW3
    if(key=='2') angleID=2;///NOW3
    if(key=='3') angleID=3;///NOW3
    if(key=='4') angleID=4;///NOW3
}
int main(int argc, char**argv)
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
    ///glutInitWindowSize(600,600);///可以開大一點的window
    glutCreateWindow("Week 13 angle motion file");
    glutKeyboardFunc(keyboard);///NOW3
    glutMouseFunc(mouse);///NOW2
    glutMotionFunc(motion);///NOW2
    glutDisplayFunc(display);

    glutMainLoop();
}




 06 在 motion 後寫入檔案



  write_file_after_motion程式碼:

#include <GL/glut.h>
#include <stdio.h>///NOW_FILE (0)
float angle[20];///NOW2
int angleID=1;///NOW3
FILE * fout = NULL;///NOW_FILE (1)
void display()
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glPushMatrix();
        glColor3f(1, 1, 1);///white
        glutSolidTeapot( 0.3 );///body

        glPushMatrix();///右邊
            glTranslatef(0.2, 0, 0);///NOW2 (3)掛上去
            glRotatef(angle[1], 0,0,1);///NOW2 ///(2)旋轉
            glTranslatef(0.15, 0, 0);///NOW2 (1)旋轉中心
            glColor3f(1, 0, 0);///red
            glutSolidTeapot( 0.2 );///right arm
            glPushMatrix();
                glTranslatef(0.2, 0, 0);///NOW2 (3)掛上去
                glRotatef(angle[2], 0,0,1);///NOW2 ///(2)旋轉
                glTranslatef(0.15, 0, 0);///NOW2 (1)旋轉中心
                glColor3f(1, 0, 0);///red
                glutSolidTeapot( 0.2 );///right lower arm
            glPopMatrix();
        glPopMatrix();

        glPushMatrix();///左邊
            glTranslatef(-0.2, 0, 0);///NOW2 (3)掛上去
            glRotatef(angle[3], 0,0,1);///NOW2 ///(2)旋轉
            glTranslatef(-0.15, 0, 0);///NOW2 (1)旋轉中心
            glColor3f(1, 0, 0);///red
            glutSolidTeapot( 0.2 );///right arm
            glPushMatrix();
                glTranslatef(-0.2, 0, 0);///NOW2 (3)掛上去
                glRotatef(angle[4], 0,0,1);///NOW2 ///(2)旋轉
                glTranslatef(-0.15, 0, 0);///NOW2 (1)旋轉中心
                glColor3f(1, 0, 0);///red
                glutSolidTeapot( 0.2 );///right lower arm
            glPopMatrix();
        glPopMatrix();
    glPopMatrix();
    glutSwapBuffers();
}
int oldX=0;///NOW2
void mouse(int button, int state, int x, int y)///NOW2
{///NOW2
    oldX = x;///NOW2
}

void motion(int x, int y)///NOW2
{///NOW2
    angle[angleID] += x-oldX;///NOW3
    oldX = x;///NOW2
    if(fout == NULL) fout = fopen("motion.txt", "w+");///NOW_FILE (2)
    for(int i=0;i<20;i++){///NOW3
        printf(" %.2f ", angle[i]);///NOW3
        fprintf(fout, " %.2f ", angle[i]);///NOW_FILE
    }
    printf("\n");///NOW3
    fprintf(fout, "\n");///NOW_FILE
    display();///NOW2
}
void keyboard(unsigned char key, int x, int y)///NOW3
{///NOW3
    if(key=='1') angleID=1;///NOW3
    if(key=='2') angleID=2;///NOW3
    if(key=='3') angleID=3;///NOW3
    if(key=='4') angleID=4;///NOW3
    if(key=='w'){///NOW_FILE
        fout = fopen("motion.txt", "w+");///NOW_FILE (2)
    }
}
int main(int argc, char**argv)
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
    ///glutInitWindowSize(600,600);///可以開大一點的window
    glutCreateWindow("Week 13 angle motion file");
    glutKeyboardFunc(keyboard);///NOW3
    glutMouseFunc(mouse);///NOW2
    glutMotionFunc(motion);///NOW2
    glutDisplayFunc(display);

    glutMainLoop();
}




  ReadFile_OK程式碼:
#include <GL/glut.h>
#include <stdio.h>///NOW_FILE (0)
float angle[20];///NOW2
int angleID=1;///NOW3
FILE * fout = NULL;///NOW_FILE (1)
FILE * fin = NULL;///NOW_FILE_READ (1)
void display()
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glPushMatrix();
        glColor3f(1, 1, 1);///white
        glutSolidTeapot( 0.3 );///body

        glPushMatrix();///右邊
            glTranslatef(0.2, 0, 0);///NOW2 (3)掛上去
            glRotatef(angle[1], 0,0,1);///NOW2 ///(2)旋轉
            glTranslatef(0.15, 0, 0);///NOW2 (1)旋轉中心
            glColor3f(1, 0, 0);///red
            glutSolidTeapot( 0.2 );///right arm
            glPushMatrix();
                glTranslatef(0.2, 0, 0);///NOW2 (3)掛上去
                glRotatef(angle[2], 0,0,1);///NOW2 ///(2)旋轉
                glTranslatef(0.15, 0, 0);///NOW2 (1)旋轉中心
                glColor3f(1, 0, 0);///red
                glutSolidTeapot( 0.2 );///right lower arm
            glPopMatrix();
        glPopMatrix();

        glPushMatrix();///左邊
            glTranslatef(-0.2, 0, 0);///NOW2 (3)掛上去
            glRotatef(angle[3], 0,0,1);///NOW2 ///(2)旋轉
            glTranslatef(-0.15, 0, 0);///NOW2 (1)旋轉中心
            glColor3f(1, 0, 0);///red
            glutSolidTeapot( 0.2 );///right arm
            glPushMatrix();
                glTranslatef(-0.2, 0, 0);///NOW2 (3)掛上去
                glRotatef(angle[4], 0,0,1);///NOW2 ///(2)旋轉
                glTranslatef(-0.15, 0, 0);///NOW2 (1)旋轉中心
                glColor3f(1, 0, 0);///red
                glutSolidTeapot( 0.2 );///right lower arm
            glPopMatrix();
        glPopMatrix();
    glPopMatrix();
    glutSwapBuffers();
}
int oldX=0;///NOW2
void mouse(int button, int state, int x, int y)///NOW2
{///NOW2
    oldX = x;///NOW2
}

void motion(int x, int y)///NOW2
{///NOW2
    angle[angleID] += x-oldX;///NOW3
    oldX = x;///NOW2
    if(fout == NULL) fout = fopen("motion.txt", "w+");///NOW_FILE (2)
    for(int i=0;i<20;i++){///NOW3
        printf(" %.2f ", angle[i]);///NOW3
        fprintf(fout, " %.2f ", angle[i]);///NOW_FILE
    }
    printf("\n");///NOW3
    fprintf(fout, "\n");///NOW_FILE
    display();///NOW2
}
void keyboard(unsigned char key, int x, int y)///NOW3
{///NOW3
    if(key=='1') angleID=1;///NOW3
    if(key=='2') angleID=2;///NOW3
    if(key=='3') angleID=3;///NOW3
    if(key=='4') angleID=4;///NOW3
    if(key=='w'){///NOW_FILE
        fout = fopen("motion.txt", "w+");///NOW_FILE (2)
    }
    if(key=='r'){
        if(fin==NULL) fin = fopen("motion.txt", "r");///NOW_FILE_READ (2)
        ///scanf("%d", &n);
        for(int i=0;i<20; i++){///NOW_FILE_READ
            fscanf(fin, "%f", &angle[i]);///NOW_FILE_READ
            printf(" %.2f ", angle[i]);///順便印出來看數字對不對
        }
    }
    glutPostRedisplay();///NOW_FILE_READ 和 display()很像
}
int main(int argc, char**argv)
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
    ///glutInitWindowSize(600,600);///可以開大一點的window
    glutCreateWindow("Week 13 angle motion file");
    glutKeyboardFunc(keyboard);///NOW3
    glutMouseFunc(mouse);///NOW2
    glutMotionFunc(motion);///NOW2
    glutDisplayFunc(display);

    glutMainLoop();
}



  回家作業: 






  程式碼: 

#include<GL/glut.h>
#include "glm.h"


GLMmodel* pmodel = NULL;
GLMmodel* pmodel2 = NULL;
GLMmodel* pmodel3 = NULL;
GLMmodel* pmodel4 = NULL;
GLMmodel* pmodel5 = NULL;
GLMmodel* pmodel6 = NULL;
GLMmodel* pmodel7 = NULL;
GLMmodel* pmodel8 = NULL;
GLMmodel* pmodel9 = NULL;
GLMmodel* pmodel10 = NULL;
float angle[20];///NOW2
int angleID=1;///NOW3


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 };


void display()

{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glPushMatrix();
    glTranslatef(-0.092,0.37,0);
    glRotatef(angle[1],0,1,0);
    glTranslatef(0,0,0);
    glScalef(0.5,0.5,0.5);
    if (!pmodel) {
     pmodel = glmReadOBJ("data/01.obj");
     if (!pmodel) exit(0);
     glmUnitize(pmodel);
     glmFacetNormals(pmodel);
     glmVertexNormals(pmodel, 90.0);
        }
    glmDraw(pmodel, GLM_SMOOTH | GLM_MATERIAL);
glPopMatrix();
glPushMatrix();
    glTranslatef(0,0,0);
    glRotatef(0,0,0,1);
    glTranslatef(-0.1,0,0);
    glScalef(0.25,0.25,0.25);
    if (!pmodel2) {
     pmodel2= glmReadOBJ("data/02.obj");
     if (!pmodel2) exit(0);
     glmUnitize(pmodel2);
     glmFacetNormals(pmodel2);
     glmVertexNormals(pmodel2, 90.0);
        }
    glmDraw(pmodel2, GLM_SMOOTH | GLM_MATERIAL);

glPopMatrix();

glPushMatrix();
    glTranslatef(-0.26,0.09,0);
    glRotatef(angle[2],0,0,3);
    glTranslatef(-0.02,-0.01,0);
    glScalef(0.4,0.4,0.4);
    if (!pmodel3) {
     pmodel3= glmReadOBJ("data/03.obj");
     if (!pmodel3) exit(0);
     glmUnitize(pmodel3);
     glmFacetNormals(pmodel3);
     glmVertexNormals(pmodel3, 90.0);
        }
    glmDraw(pmodel3, GLM_SMOOTH | GLM_MATERIAL);
        glPushMatrix();
        glTranslatef(-0.25,0.005,0);
        glRotatef(angle[5],0,0,1);
        glTranslatef(0,0,0);
        glScalef(1,1,1);
        if (!pmodel6) {
        pmodel6 = glmReadOBJ("data/06.obj");
        if (!pmodel6) exit(0);
        glmUnitize(pmodel6);
        glmFacetNormals(pmodel6);
        glmVertexNormals(pmodel6, 90.0);
            }
        glmDraw(pmodel6, GLM_SMOOTH | GLM_MATERIAL);
    glPopMatrix();
glPopMatrix();

glPushMatrix();
        glTranslatef(0.08,0.09,0);
        glRotatef(angle[4],0,0,1);
        glTranslatef(0,0,0);
        glScalef(0.4,0.4,0.4);
        if (!pmodel5) {
        pmodel5 = glmReadOBJ("data/05.obj");
        if (!pmodel5) exit(0);
        glmUnitize(pmodel5);
        glmFacetNormals(pmodel5);
        glmVertexNormals(pmodel5, 90.0);
            }
        glmDraw(pmodel5, GLM_SMOOTH | GLM_MATERIAL);
    glPushMatrix();
        glTranslatef(0.22,0.01,-0.7);
        glRotatef(angle[3],0,0,1);
        glTranslatef(0.03,0.01,0);
        glScalef(1,1,1);
        if (!pmodel4) {
        pmodel4= glmReadOBJ("data/04.obj");
        if (!pmodel4) exit(0);
        glmUnitize(pmodel4);
        glmFacetNormals(pmodel4);
        glmVertexNormals(pmodel4, 90.0);
            }
        glmDraw(pmodel4, GLM_SMOOTH | GLM_MATERIAL);
    glPopMatrix();
glPopMatrix();

glPushMatrix();
    glTranslatef(-0.02,-0.18,0);
    glRotatef(angle[6],0,0,1);
    glTranslatef(0,0,0);
    glScalef(0.1,0.1,0.1);
    if (!pmodel7) {
     pmodel7 = glmReadOBJ("data/07.obj");
     if (!pmodel7) exit(0);
     glmUnitize(pmodel7);
     glmFacetNormals(pmodel7);
     glmVertexNormals(pmodel7, 90.0);
        }
    glmDraw(pmodel7, GLM_SMOOTH | GLM_MATERIAL);
    glPushMatrix();
        glTranslatef(0.15,-0.7,0);
        glRotatef(angle[7],0,0,1);
        glTranslatef(0,-0.2,0);
        glScalef(1,1,1);
        if (!pmodel8) {
        pmodel8 = glmReadOBJ("data/08.obj");
        if (!pmodel8) exit(0);
        glmUnitize(pmodel8);
        glmFacetNormals(pmodel8);
        glmVertexNormals(pmodel8, 90.0);
            }
        glmDraw(pmodel8, GLM_SMOOTH | GLM_MATERIAL);
    glPopMatrix();
glPopMatrix();

glPushMatrix();
    glTranslatef(-0.18,-0.14,0);
    glRotatef(angle[8],0,0,1);
    glTranslatef(0,-0.04,0);
    glScalef(0.1,0.1,0.1);
    if (!pmodel9) {
    pmodel9 = glmReadOBJ("data/09.obj");
    if (!pmodel9) exit(0);
    glmUnitize(pmodel9);
    glmFacetNormals(pmodel9);
    glmVertexNormals(pmodel9, 90.0);
        }
    glmDraw(pmodel9, GLM_SMOOTH | GLM_MATERIAL);
    glPushMatrix();
        glTranslatef(-0.05,-0.42,0);
        glRotatef(angle[9],0,0,1);
        glTranslatef(0,-0.5,0);
        glScalef(0.7,0.7,0.7);
        if (!pmodel10) {
        pmodel10 = glmReadOBJ("data/10.obj");
        if (!pmodel10) exit(0);
        glmUnitize(pmodel10);
        glmFacetNormals(pmodel10);
        glmVertexNormals(pmodel10, 90.0);
            }
        glmDraw(pmodel10, GLM_SMOOTH | GLM_MATERIAL);
    glPopMatrix();
glPopMatrix();
glutSwapBuffers();

}

int oldX=0;///NOW2
void mouse(int button, int state, int x, int y)///NOW2
{///NOW2
    oldX = x;///NOW2
}
#include <stdio.h>///NOW3
void motion(int x, int y)///NOW2
{///NOW2
    angle[angleID] += x-oldX;///NOW3
    oldX = x;///NOW2

    display();///NOW2
}
void keyboard(unsigned char key, int x, int y)///NOW3
{///NOW3
    if(key=='1') angleID=1;///NOW3
    if(key=='2') angleID=2;///NOW3
    if(key=='3') angleID=3;///NOW3
    if(key=='4') angleID=4;///NOW3
    if(key=='5') angleID=5;
    if(key=='6') angleID=6;
    if(key=='7') angleID=7;
    if(key=='8') angleID=8;
    if(key=='9') angleID=9;

}


int main(int argc,char**argv)



{

    glutInit(&argc,argv);

    glutInitDisplayMode(GLUT_DOUBLE|GLUT_DEPTH);
    glutInitWindowSize(600,600);
    glutCreateWindow("06161012");
    glutKeyboardFunc(keyboard);///NOW3
    glutMouseFunc(mouse);///NOW2
    glutMotionFunc(motion);///NOW2


    glutDisplayFunc(display);

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



}

沒有留言:

張貼留言