From aecc708942f23e5591391e28a3c60e9dc19a9a28 Mon Sep 17 00:00:00 2001 From: Mark Vejvoda Date: Tue, 6 Jul 2010 13:26:23 +0000 Subject: [PATCH] - attempt to improve rendering speed --- source/glest_game/graphics/renderer.cpp | 2 + .../sources/graphics/gl/model_renderer_gl.cpp | 227 ++++++++++++++++++ .../sources/platform/sdl/gl_wrap.cpp | 2 + .../sources/platform/sdl/window.cpp | 1 + 4 files changed, 232 insertions(+) create mode 100644 source/shared_lib/sources/graphics/gl/model_renderer_gl.cpp diff --git a/source/glest_game/graphics/renderer.cpp b/source/glest_game/graphics/renderer.cpp index 0f574b27..ff75565c 100644 --- a/source/glest_game/graphics/renderer.cpp +++ b/source/glest_game/graphics/renderer.cpp @@ -265,6 +265,8 @@ void Renderer::initGame(const Game *game){ glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); + glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE); + if(shadows==sShadowMapping){ SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__); diff --git a/source/shared_lib/sources/graphics/gl/model_renderer_gl.cpp b/source/shared_lib/sources/graphics/gl/model_renderer_gl.cpp new file mode 100644 index 00000000..260df41a --- /dev/null +++ b/source/shared_lib/sources/graphics/gl/model_renderer_gl.cpp @@ -0,0 +1,227 @@ +// ============================================================== +// This file is part of Glest Shared Library (www.glest.org) +// +// Copyright (C) 2001-2008 Martio Figueroa +// +// You can redistribute this code and/or modify it under +// the terms of the GNU General Public License as published +// by the Free Software Foundation; either version 2 of the +// License, or (at your option) any later version +// ============================================================== + +#include "model_renderer_gl.h" + +#include "opengl.h" +#include "gl_wrap.h" +#include "texture_gl.h" +#include "interpolation.h" +#include "leak_dumper.h" + +using namespace Shared::Platform; + +namespace Shared{ namespace Graphics{ namespace Gl{ + +// ===================================================== +// class MyClass +// ===================================================== + +// ===================== PUBLIC ======================== + +ModelRendererGl::ModelRendererGl(){ + rendering= false; + duplicateTexCoords= false; + secondaryTexCoordUnit= 1; +} + +void ModelRendererGl::begin(bool renderNormals, bool renderTextures, bool renderColors, MeshCallback *meshCallback){ + //assertions + assert(!rendering); + assertGl(); + + this->renderTextures= renderTextures; + this->renderNormals= renderNormals; + this->renderColors= renderColors; + this->meshCallback= meshCallback; + + rendering= true; + lastTexture= 0; + glBindTexture(GL_TEXTURE_2D, 0); + + //push attribs + glPushAttrib(GL_ENABLE_BIT | GL_LIGHTING_BIT | GL_POLYGON_BIT | GL_CURRENT_BIT | GL_TEXTURE_BIT); + glPushClientAttrib(GL_CLIENT_VERTEX_ARRAY_BIT); + + //init opengl + glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); + glBindTexture(GL_TEXTURE_2D, 0); + glFrontFace(GL_CCW); + glEnable(GL_NORMALIZE); + glEnable(GL_BLEND); + + glEnableClientState(GL_VERTEX_ARRAY); + + if(renderNormals){ + glEnableClientState(GL_NORMAL_ARRAY); + } + + if(renderTextures){ + glEnableClientState(GL_TEXTURE_COORD_ARRAY); + } + +/* + glHint( GL_LINE_SMOOTH_HINT, GL_FASTEST ); + glHint( GL_FRAGMENT_SHADER_DERIVATIVE_HINT, GL_FASTEST ); + glHint( GL_GENERATE_MIPMAP_HINT, GL_FASTEST ); + glHint( GL_PERSPECTIVE_CORRECTION_HINT, GL_FASTEST ); + glHint( GL_POINT_SMOOTH_HINT, GL_FASTEST ); + glHint( GL_POLYGON_SMOOTH_HINT, GL_FASTEST ); + glHint( GL_TEXTURE_COMPRESSION_HINT, GL_FASTEST ); +*/ + //assertions + assertGl(); +} + +void ModelRendererGl::end(){ + //assertions + assert(rendering); + assertGl(); + + //set render state + rendering= false; + + //pop + glPopAttrib(); + glPopClientAttrib(); + + //assertions + assertGl(); +} + +void ModelRendererGl::render(const Model *model){ + //assertions + assert(rendering); + assertGl(); + + //render every mesh + for(uint32 i=0; igetMeshCount(); ++i){ + renderMesh(model->getMesh(i)); + } + + //assertions + assertGl(); +} + +void ModelRendererGl::renderNormalsOnly(const Model *model){ + //assertions + assert(rendering); + assertGl(); + + //render every mesh + for(uint32 i=0; igetMeshCount(); ++i){ + renderMeshNormals(model->getMesh(i)); + } + + //assertions + assertGl(); +} + +// ===================== PRIVATE ======================= + +void ModelRendererGl::renderMesh(const Mesh *mesh){ + + //assertions + assertGl(); + + //set cull face + if(mesh->getTwoSided()){ + glDisable(GL_CULL_FACE); + } + else{ + glEnable(GL_CULL_FACE); + } + + //set color + if(renderColors){ + Vec4f color(mesh->getDiffuseColor(), mesh->getOpacity()); + glColor4fv(color.ptr()); + } + + //texture state + const Texture2DGl *texture= static_cast(mesh->getTexture(mtDiffuse)); + if(texture != NULL && renderTextures){ + if(lastTexture != texture->getHandle()){ + assert(glIsTexture(texture->getHandle())); + glBindTexture(GL_TEXTURE_2D, texture->getHandle()); + lastTexture= texture->getHandle(); + } + } + else{ + glBindTexture(GL_TEXTURE_2D, 0); + lastTexture= 0; + } + + if(meshCallback!=NULL){ + meshCallback->execute(mesh); + } + + //misc vars + uint32 vertexCount= mesh->getVertexCount(); + uint32 indexCount= mesh->getIndexCount(); + + //assertions + assertGl(); + + //vertices + glVertexPointer(3, GL_FLOAT, 0, mesh->getInterpolationData()->getVertices()); + + //normals + if(renderNormals){ + glEnableClientState(GL_NORMAL_ARRAY); + glNormalPointer(GL_FLOAT, 0, mesh->getInterpolationData()->getNormals()); + } + else{ + glDisableClientState(GL_NORMAL_ARRAY); + } + + //tex coords + if(renderTextures && mesh->getTexture(mtDiffuse)!=NULL ){ + if(duplicateTexCoords){ + glActiveTexture(GL_TEXTURE0 + secondaryTexCoordUnit); + glEnableClientState(GL_TEXTURE_COORD_ARRAY); + glTexCoordPointer(2, GL_FLOAT, 0, mesh->getTexCoords()); + } + + glActiveTexture(GL_TEXTURE0); + glEnableClientState(GL_TEXTURE_COORD_ARRAY); + glTexCoordPointer(2, GL_FLOAT, 0, mesh->getTexCoords()); + } + else{ + if(duplicateTexCoords){ + glActiveTexture(GL_TEXTURE0 + secondaryTexCoordUnit); + glDisableClientState(GL_TEXTURE_COORD_ARRAY); + } + glActiveTexture(GL_TEXTURE0); + glDisableClientState(GL_TEXTURE_COORD_ARRAY); + } + + //draw model + glDrawRangeElements(GL_TRIANGLES, 0, vertexCount-1, indexCount, GL_UNSIGNED_INT, mesh->getIndices()); + + //assertions + assertGl(); +} + +void ModelRendererGl::renderMeshNormals(const Mesh *mesh){ + + glBegin(GL_LINES); + for(int i= 0; igetIndexCount(); ++i){ + Vec3f vertex= mesh->getInterpolationData()->getVertices()[mesh->getIndices()[i]]; + Vec3f normal= vertex + mesh->getInterpolationData()->getNormals()[mesh->getIndices()[i]]; + + glVertex3fv(vertex.ptr()); + glVertex3fv(normal.ptr()); + } + glEnd(); +} + +}}}//end namespace diff --git a/source/shared_lib/sources/platform/sdl/gl_wrap.cpp b/source/shared_lib/sources/platform/sdl/gl_wrap.cpp index 2758831a..b9a23c42 100644 --- a/source/shared_lib/sources/platform/sdl/gl_wrap.cpp +++ b/source/shared_lib/sources/platform/sdl/gl_wrap.cpp @@ -52,6 +52,8 @@ void PlatformContextGl::init(int colorBits, int depthBits, int stencilBits) { Window::setIsFullScreen(false); } + //flags |= SDL_HWSURFACE + int resW = PlatformCommon::Private::ScreenWidth; int resH = PlatformCommon::Private::ScreenHeight; diff --git a/source/shared_lib/sources/platform/sdl/window.cpp b/source/shared_lib/sources/platform/sdl/window.cpp index 5029fe34..aa3bd613 100644 --- a/source/shared_lib/sources/platform/sdl/window.cpp +++ b/source/shared_lib/sources/platform/sdl/window.cpp @@ -346,6 +346,7 @@ void Window::setupGraphicsScreen(int depthBits, int stencilBits) { if(stencilBits >= 0) newStencilBits = stencilBits; + //SDL_GL_SetAttribute(SDL_GL_ACCELERATED_VISUAL, 1); SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1); SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 1); SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 1);