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 | |
---|
18 | #include "core.h" |
---|
19 | #include "lolgl.h" |
---|
20 | #include "loldebug.h" |
---|
21 | |
---|
22 | using namespace std; |
---|
23 | |
---|
24 | namespace lol |
---|
25 | { |
---|
26 | |
---|
27 | /* |
---|
28 | * DebugQuad implementation class |
---|
29 | */ |
---|
30 | |
---|
31 | static int const NUM_ARRAYS = 2; |
---|
32 | static int const NUM_BUFFERS = 6; |
---|
33 | static int const NUM_ATTRS = 6; |
---|
34 | static int const NUM_SHADERS = 6; |
---|
35 | static int const NUM_TEXTURES = 1; |
---|
36 | |
---|
37 | static int const TEX_SIZE = 32; |
---|
38 | |
---|
39 | class DebugQuadData |
---|
40 | { |
---|
41 | friend class DebugQuad; |
---|
42 | |
---|
43 | private: |
---|
44 | vec2 orig, step, aa, bb; |
---|
45 | |
---|
46 | int initialised; |
---|
47 | float time; |
---|
48 | GLuint array[NUM_ARRAYS]; |
---|
49 | GLuint buffer[NUM_BUFFERS]; |
---|
50 | Shader *shader[NUM_SHADERS]; |
---|
51 | GLuint attr[NUM_ATTRS]; |
---|
52 | GLuint texture[NUM_TEXTURES]; |
---|
53 | uint8_t image[1][TEX_SIZE * TEX_SIZE * 4]; |
---|
54 | }; |
---|
55 | |
---|
56 | /* |
---|
57 | * Public DebugQuad class |
---|
58 | */ |
---|
59 | |
---|
60 | DebugQuad::DebugQuad() |
---|
61 | : data(new DebugQuadData()) |
---|
62 | { |
---|
63 | data->initialised = 0; |
---|
64 | data->time = RandF(10000.0f); |
---|
65 | |
---|
66 | drawgroup = DRAWGROUP_HUD; |
---|
67 | } |
---|
68 | |
---|
69 | void DebugQuad::TickGame(float deltams) |
---|
70 | { |
---|
71 | Entity::TickGame(deltams); |
---|
72 | |
---|
73 | data->time += deltams; |
---|
74 | } |
---|
75 | |
---|
76 | void DebugQuad::TickDraw(float deltams) |
---|
77 | { |
---|
78 | Entity::TickDraw(deltams); |
---|
79 | |
---|
80 | if (!data->initialised && !IsDestroying()) |
---|
81 | { |
---|
82 | glGenVertexArrays(NUM_ARRAYS, data->array); |
---|
83 | glGenBuffers(NUM_BUFFERS, data->buffer); |
---|
84 | glGenTextures(NUM_TEXTURES, data->texture); |
---|
85 | for (int i = 0; i < NUM_SHADERS; i++) |
---|
86 | data->shader[i] = NULL; |
---|
87 | |
---|
88 | /* Checkerboard texture */ |
---|
89 | glEnable(GL_TEXTURE_2D); |
---|
90 | glBindTexture(GL_TEXTURE_2D, data->texture[0]); |
---|
91 | for (int j = 0; j < TEX_SIZE; j++) |
---|
92 | for (int i = 0; i < TEX_SIZE; i++) |
---|
93 | { |
---|
94 | uint8_t wb = (((i / 2) ^ (j / 2)) & 1) * 0xff; |
---|
95 | data->image[0][(j * TEX_SIZE + i) * 4 + 0] = wb; |
---|
96 | data->image[0][(j * TEX_SIZE + i) * 4 + 1] = wb; |
---|
97 | data->image[0][(j * TEX_SIZE + i) * 4 + 2] = wb; |
---|
98 | data->image[0][(j * TEX_SIZE + i) * 4 + 3] = 0xff; |
---|
99 | } |
---|
100 | /* Use GL_RGBA instead of 4 for the internal format (Android) */ |
---|
101 | glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, TEX_SIZE, TEX_SIZE, 0, |
---|
102 | GL_RGBA, GL_UNSIGNED_BYTE, data->image[0]); |
---|
103 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); |
---|
104 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); |
---|
105 | |
---|
106 | data->initialised = 1; |
---|
107 | } |
---|
108 | else if (data->initialised && IsDestroying()) |
---|
109 | { |
---|
110 | glDeleteVertexArrays(NUM_ARRAYS, data->array); |
---|
111 | glDeleteBuffers(NUM_BUFFERS, data->buffer); |
---|
112 | glDeleteTextures(NUM_TEXTURES, data->texture); |
---|
113 | |
---|
114 | for (int i = 0; i < NUM_SHADERS; i++) |
---|
115 | if (data->shader[i]) |
---|
116 | Shader::Destroy(data->shader[i]); |
---|
117 | |
---|
118 | data->initialised = 0; |
---|
119 | } |
---|
120 | |
---|
121 | /* Prepare our quad coordinates */ |
---|
122 | vec2i const layout(4, 3); |
---|
123 | data->step = vec2(2.0f, -2.0f) / (3 * layout + vec2i(1)); |
---|
124 | data->orig = vec2(-1.0f, 1.0f) + data->step; |
---|
125 | data->aa = data->orig; |
---|
126 | data->bb = data->orig + 2.0f * data->step; |
---|
127 | |
---|
128 | /* Generate a few random numbers */ |
---|
129 | float f1 = 0.5f + 0.5f * sinf(0.00034f * data->time); |
---|
130 | float f2 = 0.5f + 0.5f * sinf(0.00053f * data->time + 1.0f); |
---|
131 | float f3 = 0.5f + 0.5f * sinf(0.00072f * data->time + 4.0f); |
---|
132 | float f4 = 0.5f + 0.5f * sinf(0.00091f * data->time + 8.0f); |
---|
133 | |
---|
134 | GLfloat const colors[] = { f1, f2, f3, f4, f2, f1, f3, f1, f4, |
---|
135 | f3, f1, f4, f4, f3, f2, f1, f2, f3 }; |
---|
136 | GLfloat const texcoords[] = { f1, f3, f3, f2, f2, f4, |
---|
137 | f2, f4, f4, f1, f1, f3 }; |
---|
138 | |
---|
139 | ResetState(); |
---|
140 | |
---|
141 | #if defined HAVE_GLBEGIN || defined USE_GLEW |
---|
142 | /* |
---|
143 | * Test #1: simple glBegin code |
---|
144 | * |
---|
145 | * Renders an orange square. |
---|
146 | */ |
---|
147 | glColor3f(0.8f, 0.5f, 0.2f); |
---|
148 | glBegin(GL_TRIANGLES); |
---|
149 | glVertex3f(data->aa.x, data->bb.y, 0.0f); |
---|
150 | glVertex3f(data->bb.x, data->bb.y, 0.0f); |
---|
151 | glVertex3f(data->bb.x, data->aa.y, 0.0f); |
---|
152 | |
---|
153 | glVertex3f(data->bb.x, data->aa.y, 0.0f); |
---|
154 | glVertex3f(data->aa.x, data->aa.y, 0.0f); |
---|
155 | glVertex3f(data->aa.x, data->bb.y, 0.0f); |
---|
156 | glEnd(); |
---|
157 | |
---|
158 | Advance(); |
---|
159 | ResetState(); |
---|
160 | |
---|
161 | /* |
---|
162 | * Test #2: glBegin + per-vertex coloring |
---|
163 | * |
---|
164 | * Renders a multicolored square with varying colors. |
---|
165 | */ |
---|
166 | |
---|
167 | glBegin(GL_TRIANGLES); |
---|
168 | glColor3f(f1, f2, f3); |
---|
169 | glVertex3f(data->aa.x, data->bb.y, 0.0f); |
---|
170 | glColor3f(f4, f2, f1); |
---|
171 | glVertex3f(data->bb.x, data->bb.y, 0.0f); |
---|
172 | glColor3f(f3, f1, f4); |
---|
173 | glVertex3f(data->bb.x, data->aa.y, 0.0f); |
---|
174 | |
---|
175 | glVertex3f(data->bb.x, data->aa.y, 0.0f); |
---|
176 | glColor3f(f4, f3, f2); |
---|
177 | glVertex3f(data->aa.x, data->aa.y, 0.0f); |
---|
178 | glColor3f(f1, f2, f3); |
---|
179 | glVertex3f(data->aa.x, data->bb.y, 0.0f); |
---|
180 | glEnd(); |
---|
181 | |
---|
182 | Advance(); |
---|
183 | ResetState(); |
---|
184 | |
---|
185 | /* |
---|
186 | * Test #3: glBegin + texture |
---|
187 | * |
---|
188 | * Renders an animated black-and-white distorted checkerboard. |
---|
189 | */ |
---|
190 | glEnable(GL_TEXTURE_2D); |
---|
191 | glBindTexture(GL_TEXTURE_2D, data->texture[0]); |
---|
192 | glColor3f(1.0f, 1.0f, 1.0f); |
---|
193 | glBegin(GL_TRIANGLES); |
---|
194 | glTexCoord2f(f1, f3); |
---|
195 | glVertex3f(data->aa.x, data->bb.y, 0.0f); |
---|
196 | glTexCoord2f(f3, f2); |
---|
197 | glVertex3f(data->bb.x, data->bb.y, 0.0f); |
---|
198 | glTexCoord2f(f2, f4); |
---|
199 | glVertex3f(data->bb.x, data->aa.y, 0.0f); |
---|
200 | |
---|
201 | glTexCoord2f(f2, f4); |
---|
202 | glVertex3f(data->bb.x, data->aa.y, 0.0f); |
---|
203 | glTexCoord2f(f4, f1); |
---|
204 | glVertex3f(data->aa.x, data->aa.y, 0.0f); |
---|
205 | glTexCoord2f(f1, f3); |
---|
206 | glVertex3f(data->aa.x, data->bb.y, 0.0f); |
---|
207 | glEnd(); |
---|
208 | glDisable(GL_TEXTURE_2D); |
---|
209 | |
---|
210 | Advance(); |
---|
211 | ResetState(); |
---|
212 | |
---|
213 | /* |
---|
214 | * Test #4: glBegin + color in fragment shader |
---|
215 | * |
---|
216 | * Renders a static, coloured and tiled pattern. |
---|
217 | */ |
---|
218 | if (!data->shader[0]) |
---|
219 | data->shader[0] = Shader::Create( |
---|
220 | "#version 110\n" |
---|
221 | "void main()" |
---|
222 | "{" |
---|
223 | " gl_Position = gl_Vertex;" |
---|
224 | "}", |
---|
225 | |
---|
226 | "#version 110\n" |
---|
227 | "void main()" |
---|
228 | "{" |
---|
229 | " float dx = mod(gl_FragCoord.x * gl_FragCoord.y, 2.0);" |
---|
230 | " float dy = mod(gl_FragCoord.x * 0.125, 1.0);" |
---|
231 | " float dz = mod(gl_FragCoord.y * 0.125, 1.0);" |
---|
232 | " gl_FragColor = vec4(dx, dy, dz, 1.0);" |
---|
233 | "}"); |
---|
234 | data->shader[0]->Bind(); |
---|
235 | glColor3f(0.0f, 1.0f, 1.0f); |
---|
236 | glBegin(GL_TRIANGLES); |
---|
237 | glVertex3f(data->aa.x, data->bb.y, 0.0f); |
---|
238 | glVertex3f(data->bb.x, data->bb.y, 0.0f); |
---|
239 | glVertex3f(data->bb.x, data->aa.y, 0.0f); |
---|
240 | |
---|
241 | glVertex3f(data->bb.x, data->aa.y, 0.0f); |
---|
242 | glVertex3f(data->aa.x, data->aa.y, 0.0f); |
---|
243 | glVertex3f(data->aa.x, data->bb.y, 0.0f); |
---|
244 | glEnd(); |
---|
245 | glUseProgram(0); |
---|
246 | |
---|
247 | Advance(); |
---|
248 | ResetState(); |
---|
249 | |
---|
250 | /* |
---|
251 | * Test #5: glBegin + pass vertex coord from vertex shader to fragment |
---|
252 | * shader for use as color information. |
---|
253 | * |
---|
254 | * Renders a multicolored square with varying colors. |
---|
255 | */ |
---|
256 | if (!data->shader[1]) |
---|
257 | data->shader[1] = Shader::Create( |
---|
258 | "#version 110\n" |
---|
259 | "varying vec4 pass_Color;" |
---|
260 | "void main()" |
---|
261 | "{" |
---|
262 | " float r = gl_MultiTexCoord0.x;" |
---|
263 | " float g = gl_MultiTexCoord0.y;" |
---|
264 | " float b = gl_MultiTexCoord0.z;" |
---|
265 | " pass_Color = vec4(r, g, b, 1.0);" |
---|
266 | " gl_Position = gl_Vertex;" |
---|
267 | "}", |
---|
268 | |
---|
269 | "#version 110\n" |
---|
270 | "varying vec4 pass_Color;" |
---|
271 | "void main()" |
---|
272 | "{" |
---|
273 | " gl_FragColor = pass_Color;" |
---|
274 | "}"); |
---|
275 | data->shader[1]->Bind(); |
---|
276 | glColor3f(0.0f, 1.0f, 1.0f); |
---|
277 | glBegin(GL_TRIANGLES); |
---|
278 | glTexCoord3f(f1, f2, f3); |
---|
279 | glVertex3f(data->aa.x, data->bb.y, 0.0f); |
---|
280 | glTexCoord3f(f4, f2, f1); |
---|
281 | glVertex3f(data->bb.x, data->bb.y, 0.0f); |
---|
282 | glTexCoord3f(f3, f1, f4); |
---|
283 | glVertex3f(data->bb.x, data->aa.y, 0.0f); |
---|
284 | |
---|
285 | glVertex3f(data->bb.x, data->aa.y, 0.0f); |
---|
286 | glTexCoord3f(f4, f3, f2); |
---|
287 | glVertex3f(data->aa.x, data->aa.y, 0.0f); |
---|
288 | glTexCoord3f(f1, f2, f3); |
---|
289 | glVertex3f(data->aa.x, data->bb.y, 0.0f); |
---|
290 | glEnd(); |
---|
291 | glEnd(); |
---|
292 | glUseProgram(0); |
---|
293 | |
---|
294 | Advance(); |
---|
295 | ResetState(); |
---|
296 | |
---|
297 | /* |
---|
298 | * Test #6: glBegin + apply texture in fragment shader |
---|
299 | * |
---|
300 | * Renders an animated black-and-white distorted checkerboard with a |
---|
301 | * zoom ratio twice the one in test #3. |
---|
302 | * |
---|
303 | * Note: there is no need to glEnable(GL_TEXTURE_2D) when the |
---|
304 | * texture lookup is done in a shader. |
---|
305 | */ |
---|
306 | if (!data->shader[2]) |
---|
307 | data->shader[2] = Shader::Create( |
---|
308 | "#version 110\n" |
---|
309 | "void main()" |
---|
310 | "{" |
---|
311 | " gl_TexCoord[0] = gl_MultiTexCoord0;" |
---|
312 | " gl_Position = gl_Vertex;" |
---|
313 | "}", |
---|
314 | |
---|
315 | "#version 110\n" |
---|
316 | "uniform sampler2D tex;" |
---|
317 | "void main()" |
---|
318 | "{" |
---|
319 | " gl_FragColor = texture2D(tex, gl_TexCoord[0].xy * 0.25);" |
---|
320 | "}"); |
---|
321 | data->shader[2]->Bind(); |
---|
322 | glColor3f(0.0f, 1.0f, 1.0f); |
---|
323 | glBindTexture(GL_TEXTURE_2D, data->texture[0]); |
---|
324 | glBegin(GL_TRIANGLES); |
---|
325 | glTexCoord2f(f1, f3); |
---|
326 | glVertex3f(data->aa.x, data->bb.y, 0.0f); |
---|
327 | glTexCoord2f(f3, f2); |
---|
328 | glVertex3f(data->bb.x, data->bb.y, 0.0f); |
---|
329 | glTexCoord2f(f2, f4); |
---|
330 | glVertex3f(data->bb.x, data->aa.y, 0.0f); |
---|
331 | |
---|
332 | glTexCoord2f(f2, f4); |
---|
333 | glVertex3f(data->bb.x, data->aa.y, 0.0f); |
---|
334 | glTexCoord2f(f4, f1); |
---|
335 | glVertex3f(data->aa.x, data->aa.y, 0.0f); |
---|
336 | glTexCoord2f(f1, f3); |
---|
337 | glVertex3f(data->aa.x, data->bb.y, 0.0f); |
---|
338 | glEnd(); |
---|
339 | glUseProgram(0); |
---|
340 | |
---|
341 | Advance(); |
---|
342 | ResetState(); |
---|
343 | #endif |
---|
344 | |
---|
345 | /* |
---|
346 | * Test #7: vertex buffer + per-vertex coloring |
---|
347 | * |
---|
348 | * Renders a multicolored square with varying colors. |
---|
349 | */ |
---|
350 | GLfloat const vertices1[] = { data->aa.x, data->bb.y, 0.0f, |
---|
351 | data->bb.x, data->bb.y, 0.0f, |
---|
352 | data->bb.x, data->aa.y, 0.0f, |
---|
353 | data->bb.x, data->aa.y, 0.0f, |
---|
354 | data->aa.x, data->aa.y, 0.0f, |
---|
355 | data->aa.x, data->bb.y, 0.0f }; |
---|
356 | |
---|
357 | glEnableClientState(GL_COLOR_ARRAY); |
---|
358 | glEnableClientState(GL_VERTEX_ARRAY); |
---|
359 | |
---|
360 | glColorPointer(3, GL_FLOAT, 0, colors); |
---|
361 | glVertexPointer(3, GL_FLOAT, 0, vertices1); |
---|
362 | glDrawArrays(GL_TRIANGLES, 0, 6); |
---|
363 | |
---|
364 | glDisableClientState(GL_VERTEX_ARRAY); |
---|
365 | glDisableClientState(GL_COLOR_ARRAY); |
---|
366 | |
---|
367 | Advance(); |
---|
368 | ResetState(); |
---|
369 | |
---|
370 | /* |
---|
371 | * Test #8: vertex buffer + per-vertex coloring + texture |
---|
372 | * |
---|
373 | * Renders a multicolored square with varying colors multiplied with an |
---|
374 | * animated distorted checkerboard. |
---|
375 | */ |
---|
376 | GLfloat const vertices2[] = { data->aa.x, data->bb.y, 0.0f, |
---|
377 | data->bb.x, data->bb.y, 0.0f, |
---|
378 | data->bb.x, data->aa.y, 0.0f, |
---|
379 | data->bb.x, data->aa.y, 0.0f, |
---|
380 | data->aa.x, data->aa.y, 0.0f, |
---|
381 | data->aa.x, data->bb.y, 0.0f }; |
---|
382 | |
---|
383 | glEnable(GL_TEXTURE_2D); |
---|
384 | glBindTexture(GL_TEXTURE_2D, data->texture[0]); |
---|
385 | glEnableClientState(GL_COLOR_ARRAY); |
---|
386 | glEnableClientState(GL_TEXTURE_COORD_ARRAY); |
---|
387 | glEnableClientState(GL_VERTEX_ARRAY); |
---|
388 | |
---|
389 | glColorPointer(3, GL_FLOAT, 0, colors); |
---|
390 | glTexCoordPointer(2, GL_FLOAT, 0, texcoords); |
---|
391 | glVertexPointer(3, GL_FLOAT, 0, vertices2); |
---|
392 | glDrawArrays(GL_TRIANGLES, 0, 6); |
---|
393 | |
---|
394 | glDisableClientState(GL_VERTEX_ARRAY); |
---|
395 | glDisableClientState(GL_TEXTURE_COORD_ARRAY); |
---|
396 | glDisableClientState(GL_COLOR_ARRAY); |
---|
397 | glDisable(GL_TEXTURE_2D); |
---|
398 | |
---|
399 | Advance(); |
---|
400 | ResetState(); |
---|
401 | |
---|
402 | /* |
---|
403 | * Test #9: vertex buffer + texture & color in 1.10 fragment shader |
---|
404 | * |
---|
405 | * Renders a multicolored square with varying colors xored with an |
---|
406 | * animated distorted checkerboard. |
---|
407 | */ |
---|
408 | if (!data->shader[3]) |
---|
409 | data->shader[3] = Shader::Create( |
---|
410 | "#version 110\n" |
---|
411 | "varying vec4 pass_Color;" |
---|
412 | "void main()" |
---|
413 | "{" |
---|
414 | " gl_TexCoord[0] = gl_MultiTexCoord0;" |
---|
415 | " pass_Color = gl_Color;" |
---|
416 | " gl_Position = gl_Vertex;" |
---|
417 | "}", |
---|
418 | |
---|
419 | "#version 110\n" |
---|
420 | "varying vec4 pass_Color;" |
---|
421 | "uniform sampler2D tex;" |
---|
422 | "void main()" |
---|
423 | "{" |
---|
424 | " vec4 tmp = texture2D(tex, gl_TexCoord[0].xy * 0.25);" |
---|
425 | " gl_FragColor = vec4(abs(tmp.xyz - pass_Color.xyz), 1.0);" |
---|
426 | "}"); |
---|
427 | |
---|
428 | data->shader[3]->Bind(); |
---|
429 | GLfloat const vertices3[] = { data->aa.x, data->bb.y, 0.0f, |
---|
430 | data->bb.x, data->bb.y, 0.0f, |
---|
431 | data->bb.x, data->aa.y, 0.0f, |
---|
432 | data->bb.x, data->aa.y, 0.0f, |
---|
433 | data->aa.x, data->aa.y, 0.0f, |
---|
434 | data->aa.x, data->bb.y, 0.0f }; |
---|
435 | |
---|
436 | glBindTexture(GL_TEXTURE_2D, data->texture[0]); |
---|
437 | |
---|
438 | glEnableClientState(GL_VERTEX_ARRAY); |
---|
439 | glEnableClientState(GL_COLOR_ARRAY); |
---|
440 | glEnableClientState(GL_TEXTURE_COORD_ARRAY); |
---|
441 | |
---|
442 | glVertexPointer(3, GL_FLOAT, 0, vertices3); |
---|
443 | glColorPointer(3, GL_FLOAT, 0, colors); |
---|
444 | glTexCoordPointer(2, GL_FLOAT, 0, texcoords); |
---|
445 | glDrawArrays(GL_TRIANGLES, 0, 6); |
---|
446 | |
---|
447 | glDisableClientState(GL_VERTEX_ARRAY); |
---|
448 | glDisableClientState(GL_COLOR_ARRAY); |
---|
449 | glDisableClientState(GL_TEXTURE_COORD_ARRAY); |
---|
450 | glUseProgram(0); |
---|
451 | |
---|
452 | Advance(); |
---|
453 | ResetState(); |
---|
454 | |
---|
455 | /* |
---|
456 | * Test #10: vertex buffer + texture & color in 1.20 fragment shader |
---|
457 | * |
---|
458 | * Renders a multicolored square with varying colors xored with an |
---|
459 | * animated distorted checkerboard. |
---|
460 | */ |
---|
461 | if (!data->shader[4]) |
---|
462 | { |
---|
463 | data->shader[4] = Shader::Create( |
---|
464 | "#version 120\n" |
---|
465 | "attribute vec3 in_Vertex;" |
---|
466 | "attribute vec3 in_Color;" |
---|
467 | "attribute vec2 in_MultiTexCoord0;" |
---|
468 | "varying vec4 pass_Color;" |
---|
469 | "void main()" |
---|
470 | "{" |
---|
471 | " gl_TexCoord[0] = vec4(in_MultiTexCoord0, 0.0, 0.0);" |
---|
472 | " pass_Color = vec4(in_Color, 1.0);" |
---|
473 | " gl_Position = vec4(in_Vertex, 1.0);" |
---|
474 | "}", |
---|
475 | |
---|
476 | "#version 120\n" |
---|
477 | "varying vec4 pass_Color;" |
---|
478 | "uniform sampler2D tex;" |
---|
479 | "void main()" |
---|
480 | "{" |
---|
481 | " vec4 tmp = texture2D(tex, gl_TexCoord[0].xy * 0.25);" |
---|
482 | " gl_FragColor = vec4(abs(tmp.xyz - pass_Color.xyz), 1.0);" |
---|
483 | "}"); |
---|
484 | data->attr[0] = data->shader[4]->GetAttribLocation("in_Vertex"); |
---|
485 | data->attr[1] = data->shader[4]->GetAttribLocation("in_Color"); |
---|
486 | data->attr[2] = data->shader[4]->GetAttribLocation("in_MultiTexCoord0"); |
---|
487 | } |
---|
488 | data->shader[4]->Bind(); |
---|
489 | GLfloat const vertices4[] = { data->aa.x, data->bb.y, 0.0f, |
---|
490 | data->bb.x, data->bb.y, 0.0f, |
---|
491 | data->bb.x, data->aa.y, 0.0f, |
---|
492 | data->bb.x, data->aa.y, 0.0f, |
---|
493 | data->aa.x, data->aa.y, 0.0f, |
---|
494 | data->aa.x, data->bb.y, 0.0f }; |
---|
495 | |
---|
496 | glBindTexture(GL_TEXTURE_2D, data->texture[0]); |
---|
497 | |
---|
498 | glBindVertexArray(data->array[0]); |
---|
499 | |
---|
500 | glBindBuffer(GL_ARRAY_BUFFER, data->buffer[0]); |
---|
501 | glBufferData(GL_ARRAY_BUFFER, sizeof(vertices4), vertices4, |
---|
502 | GL_DYNAMIC_DRAW); |
---|
503 | glVertexAttribPointer(data->attr[0], 3, GL_FLOAT, GL_FALSE, 0, 0); |
---|
504 | glEnableVertexAttribArray(data->attr[0]); |
---|
505 | |
---|
506 | glBindBuffer(GL_ARRAY_BUFFER, data->buffer[1]); |
---|
507 | glBufferData(GL_ARRAY_BUFFER, sizeof(colors), colors, |
---|
508 | GL_DYNAMIC_DRAW); |
---|
509 | glVertexAttribPointer(data->attr[1], 3, GL_FLOAT, GL_FALSE, 0, 0); |
---|
510 | glEnableVertexAttribArray(data->attr[1]); |
---|
511 | |
---|
512 | glBindBuffer(GL_ARRAY_BUFFER, data->buffer[2]); |
---|
513 | glBufferData(GL_ARRAY_BUFFER, sizeof(texcoords), texcoords, |
---|
514 | GL_DYNAMIC_DRAW); |
---|
515 | glVertexAttribPointer(data->attr[2], 2, GL_FLOAT, GL_FALSE, 0, 0); |
---|
516 | glEnableVertexAttribArray(data->attr[2]); |
---|
517 | |
---|
518 | glDrawArrays(GL_TRIANGLES, 0, 6); |
---|
519 | glBindVertexArray(0); |
---|
520 | |
---|
521 | glDisableVertexAttribArray(data->attr[0]); |
---|
522 | glDisableVertexAttribArray(data->attr[1]); |
---|
523 | glDisableVertexAttribArray(data->attr[2]); |
---|
524 | glUseProgram(0); |
---|
525 | |
---|
526 | Advance(); |
---|
527 | ResetState(); |
---|
528 | |
---|
529 | /* |
---|
530 | * Test #11: vertex buffer + texture & color in 1.30 fragment shader |
---|
531 | * |
---|
532 | * Renders a multicolored square with varying colors xored with an |
---|
533 | * animated distorted checkerboard. |
---|
534 | */ |
---|
535 | if (!data->shader[5]) |
---|
536 | { |
---|
537 | data->shader[5] = Shader::Create( |
---|
538 | "#version 130\n" |
---|
539 | "in vec3 in_Vertex;" |
---|
540 | "in vec3 in_Color;" |
---|
541 | "in vec2 in_MultiTexCoord0;" |
---|
542 | "out vec4 pass_Color;" |
---|
543 | "void main()" |
---|
544 | "{" |
---|
545 | " gl_TexCoord[0] = vec4(in_MultiTexCoord0, 0.0, 0.0);" |
---|
546 | " pass_Color = vec4(in_Color, 1.0);" |
---|
547 | " gl_Position = vec4(in_Vertex, 1.0);" |
---|
548 | "}", |
---|
549 | |
---|
550 | "#version 130\n" |
---|
551 | "in vec4 pass_Color;" |
---|
552 | "uniform sampler2D tex;" |
---|
553 | "void main()" |
---|
554 | "{" |
---|
555 | " vec4 tmp = texture2D(tex, gl_TexCoord[0].xy * 0.25);" |
---|
556 | " gl_FragColor = vec4(abs(tmp.xyz - pass_Color.xyz), 1.0);" |
---|
557 | "}"); |
---|
558 | data->attr[3] = data->shader[4]->GetAttribLocation("in_Vertex"); |
---|
559 | data->attr[4] = data->shader[4]->GetAttribLocation("in_Color"); |
---|
560 | data->attr[5] = data->shader[4]->GetAttribLocation("in_MultiTexCoord0"); |
---|
561 | } |
---|
562 | data->shader[5]->Bind(); |
---|
563 | GLfloat const vertices5[] = { data->aa.x, data->bb.y, 0.0f, |
---|
564 | data->bb.x, data->bb.y, 0.0f, |
---|
565 | data->bb.x, data->aa.y, 0.0f, |
---|
566 | data->bb.x, data->aa.y, 0.0f, |
---|
567 | data->aa.x, data->aa.y, 0.0f, |
---|
568 | data->aa.x, data->bb.y, 0.0f }; |
---|
569 | |
---|
570 | glBindTexture(GL_TEXTURE_2D, data->texture[0]); |
---|
571 | |
---|
572 | glBindVertexArray(data->array[1]); |
---|
573 | |
---|
574 | glBindBuffer(GL_ARRAY_BUFFER, data->buffer[3]); |
---|
575 | glBufferData(GL_ARRAY_BUFFER, sizeof(vertices5), vertices5, |
---|
576 | GL_DYNAMIC_DRAW); |
---|
577 | glVertexAttribPointer(data->attr[3], 3, GL_FLOAT, GL_FALSE, 0, 0); |
---|
578 | glEnableVertexAttribArray(data->attr[3]); |
---|
579 | |
---|
580 | glBindBuffer(GL_ARRAY_BUFFER, data->buffer[4]); |
---|
581 | glBufferData(GL_ARRAY_BUFFER, sizeof(colors), colors, |
---|
582 | GL_DYNAMIC_DRAW); |
---|
583 | glVertexAttribPointer(data->attr[4], 3, GL_FLOAT, GL_FALSE, 0, 0); |
---|
584 | glEnableVertexAttribArray(data->attr[4]); |
---|
585 | |
---|
586 | glBindBuffer(GL_ARRAY_BUFFER, data->buffer[5]); |
---|
587 | glBufferData(GL_ARRAY_BUFFER, sizeof(texcoords), texcoords, |
---|
588 | GL_DYNAMIC_DRAW); |
---|
589 | glVertexAttribPointer(data->attr[5], 2, GL_FLOAT, GL_FALSE, 0, 0); |
---|
590 | glEnableVertexAttribArray(data->attr[5]); |
---|
591 | |
---|
592 | glDrawArrays(GL_TRIANGLES, 0, 6); |
---|
593 | glBindVertexArray(0); |
---|
594 | |
---|
595 | glDisableVertexAttribArray(data->attr[3]); |
---|
596 | glDisableVertexAttribArray(data->attr[4]); |
---|
597 | glDisableVertexAttribArray(data->attr[5]); |
---|
598 | glUseProgram(0); |
---|
599 | |
---|
600 | Advance(); |
---|
601 | ResetState(); |
---|
602 | } |
---|
603 | |
---|
604 | void DebugQuad::ResetState() |
---|
605 | { |
---|
606 | /* Reset GL states to something reasonably safe */ |
---|
607 | glMatrixMode(GL_PROJECTION); |
---|
608 | glLoadIdentity(); |
---|
609 | glMatrixMode(GL_MODELVIEW); |
---|
610 | glLoadIdentity(); |
---|
611 | |
---|
612 | glEnable(GL_TEXTURE_2D); |
---|
613 | glBindTexture(GL_TEXTURE_2D, 0); |
---|
614 | glClientActiveTexture(GL_TEXTURE0); |
---|
615 | glDisable(GL_TEXTURE_2D); |
---|
616 | |
---|
617 | glBindBuffer(GL_ARRAY_BUFFER, 0); |
---|
618 | glUseProgram(0); |
---|
619 | } |
---|
620 | |
---|
621 | void DebugQuad::Advance() |
---|
622 | { |
---|
623 | data->aa.x += 3.0f * data->step.x; |
---|
624 | data->bb.x += 3.0f * data->step.x; |
---|
625 | if (data->bb.x > 1.0f) |
---|
626 | { |
---|
627 | data->aa.x = data->orig.x; |
---|
628 | data->bb.x = data->orig.x + 2.0f * data->step.x; |
---|
629 | data->aa.y += 3.0f * data->step.y; |
---|
630 | data->bb.y += 3.0f * data->step.y; |
---|
631 | } |
---|
632 | } |
---|
633 | |
---|
634 | DebugQuad::~DebugQuad() |
---|
635 | { |
---|
636 | delete data; |
---|
637 | } |
---|
638 | |
---|
639 | } /* namespace lol */ |
---|
640 | |
---|