-
Notifications
You must be signed in to change notification settings - Fork 1
/
renderer.d
379 lines (314 loc) · 12.2 KB
/
renderer.d
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
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
module unde.games.renderer;
import std.format;
import std.conv;
import std.math;
import std.string;
import std.stdio;
import derelict.sdl2.image;
import derelict.sdl2.sdl;
import derelict.opengl3.gl3;
import derelict.opengl3.gl;
import std.algorithm.comparison;
import std.utf;
import unde.global_state;
import unde.games.obj_loader;
private GLuint[string] textures;
/* ---------------------------------------------------------------------------- */
bool apply_material(GlobalState gs, shared MtlMaterial *mtl,
string delegate(GlobalState gs, string name) tex_anim = null)
{
float[4] c;
GLenum fill_mode;
int ret1, ret2;
float opacity = 1.0;
if (mtl.transparency) opacity = mtl.transparency;
if (mtl.diffuse[0].isNaN())
c = [0.8f, 0.8f, 0.8f, opacity];
else
c = mtl.diffuse ~ opacity;
glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, cast(const(float)*)c);
//if (mtl.specular[0].isNaN())
c = [0.0f, 0.0f, 0.0f, 1.0f];
//else
// c = mtl.specular ~ 1.0f;
glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, cast(const(float)*)c);
if (mtl.ambient[0].isNaN())
c = [0.2f, 0.2f, 0.2f, 1.0f];
else
c = mtl.ambient ~ 1.0f;
glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT, cast(const(float)*)c);
if (mtl.emissive[0].isNaN())
c = [0.0f, 0.0f, 0.0f, 1.0f];
else
c = mtl.emissive ~ 1.0f;
glMaterialfv(GL_FRONT_AND_BACK, GL_EMISSION, cast(const(float)*)c);
glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, 0.0f);
glPolygonMode(GL_FRONT_AND_BACK, false ? GL_LINE : GL_FILL);
//Not Two sided
glEnable(GL_CULL_FACE);
bool normals = false;
if(mtl.map_diffuse !is null)
{
string tex_name = mtl.map_diffuse;
if (tex_anim) tex_name = tex_anim(gs, tex_name);
string p = format("models/%s", tex_name);
GLuint texture_name;
bool link;
if (p !in textures)
{
glGenTextures(1, &texture_name);// generate GL-textures ID's
link = true;
}
else texture_name = textures[p];
//writefln("texture_name=%s", texture_name);
glBindTexture(GL_TEXTURE_2D, texture_name);// Binding of texture name
if (link)
{
//
//redefine standard texture values
//
// We will use linear interpolation for magnification filter
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
// tiling mode
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, true ? GL_REPEAT : GL_CLAMP);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, true ? GL_REPEAT : GL_CLAMP);
auto image = IMG_Load(p.toStringz);
if (!image)
{
throw new Exception(format("Error while loading texture: %s", p));
}
// Texture specification
glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, image.w, image.h, 0, GL_RGBA, GL_UNSIGNED_BYTE,
image.pixels);
SDL_FreeSurface(image);
textures[p] = texture_name;
}
return true;
}
else
{
glBindTexture(GL_TEXTURE_2D, 0);
}
return false;
}
GLuint[shared ObjMesh*] gl_lists;
import core.runtime;
Throwable.TraceInfo getStackTrace() {
version(Posix) {
// druntime cuts out the first few functions on the trace as they are internal
// so we'll make some dummy functions here so our actual info doesn't get cut
Throwable.TraceInfo f5() { return defaultTraceHandler(); }
Throwable.TraceInfo f4() { return f5(); }
Throwable.TraceInfo f3() { return f4(); }
Throwable.TraceInfo f2() { return f3(); }
auto stuff = f2();
} else {
auto stuff = defaultTraceHandler();
}
return stuff;
}
/* ---------------------------------------------------------------------------- */
void recursive_render (GlobalState gs, shared ObjFile *sc,
bool delegate(GlobalState gs, string name) anim = null,
string delegate(GlobalState gs, string name) tex_anim = null,
bool dontcache = false, bool reverse = false)
{
//writefln("%s", getStackTrace());
foreach(i; 0..sc.objects.length)
{
shared ObjObject *object;
if (!reverse) object = sc.objects[i];
else object = sc.objects[sc.objects.length-1-i];
GLuint prev_tex_id_idx = 0;
glPushMatrix();
bool draw = true;
string name = object.name;
if (anim) draw = anim(gs, name);
if (draw)
{
/* draw all meshes assigned to this node */
foreach (mesh; object.meshes) {
//writefln("mesh %d. material %d", n, mesh.mMaterialIndex);
bool is_texture = apply_material(gs, sc.mtl.materials[mesh.material], tex_anim);
if(object.normals is null) {
glDisable(GL_LIGHTING);
} else {
glEnable(GL_LIGHTING);
}
glDisable(GL_COLOR_MATERIAL);
bool full_draw;
if (!tex_anim && !dontcache)
{
if (mesh in gl_lists)
{
glCallList(gl_lists[mesh]);
}
else
{
gl_lists[mesh] = glGenLists(1);
if (gl_lists[mesh] <= 0)
throw new Exception(format("Error while glGenLists: %s", gl_lists[mesh]));
glNewList(gl_lists[mesh], GL_COMPILE_AND_EXECUTE);
full_draw = true;
}
}
else full_draw = true;
if (full_draw)
{
foreach (face; mesh.faces) {
GLenum face_mode;
switch(face.length) {
case 1: face_mode = GL_POINTS; break;
case 2: face_mode = GL_LINES; break;
case 3: face_mode = GL_TRIANGLES; break;
default: face_mode = GL_POLYGON; break;
}
glBegin(face_mode);
foreach(index; face) {
//glColor4fv(0.0f,0.0f,0.0f,1.0f);
if (is_texture && index.tex >= 0)
glTexCoord2f(object.texcoords[index.tex][0], 1.0-object.texcoords[index.tex][1]);
if(index.normal >= 0)
{
float[3] normal = [ object.normals[index.normal][0],
-object.normals[index.normal][1],
-object.normals[index.normal][2]];
glNormal3fv(normal.ptr);
}
if (index.vert >= 0)
{
float[3] coords = [-object.vertices[index.vert][0],
object.vertices[index.vert][1],
object.vertices[index.vert][2]];
glVertex3fv(coords.ptr);
}
}
glEnd();
}
}
if (!tex_anim && !dontcache && full_draw)
{
glEndList();
}
}
}
glPopMatrix();
}
}
void render_box (float[6] box)
{
glBegin(GL_POLYGON);
glVertex3f(box[0], box[1], box[2]);
glVertex3f(box[0], box[4], box[2]);
glVertex3f(box[3], box[4], box[2]);
glVertex3f(box[3], box[1], box[2]);
glEnd();
glBegin(GL_POLYGON);
glVertex3f(box[0], box[1], box[5]);
glVertex3f(box[0], box[4], box[5]);
glVertex3f(box[3], box[4], box[5]);
glVertex3f(box[3], box[1], box[5]);
glEnd();
glBegin(GL_POLYGON);
glVertex3f(box[0], box[1], box[2]);
glVertex3f(box[0], box[4], box[2]);
glVertex3f(box[0], box[4], box[5]);
glVertex3f(box[0], box[1], box[5]);
glEnd();
glBegin(GL_POLYGON);
glVertex3f(box[3], box[1], box[2]);
glVertex3f(box[3], box[4], box[2]);
glVertex3f(box[3], box[4], box[5]);
glVertex3f(box[3], box[1], box[5]);
glEnd();
glBegin(GL_POLYGON);
glVertex3f(box[0], box[1], box[2]);
glVertex3f(box[3], box[1], box[2]);
glVertex3f(box[3], box[1], box[5]);
glVertex3f(box[0], box[1], box[5]);
glEnd();
glBegin(GL_POLYGON);
glVertex3f(box[0], box[4], box[2]);
glVertex3f(box[3], box[4], box[2]);
glVertex3f(box[3], box[4], box[5]);
glVertex3f(box[0], box[4], box[5]);
glEnd();
}
GLuint load_texture(string path, bool grayscale = false, int skiplines=0)
{
GLuint texture_name;
glGenTextures(1, &texture_name);// generate GL-textures ID's
glBindTexture(GL_TEXTURE_2D, texture_name);// Binding of texture name
//
//redefine standard texture values
//
// We will use linear interpolation for magnification filter
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
// tiling mode
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, true ? GL_REPEAT : GL_CLAMP);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, true ? GL_REPEAT : GL_CLAMP);
auto image = IMG_Load(path.toStringz());
if (!image)
{
throw new Exception(format("Error while loading texture: %s", path));
}
// Texture specification
glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8,
image.w, image.h-skiplines, 0, grayscale?GL_LUMINANCE:GL_RGBA, GL_UNSIGNED_BYTE,
image.pixels + skiplines*image.w);
return texture_name;
}
void print_text(string text)
{
string[] encoding = ["@"," ","!",`"`,"#","$","%","&","'","(",")","*","+",",","-",".",
"/","0","1","2","3","4","5","6","7","8","9",":",";","<","=",">",
"?","@","A","B","C","D","E","F","G","H","I","J","K","L","M","N",
"O","P","Q","R","S","T","U","V","W","X","Y","Z","[",`\`,"]","^",
"_","`","a","b","c","d","e","f","g","h","i","j","k","l","m","n",
"o","p","q","r","s","t","u","v","w","x","y","z","{","|","}","~",
" "," "," ","Ё"," "," "," "," "," "," "," "," "," "," "," "," ",
" "," "," ","ё"," "," "," "," "," "," "," "," "," "," "," "," ",
"ю","а","б","ц","д","е","ф","г","х","и","й","к","л","м","н","о",
"п","я","р","с","т","у","ж","в","ь","ы","з","ш","э","щ","ч","ъ",
"Ю","А","Б","Ц","Д","Е","Ф","Г","Х","И","Й","К","Л","М","Н","О",
"П","Я","Р","С","Т","У","Ж","В","Ь","Ы","З","Ш","Э","Щ","Ч","Ъ"];
size_t x = 0;
int y = 0;
for (size_t i=0; i < text.length; i+=text.stride(i))
{
string chr = text[i..i+text.stride(i)];
if (chr == "\n")
{
y--;
x=0;
continue;
}
size_t code = -1;
foreach(j, enc; encoding)
{
if (chr == enc)
{
code = j;
break;
}
}
assert(code != -1, chr);
float cx = (code%16)/16.0;
float cy = (code/16)/12.0;
glBegin(GL_POLYGON);
glTexCoord2f(cx, cy);
glVertex3f(x, y, -10.0);
glTexCoord2f(cx, cy+1.0/12);
glVertex3f(x, y-1.0, -10.0);
glTexCoord2f(cx+1.0/16, cy+1.0/12);
glVertex3f(x+1.0, y-1.0, -10.0);
glTexCoord2f(cx+1.0/16, cy);
glVertex3f(x+1.0, y, -10.0);
glEnd();
x++;
}
}