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

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

math: move most vector and matrix member functions to global functions.

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