source: trunk/src/gradient.cpp @ 860

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

render: add a Gradient class that will be used for dithering later.

  • 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
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#if defined HAVE_GLES_2X
72            "attribute vec3 in_Vertex;\n"
73            "attribute vec4 in_Color;\n"
74            "varying vec4 pass_Color;\n"
75#else
76            "in vec3 in_Vertex;\n"
77            "in vec4 in_Color;\n"
78            "out vec4 pass_Color;\n"
79#endif
80            "uniform mat4 proj_matrix;\n"
81            "uniform mat4 view_matrix;\n"
82            "uniform mat4 model_matrix;\n"
83            "\n"
84            "void main()\n"
85            "{\n"
86            "    gl_Position = proj_matrix * view_matrix * model_matrix"
87            "                * vec4(in_Vertex, 1.0);\n"
88//"gl_Position = vec4(in_Vertex, 1.0);\n"
89#if defined HAVE_GLES_2X
90            "    pass_Color = in_Color;\n"
91            //"    pass_Color = vec4(in_Color, 1.0);\n"
92#else
93            "    pass_Color = in_Color;\n"
94            //"    pass_Color = vec4(in_Color, 1.0);\n"
95#endif
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 = col.x > t ? 1.0 : 0.0;\n"
119            "    col.y = col.y > t ? 1.0 : 0.0;\n"
120            "    col.z = col.z > t ? 1.0 : 0.0;\n"
121            "    gl_FragColor = col;\n"
122            "}\n");
123#else
124        data->shader = Shader::Create(
125            "void main(float4 in_Vertex : POSITION,"
126            "          float2 in_TexCoord : TEXCOORD0,"
127            "          uniform float4x4 proj_matrix,"
128            "          uniform float4x4 view_matrix,"
129            "          uniform float4x4 model_matrix,"
130            "          out float2 out_TexCoord : TEXCOORD0,"
131            "          out float4 out_Position : POSITION)"
132            "{"
133            "    out_Position = mul(proj_matrix, mul(view_matrix, mul(model_matrix, in_Vertex)));"
134            "    out_TexCoord = in_TexCoord;"
135            "}",
136
137            "void main(float2 in_TexCoord : TEXCOORD0,"
138            "          float4 in_FragCoord : WPOS,"
139            "          uniform sampler2D tex,"
140            "          out float4 out_FragColor : COLOR)"
141            "{"
142            "    float4 col = tex2D(tex, in_TexCoord);"
143#if 0
144            "    float t1, t2, t3;\n"
145            "    col.x = col.x > t1 ? 1.0 : 0.0;\n"
146            "    col.y = col.y > t2 ? 1.0 : 0.0;\n"
147            "    col.z = col.z > t3 ? 1.0 : 0.0;\n"
148#endif
149            "    out_FragColor = col;"
150            "}");
151#endif
152        glGenBuffers(2, data->bufs);
153#if defined HAVE_GL_2X
154        glGenVertexArrays(1, data->vaos);
155#endif
156    }
157
158    mat4 model_matrix = mat4(1.0f);
159
160    GLuint uni_mat, attr_pos, attr_col;
161#if !defined __CELLOS_LV2__
162    attr_pos = data->shader->GetAttribLocation("in_Vertex");
163    attr_col = data->shader->GetAttribLocation("in_Color");
164#endif
165
166    data->shader->Bind();
167
168    uni_mat = data->shader->GetUniformLocation("proj_matrix");
169    data->shader->SetUniform(uni_mat, Video::GetProjMatrix());
170    uni_mat = data->shader->GetUniformLocation("view_matrix");
171    data->shader->SetUniform(uni_mat, Video::GetViewMatrix());
172    uni_mat = data->shader->GetUniformLocation("model_matrix");
173    data->shader->SetUniform(uni_mat, model_matrix);
174
175//    glEnable(GL_TEXTURE_2D);
176//    glEnable(GL_DEPTH_TEST);
177//    glDepthFunc(GL_LEQUAL);
178#if defined HAVE_GL_2X
179//    glEnable(GL_ALPHA_TEST);
180//    glAlphaFunc(GL_GEQUAL, 0.01f);
181#endif
182//    glEnable(GL_BLEND);
183//    glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
184
185        /* Create a vertex array object */
186float const vertex[] = {   0.0f,   0.0f, 0.0f,
187                         640.0f,   0.0f, 0.0f,
188                           0.0f, 480.0f, 0.0f,
189                           0.0f, 480.0f, 0.0f,
190                         640.0f, 480.0f, 0.0f,
191                         640.0f,   0.0f, 0.0f, };
192
193float const color[] = { 0.73f, 0.85f, 0.85f, 1.0f,
194                        0.73f, 0.85f, 0.85f, 1.0f,
195                        0.0f, 0.0f, 1.0f, 1.0f,
196                        0.0f, 0.0f, 1.0f, 1.0f,
197                        0.0f, 0.0f, 1.0f, 1.0f,
198                        0.73f, 0.85f, 0.85f, 1.0f, };
199
200        data->shader->Bind();
201
202        /* Bind vertex, color and texture coordinate buffers */
203#if defined HAVE_GL_2X
204        glBindVertexArray(data->vaos[0]);
205#endif
206#if !defined __CELLOS_LV2__ // Use cgGLEnableClientState etc.
207        glEnableVertexAttribArray(attr_pos);
208        glEnableVertexAttribArray(attr_col);
209
210        glBindBuffer(GL_ARRAY_BUFFER, data->bufs[0]);
211        glBufferData(GL_ARRAY_BUFFER, 18 * sizeof(GLfloat),
212                     vertex, GL_DYNAMIC_DRAW);
213        glVertexAttribPointer(attr_pos, 3, GL_FLOAT, GL_FALSE, 0, 0);
214
215        glBindBuffer(GL_ARRAY_BUFFER, data->bufs[1]);
216        glBufferData(GL_ARRAY_BUFFER, 24 * sizeof(GLfloat),
217                     color, GL_DYNAMIC_DRAW);
218        glVertexAttribPointer(attr_col, 4, GL_FLOAT, GL_FALSE, 0, 0);
219#else
220        glEnableClientState(GL_VERTEX_ARRAY);
221        glEnableClientState(GL_COLOR_ARRAY);
222
223        glVertexPointer(3, GL_FLOAT, 0, vertex);
224        glTexCoordPointer(4, GL_FLOAT, 0, color);
225#endif
226
227        /* Draw arrays */
228        glDrawArrays(GL_TRIANGLES, 0, 6);
229
230#if defined HAVE_GL_2X
231        glBindVertexArray(0);
232#endif
233#if !defined __CELLOS_LV2__ // Use cgGLEnableClientState etc.
234        glDisableVertexAttribArray(attr_pos);
235        glDisableVertexAttribArray(attr_col);
236#else
237        glDisableClientState(GL_VERTEX_ARRAY);
238        glDisableClientState(GL_COLOR_ARRAY);
239#endif
240   // END
241}
242
243Gradient::~Gradient()
244{
245    /* FIXME: destroy shader */
246    delete data;
247}
248
249} /* namespace lol */
250
Note: See TracBrowser for help on using the repository browser.