diff options
author | Piotr Dziwinski <piotrdz@gmail.com> | 2012-09-29 10:40:11 +0200 |
---|---|---|
committer | Piotr Dziwinski <piotrdz@gmail.com> | 2012-09-29 10:40:11 +0200 |
commit | 677ce3960cd13cbf994311c76d75b343b22fd480 (patch) | |
tree | ee2bab43ec29cdf94d8c1173ee022fe95c2a0222 /src/graphics/opengl | |
parent | 77952a85e63ca13dd9cfc93c7b6a271d7c91e59a (diff) | |
download | colobot-677ce3960cd13cbf994311c76d75b343b22fd480.tar.gz colobot-677ce3960cd13cbf994311c76d75b343b22fd480.tar.bz2 colobot-677ce3960cd13cbf994311c76d75b343b22fd480.zip |
Some fixes and optimizations
- fixed 2nd texture setting
- added padding to some structs for faster access
- changed rendering primitives to glDrawArrays()
- optimized texture modesetting calls
- fixed some valgrind errors
Diffstat (limited to 'src/graphics/opengl')
-rw-r--r-- | src/graphics/opengl/gldevice.cpp | 172 | ||||
-rw-r--r-- | src/graphics/opengl/gldevice.h | 2 |
2 files changed, 112 insertions, 62 deletions
diff --git a/src/graphics/opengl/gldevice.cpp b/src/graphics/opengl/gldevice.cpp index bffbc3a..58b7ece 100644 --- a/src/graphics/opengl/gldevice.cpp +++ b/src/graphics/opengl/gldevice.cpp @@ -414,8 +414,6 @@ Texture CGLDevice::CreateTexture(ImageData *data, const TextureCreateParams &par glActiveTexture(GL_TEXTURE0); glEnable(GL_TEXTURE_2D); - glPixelStorei(GL_UNPACK_ALIGNMENT, 1); - glGenTextures(1, &result.id); glBindTexture(GL_TEXTURE_2D, result.id); @@ -552,10 +550,7 @@ Texture CGLDevice::CreateTexture(ImageData *data, const TextureCreateParams &par // Restore the previous state of 1st stage - if (m_currentTextures[0].Valid()) - glBindTexture(GL_TEXTURE_2D, m_currentTextures[0].id); - else - glBindTexture(GL_TEXTURE_2D, 0); + glBindTexture(GL_TEXTURE_2D, m_currentTextures[0].id); if ( (! m_texturing) || (! m_texturesEnabled[0]) ) glDisable(GL_TEXTURE_2D); @@ -565,7 +560,7 @@ Texture CGLDevice::CreateTexture(ImageData *data, const TextureCreateParams &par void CGLDevice::DestroyTexture(const Texture &texture) { - std::set<Texture>::iterator it = m_allTextures.find(texture); + auto it = m_allTextures.find(texture); if (it != m_allTextures.end()) m_allTextures.erase(it); @@ -581,10 +576,12 @@ void CGLDevice::DestroyTexture(const Texture &texture) void CGLDevice::DestroyAllTextures() { - std::set<Texture> allCopy = m_allTextures; - std::set<Texture>::iterator it; - for (it = allCopy.begin(); it != allCopy.end(); ++it) - DestroyTexture(*it); + for (auto it = m_allTextures.begin(); it != m_allTextures.end(); ++it) + glDeleteTextures(1, &(*it).id); + + // Unbind all texture stages + for (int index = 0; index < static_cast<int>( m_currentTextures.size() ); ++index) + SetTexture(index, Texture()); } int CGLDevice::GetMaxTextureCount() @@ -601,25 +598,19 @@ void CGLDevice::SetTexture(int index, const Texture &texture) assert(index >= 0); assert(index < static_cast<int>( m_currentTextures.size() )); - // Enable the given texture stage - glActiveTexture(GL_TEXTURE0 + index); - glEnable(GL_TEXTURE_2D); + bool same = m_currentTextures[index].id == texture.id; - m_currentTextures[index] = texture; // remember the change + m_currentTextures[index] = texture; // remember the new value - if (! texture.Valid()) - { - glBindTexture(GL_TEXTURE_2D, 0); // unbind texture - } - else - { - glBindTexture(GL_TEXTURE_2D, texture.id); // bind the texture - SetTextureStageParams(index, m_textureStageParams[index]); // texture stage params need to be re-set for the new texture - } + if (same) + return; // nothing to do - // Disable the stage if it is set so - if ( (! m_texturing) || (! m_texturesEnabled[index]) ) - glDisable(GL_TEXTURE_2D); + glEnable(GL_TEXTURE_2D); + glActiveTexture(GL_TEXTURE0 + index); + glBindTexture(GL_TEXTURE_2D, texture.id); + + // Params need to be updated for the new bound texture + SetTextureStageParams(index, m_textureStageParams[index]); } void CGLDevice::SetTexture(int index, unsigned int textureId) @@ -627,17 +618,17 @@ void CGLDevice::SetTexture(int index, unsigned int textureId) assert(index >= 0); assert(index < static_cast<int>( m_currentTextures.size() )); - // Enable the given texture stage - glActiveTexture(GL_TEXTURE0 + index); - glEnable(GL_TEXTURE_2D); + if (m_currentTextures[index].id == textureId) + return; // nothing to do m_currentTextures[index].id = textureId; + glEnable(GL_TEXTURE_2D); + glActiveTexture(GL_TEXTURE0 + index); glBindTexture(GL_TEXTURE_2D, textureId); - // Disable the stage if it is set so - if ( (! m_texturing) || (! m_texturesEnabled[index]) ) - glDisable(GL_TEXTURE_2D); + // Params need to be updated for the new bound texture + SetTextureStageParams(index, m_textureStageParams[index]); } /** @@ -655,8 +646,13 @@ void CGLDevice::SetTextureEnabled(int index, bool enabled) assert(index >= 0); assert(index < static_cast<int>( m_currentTextures.size() )); + bool same = m_texturesEnabled[index] == enabled; + m_texturesEnabled[index] = enabled; + if (same) + return; // nothing to do + glActiveTexture(GL_TEXTURE0 + index); if (enabled) glEnable(GL_TEXTURE_2D); @@ -689,10 +685,8 @@ void CGLDevice::SetTextureStageParams(int index, const TextureStageParams ¶m return; // Enable the given stage - glActiveTexture(GL_TEXTURE0 + index); glEnable(GL_TEXTURE_2D); - - glBindTexture(GL_TEXTURE_2D, m_currentTextures[index].id); + glActiveTexture(GL_TEXTURE0 + index); // To save some trouble if ( (params.colorOperation == TEX_MIX_OPER_DEFAULT) && @@ -810,7 +804,41 @@ after_tex_operations: else assert(false); // Disable the stage if it is set so - if ( (! m_texturing) || (! m_texturesEnabled[index]) ) + if ( (! m_texturing) || (! m_texturesEnabled[0]) ) + glDisable(GL_TEXTURE_2D); +} + +void CGLDevice::SetTextureStageWrap(int index, TexWrapMode wrapS, TexWrapMode wrapT) +{ + assert(index >= 0); + assert(index < static_cast<int>( m_currentTextures.size() )); + + // Remember the settings + m_textureStageParams[index].wrapS = wrapS; + m_textureStageParams[index].wrapT = wrapT; + + // Don't actually do anything if texture not set + if (! m_currentTextures[index].Valid()) + return; + + // Enable the given stage + glEnable(GL_TEXTURE_2D); + glActiveTexture(GL_TEXTURE0 + index); + + if (wrapS == TEX_WRAP_CLAMP) + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP); + else if (wrapS == TEX_WRAP_REPEAT) + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); + else assert(false); + + if (wrapT == TEX_WRAP_CLAMP) + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP); + else if (wrapT == TEX_WRAP_REPEAT) + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); + else assert(false); + + // Disable the stage if it is set so + if ( (! m_texturing) || (! m_texturesEnabled[0]) ) glDisable(GL_TEXTURE_2D); } @@ -872,48 +900,68 @@ GLenum TranslateGfxPrimitive(PrimitiveType type) void CGLDevice::DrawPrimitive(PrimitiveType type, const Vertex *vertices, int vertexCount) { - glBegin(TranslateGfxPrimitive(type)); + Vertex* vs = const_cast<Vertex*>(vertices); + + glEnableClientState(GL_VERTEX_ARRAY); + glVertexPointer(3, GL_FLOAT, sizeof(Vertex), reinterpret_cast<GLfloat*>(&vs[0].coord)); + + glEnableClientState(GL_NORMAL_ARRAY); + glNormalPointer(GL_FLOAT, sizeof(Vertex), reinterpret_cast<GLfloat*>(&vs[0].normal)); + + glClientActiveTexture(GL_TEXTURE0); + glEnableClientState(GL_TEXTURE_COORD_ARRAY); + glTexCoordPointer(2, GL_FLOAT, sizeof(Vertex), reinterpret_cast<GLfloat*>(&vs[0].texCoord)); glColor3f(1.0f, 1.0f, 1.0f); - for (int i = 0; i < vertexCount; ++i) - { - glNormal3fv(const_cast<GLfloat*>(vertices[i].normal.Array())); - glMultiTexCoord2fv(GL_TEXTURE0, const_cast<GLfloat*>(vertices[i].texCoord.Array())); - glVertex3fv(const_cast<GLfloat*>(vertices[i].coord.Array())); - } + glDrawArrays(TranslateGfxPrimitive(type), 0, vertexCount); - glEnd(); + glDisableClientState(GL_VERTEX_ARRAY); + glDisableClientState(GL_NORMAL_ARRAY); + glDisableClientState(GL_TEXTURE_COORD_ARRAY); // GL_TEXTURE0 } void CGLDevice::DrawPrimitive(PrimitiveType type, const VertexCol *vertices, int vertexCount) { - glBegin(TranslateGfxPrimitive(type)); + VertexCol* vs = const_cast<VertexCol*>(vertices); - for (int i = 0; i < vertexCount; ++i) - { - glColor4fv(const_cast<GLfloat*>(vertices[i].color.Array())); - glVertex3fv(const_cast<GLfloat*>(vertices[i].coord.Array())); - } + glEnableClientState(GL_VERTEX_ARRAY); + glVertexPointer(3, GL_FLOAT, sizeof(VertexCol), reinterpret_cast<GLfloat*>(&vs[0].coord)); - glEnd(); + glEnableClientState(GL_COLOR_ARRAY); + glColorPointer(4, GL_FLOAT, sizeof(VertexCol), reinterpret_cast<GLfloat*>(&vs[0].color)); + + glDrawArrays(TranslateGfxPrimitive(type), 0, vertexCount); + + glDisableClientState(GL_VERTEX_ARRAY); + glDisableClientState(GL_COLOR_ARRAY); } void CGLDevice::DrawPrimitive(PrimitiveType type, const VertexTex2 *vertices, int vertexCount) { - glBegin(TranslateGfxPrimitive(type)); + VertexTex2* vs = const_cast<VertexTex2*>(vertices); - glColor3f(1.0f, 1.0f, 1.0f); + glEnableClientState(GL_VERTEX_ARRAY); + glVertexPointer(3, GL_FLOAT, sizeof(VertexTex2), reinterpret_cast<GLfloat*>(&vs[0].coord)); - for (int i = 0; i < vertexCount; ++i) - { - glNormal3fv(const_cast<GLfloat*>(vertices[i].normal.Array())); - glMultiTexCoord2fv(GL_TEXTURE0, const_cast<GLfloat*>(vertices[i].texCoord.Array())); - glMultiTexCoord2fv(GL_TEXTURE1, const_cast<GLfloat*>(vertices[i].texCoord.Array())); - glVertex3fv(const_cast<GLfloat*>(vertices[i].coord.Array())); - } + glEnableClientState(GL_NORMAL_ARRAY); + glNormalPointer(GL_FLOAT, sizeof(VertexTex2), reinterpret_cast<GLfloat*>(&vs[0].normal)); + + glClientActiveTexture(GL_TEXTURE0); + glEnableClientState(GL_TEXTURE_COORD_ARRAY); + glTexCoordPointer(2, GL_FLOAT, sizeof(VertexTex2), reinterpret_cast<GLfloat*>(&vs[0].texCoord)); + + glClientActiveTexture(GL_TEXTURE1); + glEnableClientState(GL_TEXTURE_COORD_ARRAY); + glTexCoordPointer(2, GL_FLOAT, sizeof(VertexTex2), reinterpret_cast<GLfloat*>(&vs[0].texCoord2)); + + glDrawArrays(TranslateGfxPrimitive(type), 0, vertexCount); - glEnd(); + glDisableClientState(GL_VERTEX_ARRAY); + glDisableClientState(GL_NORMAL_ARRAY); + glDisableClientState(GL_TEXTURE_COORD_ARRAY); // GL_TEXTURE1 + glClientActiveTexture(GL_TEXTURE0); + glDisableClientState(GL_TEXTURE_COORD_ARRAY); } bool InPlane(Math::Vector normal, float originPlane, Math::Vector center, float radius) diff --git a/src/graphics/opengl/gldevice.h b/src/graphics/opengl/gldevice.h index 1274ee9..282f141 100644 --- a/src/graphics/opengl/gldevice.h +++ b/src/graphics/opengl/gldevice.h @@ -117,6 +117,8 @@ public: virtual void SetTextureStageParams(int index, const TextureStageParams ¶ms); virtual TextureStageParams GetTextureStageParams(int index); + virtual void SetTextureStageWrap(int index, Gfx::TexWrapMode wrapS, Gfx::TexWrapMode wrapT); + virtual void SetTextureFactor(const Color &color); virtual Color GetTextureFactor(); |