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

Last change on this file since 830 was 830, checked in by sam, 8 years ago

debug: add a debug example with a shader and a uniform matrix parameter, to
check that uniforms work properly on the PS3.

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