1
0
mirror of https://gitlab.com/OpenMW/openmw.git synced 2025-03-26 11:37:12 +00:00

debug draw shader uses the generic functions

renamed files

roundabout way to change case with windows noticing

fixed type

fix filename in cmakelist.txt

ported shader and c++ code so glsl 120 can be used instead of 330

new debug shader and old one are almost unified

for some reason, even though I get no compilation issue, old debug draws don't work, only the new one implemented by this MR

remove useless const cast
This commit is contained in:
florent.teppe 2022-08-31 21:11:52 +02:00
parent bd1bbc0ab8
commit 21971c08ba
8 changed files with 110 additions and 65 deletions

@ -32,9 +32,6 @@ static void generateWireCube(osg::Geometry& geom, float dim)
osg::ref_ptr<osg::Vec3Array> vertices = new osg::Vec3Array; osg::ref_ptr<osg::Vec3Array> vertices = new osg::Vec3Array;
osg::ref_ptr<osg::Vec3Array> normals = new osg::Vec3Array; osg::ref_ptr<osg::Vec3Array> normals = new osg::Vec3Array;
geom.setVertexAttribArray(0, vertices, osg::Array::BIND_PER_VERTEX);
geom.setVertexAttribArray(1, normals, osg::Array::BIND_PER_VERTEX);
osg::Vec2i indexPos[] = { osg::Vec2i(0, 0), osg::Vec2i(1, 0), osg::Vec2i(1, 1), osg::Vec2i(0, 1) }; osg::Vec2i indexPos[] = { osg::Vec2i(0, 0), osg::Vec2i(1, 0), osg::Vec2i(1, 1), osg::Vec2i(0, 1) };
for (int i = 0; i < 4; i++) for (int i = 0; i < 4; i++)
@ -55,11 +52,13 @@ static void generateWireCube(osg::Geometry& geom, float dim)
vertices->push_back(vert1 * dim); vertices->push_back(vert1 * dim);
vertices->push_back(vert3 * dim); vertices->push_back(vert3 * dim);
} }
for (unsigned long i = 0; i < vertices->size(); i++) for (std::size_t i = 0; i < vertices->size(); i++)
{ {
normals->push_back(osg::Vec3(1., 1., 1.)); normals->push_back(osg::Vec3(1., 1., 1.));
} }
geom.setVertexArray( vertices);
geom.setNormalArray( normals, osg::Array::BIND_PER_VERTEX);
geom.addPrimitiveSet(new osg::DrawArrays(osg::PrimitiveSet::LINES, 0, vertices->size())); geom.addPrimitiveSet(new osg::DrawArrays(osg::PrimitiveSet::LINES, 0, vertices->size()));
} }
@ -103,8 +102,8 @@ static void generateCube(osg::Geometry& geom, float dim)
indices->push_back(newFace2[i]); indices->push_back(newFace2[i]);
} }
} }
geom.setVertexAttribArray(0, vertices, osg::Array::BIND_PER_VERTEX); geom.setVertexArray(vertices);
geom.setVertexAttribArray(1, normals, osg::Array::BIND_PER_VERTEX); geom.setNormalArray( normals, osg::Array::BIND_PER_VERTEX);
geom.addPrimitiveSet(indices); geom.addPrimitiveSet(indices);
} }
@ -209,8 +208,8 @@ static void generateCylinder(osg::Geometry& geom, float radius, float height, in
indices->push_back(bot2); indices->push_back(bot2);
} }
geom.setVertexAttribArray(0, vertices, osg::Array::BIND_PER_VERTEX); geom.setVertexArray(vertices);
geom.setVertexAttribArray(1, normals, osg::Array::BIND_PER_VERTEX); geom.setNormalArray(normals, osg::Array::BIND_PER_VERTEX);
geom.addPrimitiveSet(indices); geom.addPrimitiveSet(indices);
} }
@ -240,10 +239,10 @@ namespace MWRenderDebug
return; return;
} }
osg::Uniform* uTrans = const_cast<osg::Uniform*>(stateSet->getUniform("trans")); const osg::Uniform* uTrans = stateSet->getUniform("trans");
osg::Uniform* uCol = const_cast<osg::Uniform*>(stateSet->getUniform("color")); const osg::Uniform* uCol = stateSet->getUniform("color");
osg::Uniform* uScale = const_cast<osg::Uniform*>(stateSet->getUniform("scale")); const osg::Uniform* uScale = stateSet->getUniform("scale");
osg::Uniform* uUseNormalAsColor = const_cast<osg::Uniform*>(stateSet->getUniform("useNormalAsColor")); const osg::Uniform* uUseNormalAsColor = stateSet->getUniform("useNormalAsColor");
auto transLocation = pcp->getUniformLocation(uTrans->getNameID()); auto transLocation = pcp->getUniformLocation(uTrans->getNameID());
auto colLocation = pcp->getUniformLocation(uCol->getNameID()); auto colLocation = pcp->getUniformLocation(uCol->getNameID());
@ -283,8 +282,8 @@ namespace MWRenderDebug
} }
} }
mShapesToDraw.clear(); mShapesToDraw.clear();
static_cast<osg::Vec3Array*>(mLinesToDraw->getVertexAttribArray(0))->clear(); static_cast<osg::Vec3Array*>(mLinesToDraw->getVertexArray())->clear();
static_cast<osg::Vec3Array*>(mLinesToDraw->getVertexAttribArray(1))->clear(); static_cast<osg::Vec3Array*>(mLinesToDraw->getNormalArray())->clear();
} }
struct DebugLines struct DebugLines
@ -299,8 +298,8 @@ namespace MWRenderDebug
lines.setUseDisplayList(false); lines.setUseDisplayList(false);
lines.setCullingActive(false); lines.setCullingActive(false);
lines.setVertexAttribArray(0, vertices, osg::Array::BIND_PER_VERTEX); lines.setVertexArray( vertices);
lines.setVertexAttribArray(1, color, osg::Array::BIND_PER_VERTEX); lines.setNormalArray(color, osg::Array::BIND_PER_VERTEX);
lines.addPrimitiveSet(new osg::DrawArrays(osg::PrimitiveSet::LINES, 0, vertices->size())); lines.addPrimitiveSet(new osg::DrawArrays(osg::PrimitiveSet::LINES, 0, vertices->size()));
} }
@ -328,7 +327,7 @@ namespace MWRenderDebug
int indexRead = getIdexBufferReadFromFrame(mDebugDrawer.mCurrentFrame); int indexRead = getIdexBufferReadFromFrame(mDebugDrawer.mCurrentFrame);
auto& lines = mDebugDrawer.mDebugLines; auto& lines = mDebugDrawer.mDebugLines;
lines->mLinesGeom[indexRead]->removePrimitiveSet(0, 1); lines->mLinesGeom[indexRead]->removePrimitiveSet(0, 1);
lines->mLinesGeom[indexRead]->addPrimitiveSet(new osg::DrawArrays(osg::PrimitiveSet::LINES, 0, static_cast<osg::Vec3Array*>(lines->mLinesGeom[indexRead]->getVertexAttribArray(0))->size())); lines->mLinesGeom[indexRead]->addPrimitiveSet(new osg::DrawArrays(osg::PrimitiveSet::LINES, 0, static_cast<osg::Vec3Array*>(lines->mLinesGeom[indexRead]->getVertexArray())->size()));
nv->pushOntoNodePath(mDebugDrawer.mCustomDebugDrawer[indexRead]); nv->pushOntoNodePath(mDebugDrawer.mCustomDebugDrawer[indexRead]);
nv->apply(*mDebugDrawer.mCustomDebugDrawer[indexRead]); nv->apply(*mDebugDrawer.mCustomDebugDrawer[indexRead]);
@ -342,8 +341,8 @@ namespace MWRenderDebug
MWRenderDebug::DebugDrawer::DebugDrawer(Shader::ShaderManager& shaderManager, osg::ref_ptr<osg::Group> parentNode) MWRenderDebug::DebugDrawer::DebugDrawer(Shader::ShaderManager& shaderManager, osg::ref_ptr<osg::Group> parentNode)
{ {
mCurrentFrame = 0; mCurrentFrame = 0;
auto vertexShader = shaderManager.getShader("debugDraw_vertex.glsl", Shader::ShaderManager::DefineMap(), osg::Shader::Type::VERTEX); auto vertexShader = shaderManager.getShader("debug_vertex.glsl", Shader::ShaderManager::DefineMap(), osg::Shader::Type::VERTEX);
auto fragmentShader = shaderManager.getShader("debugDraw_fragment.glsl", Shader::ShaderManager::DefineMap(), osg::Shader::Type::FRAGMENT); auto fragmentShader = shaderManager.getShader("debug_fragment.glsl", Shader::ShaderManager::DefineMap(), osg::Shader::Type::FRAGMENT);
auto program = shaderManager.getProgram(vertexShader, fragmentShader); auto program = shaderManager.getProgram(vertexShader, fragmentShader);
mDebugLines = std::make_unique<DebugLines>(); mDebugLines = std::make_unique<DebugLines>();
@ -355,6 +354,7 @@ MWRenderDebug::DebugDrawer::DebugDrawer(Shader::ShaderManager& shaderManager, os
stateset->addUniform(new osg::Uniform("trans", osg::Vec3f(0., 0., 0.))); 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("scale", osg::Vec3f(1., 1., 1.)));
stateset->addUniform(new osg::Uniform("useNormalAsColor", 0)); stateset->addUniform(new osg::Uniform("useNormalAsColor", 0));
stateset->addUniform(new osg::Uniform("useAdvancedShader", 1));
stateset->setAttributeAndModes(program, osg::StateAttribute::ON); stateset->setAttributeAndModes(program, osg::StateAttribute::ON);
stateset->setMode(GL_DEPTH_TEST, GL_TRUE); stateset->setMode(GL_DEPTH_TEST, GL_TRUE);
@ -375,7 +375,7 @@ MWRenderDebug::DebugDrawer::DebugDrawer(Shader::ShaderManager& shaderManager, os
wireCube->setUseVertexBufferObjects(true); wireCube->setUseVertexBufferObjects(true);
generateWireCube(*wireCube, 1.); generateWireCube(*wireCube, 1.);
for (unsigned long i = 0; i < mShapesToDraw.size(); i++) for (std::size_t i = 0; i < mShapesToDraw.size(); i++)
{ {
mCustomDebugDrawer[i] = new DebugCustomDraw(mShapesToDraw[i], mDebugLines->mLinesGeom[i]); mCustomDebugDrawer[i] = new DebugCustomDraw(mShapesToDraw[i], mDebugLines->mLinesGeom[i]);
mCustomDebugDrawer[i]->setStateSet(stateset); mCustomDebugDrawer[i]->setStateSet(stateset);
@ -416,8 +416,8 @@ void MWRenderDebug::DebugDrawer::addDrawCall(const DrawCall& draw)
void MWRenderDebug::DebugDrawer::addLine(const osg::Vec3& start, const osg::Vec3& end, const osg::Vec3 color) void MWRenderDebug::DebugDrawer::addLine(const osg::Vec3& start, const osg::Vec3& end, const osg::Vec3 color)
{ {
const int indexWrite = getIdexBufferWriteFromFrame(this->mCurrentFrame); const int indexWrite = getIdexBufferWriteFromFrame(this->mCurrentFrame);
auto vertices = static_cast<osg::Vec3Array*>(mDebugLines->mLinesGeom[indexWrite]->getVertexAttribArray(0)); auto vertices = static_cast<osg::Vec3Array*>(mDebugLines->mLinesGeom[indexWrite]->getVertexArray());
auto colors = static_cast<osg::Vec3Array*>(mDebugLines->mLinesGeom[indexWrite]->getVertexAttribArray(1)); auto colors = static_cast<osg::Vec3Array*>(mDebugLines->mLinesGeom[indexWrite]->getNormalArray());
vertices->push_back(start); vertices->push_back(start);
vertices->push_back(end); vertices->push_back(end);

@ -46,8 +46,8 @@ set(SHADER_FILES
gui_fragment.glsl gui_fragment.glsl
debug_vertex.glsl debug_vertex.glsl
debug_fragment.glsl debug_fragment.glsl
debugDraw_vertex.glsl debugdraw_vertex.glsl
debugDraw_fragment.glsl debugdraw_fragment.glsl
sky_vertex.glsl sky_vertex.glsl
sky_fragment.glsl sky_fragment.glsl
skypasses.glsl skypasses.glsl

@ -1,15 +0,0 @@
#version 330 compatibility
in vec3 vertexColor;
in vec3 vertexNormal;
out vec4 fragColor;
void main()
{
vec3 lightDir = normalize(vec3(-1., -0.5, -2.));
float lightAttenuation = dot(-lightDir, vertexNormal) * 0.5 + 0.5;
fragColor = vec4(vertexColor * lightAttenuation, 1.);
}

@ -1,22 +0,0 @@
#version 330 compatibility
uniform mat4 projectionMatrix;
uniform vec3 color;
uniform vec3 trans;
uniform vec3 scale;
uniform int useNormalAsColor;
layout(location = 0) in vec3 aPos;
layout(location = 1) in vec3 aNormal;
out vec3 vertexColor;
out vec3 vertexNormal;
void main()
{
gl_Position = projectionMatrix * gl_ModelViewMatrix * vec4(aPos * scale + trans, 1.);
vertexNormal = useNormalAsColor == 1 ? vec3(1., 1., 1.) : aNormal;
vertexColor = useNormalAsColor == 1 ? aNormal : color.xyz;
}

@ -2,7 +2,22 @@
#include "vertexcolors.glsl" #include "vertexcolors.glsl"
varying vec3 vertexNormal;
uniform int useAdvancedShader = 0;
void main() void main()
{ {
gl_FragData[0] = getDiffuseColor(); vec3 lightDir = normalize(vec3(-1., -0.5, -2.));
float lightAttenuation = dot(-lightDir, vertexNormal) * 0.5 + 0.5;
if(useAdvancedShader == 0)
{
gl_FragData[0] = getDiffuseColor();
}
else
{
gl_FragData[0] = vec4(passColor.xyz * lightAttenuation, 1.);
}
} }

@ -2,11 +2,28 @@
#include "openmw_vertex.h.glsl" #include "openmw_vertex.h.glsl"
uniform vec3 color;
uniform vec3 trans;
uniform vec3 scale;
uniform int useNormalAsColor;
uniform int useAdvancedShader = 0;
centroid varying vec4 passColor; centroid varying vec4 passColor;
varying vec3 vertexNormal;
void main() void main()
{ {
gl_Position = mw_modelToClip(gl_Vertex); gl_Position = mw_modelToClip( vec4(gl_Vertex.xyz * scale + trans,1));
if(useAdvancedShader == 0)
passColor = gl_Color; {
vertexNormal = vec3(1., 1., 1.);
passColor = gl_Color;
}
else
{
vertexNormal = useNormalAsColor == 1 ? vec3(1., 1., 1.) : gl_Normal.xyz;
vec3 colorOut = useNormalAsColor == 1 ? gl_Normal.xyz : color;
passColor = vec4(colorOut, 1.);
}
} }

@ -0,0 +1,23 @@
#version 120
#include "vertexcolors.glsl"
varying vec3 vertexColor;
varying vec3 vertexNormal;
uniform int useAdvancedShader = 0;
void main()
{
vec3 lightDir = normalize(vec3(-1., -0.5, -2.));
float lightAttenuation = dot(-lightDir, vertexNormal) * 0.5 + 0.5;
if(useAdvancedShader == 0)
{
gl_FragData[0] = getDiffuseColor();
}
else
{
gl_FragData[0] = vec4(vertexColor * lightAttenuation, 1.);
}
}

@ -0,0 +1,27 @@
#version 120
#include "openmw_vertex.h.glsl"
uniform vec3 color;
uniform vec3 trans;
uniform vec3 scale;
uniform int useNormalAsColor;
uniform int useAdvancedShader = 0;
varying vec3 vertexColor;
varying vec3 vertexNormal;
void main()
{
gl_Position = mw_modelToClip( vec4(gl_Vertex.xyz * scale + trans,1));
if(useAdvancedShader == 0)
{
vertexNormal = vec3(1., 1., 1.);
vertexColor = gl_Color.xyz;
}
else
{
vertexNormal = useNormalAsColor == 1 ? vec3(1., 1., 1.) : gl_Normal.xyz;
vertexColor = useNormalAsColor == 1 ? gl_Normal.xyz : color.xyz;
}
}