ResidualVM logo ResidualVM website - Forums - Contact us BuildBot - Doxygen - Wiki curved edge

opengltexture.cpp

Go to the documentation of this file.
00001 /* ResidualVM - A 3D game interpreter
00002  *
00003  * ResidualVM is the legal property of its developers, whose names
00004  * are too numerous to list here. Please refer to the AUTHORS
00005  * file distributed with this source distribution.
00006  *
00007  * This program is free software; you can redistribute it and/or
00008  * modify it under the terms of the GNU General Public License
00009  * as published by the Free Software Foundation; either version 2
00010  * of the License, or (at your option) any later version.
00011  *
00012  * This program is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015  * GNU General Public License for more details.
00016  *
00017  * You should have received a copy of the GNU General Public License
00018  * along with this program; if not, write to the Free Software
00019  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
00020  *
00021  */
00022 
00023 #include "engines/stark/gfx/opengltexture.h"
00024 
00025 #include "engines/stark/gfx/driver.h"
00026 
00027 #include "graphics/surface.h"
00028 
00029 namespace Stark {
00030 namespace Gfx {
00031 
00032 OpenGlTexture::OpenGlTexture() :
00033     Texture(),
00034     _id(0),
00035     _levelCount(0) {
00036     glGenTextures(1, &_id);
00037 
00038     bind();
00039 
00040     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
00041     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
00042 
00043     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
00044     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
00045 }
00046 
00047 OpenGlTexture::~OpenGlTexture() {
00048     glDeleteTextures(1, &_id);
00049 }
00050 
00051 void OpenGlTexture::bind() const {
00052     glBindTexture(GL_TEXTURE_2D, _id);
00053 }
00054 
00055 void OpenGlTexture::updateLevel(uint32 level, const Graphics::Surface *surface, const byte *palette) {
00056     if (level == 0) {
00057         _width = surface->w;
00058         _height = surface->h;
00059     }
00060 
00061     if (surface->format.bytesPerPixel != 4) {
00062         // Convert the surface to texture format
00063         Graphics::Surface *convertedSurface = surface->convertTo(Driver::getRGBAPixelFormat(), palette);
00064 
00065         glTexImage2D(GL_TEXTURE_2D, level, GL_RGBA, convertedSurface->w, convertedSurface->h, 0, GL_RGBA, GL_UNSIGNED_BYTE, convertedSurface->getPixels());
00066 
00067         convertedSurface->free();
00068         delete convertedSurface;
00069     } else {
00070         assert(surface->format == Driver::getRGBAPixelFormat());
00071 
00072         glTexImage2D(GL_TEXTURE_2D, level, GL_RGBA, surface->w, surface->h, 0, GL_RGBA, GL_UNSIGNED_BYTE, surface->getPixels());
00073     }
00074 }
00075 
00076 void OpenGlTexture::update(const Graphics::Surface *surface, const byte *palette) {
00077     bind();
00078     updateLevel(0, surface, palette);
00079 }
00080 
00081 void OpenGlTexture::setSamplingFilter(Texture::SamplingFilter filter) {
00082     assert(_levelCount == 0);
00083 
00084     switch (filter) {
00085     case kNearest:
00086         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
00087         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
00088         break;
00089     case kLinear:
00090         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
00091         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
00092         break;
00093     default:
00094         warning("Unhandled sampling filter %d", filter);
00095     }
00096 }
00097 
00098 void OpenGlTexture::setLevelCount(uint32 count) {
00099     _levelCount = count;
00100 
00101     if (count >= 1) {
00102 #if !defined(USE_GLES2)
00103         // GLES2 does not allow setting the max provided mipmap level.
00104         // It expects all the levels to be provided, which is not the case in TLJ.
00105         // FIXME: Enable mipmapping on GLES2
00106         glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAX_LEVEL, count - 1);
00107 
00108         glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_NEAREST);
00109         glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
00110 #endif
00111 
00112         glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_MIRRORED_REPEAT);
00113         glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_MIRRORED_REPEAT);
00114     }
00115 }
00116 
00117 void OpenGlTexture::addLevel(uint32 level, const Graphics::Surface *surface, const byte *palette) {
00118     assert(level < _levelCount);
00119 
00120 #if defined(USE_GLES2)
00121     if (level == 0)
00122 #endif
00123     {
00124         updateLevel(level, surface, palette);
00125     }
00126 }
00127 
00128 } // End of namespace Gfx
00129 } // End of namespace Stark


Generated on Sat May 25 2019 05:00:50 for ResidualVM by doxygen 1.7.1
curved edge   curved edge