From 76707bbac82f33ee2233956a6bf54def49a86b85 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Einar=20Johan=20Tr=C3=B8an=20S=C3=B8ma=CC=8Aen?= Date: Sun, 23 Apr 2023 22:43:47 +0200 Subject: [PATCH] WATCHMAKER: Move GL Texture logic to its own file --- engines/watchmaker/3d/render/opengl_2d.cpp | 22 +---- engines/watchmaker/3d/render/opengl_3d.cpp | 63 +----------- .../watchmaker/3d/render/opengl_renderer.cpp | 18 ++++ .../watchmaker/3d/render/opengl_renderer.h | 3 + .../watchmaker/3d/render/opengl_texture.cpp | 95 +++++++++++++++++++ engines/watchmaker/3d/render/opengl_texture.h | 32 +++++++ engines/watchmaker/module.mk | 1 + 7 files changed, 152 insertions(+), 82 deletions(-) create mode 100644 engines/watchmaker/3d/render/opengl_texture.cpp create mode 100644 engines/watchmaker/3d/render/opengl_texture.h diff --git a/engines/watchmaker/3d/render/opengl_2d.cpp b/engines/watchmaker/3d/render/opengl_2d.cpp index a1240524de1..8d0b159c47d 100644 --- a/engines/watchmaker/3d/render/opengl_2d.cpp +++ b/engines/watchmaker/3d/render/opengl_2d.cpp @@ -141,24 +141,6 @@ bool gClipToBlitterViewport(int *sposx, int *sposy, int *sdimx, int *sdimy, return true; } -void enter2Dmode(WGame &game) { - glMatrixMode(GL_PROJECTION); - glPushMatrix(); - glLoadIdentity(); - glMatrixMode(GL_MODELVIEW); - glPushMatrix(); - glLoadIdentity(); - checkGlError("Exiting enter2Dmode"); -} - -void exit2Dmode() { - glPopMatrix(); - glMatrixMode(GL_PROJECTION); - glPopMatrix(); - glMatrixMode(GL_MODELVIEW); - checkGlError("exit2Dmode"); -} - void renderTexture(WGame &game, gTexture &bitmap, Rect srcRect, Rect dstRect) { checkGlError("Entering renderTexture"); glClearColor(0, 0, 1, 0); @@ -216,9 +198,9 @@ void gTexture::render(WGame &game, Rect src, Rect dst) { void Renderer::blitScreenBuffer() { checkGlError("Entering rBlitScreenBuffer"); - enter2Dmode(*_game); + g_renderer->enter2Dmode(); _bitmapList.bitmaps[BACK_BUFFER].render(*_game, _game->_renderer->_viewport, _game->_renderer->_viewport); - exit2Dmode(); + g_renderer->exit2Dmode(); checkGlError("Exiting rBlitScreenBuffer"); } diff --git a/engines/watchmaker/3d/render/opengl_3d.cpp b/engines/watchmaker/3d/render/opengl_3d.cpp index 593aea594d7..5898454e6e7 100644 --- a/engines/watchmaker/3d/render/opengl_3d.cpp +++ b/engines/watchmaker/3d/render/opengl_3d.cpp @@ -553,63 +553,6 @@ gTexture *gUserTexture(Texture *texture, unsigned int dimx, unsigned int dimy) { return Texture; } -GLuint dxtCompressionToTextureFormat(DxtCompression compression) { - switch (compression) { - case DxtCompression::UNCOMPRESSED: - return GL_RGBA; - case DxtCompression::DXT1: - return GL_COMPRESSED_RGBA_S3TC_DXT1_EXT; - case DxtCompression::DXT2: - error("DXT2 Support is not implemented"); - case DxtCompression::DXT3: - return GL_COMPRESSED_RGBA_S3TC_DXT3_EXT; - case DxtCompression::DXT4: - error("DXT4 Support is not implemented"); - case DxtCompression::DXT5: - return GL_COMPRESSED_RGBA_S3TC_DXT5_EXT; - } -} - -class OpenGLTexture : public Texture { -public: - unsigned int _texId; - OpenGLTexture() { - glGenTextures(1, &_texId); - } - OpenGLTexture(unsigned int texId) : _texId(texId) {} - void assignData(const TextureData &data) override { - glBindTexture(GL_TEXTURE_2D, _texId); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP); - - // TODO: Check both compiletime and runtime for the existence of EXT_texture_compression_s3tc - GLuint texFormat = dxtCompressionToTextureFormat(data._compression); - bool compressed = data._compression != DxtCompression::UNCOMPRESSED; - - if (compressed) { - glCompressedTexImage2D(GL_TEXTURE_2D, // target - 0, // level - texFormat, // internalFormat - data.getWidth(), // width - data.getHeight(), // height - 0, // border - data.getDataSize(), - data.getData() - ); - checkGlError("glCompressedTexImage"); - } else { - glTexImage2D(GL_TEXTURE_2D, 0, texFormat, data.getWidth(), data.getHeight(), 0, GL_RGBA, GL_UNSIGNED_BYTE, data.getData()); - checkGlError("glTexImage2D"); - } - } - void bind() override { - glBindTexture(GL_TEXTURE_2D, _texId); - checkGlError("OpenGLTexture::bind"); - }; -}; - class SurfaceBackedTextureData : public TextureData { bool _owned = true; public: @@ -635,10 +578,6 @@ public: } }; -Texture *createGLTexture() { - return new OpenGLTexture(); -} - Common::SharedPtr createTextureFromSurface(Graphics::Surface &surface, int texFormat) { return Common::SharedPtr(new SurfaceBackedTextureData(&surface, false)); } @@ -710,7 +649,7 @@ gTexture *gLoadTexture(WorkDirs &workDirs, const char *TextName, unsigned int Lo } texture = &gTextureList[pos]; *texture = gTexture(); - texture->_texture = new OpenGLTexture(); + texture->_texture = createGLTexture(); if (bUseAlternate) { auto stream = workDirs.resolveFile(AlternateName); diff --git a/engines/watchmaker/3d/render/opengl_renderer.cpp b/engines/watchmaker/3d/render/opengl_renderer.cpp index 55b82116bac..0608fb25a06 100644 --- a/engines/watchmaker/3d/render/opengl_renderer.cpp +++ b/engines/watchmaker/3d/render/opengl_renderer.cpp @@ -209,6 +209,24 @@ void OpenGLRenderer::setBlendFunc(BlendFactor src, BlendFactor dst) { glBlendFunc(translateBlendFactorToGL(src), translateBlendFactorToGL(dst)); } +void OpenGLRenderer::enter2Dmode() { + glMatrixMode(GL_PROJECTION); + glPushMatrix(); + glLoadIdentity(); + glMatrixMode(GL_MODELVIEW); + glPushMatrix(); + glLoadIdentity(); + checkGlError("Exiting enter2Dmode"); +} + +void OpenGLRenderer::exit2Dmode() { + glPopMatrix(); + glMatrixMode(GL_PROJECTION); + glPopMatrix(); + glMatrixMode(GL_MODELVIEW); + checkGlError("exit2Dmode"); +} + } // End of namespace Watchmaker #endif // USE_OPENGL_GAME diff --git a/engines/watchmaker/3d/render/opengl_renderer.h b/engines/watchmaker/3d/render/opengl_renderer.h index 8e294e210e6..69d967cbe46 100644 --- a/engines/watchmaker/3d/render/opengl_renderer.h +++ b/engines/watchmaker/3d/render/opengl_renderer.h @@ -72,6 +72,9 @@ Texture *createGLTexture(); class OpenGLRenderer { public: + void enter2Dmode(); + void exit2Dmode(); + void pushModelView(); void popModelView(); void setTransformMatrix(TransformMatrix which, const Matrix4x4 &matrix); diff --git a/engines/watchmaker/3d/render/opengl_texture.cpp b/engines/watchmaker/3d/render/opengl_texture.cpp new file mode 100644 index 00000000000..d30ff3d7f27 --- /dev/null +++ b/engines/watchmaker/3d/render/opengl_texture.cpp @@ -0,0 +1,95 @@ +/* ScummVM - Graphic Adventure Engine +* +* ScummVM is the legal property of its developers, whose names +* are too numerous to list here. Please refer to the COPYRIGHT +* file distributed with this source distribution. +* +* This program is free software: you can redistribute it and/or modify +* it under the terms of the GNU General Public License as published by +* the Free Software Foundation, either version 3 of the License, or +* (at your option) any later version. +* +* This program is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU General Public License for more details. +* +* You should have received a copy of the GNU General Public License +* along with this program. If not, see . +* + */ + +#include "watchmaker/3d/render/opengl_texture.h" +#include "common/textconsole.h" +#include "graphics/opengl/system_headers.h" +#include "watchmaker/3d/dds_header.h" +#include "watchmaker/render.h" + +#ifdef USE_OPENGL_GAME + +namespace Watchmaker { + +GLuint dxtCompressionToTextureFormat(DxtCompression compression) { + switch (compression) { + case DxtCompression::UNCOMPRESSED: + return GL_RGBA; + case DxtCompression::DXT1: + return GL_COMPRESSED_RGBA_S3TC_DXT1_EXT; + case DxtCompression::DXT2: + error("DXT2 Support is not implemented"); + case DxtCompression::DXT3: + return GL_COMPRESSED_RGBA_S3TC_DXT3_EXT; + case DxtCompression::DXT4: + error("DXT4 Support is not implemented"); + case DxtCompression::DXT5: + return GL_COMPRESSED_RGBA_S3TC_DXT5_EXT; + } +} + +class OpenGLTexture : public Texture { +public: + unsigned int _texId; + OpenGLTexture() { + glGenTextures(1, &_texId); + } + OpenGLTexture(unsigned int texId) : _texId(texId) {} + void assignData(const TextureData &data) override { + glBindTexture(GL_TEXTURE_2D, _texId); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP); + + // TODO: Check both compiletime and runtime for the existence of EXT_texture_compression_s3tc + GLuint texFormat = dxtCompressionToTextureFormat(data._compression); + bool compressed = data._compression != DxtCompression::UNCOMPRESSED; + + if (compressed) { + glCompressedTexImage2D(GL_TEXTURE_2D, // target + 0, // level + texFormat, // internalFormat + data.getWidth(), // width + data.getHeight(), // height + 0, // border + data.getDataSize(), + data.getData() + ); + checkGlError("glCompressedTexImage"); + } else { + glTexImage2D(GL_TEXTURE_2D, 0, texFormat, data.getWidth(), data.getHeight(), 0, GL_RGBA, GL_UNSIGNED_BYTE, data.getData()); + checkGlError("glTexImage2D"); + } + } + void bind() override { + glBindTexture(GL_TEXTURE_2D, _texId); + checkGlError("OpenGLTexture::bind"); + }; +}; + +Texture *createGLTexture() { + return new OpenGLTexture(); +} + +} // End of namespace Watchmaker + +#endif // USE_OPENGL_GAME diff --git a/engines/watchmaker/3d/render/opengl_texture.h b/engines/watchmaker/3d/render/opengl_texture.h new file mode 100644 index 00000000000..8822b997026 --- /dev/null +++ b/engines/watchmaker/3d/render/opengl_texture.h @@ -0,0 +1,32 @@ +/* ScummVM - Graphic Adventure Engine +* +* ScummVM is the legal property of its developers, whose names +* are too numerous to list here. Please refer to the COPYRIGHT +* file distributed with this source distribution. +* +* This program is free software: you can redistribute it and/or modify +* it under the terms of the GNU General Public License as published by +* the Free Software Foundation, either version 3 of the License, or +* (at your option) any later version. +* +* This program is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU General Public License for more details. +* +* You should have received a copy of the GNU General Public License +* along with this program. If not, see . +* + */ + +#ifndef WATCHMAKER_3D_OPENGL_TEXTURE_H +#define WATCHMAKER_3D_OPENGL_TEXTURE_H + +namespace Watchmaker { + +class Texture; +Texture *createGLTexture(); + +} // End of namespace Watchmaker + +#endif // WATCHMAKER_3D_OPENGL_TEXTURE_H diff --git a/engines/watchmaker/module.mk b/engines/watchmaker/module.mk index 6bc8ca25795..e94b5eebc75 100644 --- a/engines/watchmaker/module.mk +++ b/engines/watchmaker/module.mk @@ -15,6 +15,7 @@ MODULE_OBJS = \ 3d/render/opengl_2d.o \ 3d/render/opengl_3d.o \ 3d/render/opengl_renderer.o \ + 3d/render/opengl_texture.o \ 3d/render/render.o \ 3d/render/shadows.o \ 3d/t3d_body.o \