顯示具有 Week12 標籤的文章。 顯示所有文章
顯示具有 Week12 標籤的文章。 顯示所有文章

2017年7月1日 星期六

week12 鄉下人筆記

1.小考練習-TRT轉動考古題(6行)

請看例圖:




請撰寫程式(6行):

glPushMatrix();
glTranslatef(0.4,-1.0,0);  //把手臂掛上右邊座標位置
glRotatef(angle,0,0,1);  //沿Z軸旋轉angle度
glTranslatef(0.2,-0.5,0);  //把左圖紅色圈圈移到座標(0,0,0)位置
drawleg();  //畫手臂
glPopMatrix();


2.openCV 2.1.0顯示圖片

利用CodeBlocks建立一個Console Application專案








完成後,請打上以下程式碼:
#include <opencv/highgui.h>

int main(void)
{
    IplImage* img = cvLoadImage("C:/1.png"); 
    cvShowImage("a",img); //秀出你的圖案到一個叫做a的視窗
    cvWaitKey(0);//阻斷式鍵盤事件

}



這樣就成功拉~


3.旋轉地球








Week 12浩丁週街的上課筆記(補)

第12週上課筆記

2017年6月4日 星期日

#week12- tp6vü[電腦圖學上課筆記]

[1]下載 win32、data、glut32,並開啟GLUT Project


glutLookAt(eyeX,eyeY,eyeZ) ///是攝影機

Perspective(fov視野,1,near ,far) ///這行很重要,如果沒有會使圖很破碎或解體
TRT:
glTranslatef( )掛上去
glRotatef( )旋轉
glTranslatef( )固定轉動的軸心











2017年5月30日 星期二

week 12 一權的上課筆記

(1)3D TRT轉動

Step 1:
            進入網站 jsyeh.org/3dcg10下載 data.zip / window.zip / glut32.dll
           
Step 2:
            轉動攝影機
程式碼:
新增:
//1
float eyeX=0, eyeY=0, eyeZ=1;
float centerX=0, centerY=0, centerZ=0;
float upX=0, upY=1, upZ=0;
//2
    glPushMatrix();
        gluLookAt(eyeX, eyeY, eyeZ,
                  centerX, centerY, centerZ,
                  upX, upY, upZ);
        glutSolidTeapot(0.3);
    glPopMatrix();
刪除:
 ///glutReshapeFunc(resize);
///glutKeyboardFunc(key);
///glutIdleFunc(idle);
///glEnable(GL_CULL_FACE);
  ///glCullFace(GL_BACK);

Step 3: 改 gluLookAt(....)
               
Step 4:旋轉物件
程式碼:
#include <GL/glut.h>
#include <math.h>
float eyeX=1, eyeY= 0, eyeZ= 0;
float centerX=0, centerY=0, centerZ=0;
float upX=0, upY=1, upZ=0;
float angle=0;
static void display(void)
{
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluPerspective(60, 1, 0.001, 10000);
    glMatrixMode(GL_MODELVIEW);
    angle+=0.01;
    eyeX=cos(angle); eyeZ=sin(angle);
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glColor3d(1,0,0);
    glPushMatrix();
        gluLookAt(eyeX, eyeY, eyeZ,
                  centerX, centerY, centerZ,
                  upX, upY, upZ);
        glutSolidTeapot(0.3);
    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 };

/* Program entry point */

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);
    ///glutKeyboardFunc(key);
    ///glutIdleFunc(idle);
    glutIdleFunc(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();

    return EXIT_SUCCESS;
}



Step5 :機器甩手(茶壺版)

