1 | // |
---|
2 | // Lol Engine |
---|
3 | // |
---|
4 | // Copyright: (c) 2010-2011 Sam Hocevar <sam@hocevar.net> |
---|
5 | // This program is free software; you can redistribute it and/or |
---|
6 | // modify it under the terms of the Do What The Fuck You Want To |
---|
7 | // Public License, Version 2, as published by Sam Hocevar. See |
---|
8 | // http://sam.zoy.org/projects/COPYING.WTFPL for more details. |
---|
9 | // |
---|
10 | |
---|
11 | #if defined HAVE_CONFIG_H |
---|
12 | # include "config.h" |
---|
13 | #endif |
---|
14 | |
---|
15 | #include <cmath> |
---|
16 | #include <cstdio> |
---|
17 | #include <cstring> |
---|
18 | |
---|
19 | #include "core.h" |
---|
20 | #include "lolgl.h" |
---|
21 | #include "loldebug.h" |
---|
22 | |
---|
23 | using namespace std; |
---|
24 | |
---|
25 | namespace lol |
---|
26 | { |
---|
27 | |
---|
28 | /* |
---|
29 | * DebugQuad implementation class |
---|
30 | */ |
---|
31 | |
---|
32 | static int const NUM_ARRAYS = 10; |
---|
33 | static int const NUM_BUFFERS = 20; |
---|
34 | static int const NUM_ATTRS = 20; |
---|
35 | static int const NUM_UNIFORMS = 20; |
---|
36 | static int const NUM_SHADERS = 20; |
---|
37 | static int const NUM_TEXTURES = 10; |
---|
38 | |
---|
39 | static int const TEX_SIZE = 32; |
---|
40 | |
---|
41 | class DebugQuadData |
---|
42 | { |
---|
43 | friend class DebugQuad; |
---|
44 | |
---|
45 | private: |
---|
46 | vec2 orig, step, aa, bb; |
---|
47 | |
---|
48 | int initialised; |
---|
49 | float time; |
---|
50 | #if !defined __CELLOS_LV2__ && !defined ANDROID_NDK |
---|
51 | GLuint array[NUM_ARRAYS]; |
---|
52 | #endif |
---|
53 | GLuint buffer[NUM_BUFFERS]; |
---|
54 | Shader *shader[NUM_SHADERS]; |
---|
55 | GLuint attr[NUM_ATTRS]; |
---|
56 | GLuint uni[NUM_UNIFORMS]; |
---|
57 | GLuint texture[NUM_TEXTURES]; |
---|
58 | uint8_t image[1][TEX_SIZE * TEX_SIZE * 4]; |
---|
59 | |
---|
60 | GLfloat const *GetVertexArray() |
---|
61 | { |
---|
62 | GLfloat tmp[18] = { aa.x, bb.y, 0, bb.x, bb.y, 0, bb.x, aa.y, 0, |
---|
63 | bb.x, aa.y, 0, aa.x, aa.y, 0, aa.x, bb.y, 0 }; |
---|
64 | memcpy(vertices, tmp, sizeof(tmp)); |
---|
65 | return vertices; |
---|
66 | } |
---|
67 | GLfloat vertices[18]; /* To cache quad coordinates */ |
---|
68 | }; |
---|
69 | |
---|
70 | /* |
---|
71 | * Public DebugQuad class |
---|
72 | */ |
---|
73 | |
---|
74 | DebugQuad::DebugQuad() |
---|
75 | : data(new DebugQuadData()) |
---|
76 | { |
---|
77 | data->initialised = 0; |
---|
78 | data->time = RandF(10000.0f); |
---|
79 | |
---|
80 | drawgroup = DRAWGROUP_HUD; |
---|
81 | } |
---|
82 | |
---|
83 | void DebugQuad::TickGame(float deltams) |
---|
84 | { |
---|
85 | Entity::TickGame(deltams); |
---|
86 | |
---|
87 | data->time += deltams; |
---|
88 | } |
---|
89 | |
---|
90 | void DebugQuad::TickDraw(float deltams) |
---|
91 | { |
---|
92 | Entity::TickDraw(deltams); |
---|
93 | |
---|
94 | if (!data->initialised && !IsDestroying()) |
---|
95 | { |
---|
96 | #if !defined __CELLOS_LV2__ && !defined ANDROID_NDK |
---|
97 | glGenVertexArrays(NUM_ARRAYS, data->array); |
---|
98 | #endif |
---|
99 | glGenBuffers(NUM_BUFFERS, data->buffer); |
---|
100 | glGenTextures(NUM_TEXTURES, data->texture); |
---|
101 | for (int i = 0; i < NUM_SHADERS; i++) |
---|
102 | data->shader[i] = NULL; |
---|
103 | |
---|
104 | /* Checkerboard texture */ |
---|
105 | glEnable(GL_TEXTURE_2D); |
---|
106 | glBindTexture(GL_TEXTURE_2D, data->texture[0]); |
---|
107 | for (int j = 0; j < TEX_SIZE; j++) |
---|
108 | for (int i = 0; i < TEX_SIZE; i++) |
---|
109 | { |
---|
110 | uint8_t wb = (((i / 2) ^ (j / 2)) & 1) * 0xff; |
---|
111 | data->image[0][(j * TEX_SIZE + i) * 4 + 0] = wb; |
---|
112 | data->image[0][(j * TEX_SIZE + i) * 4 + 1] = wb; |
---|
113 | data->image[0][(j * TEX_SIZE + i) * 4 + 2] = wb; |
---|
114 | data->image[0][(j * TEX_SIZE + i) * 4 + 3] = 0xff; |
---|
115 | } |
---|
116 | /* Use GL_RGBA instead of 4 for the internal format (Android) */ |
---|
117 | glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, TEX_SIZE, TEX_SIZE, 0, |
---|
118 | GL_RGBA, GL_UNSIGNED_BYTE, data->image[0]); |
---|
119 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); |
---|
120 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); |
---|
121 | |
---|
122 | data->initialised = 1; |
---|
123 | } |
---|
124 | else if (data->initialised && IsDestroying()) |
---|
125 | { |
---|
126 | #if !defined __CELLOS_LV2__ && !defined ANDROID_NDK |
---|
127 | glDeleteVertexArrays(NUM_ARRAYS, data->array); |
---|
128 | #endif |
---|
129 | glDeleteBuffers(NUM_BUFFERS, data->buffer); |
---|
130 | glDeleteTextures(NUM_TEXTURES, data->texture); |
---|
131 | |
---|
132 | for (int i = 0; i < NUM_SHADERS; i++) |
---|
133 | if (data->shader[i]) |
---|
134 | Shader::Destroy(data->shader[i]); |
---|
135 | |
---|
136 | data->initialised = 0; |
---|
137 | } |
---|
138 | |
---|
139 | /* Prepare our quad coordinates */ |
---|
140 | vec2i const layout(5, 4); |
---|
141 | data->step = vec2(2.0f, -2.0f) / (4 * layout + vec2i(1)); |
---|
142 | data->orig = vec2(-1.0f, 1.0f) + data->step; |
---|
143 | data->aa = data->orig; |
---|
144 | data->bb = data->orig + 3.0f * data->step; |
---|
145 | |
---|
146 | /* Generate a few random numbers */ |
---|
147 | float f1 = 0.5f + 0.5f * sinf(0.00034f * data->time); |
---|
148 | float f2 = 0.5f + 0.5f * sinf(0.00053f * data->time + 1.0f); |
---|
149 | float f3 = 0.5f + 0.5f * sinf(0.00072f * data->time + 4.0f); |
---|
150 | float f4 = 0.5f + 0.5f * sinf(0.00091f * data->time + 8.0f); |
---|
151 | |
---|
152 | GLfloat const colors[] = { f1, f2, f3, f4, f2, f1, f3, f1, f4, |
---|
153 | f3, f1, f4, f4, f3, f2, f1, f2, f3 }; |
---|
154 | GLfloat const texcoords[] = { f1, f3, f3, f2, f2, f4, |
---|
155 | f2, f4, f4, f1, f1, f3 }; |
---|
156 | |
---|
157 | /* Cheap iterators */ |
---|
158 | #if !defined __CELLOS_LV2__ && !defined ANDROID_NDK |
---|
159 | GLuint *array = data->array; |
---|
160 | #endif |
---|
161 | GLuint *buffer = data->buffer; |
---|
162 | Shader **shader = data->shader; |
---|
163 | GLuint *attr = data->attr; |
---|
164 | GLuint *uni = data->uni; |
---|
165 | |
---|
166 | ResetState(); |
---|
167 | |
---|
168 | /* |
---|
169 | * Test #1: simple glBegin code |
---|
170 | * |
---|
171 | * Renders an orange square. |
---|
172 | */ |
---|
173 | #if defined HAVE_GLBEGIN || defined USE_GLEW |
---|
174 | glColor3f(0.8f, 0.5f, 0.2f); |
---|
175 | glBegin(GL_TRIANGLES); |
---|
176 | glVertex3f(data->aa.x, data->bb.y, 0.0f); |
---|
177 | glVertex3f(data->bb.x, data->bb.y, 0.0f); |
---|
178 | glVertex3f(data->bb.x, data->aa.y, 0.0f); |
---|
179 | |
---|
180 | glVertex3f(data->bb.x, data->aa.y, 0.0f); |
---|
181 | glVertex3f(data->aa.x, data->aa.y, 0.0f); |
---|
182 | glVertex3f(data->aa.x, data->bb.y, 0.0f); |
---|
183 | glEnd(); |
---|
184 | #endif |
---|
185 | |
---|
186 | Advance(); |
---|
187 | ResetState(); |
---|
188 | |
---|
189 | /* |
---|
190 | * Test #2: glBegin + per-vertex coloring |
---|
191 | * |
---|
192 | * Renders a multicoloured square with varying colors. |
---|
193 | */ |
---|
194 | #if defined HAVE_GLBEGIN || defined USE_GLEW |
---|
195 | glBegin(GL_TRIANGLES); |
---|
196 | glColor3f(f1, f2, f3); |
---|
197 | glVertex3f(data->aa.x, data->bb.y, 0.0f); |
---|
198 | glColor3f(f4, f2, f1); |
---|
199 | glVertex3f(data->bb.x, data->bb.y, 0.0f); |
---|
200 | glColor3f(f3, f1, f4); |
---|
201 | glVertex3f(data->bb.x, data->aa.y, 0.0f); |
---|
202 | |
---|
203 | glVertex3f(data->bb.x, data->aa.y, 0.0f); |
---|
204 | glColor3f(f4, f3, f2); |
---|
205 | glVertex3f(data->aa.x, data->aa.y, 0.0f); |
---|
206 | glColor3f(f1, f2, f3); |
---|
207 | glVertex3f(data->aa.x, data->bb.y, 0.0f); |
---|
208 | glEnd(); |
---|
209 | #endif |
---|
210 | |
---|
211 | Advance(); |
---|
212 | ResetState(); |
---|
213 | |
---|
214 | /* |
---|
215 | * Test #3: glBegin + texture |
---|
216 | * |
---|
217 | * Renders a multicoloured square with varying colors multiplied with an |
---|
218 | * animated distorted checkerboard. |
---|
219 | */ |
---|
220 | #if defined HAVE_GLBEGIN || defined USE_GLEW |
---|
221 | glEnable(GL_TEXTURE_2D); |
---|
222 | glBindTexture(GL_TEXTURE_2D, data->texture[0]); |
---|
223 | glColor3f(1.0f, 1.0f, 1.0f); |
---|
224 | glBegin(GL_TRIANGLES); |
---|
225 | glColor3f(f1, f2, f3); |
---|
226 | glTexCoord2f(f1, f3); |
---|
227 | glVertex3f(data->aa.x, data->bb.y, 0.0f); |
---|
228 | glColor3f(f4, f2, f1); |
---|
229 | glTexCoord2f(f3, f2); |
---|
230 | glVertex3f(data->bb.x, data->bb.y, 0.0f); |
---|
231 | glColor3f(f3, f1, f4); |
---|
232 | glTexCoord2f(f2, f4); |
---|
233 | glVertex3f(data->bb.x, data->aa.y, 0.0f); |
---|
234 | |
---|
235 | glTexCoord2f(f2, f4); |
---|
236 | glVertex3f(data->bb.x, data->aa.y, 0.0f); |
---|
237 | glColor3f(f4, f3, f2); |
---|
238 | glTexCoord2f(f4, f1); |
---|
239 | glVertex3f(data->aa.x, data->aa.y, 0.0f); |
---|
240 | glColor3f(f1, f2, f3); |
---|
241 | glTexCoord2f(f1, f3); |
---|
242 | glVertex3f(data->aa.x, data->bb.y, 0.0f); |
---|
243 | glEnd(); |
---|
244 | glDisable(GL_TEXTURE_2D); |
---|
245 | #endif |
---|
246 | |
---|
247 | Advance(); |
---|
248 | ResetState(); |
---|
249 | |
---|
250 | /* |
---|
251 | * Test #4: glBegin + color in fragment shader |
---|
252 | * |
---|
253 | * Renders a static, coloured and tiled pattern. |
---|
254 | */ |
---|
255 | #if defined HAVE_GLBEGIN || defined USE_GLEW |
---|
256 | if (!shader[0]) |
---|
257 | shader[0] = Shader::Create( |
---|
258 | "#version 110\n" |
---|
259 | "void main()" |
---|
260 | "{" |
---|
261 | " gl_Position = gl_Vertex;" |
---|
262 | "}", |
---|
263 | |
---|
264 | "#version 110\n" |
---|
265 | "void main()" |
---|
266 | "{" |
---|
267 | " float dx = mod(gl_FragCoord.x * gl_FragCoord.y, 2.0);" |
---|
268 | " float dy = mod(gl_FragCoord.x * 0.125, 1.0);" |
---|
269 | " float dz = mod(gl_FragCoord.y * 0.125, 1.0);" |
---|
270 | " gl_FragColor = vec4(dx, dy, dz, 1.0);" |
---|
271 | "}"); |
---|
272 | shader[0]->Bind(); |
---|
273 | shader++; |
---|
274 | glColor3f(0.0f, 1.0f, 1.0f); |
---|
275 | glBegin(GL_TRIANGLES); |
---|
276 | glVertex3f(data->aa.x, data->bb.y, 0.0f); |
---|
277 | glVertex3f(data->bb.x, data->bb.y, 0.0f); |
---|
278 | glVertex3f(data->bb.x, data->aa.y, 0.0f); |
---|
279 | |
---|
280 | glVertex3f(data->bb.x, data->aa.y, 0.0f); |
---|
281 | glVertex3f(data->aa.x, data->aa.y, 0.0f); |
---|
282 | glVertex3f(data->aa.x, data->bb.y, 0.0f); |
---|
283 | glEnd(); |
---|
284 | #endif |
---|
285 | |
---|
286 | Advance(); |
---|
287 | ResetState(); |
---|
288 | |
---|
289 | /* |
---|
290 | * Test #5: glBegin + pass vertex coord from vertex shader to fragment |
---|
291 | * shader for use as color information. |
---|
292 | * |
---|
293 | * Renders a multicoloured square with varying colors. |
---|
294 | */ |
---|
295 | #if defined HAVE_GLBEGIN || defined USE_GLEW |
---|
296 | if (!shader[0]) |
---|
297 | shader[0] = Shader::Create( |
---|
298 | "#version 110\n" |
---|
299 | "varying vec4 pass_Color;" |
---|
300 | "void main()" |
---|
301 | "{" |
---|
302 | " float r = gl_MultiTexCoord0.x;" |
---|
303 | " float g = gl_MultiTexCoord0.y;" |
---|
304 | " float b = gl_MultiTexCoord0.z;" |
---|
305 | " pass_Color = vec4(r, g, b, 1.0);" |
---|
306 | " gl_Position = gl_Vertex;" |
---|
307 | "}", |
---|
308 | |
---|
309 | "#version 110\n" |
---|
310 | "varying vec4 pass_Color;" |
---|
311 | "void main()" |
---|
312 | "{" |
---|
313 | " gl_FragColor = pass_Color;" |
---|
314 | "}"); |
---|
315 | shader[0]->Bind(); |
---|
316 | shader++; |
---|
317 | glColor3f(0.0f, 1.0f, 1.0f); |
---|
318 | glBegin(GL_TRIANGLES); |
---|
319 | glTexCoord3f(f1, f2, f3); |
---|
320 | glVertex3f(data->aa.x, data->bb.y, 0.0f); |
---|
321 | glTexCoord3f(f4, f2, f1); |
---|
322 | glVertex3f(data->bb.x, data->bb.y, 0.0f); |
---|
323 | glTexCoord3f(f3, f1, f4); |
---|
324 | glVertex3f(data->bb.x, data->aa.y, 0.0f); |
---|
325 | |
---|
326 | glVertex3f(data->bb.x, data->aa.y, 0.0f); |
---|
327 | glTexCoord3f(f4, f3, f2); |
---|
328 | glVertex3f(data->aa.x, data->aa.y, 0.0f); |
---|
329 | glTexCoord3f(f1, f2, f3); |
---|
330 | glVertex3f(data->aa.x, data->bb.y, 0.0f); |
---|
331 | glEnd(); |
---|
332 | #endif |
---|
333 | |
---|
334 | Advance(); |
---|
335 | ResetState(); |
---|
336 | |
---|
337 | /* |
---|
338 | * Test #6: glBegin + apply texture in fragment shader |
---|
339 | * |
---|
340 | * Renders an animated black-and-white distorted checkerboard with a |
---|
341 | * zoom ratio twice the one in test #3. |
---|
342 | * |
---|
343 | * Note: there is no need to glEnable(GL_TEXTURE_2D) when the |
---|
344 | * texture lookup is done in a shader. |
---|
345 | */ |
---|
346 | #if defined HAVE_GLBEGIN || defined USE_GLEW |
---|
347 | if (!shader[0]) |
---|
348 | shader[0] = Shader::Create( |
---|
349 | "#version 110\n" |
---|
350 | "void main()" |
---|
351 | "{" |
---|
352 | " gl_TexCoord[0] = gl_MultiTexCoord0;" |
---|
353 | " gl_Position = gl_Vertex;" |
---|
354 | "}", |
---|
355 | |
---|
356 | "#version 110\n" |
---|
357 | "uniform sampler2D tex;" |
---|
358 | "void main()" |
---|
359 | "{" |
---|
360 | " gl_FragColor = texture2D(tex, gl_TexCoord[0].xy * 0.25);" |
---|
361 | "}"); |
---|
362 | shader[0]->Bind(); |
---|
363 | shader++; |
---|
364 | glColor3f(0.0f, 1.0f, 1.0f); |
---|
365 | glBindTexture(GL_TEXTURE_2D, data->texture[0]); |
---|
366 | glBegin(GL_TRIANGLES); |
---|
367 | glTexCoord2f(f1, f3); |
---|
368 | glVertex3f(data->aa.x, data->bb.y, 0.0f); |
---|
369 | glTexCoord2f(f3, f2); |
---|
370 | glVertex3f(data->bb.x, data->bb.y, 0.0f); |
---|
371 | glTexCoord2f(f2, f4); |
---|
372 | glVertex3f(data->bb.x, data->aa.y, 0.0f); |
---|
373 | |
---|
374 | glTexCoord2f(f2, f4); |
---|
375 | glVertex3f(data->bb.x, data->aa.y, 0.0f); |
---|
376 | glTexCoord2f(f4, f1); |
---|
377 | glVertex3f(data->aa.x, data->aa.y, 0.0f); |
---|
378 | glTexCoord2f(f1, f3); |
---|
379 | glVertex3f(data->aa.x, data->bb.y, 0.0f); |
---|
380 | glEnd(); |
---|
381 | #endif |
---|
382 | |
---|
383 | Advance(); |
---|
384 | ResetState(); |
---|
385 | |
---|
386 | /* |
---|
387 | * Test #7: simple vertex buffer |
---|
388 | * |
---|
389 | * Renders an orange square. |
---|
390 | */ |
---|
391 | #if !defined ANDROID_NDK |
---|
392 | glColor4f(0.8f, 0.5f, 0.2f, 1.0f); |
---|
393 | glEnableClientState(GL_VERTEX_ARRAY); |
---|
394 | |
---|
395 | glVertexPointer(3, GL_FLOAT, 0, data->GetVertexArray()); |
---|
396 | glDrawArrays(GL_TRIANGLES, 0, 6); |
---|
397 | |
---|
398 | glDisableClientState(GL_VERTEX_ARRAY); |
---|
399 | #endif |
---|
400 | |
---|
401 | Advance(); |
---|
402 | ResetState(); |
---|
403 | |
---|
404 | /* |
---|
405 | * Test #8: vertex buffer + per-vertex coloring |
---|
406 | * |
---|
407 | * Renders a multicoloured square with varying colors. |
---|
408 | */ |
---|
409 | #if !defined ANDROID_NDK |
---|
410 | glEnableClientState(GL_VERTEX_ARRAY); |
---|
411 | glEnableClientState(GL_COLOR_ARRAY); |
---|
412 | |
---|
413 | glVertexPointer(3, GL_FLOAT, 0, data->GetVertexArray()); |
---|
414 | glColorPointer(3, GL_FLOAT, 0, colors); |
---|
415 | glDrawArrays(GL_TRIANGLES, 0, 6); |
---|
416 | |
---|
417 | glDisableClientState(GL_VERTEX_ARRAY); |
---|
418 | glDisableClientState(GL_COLOR_ARRAY); |
---|
419 | #endif |
---|
420 | |
---|
421 | Advance(); |
---|
422 | ResetState(); |
---|
423 | |
---|
424 | /* |
---|
425 | * Test #9: vertex buffer + per-vertex coloring + texture |
---|
426 | * |
---|
427 | * Renders a multicoloured square with varying colors multiplied with an |
---|
428 | * animated distorted checkerboard. |
---|
429 | */ |
---|
430 | #if !defined ANDROID_NDK |
---|
431 | glEnable(GL_TEXTURE_2D); |
---|
432 | glBindTexture(GL_TEXTURE_2D, data->texture[0]); |
---|
433 | glEnableClientState(GL_VERTEX_ARRAY); |
---|
434 | glEnableClientState(GL_COLOR_ARRAY); |
---|
435 | glEnableClientState(GL_TEXTURE_COORD_ARRAY); |
---|
436 | |
---|
437 | glVertexPointer(3, GL_FLOAT, 0, data->GetVertexArray()); |
---|
438 | glColorPointer(3, GL_FLOAT, 0, colors); |
---|
439 | glTexCoordPointer(2, GL_FLOAT, 0, texcoords); |
---|
440 | |
---|
441 | glDrawArrays(GL_TRIANGLES, 0, 6); |
---|
442 | |
---|
443 | glDisableClientState(GL_VERTEX_ARRAY); |
---|
444 | glDisableClientState(GL_COLOR_ARRAY); |
---|
445 | glDisableClientState(GL_TEXTURE_COORD_ARRAY); |
---|
446 | glDisable(GL_TEXTURE_2D); |
---|
447 | #endif |
---|
448 | |
---|
449 | Advance(); |
---|
450 | ResetState(); |
---|
451 | |
---|
452 | /* |
---|
453 | * Test #10: vertex buffer + hardcoded color in 1.10 fragment shader |
---|
454 | * (GLSL) or in Cg fragment shader (PS3) |
---|
455 | * |
---|
456 | * Renders an orange square. |
---|
457 | */ |
---|
458 | #if !defined ANDROID_NDK |
---|
459 | if (!shader[0]) |
---|
460 | #if !defined __CELLOS_LV2__ |
---|
461 | shader[0] = Shader::Create( |
---|
462 | "#version 110\n" |
---|
463 | "void main()" |
---|
464 | "{" |
---|
465 | " gl_Position = gl_Vertex;" |
---|
466 | "}", |
---|
467 | |
---|
468 | "#version 110\n" |
---|
469 | "void main()" |
---|
470 | "{" |
---|
471 | " gl_FragColor = vec4(0.8, 0.5, 0.2, 1.0);" |
---|
472 | "}"); |
---|
473 | #else |
---|
474 | shader[0] = Shader::Create( |
---|
475 | "void main(float4 in_Position : POSITION," |
---|
476 | " out float4 out_Position : POSITION)" |
---|
477 | "{" |
---|
478 | " out_Position = in_Position;" |
---|
479 | "}", |
---|
480 | |
---|
481 | "void main(out float4 out_FragColor : COLOR)" |
---|
482 | "{" |
---|
483 | " out_FragColor = float4(0.8, 0.5, 0.2, 1.0);" |
---|
484 | "}"); |
---|
485 | #endif |
---|
486 | shader[0]->Bind(); |
---|
487 | shader++; |
---|
488 | |
---|
489 | glEnableClientState(GL_VERTEX_ARRAY); |
---|
490 | glVertexPointer(3, GL_FLOAT, 0, data->GetVertexArray()); |
---|
491 | glDrawArrays(GL_TRIANGLES, 0, 6); |
---|
492 | glDisableClientState(GL_VERTEX_ARRAY); |
---|
493 | #endif |
---|
494 | |
---|
495 | Advance(); |
---|
496 | ResetState(); |
---|
497 | |
---|
498 | /* |
---|
499 | * Test #11: vertex buffer + uniform color in 1.10 fragment shader |
---|
500 | * (GLSL) or in Cg fragment shader (PS3) |
---|
501 | * |
---|
502 | * Renders an orange square. |
---|
503 | */ |
---|
504 | #if !defined ANDROID_NDK |
---|
505 | if (!shader[0]) |
---|
506 | { |
---|
507 | #if !defined __CELLOS_LV2__ |
---|
508 | shader[0] = Shader::Create( |
---|
509 | "#version 110\n" |
---|
510 | "void main()" |
---|
511 | "{" |
---|
512 | " gl_Position = gl_Vertex;" |
---|
513 | "}", |
---|
514 | |
---|
515 | "#version 110\n" |
---|
516 | "uniform vec4 in_Color;" |
---|
517 | "void main()" |
---|
518 | "{" |
---|
519 | " gl_FragColor = in_Color;" |
---|
520 | "}"); |
---|
521 | #else |
---|
522 | shader[0] = Shader::Create( |
---|
523 | "void main(float4 in_Position : POSITION," |
---|
524 | " out float4 out_Position : POSITION)" |
---|
525 | "{" |
---|
526 | " out_Position = in_Position;" |
---|
527 | "}", |
---|
528 | |
---|
529 | "uniform float4 in_Color;" |
---|
530 | "void main(out float4 out_FragColor : COLOR)" |
---|
531 | "{" |
---|
532 | " out_FragColor = in_Color;" |
---|
533 | "}"); |
---|
534 | #endif |
---|
535 | uni[0] = shader[0]->GetUniformLocation("in_Color"); |
---|
536 | } |
---|
537 | shader[0]->Bind(); |
---|
538 | shader++; |
---|
539 | #if !defined __CELLOS_LV2__ |
---|
540 | glUniform4f(uni[0], 0.8f, 0.5f, 0.2f, 1.0f); |
---|
541 | #else |
---|
542 | cgGLSetParameter4f((CGparameter)(intptr_t)uni[0], 0.8f, 0.5f, 0.2f, 1.0f); |
---|
543 | #endif |
---|
544 | uni++; |
---|
545 | |
---|
546 | glEnableClientState(GL_VERTEX_ARRAY); |
---|
547 | glVertexPointer(3, GL_FLOAT, 0, data->GetVertexArray()); |
---|
548 | glDrawArrays(GL_TRIANGLES, 0, 6); |
---|
549 | glDisableClientState(GL_VERTEX_ARRAY); |
---|
550 | #endif |
---|
551 | |
---|
552 | Advance(); |
---|
553 | ResetState(); |
---|
554 | |
---|
555 | /* |
---|
556 | * Test #12: vertex buffer + color in 1.10 fragment shader (GLSL) or |
---|
557 | * in Cg fragment shader (PS3) |
---|
558 | * |
---|
559 | * Renders a static, coloured and tiled pattern. |
---|
560 | */ |
---|
561 | #if !defined ANDROID_NDK |
---|
562 | if (!shader[0]) |
---|
563 | #if !defined __CELLOS_LV2__ |
---|
564 | shader[0] = Shader::Create( |
---|
565 | "#version 110\n" |
---|
566 | "void main()" |
---|
567 | "{" |
---|
568 | " gl_Position = gl_Vertex;" |
---|
569 | "}", |
---|
570 | |
---|
571 | "#version 110\n" |
---|
572 | "void main()" |
---|
573 | "{" |
---|
574 | " float dx = mod(gl_FragCoord.x * gl_FragCoord.y, 2.0);" |
---|
575 | " float dy = mod(gl_FragCoord.x * 0.125, 1.0);" |
---|
576 | " float dz = mod(gl_FragCoord.y * 0.125, 1.0);" |
---|
577 | " gl_FragColor = vec4(dx, dy, dz, 1.0);" |
---|
578 | "}"); |
---|
579 | #else |
---|
580 | shader[0] = Shader::Create( |
---|
581 | "void main(float4 in_Position : POSITION," |
---|
582 | " out float4 out_Position : POSITION)" |
---|
583 | "{" |
---|
584 | " out_Position = in_Position;" |
---|
585 | "}", |
---|
586 | |
---|
587 | "void main(float4 in_FragCoord : WPOS," |
---|
588 | " out float4 out_FragColor : COLOR)" |
---|
589 | "{" |
---|
590 | " float dx = frac(in_FragCoord.x * in_FragCoord.y * 0.5) * 2.0;" |
---|
591 | " float dy = frac(in_FragCoord.x * 0.125);" |
---|
592 | " float dz = frac(in_FragCoord.y * 0.125);" |
---|
593 | " out_FragColor = float4(dx, dy, dz, 1.0);" |
---|
594 | "}"); |
---|
595 | #endif |
---|
596 | shader[0]->Bind(); |
---|
597 | shader++; |
---|
598 | |
---|
599 | glEnableClientState(GL_VERTEX_ARRAY); |
---|
600 | glVertexPointer(3, GL_FLOAT, 0, data->GetVertexArray()); |
---|
601 | glDrawArrays(GL_TRIANGLES, 0, 6); |
---|
602 | glDisableClientState(GL_VERTEX_ARRAY); |
---|
603 | #endif |
---|
604 | |
---|
605 | Advance(); |
---|
606 | ResetState(); |
---|
607 | |
---|
608 | /* |
---|
609 | * Test #13: vertex buffer + texture & color in 1.10 fragment shader |
---|
610 | * |
---|
611 | * Renders a multicoloured square with varying colors xored with an |
---|
612 | * animated distorted checkerboard. |
---|
613 | */ |
---|
614 | #if !defined ANDROID_NDK |
---|
615 | if (!shader[0]) |
---|
616 | #if !defined __CELLOS_LV2__ |
---|
617 | shader[0] = Shader::Create( |
---|
618 | "#version 110\n" |
---|
619 | "varying vec4 pass_Color;" |
---|
620 | "void main()" |
---|
621 | "{" |
---|
622 | " gl_TexCoord[0] = gl_MultiTexCoord0;" |
---|
623 | " pass_Color = gl_Color;" |
---|
624 | " gl_Position = gl_Vertex;" |
---|
625 | "}", |
---|
626 | |
---|
627 | "#version 110\n" |
---|
628 | "varying vec4 pass_Color;" |
---|
629 | "uniform sampler2D tex;" |
---|
630 | "void main()" |
---|
631 | "{" |
---|
632 | " vec4 tmp = texture2D(tex, gl_TexCoord[0].xy * 0.25);" |
---|
633 | " gl_FragColor = vec4(abs(tmp.xyz - pass_Color.xyz), 1.0);" |
---|
634 | "}"); |
---|
635 | #else |
---|
636 | shader[0] = Shader::Create( |
---|
637 | "void main(float4 in_Position : POSITION," |
---|
638 | " float2 in_TexCoord : TEXCOORD0," |
---|
639 | " float4 in_Color : COLOR," |
---|
640 | " out float4 out_Color : COLOR," |
---|
641 | " out float4 out_Position : POSITION," |
---|
642 | " out float2 out_TexCoord : TEXCOORD0)" |
---|
643 | "{" |
---|
644 | " out_TexCoord = in_TexCoord;" |
---|
645 | " out_Color = in_Color;" |
---|
646 | " out_Position = in_Position;" |
---|
647 | "}", |
---|
648 | |
---|
649 | "void main(float2 in_TexCoord : TEXCOORD0," |
---|
650 | " float4 in_Color : COLOR," |
---|
651 | " uniform sampler2D tex," |
---|
652 | " out float4 out_FragColor : COLOR)" |
---|
653 | "{" |
---|
654 | " float4 tmp = tex2D(tex, in_TexCoord * 0.25);" |
---|
655 | " out_FragColor = float4(abs(tmp.xyz - in_Color.xyz), 1);" |
---|
656 | "}"); |
---|
657 | #endif |
---|
658 | |
---|
659 | shader[0]->Bind(); |
---|
660 | shader++; |
---|
661 | |
---|
662 | glBindTexture(GL_TEXTURE_2D, data->texture[0]); |
---|
663 | |
---|
664 | glEnableClientState(GL_VERTEX_ARRAY); |
---|
665 | glEnableClientState(GL_COLOR_ARRAY); |
---|
666 | glEnableClientState(GL_TEXTURE_COORD_ARRAY); |
---|
667 | |
---|
668 | glVertexPointer(3, GL_FLOAT, 0, data->GetVertexArray()); |
---|
669 | glColorPointer(3, GL_FLOAT, 0, colors); |
---|
670 | glTexCoordPointer(2, GL_FLOAT, 0, texcoords); |
---|
671 | glDrawArrays(GL_TRIANGLES, 0, 6); |
---|
672 | |
---|
673 | glDisableClientState(GL_VERTEX_ARRAY); |
---|
674 | glDisableClientState(GL_COLOR_ARRAY); |
---|
675 | glDisableClientState(GL_TEXTURE_COORD_ARRAY); |
---|
676 | #endif |
---|
677 | |
---|
678 | Advance(); |
---|
679 | ResetState(); |
---|
680 | |
---|
681 | /* |
---|
682 | * Test #14: vertex buffer + texture & color in 1.20 fragment shader |
---|
683 | * |
---|
684 | * Renders a multicoloured square with varying colors xored with an |
---|
685 | * animated distorted checkerboard. |
---|
686 | */ |
---|
687 | #if !defined __CELLOS_LV2__ && !defined ANDROID_NDK |
---|
688 | if (!shader[0]) |
---|
689 | { |
---|
690 | shader[0] = Shader::Create( |
---|
691 | "#version 120\n" |
---|
692 | "attribute vec3 in_Vertex;" |
---|
693 | "attribute vec3 in_Color;" |
---|
694 | "attribute vec2 in_MultiTexCoord0;" |
---|
695 | "varying vec4 pass_Color;" |
---|
696 | "void main()" |
---|
697 | "{" |
---|
698 | " gl_TexCoord[0] = vec4(in_MultiTexCoord0, 0.0, 0.0);" |
---|
699 | " pass_Color = vec4(in_Color, 1.0);" |
---|
700 | " gl_Position = vec4(in_Vertex, 1.0);" |
---|
701 | "}", |
---|
702 | |
---|
703 | "#version 120\n" |
---|
704 | "varying vec4 pass_Color;" |
---|
705 | "uniform sampler2D tex;" |
---|
706 | "void main()" |
---|
707 | "{" |
---|
708 | " vec4 tmp = texture2D(tex, gl_TexCoord[0].xy * 0.25);" |
---|
709 | " gl_FragColor = vec4(abs(tmp.xyz - pass_Color.xyz), 1.0);" |
---|
710 | "}"); |
---|
711 | attr[0] = shader[0]->GetAttribLocation("in_Vertex"); |
---|
712 | attr[1] = shader[0]->GetAttribLocation("in_Color"); |
---|
713 | attr[2] = shader[0]->GetAttribLocation("in_MultiTexCoord0"); |
---|
714 | } |
---|
715 | shader[0]->Bind(); |
---|
716 | shader++; |
---|
717 | |
---|
718 | glBindTexture(GL_TEXTURE_2D, data->texture[0]); |
---|
719 | |
---|
720 | glBindVertexArray(*array++); |
---|
721 | |
---|
722 | glBindBuffer(GL_ARRAY_BUFFER, *buffer++); |
---|
723 | glBufferData(GL_ARRAY_BUFFER, 3 * 6 * sizeof(GLfloat), |
---|
724 | data->GetVertexArray(), GL_DYNAMIC_DRAW); |
---|
725 | glVertexAttribPointer(attr[0], 3, GL_FLOAT, GL_FALSE, 0, 0); |
---|
726 | glEnableVertexAttribArray(attr[0]); |
---|
727 | |
---|
728 | glBindBuffer(GL_ARRAY_BUFFER, *buffer++); |
---|
729 | glBufferData(GL_ARRAY_BUFFER, sizeof(colors), colors, |
---|
730 | GL_DYNAMIC_DRAW); |
---|
731 | glVertexAttribPointer(attr[1], 3, GL_FLOAT, GL_FALSE, 0, 0); |
---|
732 | glEnableVertexAttribArray(attr[1]); |
---|
733 | |
---|
734 | glBindBuffer(GL_ARRAY_BUFFER, *buffer++); |
---|
735 | glBufferData(GL_ARRAY_BUFFER, sizeof(texcoords), texcoords, |
---|
736 | GL_DYNAMIC_DRAW); |
---|
737 | glVertexAttribPointer(attr[2], 2, GL_FLOAT, GL_FALSE, 0, 0); |
---|
738 | glEnableVertexAttribArray(attr[2]); |
---|
739 | |
---|
740 | glDrawArrays(GL_TRIANGLES, 0, 6); |
---|
741 | glBindVertexArray(0); |
---|
742 | |
---|
743 | glDisableVertexAttribArray(*attr++); |
---|
744 | glDisableVertexAttribArray(*attr++); |
---|
745 | glDisableVertexAttribArray(*attr++); |
---|
746 | #endif |
---|
747 | |
---|
748 | Advance(); |
---|
749 | ResetState(); |
---|
750 | |
---|
751 | /* |
---|
752 | * Test #15: vertex buffer + texture & color in 1.30 fragment shader |
---|
753 | * |
---|
754 | * Renders a multicoloured square with varying colors xored with an |
---|
755 | * animated distorted checkerboard. |
---|
756 | */ |
---|
757 | #if !defined __CELLOS_LV2__ && !defined ANDROID_NDK |
---|
758 | if (!shader[0]) |
---|
759 | { |
---|
760 | shader[0] = Shader::Create( |
---|
761 | "#version 130\n" |
---|
762 | "in vec3 in_Vertex;" |
---|
763 | "in vec3 in_Color;" |
---|
764 | "in vec2 in_MultiTexCoord0;" |
---|
765 | "out vec4 pass_Color;" |
---|
766 | "void main()" |
---|
767 | "{" |
---|
768 | " gl_TexCoord[0] = vec4(in_MultiTexCoord0, 0.0, 0.0);" |
---|
769 | " pass_Color = vec4(in_Color, 1.0);" |
---|
770 | " gl_Position = vec4(in_Vertex, 1.0);" |
---|
771 | "}", |
---|
772 | |
---|
773 | "#version 130\n" |
---|
774 | "in vec4 pass_Color;" |
---|
775 | "uniform sampler2D tex;" |
---|
776 | "void main()" |
---|
777 | "{" |
---|
778 | " vec4 tmp = texture2D(tex, gl_TexCoord[0].xy * 0.25);" |
---|
779 | " gl_FragColor = vec4(abs(tmp.xyz - pass_Color.xyz), 1.0);" |
---|
780 | "}"); |
---|
781 | attr[0] = shader[0]->GetAttribLocation("in_Vertex"); |
---|
782 | attr[1] = shader[0]->GetAttribLocation("in_Color"); |
---|
783 | attr[2] = shader[0]->GetAttribLocation("in_MultiTexCoord0"); |
---|
784 | } |
---|
785 | shader[0]->Bind(); |
---|
786 | shader++; |
---|
787 | |
---|
788 | glBindTexture(GL_TEXTURE_2D, data->texture[0]); |
---|
789 | |
---|
790 | glBindVertexArray(*array++); |
---|
791 | |
---|
792 | glBindBuffer(GL_ARRAY_BUFFER, *buffer++); |
---|
793 | glBufferData(GL_ARRAY_BUFFER, 3 * 6 * sizeof(GLfloat), |
---|
794 | data->GetVertexArray(), GL_DYNAMIC_DRAW); |
---|
795 | glVertexAttribPointer(attr[0], 3, GL_FLOAT, GL_FALSE, 0, 0); |
---|
796 | glEnableVertexAttribArray(attr[0]); |
---|
797 | |
---|
798 | glBindBuffer(GL_ARRAY_BUFFER, *buffer++); |
---|
799 | glBufferData(GL_ARRAY_BUFFER, sizeof(colors), colors, |
---|
800 | GL_DYNAMIC_DRAW); |
---|
801 | glVertexAttribPointer(attr[1], 3, GL_FLOAT, GL_FALSE, 0, 0); |
---|
802 | glEnableVertexAttribArray(attr[1]); |
---|
803 | |
---|
804 | glBindBuffer(GL_ARRAY_BUFFER, *buffer++); |
---|
805 | glBufferData(GL_ARRAY_BUFFER, sizeof(texcoords), texcoords, |
---|
806 | GL_DYNAMIC_DRAW); |
---|
807 | glVertexAttribPointer(attr[2], 2, GL_FLOAT, GL_FALSE, 0, 0); |
---|
808 | glEnableVertexAttribArray(attr[2]); |
---|
809 | |
---|
810 | glDrawArrays(GL_TRIANGLES, 0, 6); |
---|
811 | glBindVertexArray(0); |
---|
812 | |
---|
813 | glDisableVertexAttribArray(*attr++); |
---|
814 | glDisableVertexAttribArray(*attr++); |
---|
815 | glDisableVertexAttribArray(*attr++); |
---|
816 | #endif |
---|
817 | |
---|
818 | Advance(); |
---|
819 | ResetState(); |
---|
820 | |
---|
821 | /* Check that we didn't overflow our list */ |
---|
822 | #if !defined __CELLOS_LV2__ && !defined ANDROID_NDK |
---|
823 | if (array > data->array + NUM_ARRAYS) |
---|
824 | Log::Error("too many arrays used\n"); |
---|
825 | #endif |
---|
826 | if (buffer > data->buffer + NUM_BUFFERS) |
---|
827 | Log::Error("too many buffers used\n"); |
---|
828 | if (shader > data->shader + NUM_SHADERS) |
---|
829 | Log::Error("too many shaders used\n"); |
---|
830 | if (attr > data->attr + NUM_ATTRS) |
---|
831 | Log::Error("too many attributes used\n"); |
---|
832 | } |
---|
833 | |
---|
834 | void DebugQuad::ResetState() |
---|
835 | { |
---|
836 | /* Reset GL states to something reasonably safe */ |
---|
837 | |
---|
838 | #if defined HAVE_GLBEGIN || defined USE_GLEW || defined __CELLOS_LV2__ |
---|
839 | glMatrixMode(GL_PROJECTION); |
---|
840 | glLoadIdentity(); |
---|
841 | glMatrixMode(GL_MODELVIEW); |
---|
842 | glLoadIdentity(); |
---|
843 | #endif |
---|
844 | |
---|
845 | glColor4f(1.0f, 1.0f, 1.0f, 1.0f); |
---|
846 | |
---|
847 | glEnable(GL_TEXTURE_2D); |
---|
848 | glBindTexture(GL_TEXTURE_2D, 0); |
---|
849 | #if defined HAVE_GLBEGIN || defined USE_GLEW || defined __CELLOS_LV2__ |
---|
850 | glClientActiveTexture(GL_TEXTURE0); |
---|
851 | #endif |
---|
852 | glDisable(GL_TEXTURE_2D); |
---|
853 | |
---|
854 | glBindBuffer(GL_ARRAY_BUFFER, 0); |
---|
855 | #if !defined __CELLOS_LV2__ |
---|
856 | glUseProgram(0); |
---|
857 | #else |
---|
858 | cgGLDisableProfile(cgGLGetLatestProfile(CG_GL_VERTEX)); |
---|
859 | cgGLDisableProfile(cgGLGetLatestProfile(CG_GL_FRAGMENT)); |
---|
860 | #endif |
---|
861 | } |
---|
862 | |
---|
863 | void DebugQuad::Advance() |
---|
864 | { |
---|
865 | data->aa.x += 4.0f * data->step.x; |
---|
866 | data->bb.x += 4.0f * data->step.x; |
---|
867 | if (data->bb.x > 1.0f) |
---|
868 | { |
---|
869 | data->aa.x = data->orig.x; |
---|
870 | data->bb.x = data->orig.x + 3.0f * data->step.x; |
---|
871 | data->aa.y += 4.0f * data->step.y; |
---|
872 | data->bb.y += 4.0f * data->step.y; |
---|
873 | } |
---|
874 | } |
---|
875 | |
---|
876 | DebugQuad::~DebugQuad() |
---|
877 | { |
---|
878 | delete data; |
---|
879 | } |
---|
880 | |
---|
881 | } /* namespace lol */ |
---|
882 | |
---|