2017年5月22日 星期一

Week 12 承諺的上課筆記

   一.計時器

   

   二.調整手臂軸心

   
   
   

   三.設定另一隻上手臂

   

   

   程式碼

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

GLMmodel* pmodel[10] = {NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL};
/*此為宣告模型變數*/

float angle=0;
void display()
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    if (!pmodel[0])
    {
        pmodel[0] = glmReadOBJ("data/body.obj");
        if (!pmodel[0]) exit(0);
        glmUnitize(pmodel[0]);
        glmFacetNormals(pmodel[0]);
        glmVertexNormals(pmodel[0], 90.0);
    }
    if (!pmodel[1])
    {
        pmodel[1] = glmReadOBJ("data/head.obj");
        if (!pmodel[1]) exit(0);
        glmUnitize(pmodel[1]);
        glmFacetNormals(pmodel[1]);
        glmVertexNormals(pmodel[1], 90.0);
    }
    if (!pmodel[2])
    {
        pmodel[2] = glmReadOBJ("data/left_arm_up.obj");
        if (!pmodel[2]) exit(0);
        glmUnitize(pmodel[2]);
        glmFacetNormals(pmodel[2]);
        glmVertexNormals(pmodel[2], 90.0);
    }
    if (!pmodel[3])
    {
        pmodel[3] = glmReadOBJ("data/left_arm_down.obj");
        if (!pmodel[3]) exit(0);
        glmUnitize(pmodel[3]);
        glmFacetNormals(pmodel[3]);
        glmVertexNormals(pmodel[3], 90.0);
    }

    if (!pmodel[4])
    {
        pmodel[4] = glmReadOBJ("data/right_arm_up.obj");
        if (!pmodel[4]) exit(0);
        glmUnitize(pmodel[4]);
        glmFacetNormals(pmodel[4]);
        glmVertexNormals(pmodel[4], 90.0);
    }

     if (!pmodel[5])
    {
        pmodel[5] = glmReadOBJ("data/right_arm_down.obj");
        if (!pmodel[5]) exit(0);
        glmUnitize(pmodel[5]);
        glmFacetNormals(pmodel[5]);
        glmVertexNormals(pmodel[5], 90.0);
    }

    if (!pmodel[6])
    {
        pmodel[6] = glmReadOBJ("data/left_leg_up.obj");
        if (!pmodel[6]) exit(0);
        glmUnitize(pmodel[6]);
        glmFacetNormals(pmodel[6]);
        glmVertexNormals(pmodel[6], 90.0);
    }

    if (!pmodel[7])
    {
        pmodel[7] = glmReadOBJ("data/left_leg_down.obj");
        if (!pmodel[7]) exit(0);
        glmUnitize(pmodel[7]);
        glmFacetNormals(pmodel[7]);
        glmVertexNormals(pmodel[7], 90.0);
    }

    if (!pmodel[8])
    {
        pmodel[8] = glmReadOBJ("data/right_leg_up.obj");
        if (!pmodel[8]) exit(0);
        glmUnitize(pmodel[8]);
        glmFacetNormals(pmodel[8]);
        glmVertexNormals(pmodel[8],90.0);
    }

    if (!pmodel[9])
    {
        pmodel[9] = glmReadOBJ("data/right_leg_down.obj");
        if (!pmodel[9]) exit(0);
        glmUnitize(pmodel[9]);
        glmFacetNormals(pmodel[9]);
        glmVertexNormals(pmodel[9],90.0);
    }
    /*此為匯入模型檔的動作,medel變數可做修改*/
    glPushMatrix();
        //glRotatef(angle, 0,1,0);

        glPushMatrix();///head
            glTranslatef(0,0,0);
            glRotatef(180,0,1,0);
            glRotatef(10,1,0,0);
            glTranslatef(0,0.65,0);
            glScalef(0.7,0.7,0.7);
            glmDraw(pmodel[1], GLM_SMOOTH | GLM_MATERIAL);
        glPopMatrix();

        glPushMatrix(); ///body
            glPushMatrix();
                glTranslatef(0,0,0);
                glRotatef(180,0,1,0);
                glScalef(0.4,0.4,0.4);
                glmDraw(pmodel[0], GLM_SMOOTH | GLM_MATERIAL);///body
            glPopMatrix();

            glPushMatrix();
                glTranslatef(0.21, 0.3, 0);
                glRotatef(angle, 0,0,1);
                glTranslatef(-0.21, -0.3, 0);
                glPushMatrix();///left_arm_up
                    glTranslatef(0.1,0,0);
                    glRotatef(20,0,1,0);
                    glTranslatef(0.17 ,0.15,0);
                    glScalef(0.27,0.27,0.27);
                    glmDraw(pmodel[2], GLM_SMOOTH | GLM_MATERIAL);
                glPopMatrix();

                glTranslatef(0.35, 0, 0);
                glRotatef(angle, 0,0,1);
                glTranslatef(-0.35, 0, 0);
                glPushMatrix(); ///left_arm_down
                    glTranslatef(0.1,0,0);
                    glRotatef(20,0,1,0);
                    glScalef(0.4,0.4,0.4);
                    glTranslatef(0.79,-0.35,0);
                    glmDraw(pmodel[3], GLM_SMOOTH | GLM_MATERIAL);
                glPopMatrix();
            glPopMatrix();

            glPushMatrix();///for test
                glTranslatef(-0.17 ,+0.2,0);
                glRotatef(-angle, 0,0,1);///先做旋轉看看
                glTranslatef(0.17 ,-0.2,0);///move rotation center to center
                glPushMatrix();///right_arm_up
                    glTranslatef(0.1,0,0);
                    glRotatef(-15,0,1,0);
                    glTranslatef(-0.35,0.15,0.15);
                    glScalef(0.27,0.27,0.27);
                    glmDraw(pmodel[4], GLM_SMOOTH | GLM_MATERIAL);
                glPopMatrix();

            glPopMatrix();

        glPopMatrix();



        glPushMatrix();///right_arm_down
            glTranslatef(0.1,0,0);
            glRotatef(-20,0,1,0);
            glTranslatef(-0.58,-0.14,-0.1);
            glScalef(0.4,0.4,0.4);
            glmDraw(pmodel[5], GLM_SMOOTH | GLM_MATERIAL);
        glPopMatrix();

        glPushMatrix();///left_leg_up
            glTranslatef(0,0,0);
            glRotatef(0,0,0,0);
            glTranslatef(0.12,-0.42,-0);
            glScalef(0.28,0.28,0.28);
            glmDraw(pmodel[6], GLM_SMOOTH | GLM_MATERIAL);
        glPopMatrix();

        glPushMatrix();///left_leg_down
            glTranslatef(0,0,0);
            glRotatef(0,0,0,0);
            glTranslatef(0.12,-0.63,-0);
            glScalef(0.45,0.56,0.43);
            glmDraw(pmodel[7], GLM_SMOOTH | GLM_MATERIAL);
        glPopMatrix();

        glPushMatrix();///right_leg_up
            glTranslatef(0,0,0);
            glRotatef(0,0,0,0);
            glTranslatef(-0.12,-0.42,-0);
            glScalef(0.28,0.28,0.28);
            glmDraw(pmodel[8], GLM_SMOOTH | GLM_MATERIAL);
        glPopMatrix();

        glPushMatrix();///right_leg_down
            glTranslatef(0,0,0);
            glRotatef(0,0,0,0);
            glTranslatef(-0.12,-0.63,-0);
            glScalef(0.45,0.56,0.43);
            glmDraw(pmodel[9], GLM_SMOOTH | GLM_MATERIAL);
        glPopMatrix();

        /*此為對匯入模型檔作變形並顯示在視窗介面的動作*/

    glPopMatrix();
    glutSwapBuffers();
}
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[] = { -3.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 };
#include <stdio.h>
void timer(int t)///計時器叫的時候, 要做什麼呢?
{
    glutTimerFunc(100, timer, t+1);///先撥下一個鬧鐘
    angle++;///做了要做的事
    printf("現在時間是 %d, 我起床了, 等一下鬧鐘會再叫一下哦  ", t);
    glutPostRedisplay();///提醒電腦,有空要畫一下畫面哦
    printf("... 然後我就又睡了....\n");
}///再度沈睡
int main(int argc, char**argv)
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH | GLUT_RGB);
    glutCreateWindow("04160152_rb");

    glutDisplayFunc(display);
    glutIdleFunc(display);
    glutTimerFunc(0, timer, 0);///(1) 第一次呼叫timer計時器

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

沒有留言:

張貼留言