2019年5月17日 星期五

棗酒母湯

寫程式建檔案

開啟Console application專案


輸入程式碼↓↓↓↓↓

#include <stdio.h>///(0)
FILE * fout=NULL;///(1)宣告 檔案變數 指標
int main()
{
    fout=fopen("filename.txt","w+");///(2)開檔案,等一下要寫
    for(int i=0;i<20;i++)
    {
        fprintf(fout,"Hello World\n");///(3)
    }
}


執行後可以在Week13_file的檔案夾中看到filename.txt


更改程式碼↓↓↓↓↓

#include <stdio.h>///(0)
FILE * fout=NULL;///(1)宣告 檔案變數 指標
float angle[20];
int main()
{
    fout=fopen("motion.txt","w+");///(2)開檔案,等一下要寫
    for(int i=0;i<20;i++)
    {
        fprintf(fout," %.1f",angle[i]);
        ///沒空格會很難看
        ///如果只打%f會印出太多0,%.1f是小數點後一位
    }
}


執行後可以在Week13_file的檔案夾中看到motion.txt


--------------------------------------------------------------------------------

開GLUT專案
打程式碼讓視窗變大↓↓↓↓↓

#include <GL/glut.h>
void display()
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    glutSolidTeapot( 0.3 );

    glutSwapBuffers();
}
int main(int argc, char**argv)
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
    glutInitWindowSize(600,600);///我們可以開很大的視窗
    glutCreateWindow("week 13 motion TRT angle file");

    glutDisplayFunc(display);

    glutMainLoop();
}


執行結果↓↓↓↓↓

--------------------------------------------------------------------------------

先按 w ,再分別按1,2,3,4移動四個關節,黑視窗會紀錄移動位置
按 r 重新整理,再一直按著 r ,會自己移動到剛剛移動的位置

程式碼↓↓↓↓↓

#include <stdio.h> ///(0)  ///NOW4
#include <GL/glut.h>
FILE * fout=NULL;///(1) ///NOW4
FILE * fin =NULL;///(1) ///NOW5
float angle[20];///NOW
int angleID=1;///第幾個要轉動的關節 ///NOW3
void display()
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    glPushMatrix();///body
        glColor3f(1,1,1);///white
        glutSolidTeapot( 0.3 );///body

        glPushMatrix();///right Arm
            glTranslatef(0.18, 0, 0);///(3)掛到肩上
            glRotatef(angle[1], 0,0,1);///(2)轉動 ///NOW
            glTranslatef(0.18, 0, 0);///(1)改變旋轉中心,放到中心

            glColor3f(1,0,0);///red
            glutSolidTeapot( 0.2 );///right upper arm

            glPushMatrix();///NOW2 長出下手臂
                glTranslatef(0.18, 0, 0);///(3)掛到肘上
                glRotatef(angle[2], 0,0,1);///(2)轉動 ///NOW
                glTranslatef(0.18, 0, 0);///(1)改變旋轉中心,放到中心

                glColor3f(1,0,0);///red
                glutSolidTeapot( 0.2 );///right lower arm

            glPopMatrix();
        glPopMatrix();

        glPushMatrix();///left Arm
            glTranslatef(-0.18, 0, 0);///(3)掛到肩上
            glRotatef(angle[3], 0,0,1);///(2)轉動 ///NOW
            glTranslatef(-0.18, 0, 0);///(1)改變旋轉中心,放到中心

            glColor3f(1,0,0);///red
            glutSolidTeapot( 0.2 );///left upper arm

            glPushMatrix();///NOW2 長出下手臂
                glTranslatef(-0.18, 0, 0);///(3)掛到肘上
                glRotatef(angle[4], 0,0,1);///(2)轉動 ///NOW
                glTranslatef(-0.18, 0, 0);///(1)改變旋轉中心,放到中心

                glColor3f(1,0,0);///red
                glutSolidTeapot( 0.2 );///left lower arm

            glPopMatrix();
        glPopMatrix();
    glPopMatrix();

    glutSwapBuffers();
}
int oldX;///NOW
void mouse(int button, int state, int x, int y)///NOW
{
    oldX = x;///NOW
}
void motion(int x, int y)///NOW
{
    angle[angleID] += (x-oldX);///NOW ///NOW3
    oldX = x;///NOW
    if(fout==NULL) fout=fopen("motion.txt", "w+");///NOW4
    for(int i=0; i<20; i++){///NOW4
        printf( "%.1f ", angle[i]);///NOW4
        fprintf( fout, "%.1f ", angle[i]);///NOW4
    }
    printf("\n");///NOW4
    fprintf(fout, "\n");///NOW4
    display();///NOW
}
void keyboard(unsigned char key, int x, int y)///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' || key=='W'){///NOW4 小寫的 w
        if(fout==NULL) fout=fopen("motion.txt", "w+");///NOW4
        for(int i=0; i<20; i++){///NOW4
            printf( "%.1f ", angle[i]);///NOW4
            fprintf( fout, "%.1f ", angle[i]);///NOW4
        }
        printf("\n");///NOW4
        fprintf(fout, "\n");///NOW4
    }
    if(key=='r'){///NOW5
        if(fin==NULL) fin = fopen("motion.txt", "r");///NOW5
        for(int i=0; i<20;i++){///NOW5
            fscanf(fin, "%f", &angle[i]);///NOW5
        }///NOW5
    }
    glutPostRedisplay();///和 display()很像,但更好 ///NOW5
}
int main(int argc, char**argv)
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
    ///glutInitWindowSize(600,600);///我們可以開很大的視窗
    glutCreateWindow("week 13 motion TRT angle file");
    glutKeyboardFunc(keyboard);///NOW3
    glutMouseFunc(mouse);///NOW
    glutMotionFunc(motion);///NOW
    glutDisplayFunc(display);

    glutMainLoop();
}

