ref: 92476bd30a8a14cddac14a74b71a8eec3eb0a7ea
parent: f0809fb901caebf2c0d9778e19e6daa777d32da1
author: Clownacy <Clownacy@users.noreply.github.com>
date: Tue Jul 30 23:00:56 EDT 2019
Made the OpenGL 2.1 backend use custom shaders Edging towards OpenGL 3.
--- a/src/Backends/Rendering/OpenGL2.cpp
+++ b/src/Backends/Rendering/OpenGL2.cpp
@@ -27,6 +27,7 @@
static SDL_Window *window;
static SDL_GLContext context;
+static GLuint normal_program_id;
static GLuint colour_key_program_id;
static GLuint framebuffer_id;
static GLfloat vertex_buffer[4][2];
@@ -35,11 +36,30 @@
static Backend_Surface framebuffer_surface;
-static const GLchar *fragment_shader_source = " \
+static const GLchar *vertex_shader_source = " \
#version 120\n \
+void main() \
+{ \
+ gl_FrontColor = gl_Color; \
+ gl_TexCoord[0] = gl_MultiTexCoord0; \
+ gl_Position = gl_ModelViewMatrix * gl_Vertex; \
+} \
+";
+
+static const GLchar *fragment_shader_source_normal = " \
+#version 120\n \
uniform sampler2D tex; \
void main() \
{ \
+ gl_FragColor = gl_Color * texture2D(tex, gl_TexCoord[0].st); \
+} \
+";
+
+static const GLchar *fragment_shader_source_colour_key = " \
+#version 120\n \
+uniform sampler2D tex; \
+void main() \
+{ \
vec4 colour = gl_Color * texture2D(tex, gl_TexCoord[0].st); \
\
if (colour.xyz == vec3(0.0f, 0.0f, 0.0f)) \
@@ -49,12 +69,22 @@
} \
";
-static GLuint CompileShader(const char *fragment_shader_source)
+static GLuint CompileShader(const char *vertex_shader_source, const char *fragment_shader_source)
{
GLint shader_status;
GLuint program_id = glCreateProgram();
+ GLuint vertex_shader = glCreateShader(GL_VERTEX_SHADER);
+ glShaderSource(vertex_shader, 1, &vertex_shader_source, NULL);
+ glCompileShader(vertex_shader);
+
+ glGetShaderiv(vertex_shader, GL_COMPILE_STATUS, &shader_status);
+ if (shader_status != GL_TRUE)
+ return 0;
+
+ glAttachShader(program_id, vertex_shader);
+
GLuint fragment_shader = glCreateShader(GL_FRAGMENT_SHADER);
glShaderSource(fragment_shader, 1, &fragment_shader_source, NULL);
glCompileShader(fragment_shader);
@@ -134,9 +164,13 @@
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
- // Set up our colour-key-enabled fragment shader
- colour_key_program_id = CompileShader(fragment_shader_source);
+ // Set up our shaders
+ normal_program_id = CompileShader(vertex_shader_source, fragment_shader_source_normal);
+ colour_key_program_id = CompileShader(vertex_shader_source, fragment_shader_source_colour_key);
+ if (normal_program_id == 0 || colour_key_program_id == 0)
+ printf("Failed to compile shaders\n");
+
// Set up framebuffer (used for surface-to-surface blitting)
glGenFramebuffersEXT(1, &framebuffer_id);
glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, framebuffer_id);
@@ -165,7 +199,7 @@
void Backend_DrawScreen(void)
{
// Disable colour-keying
- glUseProgram(0);
+ glUseProgram(normal_program_id);
// Target actual screen, and not our framebuffer
glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
@@ -271,7 +305,7 @@
return;
// Switch to colour-key shader if we have to
- glUseProgram(colour_key ? colour_key_program_id : 0);
+ glUseProgram(colour_key ? colour_key_program_id : normal_program_id);
glBindTexture(GL_TEXTURE_2D, source_surface->texture_id);
@@ -338,7 +372,7 @@
return;
// Disable colour-keying
- glUseProgram(0);
+ glUseProgram(normal_program_id);
// Use blank default texture, for a solid colour-fill
glBindTexture(GL_TEXTURE_2D, 0);
@@ -472,7 +506,7 @@
static void DrawGlyphCommon(Backend_Glyph *glyph, long x, long y, const unsigned char *colours)
{
// Disable colour-keying
- glUseProgram(0);
+ glUseProgram(normal_program_id);
glBindTexture(GL_TEXTURE_2D, glyph->texture_id);