Browse Source

video: move the last shader from the Video class to Scene itself.

legacy
Sam Hocevar sam 13 years ago
parent
commit
d1270bf691
3 changed files with 127 additions and 109 deletions
  1. +94
    -8
      src/scene.cpp
  2. +31
    -101
      src/video.cpp
  3. +2
    -0
      src/video.h

+ 94
- 8
src/scene.cpp View File

@@ -32,8 +32,7 @@ struct Tile
int x, y, z, o; int x, y, z, o;
}; };


extern Shader *stdshader;
extern mat4 model_matrix;
static Shader *stdshader = NULL;


/* /*
* Scene implementation class * Scene implementation class
@@ -52,6 +51,8 @@ private:
return t2->prio - t1->prio; return t2->prio - t1->prio;
} }


mat4 model_matrix;

Tile *tiles; Tile *tiles;
int ntiles; int ntiles;
float angle; float angle;
@@ -129,6 +130,85 @@ void Scene::AddTile(uint32_t code, int x, int y, int z, int o)


void Scene::Render() // XXX: rename to Blit() void Scene::Render() // XXX: rename to Blit()
{ {
if (!stdshader)
{
stdshader = Shader::Create(
"#version 130\n"
"\n"
#if defined HAVE_GLES_2X
"attribute vec3 in_Position;\n"
"attribute vec2 in_TexCoord;\n"
"varying vec2 pass_TexCoord;\n"
#else
"in vec3 in_Position;\n"
"in vec2 in_TexCoord;\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_Position, 1.0);\n"
#if defined HAVE_GLES_2X
" pass_TexCoord = in_TexCoord;\n"
#else
" gl_TexCoord[0] = vec4(in_TexCoord, 0.0, 0.0);\n"
#endif
"}\n",

"#version 130\n"
"\n"
"uniform sampler2D in_Texture;\n"
#if defined HAVE_GLES_2X
"varying vec2 pass_TexCoord;\n"
#endif
"\n"
"void main()\n"
"{\n"
#if defined HAVE_GLES_2X
" vec4 col = texture2D(in_Texture, pass_TexCoord);\n"
//" vec4 col = vec4(0.5, 1.0, 0.0, 0.5);\n"
//" vec4 col = vec4(pass_TexCoord * 4.0, 0.0, 0.25);\n"
#else
" vec4 col = texture2D(in_Texture, vec2(gl_TexCoord[0]));\n"
#endif
#if 0
" float mul = 2.0;\n"
#if 0
" vec2 d1 = mod(vec2(gl_FragCoord), vec2(2.0, 2.0));\n"
" float t1 = mod(3.0 * d1.x + 2.0 * d1.y, 4.0);\n"
" float dx2 = mod(floor(gl_FragCoord.x * 0.5), 2.0);\n"
" float dy2 = mod(floor(gl_FragCoord.y * 0.5), 2.0);\n"
" float t2 = mod(3.0 * dx2 + 2.0 * dy2, 4.0);\n"
" float dx3 = mod(floor(gl_FragCoord.x * 0.25), 2.0);\n"
" float dy3 = mod(floor(gl_FragCoord.y * 0.25), 2.0);\n"
" float t3 = mod(3.0 * dx3 + 2.0 * dy3, 4.0);\n"
" float t1 = (1.0 + 16.0 * t1 + 4.0 * t2 + t3) / 65.0;\n"
" float t2 = t1;\n"
" float t3 = t1;\n"
#else
" float rand = sin(gl_FragCoord.x * 1.23456) * 123.456\n"
" + cos(gl_FragCoord.y * 2.34567) * 789.012;\n"
" float t1 = mod(sin(rand) * 17.13043, 1.0);\n"
" float t2 = mod(sin(rand) * 27.13043, 1.0);\n"
" float t3 = mod(sin(rand) * 37.13043, 1.0);\n"
#endif
" float fracx = fract(col.x * mul);\n"
" float fracy = fract(col.y * mul);\n"
" float fracz = fract(col.z * mul);\n"
" fracx = fracx > t1 ? 1.0 : 0.0;\n"
" fracy = fracy > t2 ? 1.0 : 0.0;\n"
" fracz = fracz > t3 ? 1.0 : 0.0;\n"
" col.x = (floor(col.x * mul) + fracx) / mul;\n"
" col.y = (floor(col.y * mul) + fracy) / mul;\n"
" col.z = (floor(col.z * mul) + fracz) / mul;\n"
#endif
" gl_FragColor = col;\n"
"}\n");
}

#if 0 #if 0
// Randomise, then sort. // Randomise, then sort.
for (int i = 0; i < data->ntiles; i++) for (int i = 0; i < data->ntiles; i++)
@@ -142,15 +222,15 @@ void Scene::Render() // XXX: rename to Blit()
qsort(data->tiles, data->ntiles, sizeof(Tile), SceneData::Compare); qsort(data->tiles, data->ntiles, sizeof(Tile), SceneData::Compare);


// XXX: debug stuff // XXX: debug stuff
model_matrix = mat4::translate(320.0f, 240.0f, 0.0f);
model_matrix *= mat4::rotate(-data->angle, 1.0f, 0.0f, 0.0f);
data->model_matrix = mat4::translate(320.0f, 240.0f, 0.0f);
data->model_matrix *= mat4::rotate(-data->angle, 1.0f, 0.0f, 0.0f);
#if 0 #if 0
static float f = 0.0f; static float f = 0.0f;
f += 0.01f; f += 0.01f;
model_matrix *= mat4::rotate(0.1f * sinf(f), 1.0f, 0.0f, 0.0f);
model_matrix *= mat4::rotate(0.3f * cosf(f), 0.0f, 0.0f, 1.0f);
data->model_matrix *= mat4::rotate(0.1f * sinf(f), 1.0f, 0.0f, 0.0f);
data->model_matrix *= mat4::rotate(0.3f * cosf(f), 0.0f, 0.0f, 1.0f);
#endif #endif
model_matrix *= mat4::translate(-320.0f, -240.0f, 0.0f);
data->model_matrix *= mat4::translate(-320.0f, -240.0f, 0.0f);
// XXX: end of debug stuff // XXX: end of debug stuff


GLuint uni_mat, uni_tex, attr_pos, attr_tex; GLuint uni_mat, uni_tex, attr_pos, attr_tex;
@@ -159,8 +239,14 @@ void Scene::Render() // XXX: rename to Blit()


#if !defined __CELLOS_LV2__ // Use cgGetNamedParameter etc. #if !defined __CELLOS_LV2__ // Use cgGetNamedParameter etc.
stdshader->Bind(); stdshader->Bind();

uni_mat = stdshader->GetUniformLocation("proj_matrix");
glUniformMatrix4fv(uni_mat, 1, GL_FALSE, &Video::GetProjMatrix()[0][0]);
uni_mat = stdshader->GetUniformLocation("view_matrix");
glUniformMatrix4fv(uni_mat, 1, GL_FALSE, &Video::GetViewMatrix()[0][0]);
uni_mat = stdshader->GetUniformLocation("model_matrix"); uni_mat = stdshader->GetUniformLocation("model_matrix");
glUniformMatrix4fv(uni_mat, 1, GL_FALSE, &model_matrix[0][0]);
glUniformMatrix4fv(uni_mat, 1, GL_FALSE, &data->model_matrix[0][0]);

uni_tex = stdshader->GetUniformLocation("in_Texture"); uni_tex = stdshader->GetUniformLocation("in_Texture");
glUniform1i(uni_tex, 0); glUniform1i(uni_tex, 0);
#endif #endif


+ 31
- 101
src/video.cpp View File

@@ -27,91 +27,23 @@ using namespace std;
namespace lol namespace lol
{ {


class VideoData
{
friend class Video;

private:
static mat4 proj_matrix, view_matrix;
#if defined ANDROID_NDK #if defined ANDROID_NDK
vec2i saved_viewport;
static vec2i saved_viewport;
#endif #endif
};


Shader *stdshader;
mat4 proj_matrix, view_matrix, model_matrix;

#define OLD
mat4 VideoData::proj_matrix;
mat4 VideoData::view_matrix;


static char const *vertexshader =
"#version 130\n"
"\n"
#if defined HAVE_GLES_2X
"attribute vec3 in_Position;\n"
"attribute vec2 in_TexCoord;\n"
"varying vec2 pass_TexCoord;\n"
#else
"in vec3 in_Position;\n"
"in vec2 in_TexCoord;\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_Position, 1.0);\n"
#if defined HAVE_GLES_2X
" pass_TexCoord = in_TexCoord;\n"
#else
" gl_TexCoord[0] = vec4(in_TexCoord, 0.0, 0.0);\n"
#endif
"}\n";

static char const *fragmentshader =
"#version 130\n"
"\n"
"uniform sampler2D in_Texture;\n"
#if defined HAVE_GLES_2X
"varying vec2 pass_TexCoord;\n"
#endif
"\n"
"void main()\n"
"{\n"
#if defined HAVE_GLES_2X
" vec4 col = texture2D(in_Texture, pass_TexCoord);\n"
//" vec4 col = vec4(0.5, 1.0, 0.0, 0.5);\n"
//" vec4 col = vec4(pass_TexCoord * 4.0, 0.0, 0.25);\n"
#else
" vec4 col = texture2D(in_Texture, vec2(gl_TexCoord[0]));\n"
#endif
#if 0
" float mul = 2.0;\n"
#if 0
" vec2 d1 = mod(vec2(gl_FragCoord), vec2(2.0, 2.0));\n"
" float t1 = mod(3.0 * d1.x + 2.0 * d1.y, 4.0);\n"
" float dx2 = mod(floor(gl_FragCoord.x * 0.5), 2.0);\n"
" float dy2 = mod(floor(gl_FragCoord.y * 0.5), 2.0);\n"
" float t2 = mod(3.0 * dx2 + 2.0 * dy2, 4.0);\n"
" float dx3 = mod(floor(gl_FragCoord.x * 0.25), 2.0);\n"
" float dy3 = mod(floor(gl_FragCoord.y * 0.25), 2.0);\n"
" float t3 = mod(3.0 * dx3 + 2.0 * dy3, 4.0);\n"
" float t1 = (1.0 + 16.0 * t1 + 4.0 * t2 + t3) / 65.0;\n"
" float t2 = t1;\n"
" float t3 = t1;\n"
#else
" float rand = sin(gl_FragCoord.x * 1.23456) * 123.456\n"
" + cos(gl_FragCoord.y * 2.34567) * 789.012;\n"
" float t1 = mod(sin(rand) * 17.13043, 1.0);\n"
" float t2 = mod(sin(rand) * 27.13043, 1.0);\n"
" float t3 = mod(sin(rand) * 37.13043, 1.0);\n"
#endif
" float fracx = fract(col.x * mul);\n"
" float fracy = fract(col.y * mul);\n"
" float fracz = fract(col.z * mul);\n"
" fracx = fracx > t1 ? 1.0 : 0.0;\n"
" fracy = fracy > t2 ? 1.0 : 0.0;\n"
" fracz = fracz > t3 ? 1.0 : 0.0;\n"
" col.x = (floor(col.x * mul) + fracx) / mul;\n"
" col.y = (floor(col.y * mul) + fracy) / mul;\n"
" col.z = (floor(col.z * mul) + fracz) / mul;\n"
#if defined ANDROID_NDK
vec2i VideoData::saved_viewport = 0;
#endif #endif
" gl_FragColor = col;\n"
"}\n";


/* /*
* Public Video class * Public Video class
@@ -123,7 +55,7 @@ void Video::Setup(vec2i size)
glViewport(0, 0, size.x, size.y); glViewport(0, 0, size.x, size.y);


#if defined ANDROID_NDK #if defined ANDROID_NDK
saved_viewport = vec2i(size.x, size.y);
VideoData::saved_viewport = size;
#endif #endif


glClearColor(0.1f, 0.2f, 0.3f, 0.0f); glClearColor(0.1f, 0.2f, 0.3f, 0.0f);
@@ -133,16 +65,12 @@ void Video::Setup(vec2i size)
glShadeModel(GL_SMOOTH); glShadeModel(GL_SMOOTH);
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST); glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
#endif #endif

stdshader = Shader::Create(vertexshader, fragmentshader);
} }


void Video::SetFov(float theta) void Video::SetFov(float theta)
{ {
#undef near /* Fuck Microsoft */ #undef near /* Fuck Microsoft */
#undef far /* Fuck Microsoft again */ #undef far /* Fuck Microsoft again */
mat4 proj;