執行結果↓↓↓↓↓



--------------------------------------------------------------------------------

作業HW5

#include <stdio.h>
#include<GL/glut.h>
#include "glm.h"
FILE * fout=NULL;
FILE * fin =NULL;
float angle[20];
int angleID=1;
GLMmodel* pmodel0 = NULL;
GLMmodel* pmodel1 = NULL;
GLMmodel* pmodel2 = NULL;
GLMmodel* pmodel3 = NULL;
GLMmodel* pmodel4 = NULL;
GLMmodel* pmodel5 = NULL;
GLMmodel* pmodel6 = NULL;
GLMmodel* pmodel7 = NULL;
GLMmodel* pmodel8 = NULL;

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, -10.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();
        glPushMatrix();///body
            glRotatef(180, 0,1,0);
            glTranslatef(0,0.35,0);
            glScalef(0.85,0.85,0.85);
            if (!pmodel0)
            {
                pmodel0 = glmReadOBJ("data/body.obj");
                if (!pmodel0) exit(0);
                glmUnitize(pmodel0);
                glmFacetNormals(pmodel0);
                glmVertexNormals(pmodel0, 90.0);
            }
            glmDraw(pmodel0, GLM_SMOOTH | GLM_MATERIAL);
        glPopMatrix();

        glPushMatrix();///right arm
            glTranslatef(0.35,0.25,0);
            glRotatef(angle[1],0,0,1);
            glTranslatef(0,-0.1,0);
            glScalef(0.2,0.2,0.2);
            if (!pmodel1)
            {
                pmodel1 = glmReadOBJ("data/up.obj");
                if (!pmodel1) exit(0);
                glmUnitize(pmodel1);
                glmFacetNormals(pmodel1);
                glmVertexNormals(pmodel1, 90.0);
            }
            glmDraw(pmodel1, GLM_SMOOTH | GLM_MATERIAL);

            glPushMatrix();
                glTranslatef(0,-1,0);
                glRotatef(angle[2],0,0,1);
                glTranslatef(0.03,-0.7,0);
                glScalef(0.85,0.85,0.85);
                if (!pmodel2)
                {
                    pmodel2 = glmReadOBJ("data/down.obj");
                    if (!pmodel2) exit(0);
                    glmUnitize(pmodel2);
                    glmFacetNormals(pmodel2);
                    glmVertexNormals(pmodel2, 90.0);
                }
                glmDraw(pmodel2, GLM_SMOOTH | GLM_MATERIAL);
            glPopMatrix();
        glPopMatrix();

        glPushMatrix();///left arm
            glTranslatef(-0.35,0.25,0);
            glRotatef(angle[3],0,0,1);
            glTranslatef(0,-0.1,0);
            glScalef(0.2,0.2,0.2);
            if (!pmodel3)
            {
                pmodel3 = glmReadOBJ("data/up.obj");
                if (!pmodel3) exit(0);
                glmUnitize(pmodel3);
                glmFacetNormals(pmodel3);
                glmVertexNormals(pmodel3, 90.0);
            }
            glmDraw(pmodel3, GLM_SMOOTH | GLM_MATERIAL);

            glPushMatrix();
                glTranslatef(0,-1,0);
                glRotatef(angle[4],0,0,1);
                glTranslatef(-0.03,-0.7,0);
                glScalef(0.85,0.85,0.85);
                if (!pmodel4)
                {
                    pmodel4 = glmReadOBJ("data/down.obj");
                    if (!pmodel4) exit(0);
                    glmUnitize(pmodel4);
                    glmFacetNormals(pmodel4);
                    glmVertexNormals(pmodel4, 90.0);
                }
                glmDraw(pmodel4, GLM_SMOOTH | GLM_MATERIAL);
            glPopMatrix();
        glPopMatrix();

        glPushMatrix();///right leg
            glTranslatef(0.15,-0.23,0);
            glRotatef(angle[5],0,0,1);
            glTranslatef(0,-0.19,0);
            glScalef(0.2,0.2,0.2);
            if (!pmodel5)
            {
                pmodel5 = glmReadOBJ("data/up.obj");
                if (!pmodel5) exit(0);
                glmUnitize(pmodel5);
                glmFacetNormals(pmodel5);
                glmVertexNormals(pmodel5, 90.0);
            }
            glmDraw(pmodel5, GLM_SMOOTH | GLM_MATERIAL);

            glPushMatrix();
                glTranslatef(0,-1,0);
                glRotatef(angle[6],0,0,1);
                glTranslatef(0.03,-0.7,0);
                glScalef(0.85,0.85,0.85);
                if (!pmodel6)
                {
                    pmodel6 = glmReadOBJ("data/down.obj");
                    if (!pmodel6) exit(0);
                    glmUnitize(pmodel6);
                    glmFacetNormals(pmodel6);
                    glmVertexNormals(pmodel6, 90.0);
                }
                glmDraw(pmodel6, GLM_SMOOTH | GLM_MATERIAL);
            glPopMatrix();
        glPopMatrix();

        glPushMatrix();///left leg
            glTranslatef(-0.15,-0.23,0);
            glRotatef(angle[7],0,0,1);
            glTranslatef(0,-0.19,0);
            glScalef(0.2,0.2,0.2);
            if (!pmodel7)
            {
                pmodel7 = glmReadOBJ("data/up.obj");
                if (!pmodel7) exit(0);
                glmUnitize(pmodel7);
                glmFacetNormals(pmodel7);
                glmVertexNormals(pmodel7, 90.0);
            }
            glmDraw(pmodel7, GLM_SMOOTH | GLM_MATERIAL);

            glPushMatrix();
                glTranslatef(0,-1,0);
                glRotatef(angle[8],0,0,1);
                glTranslatef(-0.03,-0.7,0);
                glScalef(0.85,0.85,0.85);
                if (!pmodel8)
                {
                    pmodel8 = glmReadOBJ("data/down.obj");
                    if (!pmodel8) exit(0);
                    glmUnitize(pmodel8);
                    glmFacetNormals(pmodel8);
                    glmVertexNormals(pmodel8, 90.0);
                }
                glmDraw(pmodel8, GLM_SMOOTH | GLM_MATERIAL);
            glPopMatrix();
        glPopMatrix();
    glPopMatrix();

    glutSwapBuffers();
}

