From 2a980ecb5049d1c9ee8ebceafc709ce2a0b9fb78 Mon Sep 17 00:00:00 2001 From: "florent.teppe" Date: Sun, 28 Aug 2022 16:26:11 +0200 Subject: [PATCH] cleaned some code and fixed some naming issues Moved debug draw to components, fixed some whitespace issues, added include guard fixed uniform name, removed old files Fixes some more whitespace weirdness --- apps/openmw/CMakeLists.txt | 2 +- apps/openmw/mwmechanics/actors.cpp | 5 +- apps/openmw/mwrender/renderingmanager.cpp | 5 +- components/CMakeLists.txt | 2 +- .../debug}/debugdraw.cpp | 86 ++++++------------- .../debug}/debugdraw.hpp | 23 ++--- files/shaders/debugDraw_vertex.glsl | 9 +- 7 files changed, 50 insertions(+), 82 deletions(-) rename {apps/openmw/mwrender => components/debug}/debugdraw.cpp (83%) rename {apps/openmw/mwrender => components/debug}/debugdraw.hpp (80%) diff --git a/apps/openmw/CMakeLists.txt b/apps/openmw/CMakeLists.txt index 4f45c9bee2..13fb717147 100644 --- a/apps/openmw/CMakeLists.txt +++ b/apps/openmw/CMakeLists.txt @@ -23,7 +23,7 @@ add_openmw_dir (mwrender creatureanimation effectmanager util renderinginterface pathgrid rendermode weaponanimation screenshotmanager bulletdebugdraw globalmap characterpreview camera localmap water terrainstorage ripplesimulation renderbin actoranimation landmanager navmesh actorspaths recastmesh fogmanager objectpaging groundcover - postprocessor pingpongcull luminancecalculator pingpongcanvas transparentpass navmeshmode debugdraw + postprocessor pingpongcull luminancecalculator pingpongcanvas transparentpass navmeshmode ) add_openmw_dir (mwinput diff --git a/apps/openmw/mwmechanics/actors.cpp b/apps/openmw/mwmechanics/actors.cpp index 2d1ae081d5..1c98fb8a5c 100644 --- a/apps/openmw/mwmechanics/actors.cpp +++ b/apps/openmw/mwmechanics/actors.cpp @@ -28,7 +28,7 @@ #include "../mwbase/luamanager.hpp" #include "../mwrender/renderingmanager.hpp" -#include "../mwrender/debugdraw.hpp" +#include #include "../mwmechanics/aibreathe.hpp" @@ -1628,7 +1628,8 @@ namespace MWMechanics { debugRender.addDrawCall(MWRenderDebug::DrawCall::wireCube(actorPos)); } - + debugRender.addLine(actorPos, actorPos + osg::Vec3(0., 0., 200.),MWRenderDebug::colorBlue); + if (!isDead && (!godmode || !isPlayer) && actor.getPtr().getClass().getCreatureStats(actor.getPtr()).isParalyzed()) diff --git a/apps/openmw/mwrender/renderingmanager.cpp b/apps/openmw/mwrender/renderingmanager.cpp index 6903a33723..d67809a7aa 100644 --- a/apps/openmw/mwrender/renderingmanager.cpp +++ b/apps/openmw/mwrender/renderingmanager.cpp @@ -52,6 +52,7 @@ #include #include +#include #include "../mwworld/cellstore.hpp" #include "../mwworld/class.hpp" @@ -82,7 +83,7 @@ #include "screenshotmanager.hpp" #include "groundcover.hpp" #include "postprocessor.hpp" -#include "debugdraw.hpp" + namespace MWRender { @@ -490,7 +491,7 @@ namespace MWRender mViewer->getIncrementalCompileOperation()->setTargetFrameRate(Settings::Manager::getFloat("target framerate", "Cells")); } - mDebugDraw = std::make_unique(*this, mRootNode); + mDebugDraw = std::make_unique(mResourceSystem->getSceneManager()->getShaderManager(), mRootNode); mResourceSystem->getSceneManager()->setIncrementalCompileOperation(mViewer->getIncrementalCompileOperation()); mEffectManager = std::make_unique(sceneRoot, mResourceSystem); diff --git a/components/CMakeLists.txt b/components/CMakeLists.txt index a7b3e87f03..a392590f5c 100644 --- a/components/CMakeLists.txt +++ b/components/CMakeLists.txt @@ -205,7 +205,7 @@ add_component_dir (stereo ) add_component_dir (debug - debugging debuglog gldebug + debugging debuglog gldebug debugdraw ) IF(NOT WIN32 AND NOT APPLE) diff --git a/apps/openmw/mwrender/debugdraw.cpp b/components/debug/debugdraw.cpp similarity index 83% rename from apps/openmw/mwrender/debugdraw.cpp rename to components/debug/debugdraw.cpp index c97386745b..3a69fc42a5 100644 --- a/apps/openmw/mwrender/debugdraw.cpp +++ b/components/debug/debugdraw.cpp @@ -1,6 +1,4 @@ #include "debugdraw.hpp" -#include -#include #include @@ -12,7 +10,7 @@ #include -static osg::Vec3 sphereCoordToCarthesian(float theta ,float phi ,float r ) +static osg::Vec3 sphereCoordToCartesian(float theta ,float phi ,float r ) { osg::Vec3 returnVec = osg::Vec3(0.0,0.0,0.0); float phiToHorizontal = osg::PI_2 - phi ; @@ -68,7 +66,6 @@ static void generateWireCube(osg::Geometry& geom, float dim) static void generateCube(osg::Geometry& geom, float dim) { - osg::ref_ptr vertices = new osg::Vec3Array; osg::ref_ptr normals = new osg::Vec3Array; osg::ref_ptr indices = new osg::DrawElementsUShort(osg::DrawElementsUShort::TRIANGLES, 0); @@ -79,15 +76,12 @@ static void generateCube(osg::Geometry& geom, float dim) osg::Vec3f u(0., 0., 0.); osg::Vec3f v(0., 0., 0.); int axis = i_face / 2; - //if (axis != 2) - // continue; int dir = i_face % 2 == 0 ? -1 : 1; float float_dir = dir; normale[axis] = float_dir; u[(axis + 1) % 3] = 1.0; v[(axis + 2) % 3] = 1.0; - for (int i_point = 0; i_point < 4; i_point++) { float iu = i_point % 2 == 1 ? float_dir : -float_dir;//This is to get the right triangle orientation when the normal changes* @@ -113,7 +107,6 @@ static void generateCube(osg::Geometry& geom, float dim) geom.setVertexAttribArray(0, vertices, osg::Array::BIND_PER_VERTEX); geom.setVertexAttribArray(1, normals, osg::Array::BIND_PER_VERTEX); geom.addPrimitiveSet(indices); - } @@ -132,7 +125,7 @@ static void generateCylinder(osg::Geometry& geom, float radius, float height, in for (int i = 0 ;i < subdiv; i++) { float theta = (float(i )/ float(subdiv )) * osg::PI * 2.; - osg::Vec3 pos= sphereCoordToCarthesian(theta, osg::PI_2, 1.); + osg::Vec3 pos= sphereCoordToCartesian(theta, osg::PI_2, 1.); pos *= radius; pos.z() = height / 2.; vertices->push_back(pos); @@ -158,7 +151,7 @@ static void generateCylinder(osg::Geometry& geom, float radius, float height, in for (int i = 0 ;i < subdiv; i++) { float theta = float(i)/ float(subdiv) * osg::PI*2.; - osg::Vec3 pos= sphereCoordToCarthesian(theta, osg::PI_2, 1.); + osg::Vec3 pos= sphereCoordToCartesian(theta, osg::PI_2, 1.); pos *= radius; pos.z() = - height / 2.; vertices->push_back(pos); @@ -170,7 +163,7 @@ static void generateCylinder(osg::Geometry& geom, float radius, float height, in for (int i = 0 ;i < subdiv; i++) { float theta = float(i )/ float(subdiv) * osg::PI*2.; - osg::Vec3 normal = sphereCoordToCarthesian(theta, osg::PI_2, 1.); + osg::Vec3 normal = sphereCoordToCartesian(theta, osg::PI_2, 1.); auto posTop = normal; posTop *= radius; auto posBot = posTop; @@ -199,7 +192,6 @@ static void generateCylinder(osg::Geometry& geom, float radius, float height, in indices->push_back(v4); indices->push_back(v3); indices->push_back(v1); - } for (int i = 0 ;i < subdiv; i++) { @@ -227,7 +219,7 @@ static void generateCylinder(osg::Geometry& geom, float radius, float height, in namespace MWRenderDebug { - void CubeCustomDraw::drawImplementation(osg::RenderInfo& renderInfo) const + void DebugCustomDraw::drawImplementation(osg::RenderInfo& renderInfo) const { auto state = renderInfo.getState(); osg::GLExtensions* ext = osg::GLExtensions::Get( state->getContextID(), true ); @@ -243,24 +235,21 @@ namespace MWRenderDebug std::lock_guard lock(mDrawCallMutex); osg::Uniform* uTrans = const_cast( stateSet->getUniform("trans")); - osg::Uniform* uCol = const_cast( stateSet->getUniform("passColor")); + osg::Uniform* uCol = const_cast( stateSet->getUniform("color")); osg::Uniform* uScale = const_cast( stateSet->getUniform("scale")); osg::Uniform* uUseNormalAsColor = const_cast( stateSet->getUniform("useNormalAsColor")); - - auto transLocation = pcp->getUniformLocation(uTrans->getNameID() ); auto colLocation = pcp->getUniformLocation(uCol->getNameID() ); auto scaleLocation = pcp->getUniformLocation(uScale->getNameID() ); auto normalAsColorLocation = pcp->getUniformLocation(uUseNormalAsColor->getNameID() ); - ext->glUniform3f(transLocation, 0., 0., 0.); ext->glUniform3f(colLocation, 1., 1., 1.); ext->glUniform3f(scaleLocation, 1., 1., 1.); ext->glUniform1i(normalAsColorLocation, 1); - mlinesToDraw->drawImplementation(renderInfo); + mLinesToDraw->drawImplementation(renderInfo); ext->glUniform1i(normalAsColorLocation, 0); @@ -270,17 +259,10 @@ namespace MWRenderDebug osg::Vec3f color = shapeToDraw.mColor; osg::Vec3f scale = shapeToDraw.mDims; + ext->glUniform3f(transLocation, translation.x(), translation.y(), translation.z()); + ext->glUniform3f(colLocation, color.x(), color.y(), color.z()); + ext->glUniform3f(scaleLocation, scale.x(), scale.y(), scale.z()); - if (uTrans) - ext->glUniform3f(transLocation, translation.x(), translation.y(), translation.z()); - if (uCol) - { - ext->glUniform3f(colLocation, color.x(), color.y(), color.z()); - } - if (uScale) - { - ext->glUniform3f(scaleLocation, scale.x(), scale.y(), scale.z()); - } switch (shapeToDraw.mDrawShape) { case DrawShape::Cube: @@ -302,16 +284,8 @@ namespace MWRenderDebug static void makeLineInstance( osg::Geometry& lines) { - auto vertices = new osg::Vec3Array; - auto color = new osg::Vec3Array; - - for (int i = 0; i < 2; i++) - { - vertices->push_back(osg::Vec3()); - vertices->push_back(osg::Vec3(0., 0., 0.)); - color->push_back(osg::Vec3(1., 1., 1.)); - color->push_back(osg::Vec3(1., 1., 1.)); - } + auto vertices = new osg::Vec3Array; + auto color = new osg::Vec3Array; lines.setUseVertexArrayObject(true); lines.setUseDisplayList(false); @@ -321,19 +295,15 @@ namespace MWRenderDebug lines.setVertexAttribArray(1, color, osg::Array::BIND_PER_VERTEX); lines.addPrimitiveSet(new osg::DrawArrays(osg::PrimitiveSet::LINES, 0, vertices->size())); - - //lines.setStateSet(stateSet->clone(osg::CopyOp::DEEP_COPY_ALL)->asStateSet()); } DebugLines() { - mLinesWrite = new osg::Geometry(); - mLinesRead = new osg::Geometry(); + mLinesRead = new osg::Geometry(); makeLineInstance(*mLinesRead); makeLineInstance(*mLinesWrite); - } void update(std::mutex& mutex) @@ -346,30 +316,28 @@ namespace MWRenderDebug mLinesWrite.swap(mLinesRead); } - - static_cast(mLinesWrite->getVertexAttribArray(0))->resize(2, osg::Vec3()); - static_cast(mLinesWrite->getVertexAttribArray(1))->resize(2, osg::Vec3()); + static_cast(mLinesWrite->getVertexAttribArray(0))->clear(); + static_cast(mLinesWrite->getVertexAttribArray(1))->clear(); } - osg::ref_ptr mLinesWrite; - osg::ref_ptr mLinesRead; + osg::ref_ptr mLinesWrite; + osg::ref_ptr mLinesRead; }; } -MWRenderDebug::DebugDrawer::DebugDrawer(MWRender::RenderingManager& renderingManager,osg::ref_ptr parentNode) +MWRenderDebug::DebugDrawer::DebugDrawer(Shader::ShaderManager& shaderManager,osg::ref_ptr parentNode) { - auto& shaderManager = renderingManager.getResourceSystem()->getSceneManager()->getShaderManager(); auto vertexShader = shaderManager.getShader("debugDraw_vertex.glsl", Shader::ShaderManager::DefineMap(), osg::Shader::Type::VERTEX); auto fragmentShader = shaderManager.getShader("debugDraw_fragment.glsl", Shader::ShaderManager::DefineMap(), osg::Shader::Type::FRAGMENT); auto program = shaderManager.getProgram(vertexShader, fragmentShader); mDebugLines = std::make_unique(); - mcustomCubesDrawer = new CubeCustomDraw(mShapesToDrawRead,mDebugLines->mLinesRead, mDrawCallMutex); + mCustomDebugDrawer = new DebugCustomDraw(mShapesToDrawRead,mDebugLines->mLinesRead, mDrawCallMutex); mDebugDrawSceneObjects = new osg::Group; mDebugDrawSceneObjects->setCullingActive(false); osg::StateSet* stateset = mDebugDrawSceneObjects->getOrCreateStateSet(); - stateset->addUniform(new osg::Uniform("passColor", osg::Vec3f(1., 1., 1.))); + stateset->addUniform(new osg::Uniform("color", osg::Vec3f(1., 1., 1.))); stateset->addUniform(new osg::Uniform("trans", osg::Vec3f(0., 0., 0.))); stateset->addUniform(new osg::Uniform("scale", osg::Vec3f(1., 1., 1.))); stateset->addUniform(new osg::Uniform("useNormalAsColor", 0)); @@ -382,22 +350,22 @@ MWRenderDebug::DebugDrawer::DebugDrawer(MWRender::RenderingManager& renderingMan cubeGeometry->setSupportsDisplayList(false); cubeGeometry->setUseVertexBufferObjects(true); generateCube(*cubeGeometry,1.); - mcustomCubesDrawer->mCubeGeometry = cubeGeometry; + mCustomDebugDrawer ->mCubeGeometry = cubeGeometry; auto cylinderGeom = new osg::Geometry; cylinderGeom->setSupportsDisplayList(false); cylinderGeom->setUseVertexBufferObjects(true); generateCylinder(*cylinderGeom, .5, 1., 20); - mcustomCubesDrawer->mCylinderGeometry = cylinderGeom; + mCustomDebugDrawer ->mCylinderGeometry = cylinderGeom; auto wireCube = new osg::Geometry; wireCube->setSupportsDisplayList(false); wireCube->setUseVertexBufferObjects(true); generateWireCube(*wireCube, 1.); - mcustomCubesDrawer->mWireCubeGeometry = wireCube; - mcustomCubesDrawer->setStateSet( stateset); + mCustomDebugDrawer->mWireCubeGeometry = wireCube; + mCustomDebugDrawer->setStateSet(stateset); - mDebugDrawSceneObjects->addChild(mcustomCubesDrawer); + mDebugDrawSceneObjects->addChild(mCustomDebugDrawer); parentNode->addChild(mDebugDrawSceneObjects); } @@ -436,8 +404,8 @@ void MWRenderDebug::DebugDrawer::addDrawCall(const DrawCall& draw) void MWRenderDebug::DebugDrawer::addLine(const osg::Vec3& start, const osg::Vec3& end, const osg::Vec3 color) { - auto vertices = static_cast(mDebugLines->mLinesWrite->getVertexAttribArray(0)); - auto colors = static_cast(mDebugLines->mLinesWrite->getVertexAttribArray(1)); + auto vertices = static_cast(mDebugLines->mLinesWrite->getVertexAttribArray(0)); + auto colors = static_cast(mDebugLines->mLinesWrite->getVertexAttribArray(1)); vertices->push_back(start); vertices->push_back(end); diff --git a/apps/openmw/mwrender/debugdraw.hpp b/components/debug/debugdraw.hpp similarity index 80% rename from apps/openmw/mwrender/debugdraw.hpp rename to components/debug/debugdraw.hpp index 2c76da534a..c18ca097b7 100644 --- a/apps/openmw/mwrender/debugdraw.hpp +++ b/components/debug/debugdraw.hpp @@ -1,14 +1,19 @@ -#pragma once +#ifndef OPENMW_COMPONENTS_DEBUG_DEBUGDRAW_H +#define OPENMW_COMPONENTS_DEBUG_DEBUGDRAW_H + #include #include #include -#include "renderingmanager.hpp" namespace osg { class Group; class Geometry; } +namespace Shader +{ + class ShaderManager; +} namespace MWRenderDebug { @@ -37,16 +42,15 @@ namespace MWRenderDebug static DrawCall cube(osg::Vec3f pos, osg::Vec3 dims = osg::Vec3(50., 50., 50.), osg::Vec3 color = colorWhite) { return { pos, dims, color, DrawShape::Cube}; } static DrawCall wireCube(osg::Vec3f pos, osg::Vec3 dims = osg::Vec3(50., 50., 50.), osg::Vec3 color = colorWhite) { return { pos, dims, color, DrawShape::WireCube}; } static DrawCall cylinder(osg::Vec3f pos, osg::Vec3 dims = osg::Vec3(50., 50., 50.), osg::Vec3 color = colorWhite) { return { pos, dims, color, DrawShape::Cylinder}; } - }; - class CubeCustomDraw : public osg::Drawable + class DebugCustomDraw : public osg::Drawable { public: - CubeCustomDraw( std::vector& cubesToDraw,osg::ref_ptr& linesToDraw ,std::mutex& mutex) : mShapsToDraw(cubesToDraw),mlinesToDraw(linesToDraw), mDrawCallMutex(mutex) {} + DebugCustomDraw( std::vector& cubesToDraw,osg::ref_ptr& linesToDraw ,std::mutex& mutex) : mShapsToDraw(cubesToDraw),mLinesToDraw(linesToDraw), mDrawCallMutex(mutex) {} std::vector& mShapsToDraw; - osg::ref_ptr& mlinesToDraw; + osg::ref_ptr& mLinesToDraw; std::mutex& mDrawCallMutex; @@ -54,21 +58,19 @@ namespace MWRenderDebug osg::ref_ptr mCylinderGeometry; osg::ref_ptr mWireCubeGeometry; - virtual osg::BoundingSphere computeBound() const { return osg::BoundingSphere(); } virtual void drawImplementation(osg::RenderInfo&) const; - }; struct DebugLines; struct DebugDrawer { - DebugDrawer(MWRender::RenderingManager& manager,osg::ref_ptr parentNode); + DebugDrawer(Shader::ShaderManager& shaderManager,osg::ref_ptr parentNode); ~DebugDrawer(); void update(); @@ -85,7 +87,8 @@ namespace MWRenderDebug std::vector mShapesToDrawWrite; std::mutex mDrawCallMutex; - osg::ref_ptr mcustomCubesDrawer; + osg::ref_ptr mCustomDebugDrawer; osg::ref_ptr mDebugDrawSceneObjects; }; } +#endif // ! diff --git a/files/shaders/debugDraw_vertex.glsl b/files/shaders/debugDraw_vertex.glsl index 1c3ca9cdce..a2ab05988d 100644 --- a/files/shaders/debugDraw_vertex.glsl +++ b/files/shaders/debugDraw_vertex.glsl @@ -3,12 +3,7 @@ uniform mat4 projectionMatrix; - -// vec4 mw_modelToClip(vec4 pos); -// vec4 mw_modelToView(vec4 pos); -// vec4 mw_viewToClip(vec4 pos); - -uniform vec3 passColor; +uniform vec3 color; uniform vec3 trans; uniform vec3 scale; uniform int useNormalAsColor; @@ -24,5 +19,5 @@ void main() gl_Position = projectionMatrix * gl_ModelViewMatrix * vec4(aPos * scale + trans, 1.); vertexNormal = useNormalAsColor == 1? vec3(1.,1.,1.) : aNormal ; - vertexColor = useNormalAsColor == 1? aNormal : passColor.xyz; + vertexColor = useNormalAsColor == 1? aNormal : color.xyz; }