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

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

core: tick methods now use seconds, like any sane system.

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