mirror of
https://github.com/OpenMW/openmw.git
synced 2025-02-28 12:09:53 +00:00
initial work to get lines in
lines need the same synchronisation system as the other primitives This two points are there because if I resize to 0 it crashes, si I need a pice of line at 0.,0.,0. so there is no crash.Not ideal Lines are colored didn't commit updated shader file
This commit is contained in:
parent
80c97e48bf
commit
12b3424dd7
3 changed files with 111 additions and 8 deletions
|
@ -245,13 +245,25 @@ namespace MWRenderDebug
|
|||
osg::Uniform* uTrans = const_cast<osg::Uniform*>( stateSet->getUniform("trans"));
|
||||
osg::Uniform* uCol = const_cast<osg::Uniform*>( stateSet->getUniform("passColor"));
|
||||
osg::Uniform* uScale = const_cast<osg::Uniform*>( stateSet->getUniform("scale"));
|
||||
osg::Uniform* uUseNormalAsColor = const_cast<osg::Uniform*>( 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);
|
||||
|
||||
ext->glUniform1i(normalAsColorLocation, 0);
|
||||
|
||||
for (const auto& shapeToDraw : mShapsToDraw)
|
||||
{
|
||||
osg::Vec3f translation = shapeToDraw.mPosition;
|
||||
|
@ -284,6 +296,64 @@ namespace MWRenderDebug
|
|||
}
|
||||
|
||||
}
|
||||
|
||||
struct DebugLines
|
||||
{
|
||||
|
||||
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.));
|
||||
}
|
||||
|
||||
lines.setUseVertexArrayObject(true);
|
||||
lines.setUseDisplayList(false);
|
||||
lines.setCullingActive(false);
|
||||
|
||||
lines.setVertexAttribArray(0, vertices, osg::Array::BIND_PER_VERTEX);
|
||||
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();
|
||||
|
||||
makeLineInstance(*mLinesRead);
|
||||
makeLineInstance(*mLinesWrite);
|
||||
|
||||
}
|
||||
|
||||
void update(std::mutex& mutex)
|
||||
{
|
||||
mLinesWrite->removePrimitiveSet(0, 1);
|
||||
mLinesWrite->addPrimitiveSet(new osg::DrawArrays(osg::PrimitiveSet::LINES, 0,static_cast<osg::Vec3Array*>(mLinesWrite->getVertexAttribArray(0))->size()));
|
||||
|
||||
{
|
||||
auto lock = std::scoped_lock(mutex);
|
||||
mLinesWrite.swap(mLinesRead);
|
||||
}
|
||||
|
||||
|
||||
static_cast<osg::Vec3Array*>(mLinesWrite->getVertexAttribArray(0))->resize(2, osg::Vec3());
|
||||
static_cast<osg::Vec3Array*>(mLinesWrite->getVertexAttribArray(1))->resize(2, osg::Vec3());
|
||||
}
|
||||
|
||||
osg::ref_ptr<osg::Geometry> mLinesWrite;
|
||||
osg::ref_ptr<osg::Geometry> mLinesRead;
|
||||
};
|
||||
}
|
||||
|
||||
MWRenderDebug::DebugDrawer::DebugDrawer(MWRender::RenderingManager& renderingManager,osg::ref_ptr<osg::Group> parentNode)
|
||||
|
@ -293,14 +363,17 @@ MWRenderDebug::DebugDrawer::DebugDrawer(MWRender::RenderingManager& renderingMan
|
|||
auto fragmentShader = shaderManager.getShader("debugDraw_fragment.glsl", Shader::ShaderManager::DefineMap(), osg::Shader::Type::FRAGMENT);
|
||||
|
||||
auto program = shaderManager.getProgram(vertexShader, fragmentShader);
|
||||
mcustomCubesDrawer = new CubeCustomDraw(mShapesToDrawRead, mDrawCallMutex);
|
||||
mDebugLines = std::make_unique<DebugLines>();
|
||||
mcustomCubesDrawer = new CubeCustomDraw(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("trans", 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));
|
||||
|
||||
stateset->setAttributeAndModes(program, osg::StateAttribute::ON);
|
||||
stateset->setMode(GL_DEPTH_TEST, GL_TRUE);
|
||||
stateset->setMode(GL_CULL_FACE, GL_TRUE);
|
||||
|
@ -322,12 +395,18 @@ MWRenderDebug::DebugDrawer::DebugDrawer(MWRender::RenderingManager& renderingMan
|
|||
wireCube->setUseVertexBufferObjects(true);
|
||||
generateWireCube(*wireCube, 1.);
|
||||
mcustomCubesDrawer->mWireCubeGeometry = wireCube;
|
||||
|
||||
mcustomCubesDrawer->setStateSet( stateset);
|
||||
|
||||
mDebugDrawSceneObjects->addChild(mcustomCubesDrawer);
|
||||
|
||||
parentNode->addChild(mDebugDrawSceneObjects);
|
||||
}
|
||||
|
||||
MWRenderDebug::DebugDrawer::~DebugDrawer()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
void MWRenderDebug::DebugDrawer::update()
|
||||
{
|
||||
{
|
||||
|
@ -335,6 +414,7 @@ void MWRenderDebug::DebugDrawer::update()
|
|||
mShapesToDrawRead.swap(mShapesToDrawWrite);
|
||||
}
|
||||
mShapesToDrawWrite.clear();
|
||||
mDebugLines->update(mDrawCallMutex);
|
||||
}
|
||||
|
||||
void MWRenderDebug::DebugDrawer::drawCube(osg::Vec3f mPosition, osg::Vec3f mDims, osg::Vec3f mColor)
|
||||
|
@ -353,3 +433,18 @@ void MWRenderDebug::DebugDrawer::addDrawCall(const DrawCall& draw)
|
|||
{
|
||||
mShapesToDrawWrite.push_back(draw);
|
||||
}
|
||||
|
||||
void MWRenderDebug::DebugDrawer::addLine(const osg::Vec3& start, const osg::Vec3& end, const osg::Vec3 color)
|
||||
{
|
||||
auto vertices = static_cast<osg::Vec3Array*>(mDebugLines->mLinesWrite->getVertexAttribArray(0));
|
||||
auto colors = static_cast<osg::Vec3Array*>(mDebugLines->mLinesWrite->getVertexAttribArray(1));
|
||||
|
||||
vertices->push_back(start);
|
||||
vertices->push_back(end);
|
||||
vertices->dirty();
|
||||
|
||||
colors->push_back(color);
|
||||
colors->push_back(color);
|
||||
colors->dirty();
|
||||
|
||||
}
|
||||
|
|
|
@ -43,9 +43,10 @@ namespace MWRenderDebug
|
|||
class CubeCustomDraw : public osg::Drawable
|
||||
{
|
||||
public:
|
||||
CubeCustomDraw( std::vector<DrawCall>& cubesToDraw,std::mutex& mutex) : mShapsToDraw(cubesToDraw),mDrawCallMutex(mutex) {}
|
||||
CubeCustomDraw( std::vector<DrawCall>& cubesToDraw,osg::ref_ptr<osg::Geometry>& linesToDraw ,std::mutex& mutex) : mShapsToDraw(cubesToDraw),mlinesToDraw(linesToDraw), mDrawCallMutex(mutex) {}
|
||||
|
||||
std::vector<DrawCall>& mShapsToDraw;
|
||||
osg::ref_ptr<osg::Geometry>& mlinesToDraw;
|
||||
|
||||
std::mutex& mDrawCallMutex;
|
||||
|
||||
|
@ -63,17 +64,23 @@ namespace MWRenderDebug
|
|||
|
||||
};
|
||||
|
||||
struct DebugLines;
|
||||
|
||||
struct DebugDrawer
|
||||
{
|
||||
DebugDrawer(MWRender::RenderingManager& manager,osg::ref_ptr<osg::Group> parentNode);
|
||||
~DebugDrawer();
|
||||
|
||||
void update();
|
||||
void drawCube(osg::Vec3f mPosition, osg::Vec3f mDims = osg::Vec3(50.,50.,50.), osg::Vec3f mColor = osg::Vec3(1.,1.,1.));
|
||||
void drawCubeMinMax(osg::Vec3f min, osg::Vec3f max, osg::Vec3f mColor = osg::Vec3(1.,1.,1.));
|
||||
void drawCube(osg::Vec3f mPosition, osg::Vec3f mDims = osg::Vec3(50.,50.,50.), osg::Vec3f mColor = colorWhite);
|
||||
void drawCubeMinMax(osg::Vec3f min, osg::Vec3f max, osg::Vec3f mColor = colorWhite);
|
||||
void addDrawCall(const DrawCall& draw);
|
||||
void addLine(const osg::Vec3& start, const osg::Vec3& end, const osg::Vec3 color = colorWhite);
|
||||
|
||||
private:
|
||||
|
||||
std::unique_ptr<DebugLines> mDebugLines;
|
||||
|
||||
std::vector<DrawCall> mShapesToDrawRead;
|
||||
std::vector<DrawCall> mShapesToDrawWrite;
|
||||
std::mutex mDrawCallMutex;
|
||||
|
|
|
@ -11,6 +11,7 @@ uniform mat4 projectionMatrix;
|
|||
uniform vec3 passColor;
|
||||
uniform vec3 trans;
|
||||
uniform vec3 scale;
|
||||
uniform int useNormalAsColor;
|
||||
|
||||
layout (location = 0) in vec3 aPos;
|
||||
layout (location = 1) in vec3 aNormal;
|
||||
|
@ -22,6 +23,6 @@ void main()
|
|||
{
|
||||
gl_Position = projectionMatrix * gl_ModelViewMatrix * vec4(aPos * scale + trans, 1.);
|
||||
|
||||
vertexNormal = aNormal;
|
||||
vertexColor = passColor.xyz;
|
||||
vertexNormal = useNormalAsColor == 1? vec3(1.,1.,1.) : aNormal ;
|
||||
vertexColor = useNormalAsColor == 1? aNormal : passColor.xyz;
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue