From 68a7bafe37adef0e5ef12c2d0e8461a21e05363b Mon Sep 17 00:00:00 2001 From: Piotr Dziwinski Date: Mon, 16 Jul 2012 19:17:26 +0200 Subject: Fixes in texture loading - added other texture formats: BGR and BGRA - fixed texture loading in model viewer - moved code from texture.cpp module to texture.h --- src/CMakeLists.txt | 1 - src/graphics/common/texture.cpp | 43 ------------------------------- src/graphics/common/texture.h | 38 ++++++++++++++++++++++++--- src/graphics/opengl/gldevice.cpp | 10 +++++-- src/graphics/opengl/test/CMakeLists.txt | 6 +++-- src/graphics/opengl/test/model_test.cpp | 5 +++- src/graphics/opengl/test/texture_test.cpp | 4 +-- 7 files changed, 52 insertions(+), 55 deletions(-) delete mode 100644 src/graphics/common/texture.cpp (limited to 'src') diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 7f52e8d..fc3cfe3 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -52,7 +52,6 @@ graphics/common/planet.cpp graphics/common/pyro.cpp graphics/common/terrain.cpp graphics/common/text.cpp -graphics/common/texture.cpp graphics/common/water.cpp graphics/opengl/gldevice.cpp graphics/opengl/glengine.cpp diff --git a/src/graphics/common/texture.cpp b/src/graphics/common/texture.cpp deleted file mode 100644 index 50e71cd..0000000 --- a/src/graphics/common/texture.cpp +++ /dev/null @@ -1,43 +0,0 @@ -// * This file is part of the COLOBOT source code -// * Copyright (C) 2012, Polish Portal of Colobot (PPC) -// * -// * 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 http://www.gnu.org/licenses/. - -// texture.cpp - -#include "graphics/common/texture.h" - - -void Gfx::TextureCreateParams::LoadDefault() -{ - alpha = false; - mipmap = false; - - minFilter = Gfx::TEX_MIN_FILTER_NEAREST; - magFilter = Gfx::TEX_MAG_FILTER_NEAREST; - - wrapS = Gfx::TEX_WRAP_REPEAT; - wrapT = Gfx::TEX_WRAP_REPEAT; -} - -void Gfx::TextureParams::LoadDefault() -{ - colorOperation = Gfx::TEX_MIX_OPER_MODULATE; - colorArg1 = Gfx::TEX_MIX_ARG_CURRENT; - colorArg2 = Gfx::TEX_MIX_ARG_TEXTURE; - - alphaOperation = Gfx::TEX_MIX_OPER_MODULATE; - alphaArg1 = Gfx::TEX_MIX_ARG_CURRENT; - alphaArg2 = Gfx::TEX_MIX_ARG_TEXTURE; -} diff --git a/src/graphics/common/texture.h b/src/graphics/common/texture.h index 55b2fc2..fa374ac 100644 --- a/src/graphics/common/texture.h +++ b/src/graphics/common/texture.h @@ -20,6 +20,17 @@ namespace Gfx { +/** + \enum TexImgFormat + \brief Format of image data */ +enum TexImgFormat +{ + TEX_IMG_RGB, + TEX_IMG_BGR, + TEX_IMG_RGBA, + TEX_IMG_BGRA +}; + /** \enum TexMinFilter \brief Minification texture filter @@ -81,10 +92,10 @@ enum TexMixArgument */ struct TextureCreateParams { - //! Whether the texture image contains alpha - bool alpha; //! Whether to generate mipmaps bool mipmap; + //! Format of source image data + Gfx::TexImgFormat format; //! Minification filter Gfx::TexMinFilter minFilter; //! Magnification filter @@ -99,7 +110,17 @@ struct TextureCreateParams { LoadDefault(); } //! Loads the default values - void LoadDefault(); + inline void LoadDefault() + { + format = Gfx::TEX_IMG_RGB; + mipmap = false; + + minFilter = Gfx::TEX_MIN_FILTER_NEAREST; + magFilter = Gfx::TEX_MAG_FILTER_NEAREST; + + wrapS = Gfx::TEX_WRAP_REPEAT; + wrapT = Gfx::TEX_WRAP_REPEAT; + } }; /** @@ -126,7 +147,16 @@ struct TextureParams { LoadDefault(); } //! Loads the default values - void LoadDefault(); + inline void LoadDefault() + { + colorOperation = Gfx::TEX_MIX_OPER_MODULATE; + colorArg1 = Gfx::TEX_MIX_ARG_CURRENT; + colorArg2 = Gfx::TEX_MIX_ARG_TEXTURE; + + alphaOperation = Gfx::TEX_MIX_OPER_MODULATE; + alphaArg1 = Gfx::TEX_MIX_ARG_CURRENT; + alphaArg2 = Gfx::TEX_MIX_ARG_TEXTURE; + } }; /** \struct Texture*/ diff --git a/src/graphics/opengl/gldevice.cpp b/src/graphics/opengl/gldevice.cpp index eb4eb31..e329ff4 100644 --- a/src/graphics/opengl/gldevice.cpp +++ b/src/graphics/opengl/gldevice.cpp @@ -393,10 +393,16 @@ Gfx::Texture* Gfx::CGLDevice::CreateTexture(CImage *image, const Gfx::TextureCre glTexParameteri(GL_TEXTURE_2D, GL_GENERATE_MIPMAP, GL_FALSE); GLenum sourceFormat = 0; - if (params.alpha) + if (params.format == Gfx::TEX_IMG_RGB) + sourceFormat = GL_RGB; + else if (params.format == Gfx::TEX_IMG_BGR) + sourceFormat = GL_BGR; + else if (params.format == Gfx::TEX_IMG_RGBA) sourceFormat = GL_RGBA; + else if (params.format == Gfx::TEX_IMG_BGRA) + sourceFormat = GL_BGRA; else - sourceFormat = GL_RGB; + assert(false); glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, data->surface->w, data->surface->h, 0, sourceFormat, GL_UNSIGNED_BYTE, data->surface->pixels); diff --git a/src/graphics/opengl/test/CMakeLists.txt b/src/graphics/opengl/test/CMakeLists.txt index 36bd738..4242c77 100644 --- a/src/graphics/opengl/test/CMakeLists.txt +++ b/src/graphics/opengl/test/CMakeLists.txt @@ -8,6 +8,8 @@ find_package(PNG REQUIRED) set(CMAKE_BUILD_TYPE debug) set(CMAKE_CXX_FLAGS_DEBUG "-Wall -g -O0") +set(ADD_LIBS "") + if (${CMAKE_SYSTEM_NAME} MATCHES "Windows") set(PLATFORM_WINDOWS 1) set(PLATFORM_LINUX 0) @@ -16,6 +18,7 @@ elseif(${CMAKE_SYSTEM_NAME} MATCHES "Linux") set(PLATFORM_WINDOWS 0) set(PLATFORM_LINUX 1) set(PLATFORM_OTHER 0) + set(ADD_LIBS "-lrt") else() set(PLATFORM_WINDOWS 0) set(PLATFORM_LINUX 0) @@ -28,7 +31,6 @@ configure_file(../../../common/config.h.cmake ${CMAKE_CURRENT_BINARY_DIR}/common set(TEXTURE_SOURCES ../gldevice.cpp ../../common/device.cpp -../../common/texture.cpp ../../../common/logger.cpp ../../../common/image.cpp texture_test.cpp @@ -38,7 +40,6 @@ set(MODEL_SOURCES ../gldevice.cpp ../../common/device.cpp ../../common/modelfile.cpp -../../common/texture.cpp ../../../common/logger.cpp ../../../common/image.cpp ../../../common/iman.cpp @@ -54,6 +55,7 @@ ${SDL_LIBRARY} ${SDLIMAGE_LIBRARY} ${OPENGL_LIBRARY} ${PNG_LIBRARIES} +${ADD_LIBS} ) add_executable(texture_test ${TEXTURE_SOURCES}) diff --git a/src/graphics/opengl/test/model_test.cpp b/src/graphics/opengl/test/model_test.cpp index b73dc71..88404b7 100644 --- a/src/graphics/opengl/test/model_test.cpp +++ b/src/graphics/opengl/test/model_test.cpp @@ -66,8 +66,11 @@ void LoadTexture(Gfx::CGLDevice *device, const std::string &name) else { Gfx::TextureCreateParams texCreateParams; - texCreateParams.alpha = false; texCreateParams.mipmap = true; + if (img.GetData()->surface->format->Amask == 0) + texCreateParams.format = Gfx::TEX_IMG_BGR; + else + texCreateParams.format = Gfx::TEX_IMG_BGRA; texCreateParams.minFilter = Gfx::TEX_MIN_FILTER_LINEAR_MIPMAP_LINEAR; texCreateParams.magFilter = Gfx::TEX_MAG_FILTER_LINEAR; texCreateParams.wrapT = Gfx::TEX_WRAP_CLAMP; diff --git a/src/graphics/opengl/test/texture_test.cpp b/src/graphics/opengl/test/texture_test.cpp index 79518f8..03847fc 100644 --- a/src/graphics/opengl/test/texture_test.cpp +++ b/src/graphics/opengl/test/texture_test.cpp @@ -27,15 +27,15 @@ void Init(Gfx::CGLDevice *device) } Gfx::TextureCreateParams tex1CreateParams; - tex1CreateParams.alpha = true; tex1CreateParams.mipmap = true; + tex1CreateParams.format = Gfx::TEX_IMG_RGBA; tex1CreateParams.minFilter = Gfx::TEX_MIN_FILTER_LINEAR_MIPMAP_LINEAR; tex1CreateParams.magFilter = Gfx::TEX_MAG_FILTER_LINEAR; tex1CreateParams.wrapT = Gfx::TEX_WRAP_CLAMP; Gfx::TextureCreateParams tex2CreateParams; - tex2CreateParams.alpha = true; tex2CreateParams.mipmap = true; + tex2CreateParams.format = Gfx::TEX_IMG_RGBA; tex2CreateParams.minFilter = Gfx::TEX_MIN_FILTER_NEAREST_MIPMAP_NEAREST; tex2CreateParams.magFilter = Gfx::TEX_MAG_FILTER_NEAREST; tex2CreateParams.wrapS = Gfx::TEX_WRAP_CLAMP; -- cgit v1.2.3-1-g7c22