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

Last change on this file since 1513 was 1513, checked in by sam, 9 years ago

core: replace usage of sin() or std::sin() with lol::sin() where appropriate.

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