source: trunk/src/gradient.cpp @ 960

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

osx: link with the proper OpenGL libraries on OS X.

  • Property svn:keywords set to Id
File size: 7.3 KB
Line 
1//
2// Lol Engine
3//
4// Copyright: (c) 2010-2011 Sam Hocevar <sam@hocevar.net>
5//   This program is free software; you can redistribute it and/or
6//   modify it under the terms of the Do What The Fuck You Want To
7//   Public License, Version 2, as published by Sam Hocevar. See
8//   http://sam.zoy.org/projects/COPYING.WTFPL for more details.
9//
10
11#if defined HAVE_CONFIG_H
12#   include "config.h"
13#endif
14
15#include <cmath>
16
17#include "core.h"
18#include "lolgl.h"
19
20using namespace std;
21
22namespace lol
23{
24
25/*
26 * Gradient implementation class
27 */
28
29class GradientData
30{
31    friend class Gradient;
32
33private:
34    Shader *shader;
35    GLuint bufs[2];
36#if defined HAVE_GL_2X && !defined __APPLE__
37    GLuint vaos[1];
38#endif
39};
40
41/*
42 * Public Gradient class
43 */
44
45Gradient::Gradient(vec3 aa, vec3 bb)
46  : data(new GradientData())
47{
48    /* FIXME: this should not be hardcoded */
49    position = aa;
50    bbox[0] = aa;
51    bbox[1] = bb;
52
53    data->shader = NULL;
54}
55
56void Gradient::TickGame(float deltams)
57{
58    Entity::TickGame(deltams);
59}
60
61void Gradient::TickDraw(float deltams)
62{
63    Entity::TickDraw(deltams);
64
65    if (!data->shader)
66    {
67#if !defined __CELLOS_LV2__
68        data->shader = Shader::Create(
69            "#version 130\n"
70            "\n"
71            "in vec3 in_Vertex;\n"
72            "in vec4 in_Color;\n"
73            "out vec4 pass_Color;\n"
74            "\n"
75            "uniform mat4 proj_matrix;\n"
76            "uniform mat4 view_matrix;\n"
77            "uniform mat4 model_matrix;\n"
78            "\n"
79            "void main()\n"
80            "{\n"
81            "    gl_Position = proj_matrix * view_matrix * model_matrix"
82            "                * vec4(in_Vertex, 1.0);\n"
83            "    pass_Color = in_Color;\n"
84            "}\n",
85
86            "#version 130\n"
87            "\n"
88            "in vec4 pass_Color;\n"
89            "\n"
90            "mat4 bayer = mat4( 0.0, 12.0,  3.0, 15.0,"
91            "                   8.0,  4.0, 11.0,  7.0,"
92            "                   2.0, 14.0,  1.0, 13.0,"
93            "                  10.0,  6.0,  9.0,  5.0);\n"
94            ""
95            "mat4 cluster = mat4(12.0,  5.0,  6.0, 13.0,"
96            "                     4.0,  0.0,  1.0,  7.0,"
97            "                    11.0,  3.0,  2.0,  8.0,"
98            "                    15.0, 10.0,  9.0, 14.0);\n"
99            "\n"
100            "void main()\n"
101            "{\n"
102            "    vec4 col = pass_Color;\n"
103            "    float t = cluster[int(mod(gl_FragCoord.x, 4.0))]"
104            "                     [int(mod(gl_FragCoord.y, 4.0))];\n"
105            "    t = (t + 0.5) / 17.0;\n"
106            "    col.x += fract(t - col.x) - t;\n"
107            "    col.y += fract(t - col.y) - t;\n"
108            "    col.z += fract(t - col.z) - t;\n"
109            "    gl_FragColor = col;\n"
110            "}\n");
111#else
112        data->shader = Shader::Create(
113            "void main(float4 in_Vertex : POSITION,"
114            "          float4 in_Color : COLOR,"
115            "          uniform float4x4 proj_matrix,"
116            "          uniform float4x4 view_matrix,"
117            "          uniform float4x4 model_matrix,"
118            "          out float4 out_Color : COLOR,"
119            "          out float4 out_Position : POSITION)"
120            "{"
121            "    out_Position = mul(proj_matrix, mul(view_matrix, mul(model_matrix, in_Vertex)));"
122            "    out_Color = in_Color;"
123            "}",
124
125            "float4x4 bayer = float4x4( 0.0, 12.0,  3.0, 15.0,"
126            "                           8.0,  4.0, 11.0,  7.0,"
127            "                           2.0, 14.0,  1.0, 13.0,"
128            "                          10.0,  6.0,  9.0,  5.0);\n"
129            ""
130#if 1
131            "float4x4 cluster = float4x4(12.0,  5.0,  6.0, 13.0,"
132            "                             4.0,  0.0,  1.0,  7.0,"
133            "                            11.0,  3.0,  2.0,  8.0,"
134            "                            15.0, 10.0,  9.0, 14.0);\n"
135#endif
136            "\n"
137            "void main(float4 in_Color : COLOR,"
138            "          float4 in_FragCoord : WPOS,"
139            "          out float4 out_FragColor : COLOR)"
140            "{"
141            "    float4 col = in_Color;"
142#if 1
143            "    int x = (int)in_FragCoord.x;"
144            "    int y = (int)in_FragCoord.y;"
145            // FIXME: we cannot address this matrix directly on the PS3
146            "    float t = bayer[0][0];\n"
147            "    t = (t + 0.5) / 17.0 + z - x;\n"
148            "    col.x += frac(t - col.x) - t;\n"
149            "    col.y += frac(t - col.y) - t;\n"
150            "    col.z += frac(t - col.z) - t;\n"
151#endif
152            "    out_FragColor = col;"
153            "}");
154#endif
155#if !defined __CELLOS_LV2__
156        glGenBuffers(2, data->bufs);
157#   if defined HAVE_GL_2X && !defined __APPLE__
158        glGenVertexArrays(1, data->vaos);
159#   endif
160#endif
161    }
162
163    mat4 model_matrix = mat4(1.0f);
164
165    GLuint uni_mat, attr_pos, attr_col;
166#if !defined __CELLOS_LV2__
167    attr_pos = data->shader->GetAttribLocation("in_Vertex");
168    attr_col = data->shader->GetAttribLocation("in_Color");
169#endif
170
171    data->shader->Bind();
172
173    uni_mat = data->shader->GetUniformLocation("proj_matrix");
174    data->shader->SetUniform(uni_mat, Video::GetProjMatrix());
175    uni_mat = data->shader->GetUniformLocation("view_matrix");
176    data->shader->SetUniform(uni_mat, Video::GetViewMatrix());
177    uni_mat = data->shader->GetUniformLocation("model_matrix");
178    data->shader->SetUniform(uni_mat, model_matrix);
179
180    float const vertex[] = {   0.0f,   0.0f, 0.0f,
181                             640.0f,   0.0f, 0.0f,
182                               0.0f, 480.0f, 0.0f,
183                               0.0f, 480.0f, 0.0f,
184                             640.0f, 480.0f, 0.0f,
185                             640.0f,   0.0f, 0.0f, };
186
187    float const color[] = { 0.73f, 0.85f, 0.85f, 1.0f,
188                            0.73f, 0.85f, 0.85f, 1.0f,
189                            0.0f, 0.0f, 1.0f, 1.0f,
190                            0.0f, 0.0f, 1.0f, 1.0f,
191                            0.0f, 0.0f, 1.0f, 1.0f,
192                            0.73f, 0.85f, 0.85f, 1.0f, };
193
194    data->shader->Bind();
195
196    /* Bind vertex, color and texture coordinate buffers */
197#if !defined __CELLOS_LV2__
198#   if defined HAVE_GL_2X && !defined __APPLE__
199    glBindVertexArray(data->vaos[0]);
200#   endif
201    glEnableVertexAttribArray(attr_pos);
202    glEnableVertexAttribArray(attr_col);
203
204    glBindBuffer(GL_ARRAY_BUFFER, data->bufs[0]);
205    glBufferData(GL_ARRAY_BUFFER, 18 * sizeof(GLfloat),
206                 vertex, GL_DYNAMIC_DRAW);
207    glVertexAttribPointer(attr_pos, 3, GL_FLOAT, GL_FALSE, 0, 0);
208
209    glBindBuffer(GL_ARRAY_BUFFER, data->bufs[1]);
210    glBufferData(GL_ARRAY_BUFFER, 24 * sizeof(GLfloat),
211                 color, GL_DYNAMIC_DRAW);
212    glVertexAttribPointer(attr_col, 4, GL_FLOAT, GL_FALSE, 0, 0);
213#else
214    glEnableClientState(GL_VERTEX_ARRAY);
215    glEnableClientState(GL_COLOR_ARRAY);
216
217    glVertexPointer(3, GL_FLOAT, 0, vertex);
218    glColorPointer(4, GL_FLOAT, 0, color);
219#endif
220
221    /* Draw arrays */
222    glDrawArrays(GL_TRIANGLES, 0, 6);
223
224#if !defined __CELLOS_LV2__
225#   if defined HAVE_GL_2X && !defined __APPLE__
226    glBindVertexArray(0);
227#   endif
228    glDisableVertexAttribArray(attr_pos);
229    glDisableVertexAttribArray(attr_col);
230#else
231    glDisableClientState(GL_VERTEX_ARRAY);
232    glDisableClientState(GL_COLOR_ARRAY);
233#endif
234}
235
236Gradient::~Gradient()
237{
238    /* FIXME: destroy shader */
239    delete data;
240}
241
242} /* namespace lol */
243
Note: See TracBrowser for help on using the repository browser.