From d6888040a2db4065a2550c147f754258e8ac0a5c Mon Sep 17 00:00:00 2001 From: Clownacy Date: Wed, 24 Jul 2019 22:08:37 +0100 Subject: [PATCH] Backport some OpenGL 2.1 fixes --- src/Backends/Rendering/OpenGL2.cpp | 45 ++++++++++++++++++------------ 1 file changed, 27 insertions(+), 18 deletions(-) diff --git a/src/Backends/Rendering/OpenGL2.cpp b/src/Backends/Rendering/OpenGL2.cpp index 60a0c9fb..ff764b92 100644 --- a/src/Backends/Rendering/OpenGL2.cpp +++ b/src/Backends/Rendering/OpenGL2.cpp @@ -71,6 +71,16 @@ static GLuint CompileShader(const char *fragment_shader_source) return program_id; } +static void SetRenderTarget(Backend_Surface *surface) +{ + glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT, GL_TEXTURE_2D, surface->texture_id, 0); + + glViewport(0, 0, surface->width, surface->height); + + glLoadIdentity(); + glOrtho(0.0, surface->width, 0.0, surface->height, 1.0, -1.0); +} + SDL_Window* Backend_CreateWindow(const char *title, int width, int height) { SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_COMPATIBILITY); @@ -84,8 +94,8 @@ BOOL Backend_Init(SDL_Window *p_window) { window = p_window; - int screen_width, screen_height; - SDL_GetWindowSize(window, &screen_width, &screen_height); + int window_width, window_height; + SDL_GetWindowSize(window, &window_width, &window_height); context = SDL_GL_CreateContext(window); @@ -96,12 +106,6 @@ BOOL Backend_Init(SDL_Window *p_window) if (!GLEW_EXT_framebuffer_object) return FALSE; -// glMatrixMode(GL_MODELVIEW); - glLoadIdentity(); - glOrtho(0.0, screen_width, 0.0, screen_height, 1.0, -1.0); - -// glMatrixMode(GL_PROJECTION); -// glLoadIdentity(); glEnable(GL_TEXTURE_2D); glEnable(GL_BLEND); @@ -127,12 +131,12 @@ BOOL Backend_Init(SDL_Window *p_window) // Set up framebuffer screen texture (used for screen-to-surface blitting) glGenTextures(1, &framebuffer_surface.texture_id); glBindTexture(GL_TEXTURE_2D, framebuffer_surface.texture_id); - glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB8, screen_width, screen_height, 0, GL_RGB, GL_UNSIGNED_BYTE, NULL); + glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB8, window_width, window_height, 0, GL_RGB, GL_UNSIGNED_BYTE, NULL); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); - framebuffer_surface.width = screen_width; - framebuffer_surface.height = screen_height; + framebuffer_surface.width = window_width; + framebuffer_surface.height = window_height; return TRUE; } @@ -153,6 +157,11 @@ void Backend_DrawScreen(void) // Target actual screen, and not our framebuffer glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0); + glViewport(0, 0, framebuffer_surface.width, framebuffer_surface.height); + + glLoadIdentity(); + glOrtho(0.0, framebuffer_surface.width, 0.0, framebuffer_surface.height, 1.0, -1.0); + // Draw framebuffer to screen glPushMatrix(); glLoadIdentity(); @@ -248,7 +257,7 @@ static void BlitCommon(Backend_Surface *source_surface, const RECT *rect, long x void Backend_Blit(Backend_Surface *source_surface, const RECT *rect, Backend_Surface *destination_surface, long x, long y, BOOL colour_key) { // Point our framebuffer to the destination texture - glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT, GL_TEXTURE_2D, destination_surface->texture_id, 0); + SetRenderTarget(destination_surface); BlitCommon(source_surface, rect, x, y, colour_key); } @@ -256,7 +265,7 @@ void Backend_Blit(Backend_Surface *source_surface, const RECT *rect, Backend_Sur void Backend_BlitToScreen(Backend_Surface *source_surface, const RECT *rect, long x, long y, BOOL colour_key) { // Point our framebuffer to the screen texture - glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT, GL_TEXTURE_2D, framebuffer_surface.texture_id, 0); + SetRenderTarget(&framebuffer_surface); BlitCommon(source_surface, rect, x, y, colour_key); } @@ -284,7 +293,7 @@ static void ColourFillCommon(const RECT *rect, unsigned char red, unsigned char void Backend_ColourFill(Backend_Surface *surface, const RECT *rect, unsigned char red, unsigned char green, unsigned char blue) { // Point our framebuffer to the destination texture - glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT, GL_TEXTURE_2D, surface->texture_id, 0); + SetRenderTarget(surface); ColourFillCommon(rect, red, green, blue); } @@ -292,7 +301,7 @@ void Backend_ColourFill(Backend_Surface *surface, const RECT *rect, unsigned cha void Backend_ColourFillToScreen(const RECT *rect, unsigned char red, unsigned char green, unsigned char blue) { // Point our framebuffer to the screen texture - glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT, GL_TEXTURE_2D, framebuffer_surface.texture_id, 0); + SetRenderTarget(&framebuffer_surface); ColourFillCommon(rect, red, green, blue); } @@ -300,7 +309,7 @@ void Backend_ColourFillToScreen(const RECT *rect, unsigned char red, unsigned ch void Backend_ScreenToSurface(Backend_Surface *surface, const RECT *rect) { // Point our framebuffer to the destination texture - glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT, GL_TEXTURE_2D, surface->texture_id, 0); + SetRenderTarget(surface); BlitCommon(&framebuffer_surface, rect, rect->left, rect->top, FALSE); } @@ -407,7 +416,7 @@ static void DrawGlyphCommon(Backend_Glyph *glyph, long x, long y, const unsigned void Backend_DrawGlyph(Backend_Surface *surface, Backend_Glyph *glyph, long x, long y, const unsigned char *colours) { // Point our framebuffer to the destination texture - glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT, GL_TEXTURE_2D, surface->texture_id, 0); + SetRenderTarget(surface); DrawGlyphCommon(glyph, x, y, colours); } @@ -415,7 +424,7 @@ void Backend_DrawGlyph(Backend_Surface *surface, Backend_Glyph *glyph, long x, l void Backend_DrawGlyphToScreen(Backend_Glyph *glyph, long x, long y, const unsigned char *colours) { // Point our framebuffer to the screen texture - glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT, GL_TEXTURE_2D, framebuffer_surface.texture_id, 0); + SetRenderTarget(&framebuffer_surface); DrawGlyphCommon(glyph, x, y, colours); }