source: trunk/src/gradient.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: 6.9 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 "core.h"
16#include "lolgl.h"
17
18using namespace std;
19
20namespace lol
21{
22
23/*
24 * Gradient implementation class
25 */
26
27class GradientData
28{
29    friend class Gradient;
30
31private:
32    Shader *shader;
33    VertexDeclaration *m_vdecl;
34    VertexBuffer *m_vbo, *m_cbo;
35};
36
37/*
38 * Public Gradient class
39 */
40
41Gradient::Gradient(vec3 aa, vec3 bb)
42  : data(new GradientData())
43{
44    /* FIXME: this should not be hardcoded */
45    m_position = aa;
46    m_bbox[0] = aa;
47    m_bbox[1] = bb;
48
49    data->shader = NULL;
50}
51
52void Gradient::TickGame(float seconds)
53{
54    Entity::TickGame(seconds);
55}
56
57void Gradient::TickDraw(float seconds)
58{
59    Entity::TickDraw(seconds);
60
61    float const vertex[] = {   0.0f,   0.0f, 0.0f,
62                             640.0f,   0.0f, 0.0f,
63                               0.0f, 480.0f, 0.0f,
64                             640.0f, 480.0f, 0.0f,
65                               0.0f, 480.0f, 0.0f,
66                             640.0f,   0.0f, 0.0f, };
67
68    float const color[] = { 0.73f, 0.85f, 0.85f, 1.0f,
69                            0.73f, 0.85f, 0.85f, 1.0f,
70                            0.0f, 0.0f, 1.0f, 1.0f,
71                            0.0f, 0.0f, 1.0f, 1.0f,
72                            0.0f, 0.0f, 1.0f, 1.0f,
73                            0.73f, 0.85f, 0.85f, 1.0f, };
74
75    if (!data->shader)
76    {
77#if !defined __CELLOS_LV2__ && !defined USE_D3D9 && !defined _XBOX
78        data->shader = Shader::Create(
79            "#version 130\n"
80            "\n"
81            "in vec3 in_Vertex;\n"
82            "in vec4 in_Color;\n"
83            "out vec4 pass_Color;\n"
84            "\n"
85            "uniform mat4 proj_matrix;\n"
86            "uniform mat4 view_matrix;\n"
87            "uniform mat4 model_matrix;\n"
88            "\n"
89            "void main()\n"
90            "{\n"
91            "    gl_Position = proj_matrix * view_matrix * model_matrix"
92            "                * vec4(in_Vertex, 1.0);\n"
93            "    pass_Color = in_Color;\n"
94            "}\n",
95
96            "#version 130\n"
97            "\n"
98            "in vec4 pass_Color;\n"
99            "\n"
100            "mat4 bayer = mat4( 0.0, 12.0,  3.0, 15.0,"
101            "                   8.0,  4.0, 11.0,  7.0,"
102            "                   2.0, 14.0,  1.0, 13.0,"
103            "                  10.0,  6.0,  9.0,  5.0);\n"
104            ""
105            "mat4 cluster = mat4(12.0,  5.0,  6.0, 13.0,"
106            "                     4.0,  0.0,  1.0,  7.0,"
107            "                    11.0,  3.0,  2.0,  8.0,"
108            "                    15.0, 10.0,  9.0, 14.0);\n"
109            "\n"
110            "void main()\n"
111            "{\n"
112            "    vec4 col = pass_Color;\n"
113            "    float t = cluster[int(mod(gl_FragCoord.x, 4.0))]"
114            "                     [int(mod(gl_FragCoord.y, 4.0))];\n"
115            "    t = (t + 0.5) / 17.0;\n"
116            "    col.x += fract(t - col.x) - t;\n"
117            "    col.y += fract(t - col.y) - t;\n"
118            "    col.z += fract(t - col.z) - t;\n"
119            "    gl_FragColor = col;\n"
120            "}\n");
121#else
122        data->shader = Shader::Create(
123            "void main(float4 in_Vertex : POSITION,"
124            "          float4 in_Color : COLOR,"
125            "          uniform float4x4 proj_matrix,"
126            "          uniform float4x4 view_matrix,"
127            "          uniform float4x4 model_matrix,"
128            "          out float4 out_Color : COLOR,"
129            "          out float4 out_Position : POSITION)"
130            "{"
131            "    out_Position = mul(proj_matrix, mul(view_matrix, mul(model_matrix, in_Vertex)));"
132            "    out_Color = in_Color;"
133            "}",
134
135            "float4x4 bayer = float4x4( 0.0, 12.0,  3.0, 15.0,"
136            "                           8.0,  4.0, 11.0,  7.0,"
137            "                           2.0, 14.0,  1.0, 13.0,"
138            "                          10.0,  6.0,  9.0,  5.0);\n"
139            ""
140#if 1
141            "float4x4 cluster = float4x4(12.0,  5.0,  6.0, 13.0,"
142            "                             4.0,  0.0,  1.0,  7.0,"
143            "                            11.0,  3.0,  2.0,  8.0,"
144            "                            15.0, 10.0,  9.0, 14.0);\n"
145#endif
146            "\n"
147            "void main(float4 in_Color : COLOR,"
148            "          float4 in_FragCoord : WPOS,"
149            "          out float4 out_FragColor : COLOR)"
150            "{"
151            "    float4 col = in_Color;"
152#if 1
153            "    int x = (int)in_FragCoord.x;"
154            "    int y = (int)in_FragCoord.y;"
155#if defined USE_D3D9 || defined _XBOX
156            "    float t = bayer[int(frac(x * 0.25) * 4.0)]"
157            "                   [int(frac(y * 0.25) * 4.0)];\n"
158#else
159            // FIXME: we cannot address this matrix directly on the PS3
160            "    float t = bayer[0][0];\n"
161#endif
162            "    t = (t + 0.5) / 17.0;\n"
163            "    col.x += frac(t - col.x) - t;\n"
164            "    col.y += frac(t - col.y) - t;\n"
165            "    col.z += frac(t - col.z) - t;\n"
166#endif
167            "    out_FragColor = col;"
168            "}");
169#endif
170        data->m_vbo = new VertexBuffer(sizeof(vertex));
171        data->m_cbo = new VertexBuffer(sizeof(color));
172
173        data->m_vdecl = new VertexDeclaration(VertexStream<vec3>(VertexUsage::Position),
174                                              VertexStream<vec4>(VertexUsage::Color));
175    }
176
177    mat4 model_matrix = mat4(1.0f);
178
179    ShaderUniform uni_mat;
180    ShaderAttrib attr_pos, attr_col;
181    attr_pos = data->shader->GetAttribLocation("in_Vertex", VertexUsage::Position, 0);
182    attr_col = data->shader->GetAttribLocation("in_Color", VertexUsage::Color, 0);
183
184    data->shader->Bind();
185
186    uni_mat = data->shader->GetUniformLocation("proj_matrix");
187    data->shader->SetUniform(uni_mat, Scene::GetDefault()->GetProjMatrix());
188    uni_mat = data->shader->GetUniformLocation("view_matrix");
189    data->shader->SetUniform(uni_mat, Scene::GetDefault()->GetViewMatrix());
190    uni_mat = data->shader->GetUniformLocation("model_matrix");
191    data->shader->SetUniform(uni_mat, model_matrix);
192
193    data->shader->Bind();
194    data->m_vdecl->Bind();
195
196    void *tmp = data->m_vbo->Lock(0, 0);
197    memcpy(tmp, vertex, sizeof(vertex));
198    data->m_vbo->Unlock();
199
200    tmp = data->m_cbo->Lock(0, 0);
201    memcpy(tmp, color, sizeof(color));
202    data->m_cbo->Unlock();
203
204    /* Bind vertex and color buffers */
205    data->m_vdecl->SetStream(data->m_vbo, attr_pos);
206    data->m_vdecl->SetStream(data->m_cbo, attr_col);
207
208    /* Draw arrays */
209    data->m_vdecl->DrawElements(MeshPrimitive::Triangles, 0, 2);
210}
211
212Gradient::~Gradient()
213{
214    /* FIXME: destroy shader */
215    delete data;
216}
217
218} /* namespace lol */
Note: See TracBrowser for help on using the repository browser.