程式碼:
加入
float angle=0, dir=1;

    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluPerspective(60, 1, 0.001, 10000);
    glMatrixMode(GL_MODELVIEW);
    angle+=dir;
    if(angle>60) dir=-dir;
    if(angle<0) dir=-dir;
        glPushMatrix();
            glColor3f(1,0,0);
            glutSolidTeapot(0.3);///head
            glPushMatrix();
                glTranslatef(0, -0.3, 0);
                glColor3f(1,1,1);
                glutSolidTeapot(0.3);///body
                glPushMatrix();
                    ///glTranslatef(0.6,0,0);///old rotate
                    glTranslatef(0.3,0,0);/// T
                    glRotatef(angle, 0,0,1);///R
                    glTranslatef(0.3,0,0);/// T
                    glutSolidTeapot(0.3);///right upper arm
                    glPushMatrix();
                        glTranslatef(0.3, 0,0);///T
                        glRotatef(angle, 0,0,1);///R
                        glTranslatef(0.3, 0,0);///T
                        glutSolidTeapot(0.3);///right lower arm
                    glPopMatrix();
                glPopMatrix();
                glPushMatrix();
                    ///glTranslatef(0.6,0,0);///old rotate
                    glTranslatef(-0.3,0,0);/// T
                    glRotatef(-angle, 0,0,1);///R
                    glTranslatef(-0.3,0,0);/// T
                    glutSolidTeapot(0.3);///left upper arm
                    glPushMatrix();
                        glTranslatef(-0.3, 0,0);///T
                        glRotatef(-angle, 0,0,1);///R
                        glTranslatef(-0.3, 0,0);///T
                        glutSolidTeapot(0.3);///left lower arm
                    glPopMatrix();
                glPopMatrix();
            glPopMatrix();
        glPopMatrix();
    glPopMatrix();
(2)矩陣、投影矩陣

(3)攝影矩陣
(4)會動的機器人

2017年5月22日 星期一

Week 12 20170522

課堂作業一
利用CodeBlocks開啟並調整參數值
課堂作業二

在main()主函式中加入計時器程式碼。

      glutTimerFunc( sec , timer , 0 );

     
程式碼

#include <stdio.h>

void timer(int t)  /*鬧鐘叫醒時要做什麼事~*/
{
    glutTimerFunc(10,timer,t+1); 
    /*代表先設定下一次什麼時候鬧鐘會叫醒*/   
    angle++;
    printf("現在時間是 %d ",t);
    glutPostRedisplay();
    /*提醒電腦有空要畫一下畫面哦哦哦*/
}
/*再度進入沉睡時間*/

week 12 楊語琛的上課筆記 レ(゚∀゚;)ヘ=З=З=З

一 旋轉模型
    
 先建出自己想要使用的模型
    上面先設float angle=0;
    float++;
    glPushMatrix();
    glRotatef(angle, 0,1,0);

    中間放入自己想使之旋轉的部件

    glPopMatrix();

二 加入時間流逝,配合Timer旋轉
  
   #include <stdio.h>
     void timer(int t)///計時器叫的時候, 要做什麼呢?
     {
         glutTimerFunc(100, timer, t+1);///先撥下一個鬧鐘
         angle++;///做了要做的事
         printf("現在時間是 %d, 我起床了, 等一下鬧鐘會再叫一下哦  ", t);
         glutPostRedisplay();///提醒電腦,有空要畫一下畫面哦
         printf("... 然後我就又睡了....\n");
     }///再度沈睡
    

Week12陳冠霖的筆記

