source: trunk/src/debug/quad.cpp @ 859

Last change on this file since 859 was 859, checked in by sam, 10 years ago

debug: more iOS compilation fixes; GL_VERTEX_ARRAY isn't available.

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