-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathexample.model.h
More file actions
211 lines (178 loc) · 6.07 KB
/
example.model.h
File metadata and controls
211 lines (178 loc) · 6.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
#pragma once
#include "example.h"
#include <glwnd/model.h>
#include <glwnd/shader.h>
#include <glm/gtc/type_ptr.hpp>
#include <imgui.h>
static Model m_model_material; // static for sharing between views
class GLViewExampleModel : public GLView
{
public:
GLViewExampleModel() : GLView() {}
virtual ~GLViewExampleModel() {}
virtual void initial()
{
// model chest
static model_t chest;
chest.n = "assets\\Chest\\Chest.obj";
chest.t = glm::vec3(0.F, 0.F, 0.F);
chest.r = glm::vec3(-10.F, 120.F, 0.F);
chest.s = glm::vec3(2.F);
chest.c = glm::vec3(1.F, 1.F, 1.F);
chest.p = glm::vec3(0.F, 0.F, 0.F);
chest.d = glm::vec3(0.F, 1.F, 0.F);
chest.a = true;
// model raptor
static model_t raptor;
raptor.n = "assets\\Raptor\\Raptor.obj";
raptor.t = glm::vec3(0.0F);
raptor.r = glm::vec3(0.0F, 0.F, 0.0F);
raptor.s = glm::vec3(0.5F);
raptor.c = glm::vec3(0.0F, 1.F, 0.0F);
raptor.p = glm::vec3(0.0F, 0.F, 0.0F);
raptor.d = glm::vec3(0.0F, 1.F, 0.0F);
raptor.a = true;
// model bench
static model_t bench;
bench.n = "assets\\Bench\\bench.obj";
bench.t = glm::vec3(0.0F, -0.5F, 0.F);
bench.r = glm::vec3(0.0F, 0.F, 0.0F);
bench.s = glm::vec3(0.2F);
bench.c = glm::vec3(0.0F, 1.F, 0.0F);
bench.p = glm::vec3(0.0F, 0.F, 0.0F);
bench.d = glm::vec3(0.0F, 1.F, 0.0F);
bench.a = true;
// model teapot
static model_t teapot;
teapot.n = "assets\\Teapot\\teapot.obj";
teapot.t = glm::vec3(0.0F, -0.5F, 0.F);
teapot.r = glm::vec3(0.0F, 0.F, 0.0F);
teapot.s = glm::vec3(0.02F);
teapot.c = glm::vec3(0.0F, 1.F, 0.0F);
teapot.p = glm::vec3(0.0F, 0.F, 0.0F);
teapot.d = glm::vec3(0.0F, 1.F, 0.0F);
teapot.a = true;
// model wolf
static model_t wolf;
wolf.n = "assets\\Wolf\\Wolf.obj";
wolf.t = glm::vec3(0.0F, -1.0F, 0.F);
wolf.r = glm::vec3(0.0F, 0.F, 0.0F);
wolf.s = glm::vec3(0.7F);
wolf.c = glm::vec3(0.0F, 1.F, 0.0F);
wolf.p = glm::vec3(0.0F, 0.F, 0.0F);
wolf.d = glm::vec3(0.0F, 1.F, 0.0F);
wolf.a = true;
// model house
static model_t house;
house.n = "assets\\House\\house.3ds";
house.t = glm::vec3(0.0F, -1.0F, 0.F);
house.r = glm::vec3(0.0F, 0.F, 0.0F);
house.s = glm::vec3(0.5F);
house.c = glm::vec3(0.0F, 1.F, 0.0F);
house.p = glm::vec3(0.0F, 0.F, 0.0F);
house.d = glm::vec3(0.0F, 1.F, 0.0F);
house.a = true;
// model suzanne
static model_t suzanne;
suzanne.n = "assets\\Suzanne\\Suzanne.obj";
suzanne.t = glm::vec3(0.0F, 0.0F, 0.F);
suzanne.r = glm::vec3(0.0F, 180.F, 0.0F);
suzanne.s = glm::vec3(1.0F);
suzanne.c = glm::vec3(0.0F, 1.F, 0.0F);
suzanne.p = glm::vec3(0.0F, 0.F, 0.0F);
suzanne.d = glm::vec3(0.0F, 1.F, 0.0F);
suzanne.a = true;
// model cyborg
static model_t cyborg;
cyborg.n = "assets\\Cyborg\\cyborg.obj";
cyborg.t = glm::vec3(0.0F, -1.0F, 0.F);
cyborg.r = glm::vec3(0.0F, 180.F, 0.0F);
cyborg.s = glm::vec3(0.7F);
cyborg.c = glm::vec3(0.0F, 1.F, 0.0F);
cyborg.p = glm::vec3(0.0F, 0.F, 0.0F);
cyborg.d = glm::vec3(0.0F, 1.F, 0.0F);
cyborg.a = true;
// model gun
static model_t gun;
gun.n = "assets\\E-45-Aircraft\\E 45 Aircraft_obj.obj";
gun.t = glm::vec3(0.0F, -0.5F, 0.F);
gun.r = glm::vec3(0.0F, 180.F, 0.0F);
gun.s = glm::vec3(0.7F);
gun.c = glm::vec3(0.0F, 1.F, 0.0F);
gun.p = glm::vec3(0.0F, 0.F, 0.0F);
gun.d = glm::vec3(0.0F, 1.F, 0.0F);
gun.a = true;
// model gun 2
static model_t gun_2;
gun_2.n = "assets\\GUNs\\guns2.obj";
gun_2.t = glm::vec3(0.0F, -1.5F, 0.F);
gun_2.r = glm::vec3(0.0F, 180.F, 0.0F);
gun_2.s = glm::vec3(0.6F);
gun_2.c = glm::vec3(0.0F, 1.F, 0.0F);
gun_2.p = glm::vec3(0.0F, 0.F, 0.0F);
gun_2.d = glm::vec3(0.0F, 1.F, 0.0F);
gun_2.a = true;
// select a demo model for loading & rendering
m_model = raptor;
if (!m_model_material.ready())
{
m_model_material.load(m_model.n);
}
m_shader.build_file("assets\\shader\\model.vs", "assets\\shader\\model.fs");
}
virtual void on_display()
{
// demo combine the built-in model-view matrix of the current context with user-defined matrix
{
GLfloat eyes[4][3] =
{
{+0.F, 0.F, +.5F}, // from front
{+0.F, 0.F, -.5F}, // from back
{+.5F, 0.F, +0.F}, // turn left
{-.5F, 0.F, +0.F}, // turn right
};
const auto& eye = eyes[this->index()];
const auto& pos = m_model.p;
const auto& dir = m_model.d;
gluLookAt(eye[0], eye[1], eye[2], pos.x, pos.y, pos.z, dir.x, dir.y, dir.z);
}
// view matrix
glm::vec3 eye(0.F, 0.F, -15.F);
glm::vec3 pos = m_model.p;
glm::vec3 dir = m_model.d;
glm::mat4 mtx_view = glm::lookAt(eye, pos, dir);
mtx_view *= this->get_context_matrix(GL_MODELVIEW_MATRIX);
// projection matrix
auto win = this->viewport().coordinate().win;
glm::mat4 mtx_projection = glm::perspective(15.F, win.aspect(), 0.1F, 20.F);
// model matrix
if (m_model.a)
{
m_model.r.y += 0.3F;
if (m_model.r.y > 360.F) m_model.r.y = 0.F;
}
glm::mat4 mtx_model = this->transform_matrix(m_model.t, m_model.r, m_model.s);
// enable shader
m_shader.use(true);
// setup camera and mvp matrix
m_shader.set_variable("model", mtx_model);
m_shader.set_variable("view", mtx_view);
m_shader.set_variable("projection", mtx_projection);
// setup model
m_shader.set_variable("target", m_model.p);
// m_shader.set_variable("color", m_model.c);
// render model
m_model_material.render(m_shader);
// disable shader
m_shader.use(false);
}
private:
Shader m_shader;
struct model_t
{
std::string n; // name
glm::vec3 t, r, s, c, p, d; // translate, rotate, scale, color, position, direction (pitch, yaw, roll)
bool a = false; // animate
};
model_t m_model;
};