本周課程:
機器人的頭跟手跟著身體旋轉
將程式碼中的display變數中在新增一組glPushMatrix();、glPopMatrix();將要旋轉的物體丟進去,接著在裡面打上旋轉的程式碼glRotatef(headAngle,0,1,0);,這樣放在glPushMatrix();、glPopMatrix();之間的物體都能跟身體旋轉,簡單說就是在glPushMatrix();、glPopMatrix();之間的物體,一起旋轉。
程式碼如下:
#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;
GLMmodel* pmodel7 = NULL;
GLMmodel* pmodel8 = NULL;
float angle=0;
float headAngle=0;
void display()
{
    headAngle++;
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glPushMatrix();

        glRotatef(angle, 0,1,0);
        if (!pmodel1) {//head

        pmodel1 = glmReadOBJ("C:\\Users\\user\\Desktop\\hw122/head.obj");
        if (!pmodel1) exit(0);

        glmUnitize(pmodel1);
        glmFacetNormals(pmodel1);
        glmVertexNormals(pmodel1, 90.0);
        }
           if (!pmodel2) {//body

        pmodel2 = glmReadOBJ("C:\\Users\\user\\Desktop\\hw122/body.obj");
        if (!pmodel2) exit(0);

        glmUnitize(pmodel2);
        glmFacetNormals(pmodel2);
        glmVertexNormals(pmodel2, 90.0);
        }
             if (!pmodel3) {

        pmodel3 = glmReadOBJ("C:\\Users\\user\\Desktop\\hw122/up1.obj");
        if (!pmodel3) exit(0);

        glmUnitize(pmodel3);
        glmFacetNormals(pmodel3);
        glmVertexNormals(pmodel3, 90.0);
        }
             if (!pmodel4) {

        pmodel4 = glmReadOBJ("C:\\Users\\user\\Desktop\\hw122/up2.obj");
        if (!pmodel4) exit(0);

        glmUnitize(pmodel4);
        glmFacetNormals(pmodel4);
        glmVertexNormals(pmodel4, 90.0);
        }
            if (!pmodel5) {

        pmodel5 = glmReadOBJ("C:\\Users\\user\\Desktop\\hw122/hand.obj");
        if (!pmodel5) exit(0);

        glmUnitize(pmodel5);
        glmFacetNormals(pmodel5);
        glmVertexNormals(pmodel5, 90.0);
        }
            if (!pmodel6) {

        pmodel6 = glmReadOBJ("C:\\Users\\user\\Desktop\\hw122/down1.obj");
        if (!pmodel6) exit(0);

        glmUnitize(pmodel6);
        glmFacetNormals(pmodel6);
        glmVertexNormals(pmodel6, 90.0);
        }
            if (!pmodel7) {

        pmodel7 = glmReadOBJ("C:\\Users\\user\\Desktop\\hw122/down2.obj");
        if (!pmodel7) exit(0);

        glmUnitize(pmodel7);
        glmFacetNormals(pmodel7);
        glmVertexNormals(pmodel7, 90.0);
        }
            if (!pmodel8) {

        pmodel8 = glmReadOBJ("C:\\Users\\user\\Desktop\\hw122/down3.obj");
        if (!pmodel8) exit(0);

        glmUnitize(pmodel8);
        glmFacetNormals(pmodel8);
        glmVertexNormals(pmodel8, 90.0);
        }

         glPushMatrix();  //頭
            glRotatef(headAngle,0,1,0);//旋轉
            glPushMatrix();  //身體
                glTranslatef(0,0.1,0);//移動
                glRotatef(90,0,1,0);//旋轉
                glScalef(0.4,0.4,0.4);//縮放
                glmDraw(pmodel2, GLM_SMOOTH | GLM_MATERIAL);
            glPopMatrix();
            glPushMatrix();
                glTranslatef(0,0.65,0);//移動
                glRotatef(90,0,1,0);//旋轉
                glScalef(0.6,0.6,0.6);//縮放
                glmDraw(pmodel1, GLM_SMOOTH | GLM_MATERIAL);
            glPopMatrix();
            glPushMatrix();//右臂
                glTranslatef(0.45,0.3,0);
                glRotatef(90,0,1,0);
                glScalef(0.5,0.5,0.5);
                glmDraw(pmodel3, GLM_SMOOTH | GLM_MATERIAL);
            glPopMatrix();
            glPushMatrix();//右籌
                glTranslatef(0.55,0,0);
                glRotatef(90,0,1,0);
                glScalef(0.5,0.5,0.5);
                glmDraw(pmodel4, GLM_SMOOTH | GLM_MATERIAL);
            glPopMatrix();
            glPushMatrix();//右長
                glTranslatef(0.8,0,0);
                glRotatef(270,0,1,0);
                glScalef(0.5,0.5,0.5);
                glmDraw(pmodel5, GLM_SMOOTH | GLM_MATERIAL);
            glPopMatrix();
            glPushMatrix();//左臂
                glTranslatef(-0.45,0.3,0);
                glRotatef(90,0,1,0);
                glScalef(0.5,0.5,0.5);
                glmDraw(pmodel3, GLM_SMOOTH | GLM_MATERIAL);
            glPopMatrix();
                glPushMatrix();//左籌
                glTranslatef(-0.55,0,0);
                glRotatef(90,0,1,0);
                glScalef(0.5,0.5,0.5);
                glmDraw(pmodel4, GLM_SMOOTH | GLM_MATERIAL);
            glPopMatrix();
                glPushMatrix();//左掌
                glTranslatef(-0.8,0,0);
                glRotatef(90,0,1,0);
                glScalef(0.5,0.5,0.5);
                glmDraw(pmodel5, GLM_SMOOTH | GLM_MATERIAL);
            glPopMatrix();
         glPopMatrix();




         glPushMatrix();//左大腿
            glTranslatef(-0.2,-0.35,0);
            glRotatef(90,0,1,0);
            glScalef(0.2,0.25,0.25);
            glmDraw(pmodel6, GLM_SMOOTH | GLM_MATERIAL);
         glPopMatrix();
        glPushMatrix();//左小腿
            glTranslatef(-0.25,-0.7,0);
            glRotatef(90,0,1,0);
            glScalef(0.5,0.4,0.5);
            glmDraw(pmodel7, GLM_SMOOTH | GLM_MATERIAL);
         glPopMatrix();
         glPushMatrix();//左掌
            glTranslatef(-0.3,-0.9,0);
            glRotatef(90,0,1,0);
            glScalef(0.5,0.5,0.5);
            glmDraw(pmodel8, GLM_SMOOTH | GLM_MATERIAL);
         glPopMatrix();

         glPushMatrix();//右大腿
            glTranslatef(0.2,-0.35,0);
            glRotatef(270,0,1,0);
            glScalef(0.2,0.25,0.25);
            glmDraw(pmodel6, GLM_SMOOTH | GLM_MATERIAL);
         glPopMatrix();
        glPushMatrix();//右小腿
            glTranslatef(0.25,-0.7,0);
            glRotatef(90,0,1,0);
            glScalef(0.5,0.4,0.5);
            glmDraw(pmodel7, GLM_SMOOTH | GLM_MATERIAL);
         glPopMatrix();
         glPushMatrix();//右腳掌
            glTranslatef(0.3,-0.9,0);
            glRotatef(90,0,1,0);
            glScalef(0.5,0.5,0.5);
            glmDraw(pmodel8, 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[] = { 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 };

int main(int argc, char**argv)
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH | GLUT_RGB);
    glutCreateWindow("04160496hw");

    glutDisplayFunc(display);
    glutIdleFunc(display);

    glClearColor(1,1,1,1);


    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();
}
利用計時氣旋轉
在原本的程式碼裡,加入 glutTimerFunc(0,timer,0);在主程式中,然後設定void timer的函數,在裡面打上 glutTimerFunc(10,timer,t+1);、angle++;,讓物體能跟著計時氣旋轉。
程式碼如下:
#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;
GLMmodel* pmodel7 = NULL;
GLMmodel* pmodel8 = NULL;
float angle=0;
void display()
{

    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glPushMatrix();


        if (!pmodel1) {//head

        pmodel1 = glmReadOBJ("C:\\Users\\user\\Desktop\\hw122/head.obj");
        if (!pmodel1) exit(0);

        glmUnitize(pmodel1);
        glmFacetNormals(pmodel1);
        glmVertexNormals(pmodel1, 90.0);
        }
           if (!pmodel2) {//body

        pmodel2 = glmReadOBJ("C:\\Users\\user\\Desktop\\hw122/body.obj");
        if (!pmodel2) exit(0);

        glmUnitize(pmodel2);
        glmFacetNormals(pmodel2);
        glmVertexNormals(pmodel2, 90.0);
        }
             if (!pmodel3) {

        pmodel3 = glmReadOBJ("C:\\Users\\user\\Desktop\\hw122/up1.obj");
        if (!pmodel3) exit(0);

        glmUnitize(pmodel3);
        glmFacetNormals(pmodel3);
        glmVertexNormals(pmodel3, 90.0);
        }
             if (!pmodel4) {

        pmodel4 = glmReadOBJ("C:\\Users\\user\\Desktop\\hw122/up2.obj");
        if (!pmodel4) exit(0);

        glmUnitize(pmodel4);
        glmFacetNormals(pmodel4);
        glmVertexNormals(pmodel4, 90.0);
        }
            if (!pmodel5) {

        pmodel5 = glmReadOBJ("C:\\Users\\user\\Desktop\\hw122/hand.obj");
        if (!pmodel5) exit(0);

        glmUnitize(pmodel5);
        glmFacetNormals(pmodel5);
        glmVertexNormals(pmodel5, 90.0);
        }
            if (!pmodel6) {

        pmodel6 = glmReadOBJ("C:\\Users\\user\\Desktop\\hw122/down1.obj");
        if (!pmodel6) exit(0);

        glmUnitize(pmodel6);
        glmFacetNormals(pmodel6);
        glmVertexNormals(pmodel6, 90.0);
        }
            if (!pmodel7) {

        pmodel7 = glmReadOBJ("C:\\Users\\user\\Desktop\\hw122/down2.obj");
        if (!pmodel7) exit(0);

        glmUnitize(pmodel7);
        glmFacetNormals(pmodel7);
        glmVertexNormals(pmodel7, 90.0);
        }
            if (!pmodel8) {

        pmodel8 = glmReadOBJ("C:\\Users\\user\\Desktop\\hw122/down3.obj");
        if (!pmodel8) exit(0);

        glmUnitize(pmodel8);
        glmFacetNormals(pmodel8);
        glmVertexNormals(pmodel8, 90.0);
        }

         glPushMatrix();
            //glRotatef(angle,0,1,0);//旋轉
            glPushMatrix();//頭
                glTranslatef(0,0.65,0);//移動
                glRotatef(90,0,1,0);//旋轉
                glScalef(0.6,0.6,0.6);//縮放
                glmDraw(pmodel1, GLM_SMOOTH | GLM_MATERIAL);
            glPopMatrix();
            glPushMatrix();  //身體
                glTranslatef(0,0.1,0);//移動
                glRotatef(90,0,1,0);//旋轉
                glScalef(0.4,0.4,0.4);//縮放
                glmDraw(pmodel2, GLM_SMOOTH | GLM_MATERIAL);
            glPopMatrix();
            glPushMatrix();//右臂
                    glTranslatef(0.43,0.3,0);
                    glRotatef(angle,1,0,0);
                    glTranslatef(-0.45,-0.3,0);
                glPushMatrix();
                    glTranslatef(0.45,0.3,0);
                    glRotatef(90,0,1,0);
                    glScalef(0.5,0.5,0.5);
                    glmDraw(pmodel3, GLM_SMOOTH | GLM_MATERIAL);
                glPopMatrix();
                glPushMatrix();//右籌
                    glTranslatef(0.55,0,0);
                    glRotatef(90,0,1,0);
                    glScalef(0.5,0.5,0.5);
                    glmDraw(pmodel4, GLM_SMOOTH | GLM_MATERIAL);
                glPopMatrix();
                glPushMatrix();//右長
                    glTranslatef(0.8,0,0);
                    glRotatef(270,0,1,0);
                    glScalef(0.5,0.5,0.5);
                    glmDraw(pmodel5, GLM_SMOOTH | GLM_MATERIAL);
                glPopMatrix();
            glPopMatrix();
            glPushMatrix();
                    glTranslatef(0.46,0.3,0);
                    glRotatef(angle,1,0,0);
                    glTranslatef(-0.45,-0.3,0);
                glPushMatrix();//左臂
                        glTranslatef(-0.45,0.3,0);
                        glRotatef(90,0,1,0);
                        glScalef(0.5,0.5,0.5);
                        glmDraw(pmodel3, GLM_SMOOTH | GLM_MATERIAL);
                glPopMatrix();
                glPushMatrix();//左籌
                    glTranslatef(-0.55,0,0);
                        glRotatef(90,0,1,0);
                        glScalef(0.5,0.5,0.5);
                        glmDraw(pmodel4, GLM_SMOOTH | GLM_MATERIAL);
                glPopMatrix();
                glPushMatrix();//左掌
                        glTranslatef(-0.8,0,0);
                        glRotatef(90,0,1,0);
                        glScalef(0.5,0.5,0.5);
                        glmDraw(pmodel5, GLM_SMOOTH | GLM_MATERIAL);
                glPopMatrix();
            glPopMatrix();
        glPopMatrix();
    glPopMatrix();




         glPushMatrix();//左大腿
            glTranslatef(-0.2,-0.35,0);
            glRotatef(90,0,1,0);
            glScalef(0.2,0.25,0.25);
            glmDraw(pmodel6, GLM_SMOOTH | GLM_MATERIAL);
         glPopMatrix();
        glPushMatrix();//左小腿
            glTranslatef(-0.25,-0.7,0);
            glRotatef(90,0,1,0);
            glScalef(0.5,0.4,0.5);
            glmDraw(pmodel7, GLM_SMOOTH | GLM_MATERIAL);
         glPopMatrix();
         glPushMatrix();//左掌
            glTranslatef(-0.3,-0.9,0);
            glRotatef(90,0,1,0);
            glScalef(0.5,0.5,0.5);
            glmDraw(pmodel8, GLM_SMOOTH | GLM_MATERIAL);
         glPopMatrix();

         glPushMatrix();//右大腿
            glTranslatef(0.2,-0.35,0);
            glRotatef(270,0,1,0);
            glScalef(0.2,0.25,0.25);
            glmDraw(pmodel6, GLM_SMOOTH | GLM_MATERIAL);
         glPopMatrix();
        glPushMatrix();//右小腿
            glTranslatef(0.25,-0.7,0);
            glRotatef(90,0,1,0);
            glScalef(0.5,0.4,0.5);
            glmDraw(pmodel7, GLM_SMOOTH | GLM_MATERIAL);
         glPopMatrix();
         glPushMatrix();//右腳掌
            glTranslatef(0.3,-0.9,0);
            glRotatef(90,0,1,0);
            glScalef(0.5,0.5,0.5);
            glmDraw(pmodel8, 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[] = { 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(10,timer,t+1);
    angle++;
    printf("%d\n",t);
}
int main(int argc, char**argv)
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH | GLUT_RGB);
    glutCreateWindow("04160496hw");

    glutDisplayFunc(display);
    glutIdleFunc(display);
    glutTimerFunc(0,timer,0);
    glClearColor(1,1,1,1);


    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();
}
利用TRT讓手臂旋轉
將display函數裡手臂的程式碼再多加一組glPushMatrix();、glPopMatrix();,然後在裡面加入TRT,glTranslatef(0.46,0.3,0);、glRotatef(angle,1,0,0);、glTranslatef(-0.45,-0.3,0);裡面的數職自行調整到合適。
程式碼如下:
#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;
GLMmodel* pmodel7 = NULL;
GLMmodel* pmodel8 = NULL;
float angle=0;
void display()
{

    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glPushMatrix();


        if (!pmodel1) {//head

        pmodel1 = glmReadOBJ("C:\\Users\\user\\Desktop\\hw122/head.obj");
        if (!pmodel1) exit(0);

        glmUnitize(pmodel1);
        glmFacetNormals(pmodel1);
        glmVertexNormals(pmodel1, 90.0);
        }
           if (!pmodel2) {//body

        pmodel2 = glmReadOBJ("C:\\Users\\user\\Desktop\\hw122/body.obj");
        if (!pmodel2) exit(0);

        glmUnitize(pmodel2);
        glmFacetNormals(pmodel2);
        glmVertexNormals(pmodel2, 90.0);
        }
             if (!pmodel3) {

        pmodel3 = glmReadOBJ("C:\\Users\\user\\Desktop\\hw122/up1.obj");
        if (!pmodel3) exit(0);

        glmUnitize(pmodel3);
        glmFacetNormals(pmodel3);
        glmVertexNormals(pmodel3, 90.0);
        }
             if (!pmodel4) {

        pmodel4 = glmReadOBJ("C:\\Users\\user\\Desktop\\hw122/up2.obj");
        if (!pmodel4) exit(0);

        glmUnitize(pmodel4);
        glmFacetNormals(pmodel4);
        glmVertexNormals(pmodel4, 90.0);
        }
            if (!pmodel5) {

        pmodel5 = glmReadOBJ("C:\\Users\\user\\Desktop\\hw122/hand.obj");
        if (!pmodel5) exit(0);

        glmUnitize(pmodel5);
        glmFacetNormals(pmodel5);
        glmVertexNormals(pmodel5, 90.0);
        }
            if (!pmodel6) {

        pmodel6 = glmReadOBJ("C:\\Users\\user\\Desktop\\hw122/down1.obj");
        if (!pmodel6) exit(0);

        glmUnitize(pmodel6);
        glmFacetNormals(pmodel6);
        glmVertexNormals(pmodel6, 90.0);
        }
            if (!pmodel7) {

        pmodel7 = glmReadOBJ("C:\\Users\\user\\Desktop\\hw122/down2.obj");
        if (!pmodel7) exit(0);

        glmUnitize(pmodel7);
        glmFacetNormals(pmodel7);
        glmVertexNormals(pmodel7, 90.0);
        }
            if (!pmodel8) {

        pmodel8 = glmReadOBJ("C:\\Users\\user\\Desktop\\hw122/down3.obj");
        if (!pmodel8) exit(0);

        glmUnitize(pmodel8);
        glmFacetNormals(pmodel8);
        glmVertexNormals(pmodel8, 90.0);
        }

         glPushMatrix();
            //glRotatef(angle,0,1,0);//旋轉
            glPushMatrix();//頭
                glTranslatef(0,0.65,0);//移動
                glRotatef(90,0,1,0);//旋轉
                glScalef(0.6,0.6,0.6);//縮放
                glmDraw(pmodel1, GLM_SMOOTH | GLM_MATERIAL);
            glPopMatrix();
            glPushMatrix();  //身體
                glTranslatef(0,0.1,0);//移動
                glRotatef(90,0,1,0);//旋轉
                glScalef(0.4,0.4,0.4);//縮放
                glmDraw(pmodel2, GLM_SMOOTH | GLM_MATERIAL);
            glPopMatrix();
            glPushMatrix();//右臂
                    glTranslatef(0.43,0.3,0);
                    glRotatef(angle,1,0,0);
                    glTranslatef(-0.45,-0.3,0);
                glPushMatrix();
                    glTranslatef(0.45,0.3,0);
                    glRotatef(90,0,1,0);
                    glScalef(0.5,0.5,0.5);
                    glmDraw(pmodel3, GLM_SMOOTH | GLM_MATERIAL);
                glPopMatrix();
                glPushMatrix();//右籌
                    glTranslatef(0.55,0,0);
                    glRotatef(90,0,1,0);
                    glScalef(0.5,0.5,0.5);
                    glmDraw(pmodel4, GLM_SMOOTH | GLM_MATERIAL);
                glPopMatrix();
                glPushMatrix();//右長
                    glTranslatef(0.8,0,0);
                    glRotatef(270,0,1,0);
                    glScalef(0.5,0.5,0.5);
                    glmDraw(pmodel5, GLM_SMOOTH | GLM_MATERIAL);
                glPopMatrix();
            glPopMatrix();
            glPushMatrix();
                    glTranslatef(0.46,0.3,0);
                    glRotatef(angle,1,0,0);
                    glTranslatef(-0.45,-0.3,0);
                glPushMatrix();//左臂
                        glTranslatef(-0.45,0.3,0);
                        glRotatef(90,0,1,0);
                        glScalef(0.5,0.5,0.5);
                        glmDraw(pmodel3, GLM_SMOOTH | GLM_MATERIAL);
                glPopMatrix();
                glPushMatrix();//左籌
                    glTranslatef(-0.55,0,0);
                        glRotatef(90,0,1,0);
                        glScalef(0.5,0.5,0.5);
                        glmDraw(pmodel4, GLM_SMOOTH | GLM_MATERIAL);
                glPopMatrix();
                glPushMatrix();//左掌
                        glTranslatef(-0.8,0,0);
                        glRotatef(90,0,1,0);
                        glScalef(0.5,0.5,0.5);
                        glmDraw(pmodel5, GLM_SMOOTH | GLM_MATERIAL);
                glPopMatrix();
            glPopMatrix();
        glPopMatrix();
    glPopMatrix();




         glPushMatrix();//左大腿
            glTranslatef(-0.2,-0.35,0);
            glRotatef(90,0,1,0);
            glScalef(0.2,0.25,0.25);
            glmDraw(pmodel6, GLM_SMOOTH | GLM_MATERIAL);
         glPopMatrix();
        glPushMatrix();//左小腿
            glTranslatef(-0.25,-0.7,0);
            glRotatef(90,0,1,0);
            glScalef(0.5,0.4,0.5);
            glmDraw(pmodel7, GLM_SMOOTH | GLM_MATERIAL);
         glPopMatrix();
         glPushMatrix();//左掌
            glTranslatef(-0.3,-0.9,0);
            glRotatef(90,0,1,0);
            glScalef(0.5,0.5,0.5);
            glmDraw(pmodel8, GLM_SMOOTH | GLM_MATERIAL);
         glPopMatrix();

         glPushMatrix();//右大腿
            glTranslatef(0.2,-0.35,0);
            glRotatef(270,0,1,0);
            glScalef(0.2,0.25,0.25);
            glmDraw(pmodel6, GLM_SMOOTH | GLM_MATERIAL);
         glPopMatrix();
        glPushMatrix();//右小腿
            glTranslatef(0.25,-0.7,0);
            glRotatef(90,0,1,0);
            glScalef(0.5,0.4,0.5);
            glmDraw(pmodel7, GLM_SMOOTH | GLM_MATERIAL);
         glPopMatrix();
         glPushMatrix();//右腳掌
            glTranslatef(0.3,-0.9,0);
            glRotatef(90,0,1,0);
            glScalef(0.5,0.5,0.5);
            glmDraw(pmodel8, 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[] = { 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(10,timer,t+1);
    angle++;
    printf("%d\n",t);
}
int main(int argc, char**argv)
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH | GLUT_RGB);
    glutCreateWindow("04160496hw");

    glutDisplayFunc(display);
    glutIdleFunc(display);
    glutTimerFunc(0,timer,0);
    glClearColor(1,1,1,1);


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