int oldX;
void mouse(int button, int state, int x, int y)
{
    oldX = x;
}
void motion(int x, int y)
{
    angle[angleID] += (x-oldX);
    oldX = x;
    if(fout==NULL) fout=fopen("motion.txt", "w+");
    for(int i=0; i<20; i++)
    {
        printf( "%.1f ", angle[i]);
        fprintf( fout, "%.1f ", angle[i]);
    }
    printf("\n");
    fprintf(fout, "\n");
    display();
}
void keyboard(unsigned char key, int x, int y)
{
    if(key=='1') angleID=1;
    if(key=='2') angleID=2;
    if(key=='3') angleID=3;
    if(key=='4') angleID=4;
    if(key=='5') angleID=5;
    if(key=='6') angleID=6;
    if(key=='7') angleID=7;
    if(key=='8') angleID=8;
    if(key=='w' || key=='W')
    {
        if(fout==NULL) fout=fopen("motion.txt", "w+");
        for(int i=0; i<20; i++)
        {
            printf( "%.1f ", angle[i]);
            fprintf( fout, "%.1f ", angle[i]);
        }
        printf("\n");
        fprintf(fout, "\n");
    }
    if(key=='r')
    {
        if(fin==NULL) fin = fopen("motion.txt", "r");
        for(int i=0; i<20;i++)
        {
            fscanf(fin, "%f", &angle[i]);
        }
    }
    glutPostRedisplay();
}

int main(int argc,char ** argv)
{
    glutInit(&argc,argv);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
     glutInitWindowSize(600,600);
    glutCreateWindow("06163036");
    glutKeyboardFunc(keyboard);
    glutMouseFunc(mouse);
    glutMotionFunc(motion);

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

沒有留言:

張貼留言