Browse Source

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

legacy
Sam Hocevar sam 13 years ago
parent
commit
533b3b33e0
4 changed files with 299 additions and 2 deletions
  1. +1
    -1
      src/Makefile.am
  2. +3
    -1
      src/core.h
  3. +250
    -0
      src/gradient.cpp
  4. +45
    -0
      src/gradient.h

+ 1
- 1
src/Makefile.am View File

@@ -9,7 +9,7 @@ liblol_a_SOURCES = \
timer.cpp timer.h bitfield.h profiler.cpp profiler.h input.h input.cpp \
world.cpp world.h sample.cpp sample.h sampler.cpp sampler.h \
text.cpp text.h emitter.cpp emitter.h numeric.h hash.cpp hash.h \
worldentity.cpp worldentity.h image.cpp image.h \
worldentity.cpp worldentity.h image.cpp image.h gradient.cpp gradient.h \
platform.cpp platform.h \
\
sdlapp.cpp sdlapp.h sdlinput.cpp sdlinput.h \


+ 3
- 1
src/core.h View File

@@ -32,12 +32,14 @@

// Entities
#include "entity.h"
#include "worldentity.h"

#include "emitter.h"
#include "font.h"
#include "gradient.h"
#include "sample.h"
#include "text.h"
#include "tileset.h"
#include "worldentity.h"
#include "world.h"

// Other objects


+ 250
- 0
src/gradient.cpp View File

@@ -0,0 +1,250 @@
//
// Lol Engine
//
// Copyright: (c) 2010-2011 Sam Hocevar <sam@hocevar.net>
// This program is free software; you can redistribute it and/or
// modify it under the terms of the Do What The Fuck You Want To
// Public License, Version 2, as published by Sam Hocevar. See
// http://sam.zoy.org/projects/COPYING.WTFPL for more details.
//

#if defined HAVE_CONFIG_H
# include "config.h"
#endif

#include <cmath>

#include "core.h"
#include "lolgl.h"

using namespace std;

