source: trunk/src/gradient.cpp @ 1215

Last change on this file since 1215 was 1215, checked in by sam, 10 years ago

gpu: add support for integer uniforms and fix a few PS3 and Linux compilation
issues that were introduced with the Direct3D changes.

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