From 58e12ca4d19be9ad56d8d59f51feca243a95d179 Mon Sep 17 00:00:00 2001 From: Sam Hocevar Date: Wed, 16 Feb 2011 18:04:11 +0000 Subject: [PATCH] More shader crap for GLSL 1.30. Still disabled. --- src/core.h | 3 ++ src/scene.cpp | 78 +++++------------------------------ src/video.cpp | 111 ++++++++++++++++++++++++++++++++++++++++++++++++++ src/video.h | 1 + 4 files changed, 126 insertions(+), 67 deletions(-) diff --git a/src/core.h b/src/core.h index e7778baa..cd81db0d 100644 --- a/src/core.h +++ b/src/core.h @@ -16,6 +16,9 @@ #if !defined __DH_CORE_H__ #define __DH_CORE_H__ +// Temporary stuff +//#define SHADER_CRAP 1 + // Base types #include "matrix.h" #include "numeric.h" diff --git a/src/scene.cpp b/src/scene.cpp index bc4f0a5e..54e33e40 100644 --- a/src/scene.cpp +++ b/src/scene.cpp @@ -29,15 +29,18 @@ #include "core.h" -#define ATTRIB_POSITION 42 /* arbitrary id */ -#define SHADER_CRAP 0 - struct Tile { uint32_t prio, code; int x, y, z, o; }; +#if SHADER_CRAP +# define ATTRIB_POSITION 42 /* arbitrary id */ + extern GLuint prog; + extern GLint uni_mvp, uni_color; +#endif + /* * Scene implementation class */ @@ -59,10 +62,6 @@ private: int ntiles; float angle; -#if SHADER_CRAP - GLuint prog, sh1, sh2; - GLint uni_mvp, uni_color; -#endif GLuint *bufs; int nbufs; @@ -71,25 +70,6 @@ private: Scene *SceneData::scene = NULL; -#if SHADER_CRAP -static char const *vertexshader = - "attribute vec4 position;\n" - "uniform mat4 mvp;\n" - "\n" - "void main()\n" - "{\n" - " gl_Position = mvp * position;\n" - "}\n"; - -static char const *fragmentshader = - "uniform /*lowp*/ vec4 color;\n" - "\n" - "void main()\n" - "{\n" - " gl_FragColor = color;\n" - "}\n"; -#endif - /* * Public Scene class */ @@ -103,48 +83,12 @@ Scene::Scene(float angle) data->bufs = 0; data->nbufs = 0; - -#if SHADER_CRAP - data->sh1 = glCreateShader(GL_VERTEX_SHADER); - glShaderSource(data->sh1, 1, &vertexshader, NULL); - glCompileShader(data->sh1); - - char buf[4096]; - GLsizei dummy; - glGetShaderInfoLog(data->sh1, 4096, &dummy, buf); - fprintf(stderr, "sh1 %i: %s", data->sh1, buf); - - data->sh2 = glCreateShader(GL_FRAGMENT_SHADER); - glShaderSource(data->sh2, 1, &fragmentshader, NULL); - glCompileShader(data->sh2); - - glGetShaderInfoLog(data->sh2, 4096, &dummy, buf); - fprintf(stderr, "sh2 %i: %s", data->sh2, buf); - - data->prog = glCreateProgram(); - glAttachShader(data->prog, data->sh1); - glAttachShader(data->prog, data->sh2); - - glBindAttribLocation(data->prog, ATTRIB_POSITION, "position"); - glLinkProgram(data->prog); - glValidateProgram(data->prog); - - data->uni_mvp = glGetUniformLocation(data->prog, "mvp"); - data->uni_color = glGetUniformLocation(data->prog, "color"); -#endif } Scene::~Scene() { /* FIXME: this must be done while the GL context is still active. * Change the architecture to make sure of that. */ -#if SHADER_CRAP - glDetachShader(data->prog, data->sh1); - glDetachShader(data->prog, data->sh2); - glDeleteShader(data->sh1); - glDeleteShader(data->sh2); - glDeleteProgram(data->prog); -#endif glDeleteBuffers(data->nbufs, data->bufs); delete data; } @@ -234,11 +178,11 @@ void Scene::Render() // XXX: rename to Blit() glEnableClientState(GL_VERTEX_ARRAY); glEnableClientState(GL_TEXTURE_COORD_ARRAY); #if SHADER_CRAP - glUseProgram(data->prog); - float4x4 mvp = float4x4::identity(); - mvp = mvp - mvp; - glUniformMatrix4fv(data->uni_mvp, 1, GL_FALSE, (GLfloat *)&mvp[0][0]); - glUniform4f(data->uni_color, 1.0f, 0.0f, 1.0f, 1.0f); + glUseProgram(prog); + //float4x4 mvp(1.0f); + //mvp = mvp - mvp; + //glUniformMatrix4fv(uni_mvp, 1, GL_FALSE, (GLfloat *)&mvp[0][0]); + //glUniform4f(uni_color, 1.0f, 0.0f, 1.0f, 1.0f); #endif glBindBuffer(GL_ARRAY_BUFFER, data->bufs[buf]); diff --git a/src/video.cpp b/src/video.cpp index 00ee5678..ecbd7125 100644 --- a/src/video.cpp +++ b/src/video.cpp @@ -13,6 +13,7 @@ #endif #include +#include #ifdef WIN32 # define WIN32_LEAN_AND_MEAN @@ -27,6 +28,44 @@ #include "core.h" +#if SHADER_CRAP +# define ATTRIB_POSITION 42 /* arbitrary id */ + GLuint prog, sh1, sh2; + GLint uni_m1, uni_m2, uni_m3; + + float4x4 projection_matrix, view_matrix, model_matrix; +#endif + +#if SHADER_CRAP +static char const *vertexshader = + "#version 130\n" + "\n" + "in vec3 in_Position;\n" + "in vec3 in_Color;\n" + "out vec3 pass_Color;\n" + "//attribute vec4 position;\n" + "uniform mat4 projection_matrix;\n" + "uniform mat4 view_matrix;\n" + "uniform mat4 model_matrix;\n" + "\n" + "void main()\n" + "{\n" + " gl_Position = projection_matrix * view_matrix * model_matrix * vec4(in_Position, 1.0f);\n" + " pass_Color = in_Color;\n" + "}\n"; + +static char const *fragmentshader = + "#version 130\n" + "\n" + "in vec3 pass_Color;\n" + "out vec4 out_Color;\n" + "\n" + "void main()\n" + "{\n" + " gl_FragColor = pass_Color;\n" + "}\n"; +#endif + /* * Public Video class */ @@ -42,10 +81,51 @@ void Video::Setup(int width, int height) glClearDepth(1.0); glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST); + +#if SHADER_CRAP + sh1 = glCreateShader(GL_VERTEX_SHADER); + glShaderSource(sh1, 1, &vertexshader, NULL); + glCompileShader(sh1); + + char buf[4096]; + GLsizei dummy; + glGetShaderInfoLog(sh1, 4096, &dummy, buf); + fprintf(stderr, "sh1 %i: %s", sh1, buf); + + sh2 = glCreateShader(GL_FRAGMENT_SHADER); + glShaderSource(sh2, 1, &fragmentshader, NULL); + glCompileShader(sh2); + + glGetShaderInfoLog(sh2, 4096, &dummy, buf); + fprintf(stderr, "sh2 %i: %s", sh2, buf); + + prog = glCreateProgram(); + glAttachShader(prog, sh1); + glAttachShader(prog, sh2); + + //glBindAttribLocation(prog, ATTRIB_POSITION, "position"); + glBindAttribLocation(prog, 0, "in_Position"); + glBindAttribLocation(prog, 1, "in_Color"); + glLinkProgram(prog); + glValidateProgram(prog); + + uni_m1 = glGetUniformLocation(prog, "projection_matrix"); + uni_m2 = glGetUniformLocation(prog, "view_matrix"); + uni_m3 = glGetUniformLocation(prog, "model_matrix"); + + glClearColor(0.4f, 0.6f, 0.9f, 0.0f); +#endif } void Video::SetFov(float theta) { +#if SHADER_CRAP + float width = GetWidth(); + float height = GetHeight(); + float near = -width - height; + float far = width + height; + projection_matrix = float4x4::perspective(theta, width, height, near, far); +#else #undef near /* Fuck Microsoft */ #undef far /* Fuck Microsoft again */ glMatrixMode(GL_PROJECTION); @@ -87,6 +167,7 @@ void Video::SetFov(float theta) /* Reset the model view matrix, just in case */ glMatrixMode(GL_MODELVIEW); glLoadIdentity(); +#endif } void Video::SetDepth(bool set) @@ -99,6 +180,24 @@ void Video::SetDepth(bool set) void Video::Clear() { +#if SHADER_CRAP + glViewport(0, 0, GetWidth(), GetHeight()); + glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT); + + view_matrix = float4x4(1.0f); + view_matrix[3][0] = 0.0f; + view_matrix[3][0] = 0.0f; + view_matrix[3][0] = -5.0f; + + model_matrix = float4x4(1.0f); + model_matrix[0][0] = 0.5f; + model_matrix[1][1] = 0.5f; + model_matrix[2][2] = 0.5f; + + glUniformMatrix4fv(uni_m1, 1, GL_FALSE, &projection_matrix[0][0]); + glUniformMatrix4fv(uni_m2, 1, GL_FALSE, &view_matrix[0][0]); + glUniformMatrix4fv(uni_m3, 1, GL_FALSE, &model_matrix[0][0]); +#else glEnable(GL_DEPTH_TEST); glDepthFunc(GL_LEQUAL); glEnable(GL_ALPHA_TEST); @@ -107,10 +206,22 @@ void Video::Clear() glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); +#endif SetFov(0.0f); } +void Video::Destroy() +{ +#if SHADER_CRAP + glDetachShader(prog, sh1); + glDetachShader(prog, sh2); + glDeleteShader(sh1); + glDeleteShader(sh2); + glDeleteProgram(prog); +#endif +} + void Video::Capture(uint32_t *buffer) { GLint v[4]; diff --git a/src/video.h b/src/video.h index d573d4bb..0da28756 100644 --- a/src/video.h +++ b/src/video.h @@ -23,6 +23,7 @@ class Video { public: static void Setup(int width, int height); + static void Destroy(); static void SetFov(float theta); static void SetDepth(bool set); static void Clear();