2017年5月22日 星期一

Week 12 20170522 鄭雅至課堂筆記

Week 12 

HW 1

1. 先開啟作業做好的專案。

















2. 調整模型。
















3. 增加計時器。

在 main 函式裡,加一句 glutTimerFunc(0,timer,0); ///(等多久,叫誰,參數)

void timer(int t)
{
        glutTimerFunc(10,timer,t+1);
        angle++;
        glutPostRedisplay();    
}


4. 大概確定好位置。


















程式碼:

#include <string.h>
#include <stdlib.h>
#include <GL/glut.h>
#include "glm.h"
GLMmodel* pmodel1 = NULL;
GLMmodel* pmodel2 = NULL;
GLMmodel* pmodel3 = NULL;
GLMmodel* pmodel4 = NULL;
GLMmodel* pmodel5 = NULL;
GLMmodel* pmodel6 = NULL;
float angle=0;
void display()
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glPushMatrix();
        //angle++;
        glRotatef(90, 0,1,0);
        if (!pmodel1) {
        pmodel1 = glmReadOBJ("data/hand.obj");
        if (!pmodel1) exit(0);
        glmUnitize(pmodel1);
        glmFacetNormals(pmodel1);
        glmVertexNormals(pmodel1, 90.0);
        }
        if (!pmodel2) {
        pmodel2 = glmReadOBJ("data/body.obj");
        if (!pmodel2) exit(0);
        glmUnitize(pmodel2);
        glmFacetNormals(pmodel2);
        glmVertexNormals(pmodel2, 90.0);
        }
        if (!pmodel3) {
        pmodel3 = glmReadOBJ("data/righthand.obj");
        if (!pmodel3) exit(0);
        glmUnitize(pmodel3);
        glmFacetNormals(pmodel3);
        glmVertexNormals(pmodel3, 90.0);
        }
        if (!pmodel4) {
        pmodel4 = glmReadOBJ("data/lefthand.obj");
        if (!pmodel4) exit(0);
        glmUnitize(pmodel4);
        glmFacetNormals(pmodel4);
        glmVertexNormals(pmodel4, 90.0);
        }
        if (!pmodel5) {
        pmodel5 = glmReadOBJ("data/rightleg.obj");
        if (!pmodel5) exit(0);
        glmUnitize(pmodel5);
        glmFacetNormals(pmodel5);
        glmVertexNormals(pmodel5, 90.0);
        }
        if (!pmodel6) {
        pmodel6 = glmReadOBJ("data/leftleg.obj");
        if (!pmodel6) exit(0);
        glmUnitize(pmodel6);
        glmFacetNormals(pmodel6);
        glmVertexNormals(pmodel6, 90.0);
        }


        glPushMatrix();///以身體body為主,是大括號
            glRotatef(angle, 0,1,0);

            glPushMatrix();
                glTranslatef(0,0.38,0);
                glScalef(1.2,1.2,1.2);///head
                glmDraw(pmodel1, GLM_SMOOTH | GLM_MATERIAL);
            glPopMatrix();

            glPushMatrix();
                ///glTranslatef(0,0,0);
                glScalef(0.9,0.9,0.9);///body
                glmDraw(pmodel2, GLM_SMOOTH | GLM_MATERIAL);
            glPopMatrix();

            glPushMatrix();
                glTranslatef(0,0,0.25);
                glScalef(0.8,0.8,0.8);///left hand
                glmDraw(pmodel3, GLM_SMOOTH | GLM_MATERIAL);
            glPopMatrix();

            glPushMatrix();
                glTranslatef(0,0,-0.25);
                glScalef(0.8,0.8,0.8);///right hand
                glmDraw(pmodel4, GLM_SMOOTH | GLM_MATERIAL);
            glPopMatrix();

            glPushMatrix();
                glTranslatef(0,-0.38,0.1);
                glScalef(0.6,0.6,0.6);
                glmDraw(pmodel5, GLM_SMOOTH | GLM_MATERIAL);
            glPopMatrix();

            glPushMatrix();
                glTranslatef(0,-0.38,-0.1);
                glScalef(0.6,0.6,0.6);
                glmDraw(pmodel6, GLM_SMOOTH | GLM_MATERIAL);
            glPopMatrix();

        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[] = { 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 };

/*#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("Yes, 3D Model Here");

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


5. 開始旋轉左右手。


















程式碼:

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


沒有留言:

張貼留言