vec2 size = GetSize(); vec2 size = GetSize();
float near = -size.x - size.y; float near = -size.x - size.y;
float far = size.x + size.y; float far = size.x + size.y;
@@ -155,7 +83,7 @@ void Video::SetFov(float theta)
if (theta < 1e-4f) if (theta < 1e-4f)
{ {
/* The easy way: purely orthogonal projection. */ /* The easy way: purely orthogonal projection. */
proj_matrix = mat4::ortho(0, size.x, 0, size.y, near, far);
VideoData::proj_matrix = mat4::ortho(0, size.x, 0, size.y, near, far);
} }
else else
{ {
@@ -175,21 +103,13 @@ void Video::SetFov(float theta)
near = 1.0f; near = 1.0f;
} }


proj_matrix = mat4::frustum(-near * t1, near * t1,
-near * t2, near * t2, near, far)
* mat4::translate(-0.5f * size.x, -0.5f * size.y, -dist);
mat4 proj = mat4::frustum(-near * t1, near * t1,
-near * t2, near * t2, near, far);
mat4 trans = mat4::translate(-0.5f * size.x, -0.5f * size.y, -dist);
VideoData::proj_matrix = proj * trans;
} }


view_matrix = mat4(1.0f);

stdshader->Bind(); /* Required on GLES 2.x? */
#if !defined __CELLOS_LV2__ // Use cgGetNamedParameter etc.
GLuint uni;
uni = stdshader->GetUniformLocation("proj_matrix");
glUniformMatrix4fv(uni, 1, GL_FALSE, &proj_matrix[0][0]);
uni = stdshader->GetUniformLocation("view_matrix");
glUniformMatrix4fv(uni, 1, GL_FALSE, &view_matrix[0][0]);
#endif
VideoData::view_matrix = mat4(1.0f);
} }