namespace lol
{

/*
* Gradient implementation class
*/

class GradientData
{
friend class Gradient;

private:
Shader *shader;
GLuint bufs[2];
#if defined HAVE_GL_2X
GLuint vaos[1];
#endif
};

/*
* Public Gradient class
*/

Gradient::Gradient(vec3 aa, vec3 bb)
: data(new GradientData())
{
/* FIXME: this should not be hardcoded */
position = aa;
bbox[0] = aa;
bbox[1] = bb;

data->shader = NULL;
}

void Gradient::TickGame(float deltams)
{
Entity::TickGame(deltams);
}

void Gradient::TickDraw(float deltams)
{
Entity::TickDraw(deltams);

if (!data->shader)
{
#if !defined __CELLOS_LV2__
data->shader = Shader::Create(
"#version 130\n"
"\n"
#if defined HAVE_GLES_2X
"attribute vec3 in_Vertex;\n"
"attribute vec4 in_Color;\n"
"varying vec4 pass_Color;\n"
#else
"in vec3 in_Vertex;\n"
"in vec4 in_Color;\n"
"out vec4 pass_Color;\n"
#endif
"uniform mat4 proj_matrix;\n"
"uniform mat4 view_matrix;\n"
"uniform mat4 model_matrix;\n"
"\n"
"void main()\n"
"{\n"
" gl_Position = proj_matrix * view_matrix * model_matrix"
" * vec4(in_Vertex, 1.0);\n"
//"gl_Position = vec4(in_Vertex, 1.0);\n"
#if defined HAVE_GLES_2X
" pass_Color = in_Color;\n"
//" pass_Color = vec4(in_Color, 1.0);\n"
#else
" pass_Color = in_Color;\n"
//" pass_Color = vec4(in_Color, 1.0);\n"
#endif
"}\n",

"#version 130\n"
"\n"
"in vec4 pass_Color;\n"
"\n"
"mat4 bayer = mat4( 0.0, 12.0, 3.0, 15.0,"
" 8.0, 4.0, 11.0, 7.0,"
" 2.0, 14.0, 1.0, 13.0,"
" 10.0, 6.0, 9.0, 5.0);\n"
""
"mat4 cluster = mat4(12.0, 5.0, 6.0, 13.0,"
" 4.0, 0.0, 1.0, 7.0,"
" 11.0, 3.0, 2.0, 8.0,"
" 15.0, 10.0, 9.0, 14.0);\n"
"\n"
"void main()\n"
"{\n"
" vec4 col = pass_Color;\n"
" float t = cluster[int(mod(gl_FragCoord.x, 4.0))]"
" [int(mod(gl_FragCoord.y, 4.0))];\n"
" t = (t + 0.5) / 17.0;\n"
" col.x = col.x > t ? 1.0 : 0.0;\n"
" col.y = col.y > t ? 1.0 : 0.0;\n"
" col.z = col.z > t ? 1.0 : 0.0;\n"
" gl_FragColor = col;\n"
"}\n");
#else
data->shader = Shader::Create(
"void main(float4 in_Vertex : POSITION,"
" float2 in_TexCoord : TEXCOORD0,"
" uniform float4x4 proj_matrix,"
" uniform float4x4 view_matrix,"
" uniform float4x4 model_matrix,"
" out float2 out_TexCoord : TEXCOORD0,"
" out float4 out_Position : POSITION)"
"{"
" out_Position = mul(proj_matrix, mul(view_matrix, mul(model_matrix, in_Vertex)));"
" out_TexCoord = in_TexCoord;"
"}",

"void main(float2 in_TexCoord : TEXCOORD0,"
" float4 in_FragCoord : WPOS,"
" uniform sampler2D tex,"
" out float4 out_FragColor : COLOR)"
"{"
" float4 col = tex2D(tex, in_TexCoord);"
#if 0
" float t1, t2, t3;\n"
" col.x = col.x > t1 ? 1.0 : 0.0;\n"
" col.y = col.y > t2 ? 1.0 : 0.0;\n"
" col.z = col.z > t3 ? 1.0 : 0.0;\n"
#endif
" out_FragColor = col;"
"}");
#endif
glGenBuffers(2, data->bufs);
#if defined HAVE_GL_2X
glGenVertexArrays(1, data->vaos);
#endif
}

mat4 model_matrix = mat4(1.0f);

GLuint uni_mat, attr_pos, attr_col;
#if !defined __CELLOS_LV2__
attr_pos = data->shader->GetAttribLocation("in_Vertex");
attr_col = data->shader->GetAttribLocation("in_Color");
#endif

data->shader->Bind();

uni_mat = data->shader->GetUniformLocation("proj_matrix");
data->shader->SetUniform(uni_mat, Video::GetProjMatrix());
uni_mat = data->shader->GetUniformLocation("view_matrix");
data->shader->SetUniform(uni_mat, Video::GetViewMatrix());
uni_mat = data->shader->GetUniformLocation("model_matrix");
data->shader->SetUniform(uni_mat, model_matrix);

// glEnable(GL_TEXTURE_2D);
// glEnable(GL_DEPTH_TEST);
// glDepthFunc(GL_LEQUAL);
#if defined HAVE_GL_2X
// glEnable(GL_ALPHA_TEST);
// glAlphaFunc(GL_GEQUAL, 0.01f);
#endif
// glEnable(GL_BLEND);
// glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

/* Create a vertex array object */
float const vertex[] = { 0.0f, 0.0f, 0.0f,
640.0f, 0.0f, 0.0f,
0.0f, 480.0f, 0.0f,
0.0f, 480.0f, 0.0f,
640.0f, 480.0f, 0.0f,
640.0f, 0.0f, 0.0f, };

float const color[] = { 0.73f, 0.85f, 0.85f, 1.0f,
0.73f, 0.85f, 0.85f, 1.0f,
0.0f, 0.0f, 1.0f, 1.0f,
0.0f, 0.0f, 1.0f, 1.0f,
0.0f, 0.0f, 1.0f, 1.0f,
0.73f, 0.85f, 0.85f, 1.0f, };

data->shader->Bind();

/* Bind vertex, color and texture coordinate buffers */
#if defined HAVE_GL_2X
glBindVertexArray(data->vaos[0]);
#endif
#if !defined __CELLOS_LV2__ // Use cgGLEnableClientState etc.
glEnableVertexAttribArray(attr_pos);
glEnableVertexAttribArray(attr_col);

glBindBuffer(GL_ARRAY_BUFFER, data->bufs[0]);
glBufferData(GL_ARRAY_BUFFER, 18 * sizeof(GLfloat),
vertex, GL_DYNAMIC_DRAW);
glVertexAttribPointer(attr_pos, 3, GL_FLOAT, GL_FALSE, 0, 0);

glBindBuffer(GL_ARRAY_BUFFER, data->bufs[1]);
glBufferData(GL_ARRAY_BUFFER, 24 * sizeof(GLfloat),
color, GL_DYNAMIC_DRAW);
glVertexAttribPointer(attr_col, 4, GL_FLOAT, GL_FALSE, 0, 0);
#else
glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_COLOR_ARRAY);

glVertexPointer(3, GL_FLOAT, 0, vertex);
glTexCoordPointer(4, GL_FLOAT, 0, color);
#endif

/* Draw arrays */
glDrawArrays(GL_TRIANGLES, 0, 6);

#if defined HAVE_GL_2X
glBindVertexArray(0);
#endif
#if !defined __CELLOS_LV2__ // Use cgGLEnableClientState etc.
glDisableVertexAttribArray(attr_pos);
glDisableVertexAttribArray(attr_col);
#else
glDisableClientState(GL_VERTEX_ARRAY);
glDisableClientState(GL_COLOR_ARRAY);
#endif
// END
}

Gradient::~Gradient()
{
/* FIXME: destroy shader */
delete data;
}

} /* namespace lol */


+ 45
- 0
src/gradient.h View File

@@ -0,0 +1,45 @@
//
// Lol Engine
//
// Copyright: (c) 2010-2011 Sam Hocevar <sam@hocevar.net>
// This program is free software; you can redistribute it and/or
// modify it under the terms of the Do What The Fuck You Want To
// Public License, Version 2, as published by Sam Hocevar. See
// http://sam.zoy.org/projects/COPYING.WTFPL for more details.
//

//
// The Gradient class
// ------------------
//

#if !defined __LOL_GRADIENT_H__
#define __LOL_GRADIENT_H__

#include "worldentity.h"

namespace lol
{

class GradientData;

class Gradient : public WorldEntity
{
public:
Gradient(vec3 aa, vec3 bb);
virtual ~Gradient();

char const *GetName() { return "<gradient>"; }

protected:
virtual void TickGame(float deltams);
virtual void TickDraw(float deltams);

private:
GradientData *data;
};

} /* namespace lol */

#endif // __LOL_GRADIENT_H__


Loading…
Cancel
Save