diff --git a/components/debug/debugdraw.cpp b/components/debug/debugdraw.cpp index 1b1d7bbf4f..ee29dc95f5 100644 --- a/components/debug/debugdraw.cpp +++ b/components/debug/debugdraw.cpp @@ -32,9 +32,6 @@ static void generateWireCube(osg::Geometry& geom, float dim) osg::ref_ptr vertices = new osg::Vec3Array; osg::ref_ptr 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) }; 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(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.)); } + geom.setVertexArray( vertices); + geom.setNormalArray( normals, osg::Array::BIND_PER_VERTEX); 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]); } } - geom.setVertexAttribArray(0, vertices, osg::Array::BIND_PER_VERTEX); - geom.setVertexAttribArray(1, normals, osg::Array::BIND_PER_VERTEX); + geom.setVertexArray(vertices); + geom.setNormalArray( normals, osg::Array::BIND_PER_VERTEX); geom.addPrimitiveSet(indices); } @@ -209,8 +208,8 @@ static void generateCylinder(osg::Geometry& geom, float radius, float height, in indices->push_back(bot2); } - geom.setVertexAttribArray(0, vertices, osg::Array::BIND_PER_VERTEX); - geom.setVertexAttribArray(1, normals, osg::Array::BIND_PER_VERTEX); + geom.setVertexArray(vertices); + geom.setNormalArray(normals, osg::Array::BIND_PER_VERTEX); geom.addPrimitiveSet(indices); } @@ -240,10 +239,10 @@ namespace MWRenderDebug return; } - osg::Uniform* uTrans = const_cast(stateSet->getUniform("trans")); - osg::Uniform* uCol = const_cast(stateSet->getUniform("color")); - osg::Uniform* uScale = const_cast(stateSet->getUniform("scale")); - osg::Uniform* uUseNormalAsColor = const_cast(stateSet->getUniform("useNormalAsColor")); + const osg::Uniform* uTrans = stateSet->getUniform("trans"); + const osg::Uniform* uCol = stateSet->getUniform("color"); + const osg::Uniform* uScale = stateSet->getUniform("scale"); + const osg::Uniform* uUseNormalAsColor = stateSet->getUniform("useNormalAsColor"); auto transLocation = pcp->getUniformLocation(uTrans->getNameID()); auto colLocation = pcp->getUniformLocation(uCol->getNameID()); @@ -283,8 +282,8 @@ namespace MWRenderDebug } } mShapesToDraw.clear(); - static_cast(mLinesToDraw->getVertexAttribArray(0))->clear(); - static_cast(mLinesToDraw->getVertexAttribArray(1))->clear(); + static_cast(mLinesToDraw->getVertexArray())->clear(); + static_cast(mLinesToDraw->getNormalArray())->clear(); } struct DebugLines @@ -299,8 +298,8 @@ namespace MWRenderDebug 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.setVertexArray( vertices); + lines.setNormalArray(color, osg::Array::BIND_PER_VERTEX); lines.addPrimitiveSet(new osg::DrawArrays(osg::PrimitiveSet::LINES, 0, vertices->size())); } @@ -328,7 +327,7 @@ namespace MWRenderDebug int indexRead = getIdexBufferReadFromFrame(mDebugDrawer.mCurrentFrame); auto& lines = mDebugDrawer.mDebugLines; lines->mLinesGeom[indexRead]->removePrimitiveSet(0, 1); - lines->mLinesGeom[indexRead]->addPrimitiveSet(new osg::DrawArrays(osg::PrimitiveSet::LINES, 0, static_cast(lines->mLinesGeom[indexRead]->getVertexAttribArray(0))->size())); + lines->mLinesGeom[indexRead]->addPrimitiveSet(new osg::DrawArrays(osg::PrimitiveSet::LINES, 0, static_cast(lines->mLinesGeom[indexRead]->getVertexArray())->size())); nv->pushOntoNodePath(mDebugDrawer.mCustomDebugDrawer[indexRead]); nv->apply(*mDebugDrawer.mCustomDebugDrawer[indexRead]); @@ -342,8 +341,8 @@ namespace MWRenderDebug MWRenderDebug::DebugDrawer::DebugDrawer(Shader::ShaderManager& shaderManager, osg::ref_ptr parentNode) { mCurrentFrame = 0; - 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 vertexShader = shaderManager.getShader("debug_vertex.glsl", Shader::ShaderManager::DefineMap(), osg::Shader::Type::VERTEX); + auto fragmentShader = shaderManager.getShader("debug_fragment.glsl", Shader::ShaderManager::DefineMap(), osg::Shader::Type::FRAGMENT); auto program = shaderManager.getProgram(vertexShader, fragmentShader); mDebugLines = std::make_unique(); @@ -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("scale", osg::Vec3f(1., 1., 1.))); stateset->addUniform(new osg::Uniform("useNormalAsColor", 0)); + stateset->addUniform(new osg::Uniform("useAdvancedShader", 1)); stateset->setAttributeAndModes(program, osg::StateAttribute::ON); stateset->setMode(GL_DEPTH_TEST, GL_TRUE); @@ -375,7 +375,7 @@ MWRenderDebug::DebugDrawer::DebugDrawer(Shader::ShaderManager& shaderManager, os wireCube->setUseVertexBufferObjects(true); 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]->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) { const int indexWrite = getIdexBufferWriteFromFrame(this->mCurrentFrame); - auto vertices = static_cast(mDebugLines->mLinesGeom[indexWrite]->getVertexAttribArray(0)); - auto colors = static_cast(mDebugLines->mLinesGeom[indexWrite]->getVertexAttribArray(1)); + auto vertices = static_cast(mDebugLines->mLinesGeom[indexWrite]->getVertexArray()); + auto colors = static_cast(mDebugLines->mLinesGeom[indexWrite]->getNormalArray()); vertices->push_back(start); vertices->push_back(end); diff --git a/files/shaders/CMakeLists.txt b/files/shaders/CMakeLists.txt index c171834e82..93a627c887 100644 --- a/files/shaders/CMakeLists.txt +++ b/files/shaders/CMakeLists.txt @@ -46,8 +46,8 @@ set(SHADER_FILES gui_fragment.glsl debug_vertex.glsl debug_fragment.glsl - debugDraw_vertex.glsl - debugDraw_fragment.glsl + debugdraw_vertex.glsl + debugdraw_fragment.glsl sky_vertex.glsl sky_fragment.glsl skypasses.glsl diff --git a/files/shaders/debugDraw_fragment.glsl b/files/shaders/debugDraw_fragment.glsl deleted file mode 100644 index 10309a3d9d..0000000000 --- a/files/shaders/debugDraw_fragment.glsl +++ /dev/null @@ -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.); -} diff --git a/files/shaders/debugDraw_vertex.glsl b/files/shaders/debugDraw_vertex.glsl deleted file mode 100644 index 30628738b9..0000000000 --- a/files/shaders/debugDraw_vertex.glsl +++ /dev/null @@ -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; -} diff --git a/files/shaders/debug_fragment.glsl b/files/shaders/debug_fragment.glsl index 1b25510d66..92ba2b6b94 100644 --- a/files/shaders/debug_fragment.glsl +++ b/files/shaders/debug_fragment.glsl @@ -2,7 +2,22 @@ #include "vertexcolors.glsl" +varying vec3 vertexNormal; + +uniform int useAdvancedShader = 0; + 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.); + } } diff --git a/files/shaders/debug_vertex.glsl b/files/shaders/debug_vertex.glsl index fd41a6ff48..dbadb64788 100644 --- a/files/shaders/debug_vertex.glsl +++ b/files/shaders/debug_vertex.glsl @@ -2,11 +2,28 @@ #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; +varying vec3 vertexNormal; void main() { - gl_Position = mw_modelToClip(gl_Vertex); - - passColor = gl_Color; + gl_Position = mw_modelToClip( vec4(gl_Vertex.xyz * scale + trans,1)); + if(useAdvancedShader == 0) + { + 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.); + } + } diff --git a/files/shaders/debugdraw_fragment.glsl b/files/shaders/debugdraw_fragment.glsl new file mode 100644 index 0000000000..6bde7ede82 --- /dev/null +++ b/files/shaders/debugdraw_fragment.glsl @@ -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.); + } +} diff --git a/files/shaders/debugdraw_vertex.glsl b/files/shaders/debugdraw_vertex.glsl new file mode 100644 index 0000000000..d48a1b2cb1 --- /dev/null +++ b/files/shaders/debugdraw_vertex.glsl @@ -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; + } +}