void Video::SetDepth(bool set) void Video::SetDepth(bool set)
@@ -211,7 +131,7 @@ void Video::Clear()


void Video::Destroy() void Video::Destroy()
{ {
Shader::Destroy(stdshader);
;
} }


void Video::Capture(uint32_t *buffer) void Video::Capture(uint32_t *buffer)
@@ -249,7 +169,7 @@ void Video::Capture(uint32_t *buffer)
vec2i Video::GetSize() vec2i Video::GetSize()
{ {
#if defined ANDROID_NDK #if defined ANDROID_NDK
return saved_viewport;
return VideoData::saved_viewport;
#elif defined __CELLOS_LV2__ #elif defined __CELLOS_LV2__
// FIXME: use psglCreateDeviceAuto && psglGetDeviceDimensions // FIXME: use psglCreateDeviceAuto && psglGetDeviceDimensions
return vec2i(1920, 1080); return vec2i(1920, 1080);
@@ -260,5 +180,15 @@ vec2i Video::GetSize()
#endif #endif
} }


mat4 const & Video::GetProjMatrix()
{
return VideoData::proj_matrix;
}

mat4 const & Video::GetViewMatrix()
{
return VideoData::view_matrix;
}

} /* namespace lol */ } /* namespace lol */



+ 2
- 0
src/video.h View File

@@ -32,6 +32,8 @@ public:
static void Clear(); static void Clear();
static void Capture(uint32_t *buffer); static void Capture(uint32_t *buffer);
static vec2i GetSize(); static vec2i GetSize();
static mat4 const & GetProjMatrix();
static mat4 const & GetViewMatrix();
}; };


} /* namespace lol */ } /* namespace lol */


Loading…
Cancel
Save