From 4276bd680d3c8afec74f386a68d806bdf04a6276 Mon Sep 17 00:00:00 2001 From: Eduardo Bart Date: Sat, 28 Jan 2012 16:29:03 -0200 Subject: [PATCH] allow use of mipmaps in framebuffers --- modules/client_tibiafiles | 2 +- src/framework/graphics/framebuffer.cpp | 5 +++ src/framework/graphics/framebuffer.h | 1 + src/framework/graphics/texture.cpp | 47 ++++++++++++++++++-------- src/framework/graphics/texture.h | 7 +++- 5 files changed, 45 insertions(+), 17 deletions(-) diff --git a/modules/client_tibiafiles b/modules/client_tibiafiles index dd648e14..9beb17da 160000 --- a/modules/client_tibiafiles +++ b/modules/client_tibiafiles @@ -1 +1 @@ -Subproject commit dd648e1431171bffe091b748744395780df7eba1 +Subproject commit 9beb17daaeb170c127c39c5a5e4feb9d95ebed92 diff --git a/src/framework/graphics/framebuffer.cpp b/src/framework/graphics/framebuffer.cpp index 4dafd5af..0622f570 100644 --- a/src/framework/graphics/framebuffer.cpp +++ b/src/framework/graphics/framebuffer.cpp @@ -83,6 +83,11 @@ void FrameBuffer::release() g_graphics.setViewportSize(m_oldViewportSize); } +void FrameBuffer::generateMipmaps() +{ + m_texture->generateMipmaps(); +} + void FrameBuffer::draw(const Rect& dest) { g_painter.drawTexturedRect(dest, m_texture); diff --git a/src/framework/graphics/framebuffer.h b/src/framework/graphics/framebuffer.h index bd81d6a4..2f242db5 100644 --- a/src/framework/graphics/framebuffer.h +++ b/src/framework/graphics/framebuffer.h @@ -34,6 +34,7 @@ public: void resize(const Size& size); void bind(bool clear = true); void release(); + void generateMipmaps(); void draw(const Rect& dest); void setClearColor(const Color& color) { m_clearColor = color; } diff --git a/src/framework/graphics/texture.cpp b/src/framework/graphics/texture.cpp index 0d9cd821..b6ff7b23 100644 --- a/src/framework/graphics/texture.cpp +++ b/src/framework/graphics/texture.cpp @@ -89,29 +89,31 @@ uint Texture::internalLoadGLTexture(uchar *pixels, int channels, int width, int glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); - // nearest filtering (non smooth) - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); + setupFilters(); + return id; } +void Texture::generateMipmaps() +{ + bind(); + + if(!m_useMipmaps) { + m_useMipmaps = true; + setupFilters(); + } + + glGenerateMipmap(GL_TEXTURE_2D); +} + void Texture::setSmooth(bool smooth) { if(smooth == m_smooth) return; - if(smooth) { - // enable smooth texture - glBindTexture(GL_TEXTURE_2D, m_textureId); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); - } else { - // nearest filtering (non smooth) - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); - } - - m_smooth = true; + m_smooth = smooth; + bind(); + setupFilters(); } std::vector Texture::getPixels() @@ -125,3 +127,18 @@ std::vector Texture::getPixels() fb->release(); return pixels; } + +void Texture::setupFilters() +{ + GLint minFilter; + GLint magFilter; + if(m_smooth) { + minFilter = m_useMipmaps ? GL_LINEAR_MIPMAP_LINEAR : GL_LINEAR; + magFilter = GL_LINEAR; + } else { + minFilter = m_useMipmaps ? GL_NEAREST_MIPMAP_NEAREST : GL_NEAREST; + magFilter = GL_NEAREST; + } + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, minFilter); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, magFilter); +} diff --git a/src/framework/graphics/texture.h b/src/framework/graphics/texture.h index 095e44d7..9d0ed6e5 100644 --- a/src/framework/graphics/texture.h +++ b/src/framework/graphics/texture.h @@ -32,7 +32,10 @@ public: Texture(int width, int height, int channels, uchar* pixels = NULL); virtual ~Texture(); - virtual void setSmooth(bool smooth); + void bind() { glBindTexture(GL_TEXTURE_2D, m_textureId); } + + void generateMipmaps(); + void setSmooth(bool smooth); GLuint getId() { return m_textureId; } std::vector getPixels(); @@ -44,10 +47,12 @@ public: bool isEmpty() const { return m_textureId == 0; } protected: + void setupFilters(); GLuint internalLoadGLTexture(uchar* pixels, int channels, int w, int h); GLuint m_textureId; Size m_size; + Boolean m_useMipmaps; Boolean m_smooth; };