-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathlit-obj-model.php
More file actions
307 lines (256 loc) · 10.1 KB
/
Copy pathlit-obj-model.php
File metadata and controls
307 lines (256 loc) · 10.1 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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
<?php
require 'bootstrap.php';
require 'vendor/autoload.php';
ini_set('memory_limit', '2048M');
use \Mammoth\Graphic\Camera;
use \Mammoth\Graphic\Shader;
use Mammoth\Graphic\WavefrontObjLoader;
use Mammoth\Math\Angle;
use Mammoth\Math\Matrix;
use Mammoth\Math\Transform;
use Mammoth\Math\Vector;
// Window dimensions
define('WIDTH', 800);
define('HEIGHT', 600);
$camera = new Camera(new Vector(0, 0, 3));
$lastX = WIDTH / 2.0;
$lastY = HEIGHT / 2.0;
$keys = array_fill_keys(range('a', 'z'), false);
$mouse_callback= function($xpos, $ypos)
{
global $camera;
$lastX = &$GLOBALS['lastX'];
$lastY = &$GLOBALS['lastY'];
static $firstMouse = true;
if ($firstMouse)
{
$lastX = $xpos;
$lastY = $ypos;
$firstMouse = false;
}
$xoffset = $xpos - $lastX;
$yoffset = $lastY - $ypos; // Reversed since y-coordinates go from bottom to left
$lastX = $xpos;
$lastY = $ypos;
$camera->ProcessMouseMovement($xoffset, $yoffset);
};
// Light attributes
$lightPos = new Vector(1.2, 1.0, 2.0);
// Deltatime
$deltaTime = 0.0; // Time between current frame and last frame
$lastFrame = 0.0; // Time of last frame
SDL_Init(SDL_INIT_EVERYTHING);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 3);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE);
$window = SDL_CreateWindow("Fixed pipeline example", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 640, 480, SDL_WINDOW_OPENGL | SDL_WINDOW_SHOWN);
SDL_GL_CreateContext($window);
// Define the viewport dimensions
glViewport(0, 0, WIDTH, HEIGHT);
// OpenGL options
glEnable(GL_DEPTH_TEST);
$lightingShader = new Shader\Program;
$lightingShader->add(new Shader\Vertex("shaders/basic_lighting.vs"));
$lightingShader->add(new Shader\Fragment("shaders/basic_lighting.frag"));
$lightingShader->compile();
$lightingShader->link();
$lampShader = new shader\Program;
$lampShader->add(new Shader\Vertex("shaders/lamp.vs"));
$lampShader->add(new Shader\Fragment("shaders/lamp.frag"));
$lampShader->compile();
$lampShader->link();
$lightvertices = [
-0.5, -0.5, -0.5, 0.0, 0.0, -1.0,
0.5, -0.5, -0.5, 0.0, 0.0, -1.0,
0.5, 0.5, -0.5, 0.0, 0.0, -1.0,
0.5, 0.5, -0.5, 0.0, 0.0, -1.0,
-0.5, 0.5, -0.5, 0.0, 0.0, -1.0,
-0.5, -0.5, -0.5, 0.0, 0.0, -1.0,
-0.5, -0.5, 0.5, 0.0, 0.0, 1.0,
0.5, -0.5, 0.5, 0.0, 0.0, 1.0,
0.5, 0.5, 0.5, 0.0, 0.0, 1.0,
0.5, 0.5, 0.5, 0.0, 0.0, 1.0,
-0.5, 0.5, 0.5, 0.0, 0.0, 1.0,
-0.5, -0.5, 0.5, 0.0, 0.0, 1.0,
-0.5, 0.5, 0.5, -1.0, 0.0, 0.0,
-0.5, 0.5, -0.5, -1.0, 0.0, 0.0,
-0.5, -0.5, -0.5, -1.0, 0.0, 0.0,
-0.5, -0.5, -0.5, -1.0, 0.0, 0.0,
-0.5, -0.5, 0.5, -1.0, 0.0, 0.0,
-0.5, 0.5, 0.5, -1.0, 0.0, 0.0,
0.5, 0.5, 0.5, 1.0, 0.0, 0.0,
0.5, 0.5, -0.5, 1.0, 0.0, 0.0,
0.5, -0.5, -0.5, 1.0, 0.0, 0.0,
0.5, -0.5, -0.5, 1.0, 0.0, 0.0,
0.5, -0.5, 0.5, 1.0, 0.0, 0.0,
0.5, 0.5, 0.5, 1.0, 0.0, 0.0,
-0.5, -0.5, -0.5, 0.0, -1.0, 0.0,
0.5, -0.5, -0.5, 0.0, -1.0, 0.0,
0.5, -0.5, 0.5, 0.0, -1.0, 0.0,
0.5, -0.5, 0.5, 0.0, -1.0, 0.0,
-0.5, -0.5, 0.5, 0.0, -1.0, 0.0,
-0.5, -0.5, -0.5, 0.0, -1.0, 0.0,
-0.5, 0.5, -0.5, 0.0, 1.0, 0.0,
0.5, 0.5, -0.5, 0.0, 1.0, 0.0,
0.5, 0.5, 0.5, 0.0, 1.0, 0.0,
0.5, 0.5, 0.5, 0.0, 1.0, 0.0,
-0.5, 0.5, 0.5, 0.0, 1.0, 0.0,
-0.5, 0.5, -0.5, 0.0, 1.0, 0.0
];
$loader = new WavefrontObjLoader;
$obj = $loader->load($argc === 2 ? $argv[1] : 'models/pumpkin.obj');
// Set up vertex data (and buffer(s)) and attribute pointers
$verticesVectors = $obj->getVertices();
$vertices = [];
foreach($verticesVectors as $vector) {
$vertices[] = $vector->x;
$vertices[] = $vector->y;
$vertices[] = $vector->z;
}
$indices = $obj->getVertexFaces();
$indices = array_map(function($index) { return $index - 1; }, $indices);
$normalObjects = $obj->getVertexNormals();
$normals = [];
foreach($normalObjects as $index => $normalObject) {
if(!($normalObject instanceof Vector)) {
echo "INDEX: $index\n";
var_dump($normalObject);
die;
}
else {
$normals[] = $normalObject->x;
$normals[] = $normalObject->y;
$normals[] = $normalObject->z;
}
}
// First, set the container's VAO (and VBO)
glGenVertexArrays(1, $containerVAOS); $containerVAO = $containerVAOS[0];
glBindVertexArray($containerVAO);
glGenBuffers(1, $VBOS); $VBO = $VBOS[0];
glBindBuffer(GL_ARRAY_BUFFER, $VBO);
glBufferData(GL_ARRAY_BUFFER, sizeof($vertices) * 4, $vertices, GL_STATIC_DRAW);
glGenBuffers(1, $vbo_normals); $vbo_normal = $vbo_normals[0];
glBindBuffer(GL_ARRAY_BUFFER, $vbo_normal);
glBufferData(GL_ARRAY_BUFFER, count($normals) * 4, $normals, GL_STATIC_DRAW);
glGenBuffers(1, $ebos); $ebo = $ebos[0];
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, $ebo);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, count($indices) * 4, $indices, GL_STATIC_DRAW);
// Position attribute
glEnableVertexAttribArray(0);
glBindBuffer(GL_ARRAY_BUFFER, $VBO);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, 0);
// Normal attribute
glEnableVertexAttribArray(1);
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 0, 0);
glBindVertexArray(0);
// Then, we set the light's VAO (VBO stays the same. After all, the vertices are the same for the light object (also a 3D cube))
glGenVertexArrays(1, $lightVAOS); $lightVAO = $lightVAOS[0];
glBindVertexArray($lightVAO);
glGenBuffers(1, $VBOS_light); $VBO_light = $VBOS_light[0];
glBindBuffer(GL_ARRAY_BUFFER, $VBO_light);
glBufferData(GL_ARRAY_BUFFER, sizeof($lightvertices) * 4, $lightvertices, GL_STATIC_DRAW);
// Set the vertex attributes (only position data for the lamp))
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 6 * 4, 0); // Note that we skip over the normal vectors
glEnableVertexAttribArray(0);
glBindVertexArray(0);
$event = new SDL_Event;
while(true) {
// Calculate deltatime of current frame
$lightingShader = &$GLOBALS['lightingShader'];
$lampShader = &$GLOBALS['lampShader'];
$lastFrame = &$GLOBALS['lastFrame'];
$deltaTime = &$GLOBALS['deltaTime'];
$currentFrame = microtime(true);
$deltaTime = $currentFrame - $lastFrame;
$lastFrame = $currentFrame;
// Check if any events have been activiated (key pressed, mouse moved etc.) and call corresponding response functions
do_movement();
// Clear the colorbuffer
glClearColor(0.1, 0.1, 0.1, 1.0);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
// Use cooresponding shader when setting uniforms/drawing objects
$lightingShader->use();
$objectColorLoc = glGetUniformLocation($lightingShader->getId(), "objectColor");
glUniform3f($objectColorLoc, 1.0, .5, 0.31);
$lightColorLoc = glGetUniformLocation($lightingShader->getId(), "lightColor");
glUniform3f($lightColorLoc, 1.0, 1.0, 1.0);
$lightPosLoc = glGetUniformLocation($lightingShader->getId(), "lightPos");
glUniform3f($lightPosLoc, $lightPos->x, $lightPos->y, $lightPos->z);
$viewPosLoc = glGetUniformLocation($lightingShader->getId(), "viewPos");
glUniform3f($viewPosLoc, $camera->position->x, $camera->position->y, $camera->position->z);
// Create camera transformations
$view = new Matrix();
$view = $camera->GetViewMatrix();
$projection = Transform::perspective($camera->zoom, floatval(WIDTH / HEIGHT), 0.1, 100.0);
// Get the uniform locations
$modelLoc = glGetUniformLocation($lightingShader->getId(), "model");
$viewLoc = glGetUniformLocation($lightingShader->getId(), "view");
$projLoc = glGetUniformLocation($lightingShader->getId(), "projection");
// Pass the matrices to the shader
glUniformMatrix4fv($viewLoc, 1, GL_FALSE, $view->toRowVector());
glUniformMatrix4fv($projLoc, 1, GL_FALSE, $projection->toRowVector());
// Draw the container (using container's vertex attributes)
glBindVertexArray($containerVAO);
$model = new Matrix;
$model = $model->scale(0.3);
$model = Transform::rotate($model, deg2rad(90), new Vector(1, 1, 1));
glUniformMatrix4fv($modelLoc, 1, GL_FALSE, $model->toRowVector());
$a = count($indices);
glDrawElements(GL_TRIANGLES, $a, GL_UNSIGNED_INT, null);
glBindVertexArray(0);
// Also draw the lamp object, again binding the appropriate shader
$lampShader->use();
// Get location objects for the matrices on the lamp shader (these could be different on a different shader)
$modelLoc = glGetUniformLocation($lampShader->getId(), "model");
$viewLoc = glGetUniformLocation($lampShader->getId(), "view");
$projLoc = glGetUniformLocation($lampShader->getId(), "projection");
// Set matrices
glUniformMatrix4fv($viewLoc, 1, GL_FALSE, $view->toRowVector());
glUniformMatrix4fv($projLoc, 1, GL_FALSE, $projection->toRowVector());
$model = new Matrix;
$model = Transform::translate($model, $lightPos);
$model = $model->scale(0.2); // Make it a smaller cube
glUniformMatrix4fv($modelLoc, 1, GL_FALSE, $model->toRowVector());
// Draw the light object (using light's vertex attributes)
glBindVertexArray($lightVAO);
glDrawArrays(GL_TRIANGLES, 0, 36);
glBindVertexArray(0);
// Swap the screen buffers
SDL_GL_SwapWindow($window);
while(SDL_PollEvent($event)) {
switch($event->type) {
case SDL_MOUSEMOTION:
$mouse_callback($event->motion->x, $event->motion->y);
break;
case SDL_KEYDOWN:
$symChar = chr($event->key->keysym->sym);
$keys['w'] = $symChar == 'w';
$keys['s'] = $symChar == 's';
$keys['a'] = $symChar == 'a';
$keys['d'] = $symChar == 'd';
break;
case SDL_KEYUP:
$keys = array_fill_keys(range('a', 'z'), false);
break;
}
}
}
function do_movement()
{
global $deltaTime;
$camera = &$GLOBALS['camera'];
$keys = &$GLOBALS['keys'];
// Camera controls
if ($keys['w']) {
$camera->ProcessKeyboard(Camera::FORWARD, $deltaTime);
}
if ($keys['s']) {
$camera->ProcessKeyboard(Camera::BACKWARD, $deltaTime);
}
if ($keys['a']) {
$camera->ProcessKeyboard(Camera::LEFT, $deltaTime);
}
if ($keys['d']) {
$camera->ProcessKeyboard(Camera::RIGHT, $deltaTime);
}
}