source: trunk/src/gradient.cpp @ 1173

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

xbox: start working on an Xbox/Direct3D port.

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