2017年5月8日 星期一

Week11 閻覃的上課筆記

Week11 閻覃的上課筆記.md

Week11 閻覃的上課筆記

使用3D檔案總管匯出CPP并運行

一個很黯的鋼鐵人

擷取

分割机器人

25BAD016-0D75-4292-9D59-66EE9A6F6CCF

组合机器人

363856D3-002C-4D89-8AD3-BD199219ED58

 
121
1
#include <string.h>
2
#include <stdlib.h>
3
#include <GLUT/glut.h>
4
#include "glm.h"
5
6
GLMmodel *armL = NULL;
7
GLMmodel *armR = NULL;
8
GLMmodel *legL = NULL;
9
GLMmodel *legR = NULL;
10
GLMmodel *body = NULL;
11
float angle = 0;
12
13
void display() {
14
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
15
    glPushMatrix();
16
    {
17
        angle++;
18
        glRotatef(angle, 0, 1, 0);
19
        const float armScale = 1.3;
20
        glPushMatrix();
21
        {
22
            glTranslatef(-0.47, 0.32, 0);
23
            glScalef(armScale, armScale, armScale);
24
            glmDraw(armL, GLM_SMOOTH | GLM_MATERIAL);
25
        }
26
        glPopMatrix();
27
        glPushMatrix();
28
        {
29
            glTranslatef(0.47, 0.32, 0);
30
            glScalef(armScale, armScale, armScale);
31
            glmDraw(armR, GLM_SMOOTH | GLM_MATERIAL);
32
        }
33
        glPopMatrix();
34
        const float legScale = 0.39;
35
        glPushMatrix();
36
        {
37
            glTranslatef(-0.08, -0.42, 0.05);
38
            glScalef(legScale, legScale, legScale);
39
            glmDraw(legL, GLM_SMOOTH | GLM_MATERIAL);
40
        }
41
        glPopMatrix();
42
        glPushMatrix();
43
        {
44
            glTranslatef(0.08, -0.42, 0.05);
45
            glScalef(legScale, legScale, legScale);
46
            glmDraw(legR, GLM_SMOOTH | GLM_MATERIAL);
47
        }
48
        glPopMatrix();
49
        glPushMatrix();
50
        {
51
52
53
            glTranslatef(0, 0.2, 0);
54
            const float scale = 2;
55
            glScalef(scale, scale, scale);
56
            glmDraw(body, GLM_SMOOTH | GLM_MATERIAL);
57
        }
58
        glPopMatrix();
59
60
    }
61
    glPopMatrix();
62
    glutSwapBuffers();
63
}
64
65
const GLfloat light_ambient[] = {0.0f, 0.0f, 0.0f, 1.0f};
66
const GLfloat light_diffuse[] = {1.0f, 1.0f, 1.0f, 1.0f};
67
const GLfloat light_specular[] = {1.0f, 1.0f, 1.0f, 1.0f};
68
const GLfloat light_position[] = {2.0f, 5.0f, -5.0f, 0.0f};
69
70
const GLfloat mat_ambient[] = {0.7f, 0.7f, 0.7f, 1.0f};
71
const GLfloat mat_diffuse[] = {0.8f, 0.8f, 0.8f, 1.0f};
72
const GLfloat mat_specular[] = {1.0f, 1.0f, 1.0f, 1.0f};
73
const GLfloat high_shininess[] = {100.0f};
74
75
void initModel(char *path, GLMmodel *&pmodel) {
76
    pmodel = glmReadOBJ(path);
77
    if (!pmodel) exit(0);
78
    glmUnitize(pmodel);
79
    glmFacetNormals(pmodel);
80
    glmVertexNormals(pmodel, 90.0);
81
}
82
83
int main(int argc, char **argv) {
84
85
    initModel("IronMan/leg_l.obj", legR);
86
    initModel("IronMan/leg_r.obj", legL);
87
    initModel("IronMan/arm_l.obj", armR);
88
    initModel("IronMan/arm_r.obj", armL);
89
    initModel("IronMan/body.obj", body);
90
91
    glutInit(&argc, argv);
92
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH | GLUT_RGB);
93
    glutCreateWindow("Yes, Iron Man Here");
94
95
    glutDisplayFunc(display);
96
    glutIdleFunc(display);
97
98
    glClearColor(1, 1, 1, 1);
99
    //glEnable(GL_CULL_FACE);
100
    //glCullFace(GL_BACK);
101
102
    glEnable(GL_DEPTH_TEST);
103
    glDepthFunc(GL_LESS);
104
105
    glEnable(GL_LIGHT0);
106
    glEnable(GL_NORMALIZE);
107
    glEnable(GL_COLOR_MATERIAL);
108
    glEnable(GL_LIGHTING);
109
110
    glLightfv(GL_LIGHT0, GL_AMBIENT, light_ambient);
111
    glLightfv(GL_LIGHT0, GL_DIFFUSE, light_diffuse);
112
    glLightfv(GL_LIGHT0, GL_SPECULAR, light_specular);
113
    glLightfv(GL_LIGHT0, GL_POSITION, light_position);
114
115
    glMaterialfv(GL_FRONT, GL_AMBIENT, mat_ambient);
116
    glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_diffuse);
117
    glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);
118
    glMaterialfv(GL_FRONT, GL_SHININESS, high_shininess);
119
120
    glutMainLoop();
121
}

沒有留言:

張貼留言