source: trunk/src/gradient.cpp @ 1310

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

core: tick methods now use seconds, like any sane system.

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