From 529d25306ef29fcb30429690b71e687efecab501 Mon Sep 17 00:00:00 2001 From: Ian Chen Date: Wed, 22 Dec 2021 14:41:15 -0800 Subject: [PATCH 1/5] testing Signed-off-by: Ian Chen --- .../ignition/rendering/ogre2/Ogre2Material.hh | 26 ++++ ogre2/src/Ogre2Material.cc | 115 ++++++++++++++++++ 2 files changed, 141 insertions(+) diff --git a/ogre2/include/ignition/rendering/ogre2/Ogre2Material.hh b/ogre2/include/ignition/rendering/ogre2/Ogre2Material.hh index 75716d4d3..67f16a656 100644 --- a/ogre2/include/ignition/rendering/ogre2/Ogre2Material.hh +++ b/ogre2/include/ignition/rendering/ogre2/Ogre2Material.hh @@ -241,6 +241,32 @@ namespace ignition // Documentation inherited public: virtual void SetDepthWriteEnabled(bool _enabled) override; + // Documentation inherited. + // \sa Material::SetVertexShader(const std::string &) + public: virtual void SetVertexShader(const std::string &_path) override; + + // Documentation inherited. + // \sa Material::VertexShader() const + public: virtual std::string VertexShader() const override; + + // Documentation inherited. + // \sa Material::VertexShaderParams() + public: virtual ShaderParamsPtr VertexShaderParams() override; + + // Documentation inherited. + // \sa Material::SetFragmentShader(const std::string &) + public: virtual void SetFragmentShader(const std::string &_path) + override; + + // Documentation inherited. + // \sa Material::FragmentShader() const + public: virtual std::string FragmentShader() const override; + + // Documentation inherited. + // \sa Material::FragmentShaderParams() + public: virtual ShaderParamsPtr FragmentShaderParams() override; + + /// \brief Set the texture map for this material /// \param[in] _texture Name of the texture. /// \param[in] _type Type of texture, i.e. diffuse, normal, roughness, diff --git a/ogre2/src/Ogre2Material.cc b/ogre2/src/Ogre2Material.cc index 66f6e29f0..6f66b9dca 100644 --- a/ogre2/src/Ogre2Material.cc +++ b/ogre2/src/Ogre2Material.cc @@ -24,9 +24,12 @@ #include #include #include +#include +#include #include #include #include +#include #include #include #ifdef _MSC_VER @@ -51,6 +54,18 @@ class ignition::rendering::Ogre2MaterialPrivate /// \brief Ogre stores the name using hashes. This variable will /// store the material hash name public: std::string hashName; + + /// \brief Path to vertex shader program. + public: std::string vertexShaderPath; + + /// \brief Path to fragment shader program. + public: std::string fragmentShaderPath; + + /// \brief Parameters to be bound to the vertex shader + public: ShaderParamsPtr vertexShaderParams; + + /// \brief Parameters to be bound to the fragment shader + public: ShaderParamsPtr fragmentShaderParams; }; using namespace ignition; @@ -903,3 +918,103 @@ void Ogre2Material::FillUnlitDatablock(Ogre::HlmsUnlitDatablock *_datablock) Ogre::Vector3 c = this->ogreDatablock->getDiffuse(); _datablock->setColour(Ogre::ColourValue(c.x, c.y, c.z)); } + +////////////////////////////////////////////////// +void Ogre2Material::SetVertexShader(const std::string &_path) +{ + if (_path.empty()) + return; + + if (!common::exists(_path)) + { + ignerr << "Vertex shader path does not exist: " << _path << std::endl; + return; + } + + Ogre::ResourceGroupManager::getSingleton().addResourceLocation(_path, + "FileSystem", "General", false); + + Ogre::HighLevelGpuProgramPtr vertexShader = + Ogre::HighLevelGpuProgramManager::getSingletonPtr()->createProgram( + "__ignition_rendering_vertex__" + _path, + Ogre::ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME, + "glsl", Ogre::GpuProgramType::GPT_VERTEX_PROGRAM); + + vertexShader->setSourceFile(_path); + vertexShader->load(); + + assert(vertexShader->isLoaded()); + assert(!(vertexShader->hasCompileError())); + assert(vertexShader->isSupported()); + + auto mat = this->Material(); + auto pass = mat->getTechnique(0u)->getPass(0); + pass->setVertexProgram(vertexShader->getName()); + mat->compile(); + mat->load(); + + this->dataPtr->vertexShaderPath = _path; + this->dataPtr->vertexShaderParams.reset(new ShaderParams); +} + +////////////////////////////////////////////////// +std::string Ogre2Material::VertexShader() const +{ + return this->dataPtr->vertexShaderPath; +} + +////////////////////////////////////////////////// +ShaderParamsPtr Ogre2Material::VertexShaderParams() +{ + return this->dataPtr->vertexShaderParams; +} + +////////////////////////////////////////////////// +void Ogre2Material::SetFragmentShader(const std::string &_path) +{ + if (_path.empty()) + return; + + if (!common::exists(_path)) + { + ignerr << "Fragment shader path does not exist: " << _path << std::endl; + return; + } + + Ogre::ResourceGroupManager::getSingleton().addResourceLocation(_path, + "FileSystem", "General", false); + + Ogre::HighLevelGpuProgramPtr fragmentShader = + Ogre::HighLevelGpuProgramManager::getSingleton().createProgram( + "__ignition_rendering_fragment__" + _path, + Ogre::ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME, + "glsl", Ogre::GpuProgramType::GPT_FRAGMENT_PROGRAM); + + fragmentShader->setSourceFile(_path); + fragmentShader->load(); + + assert(fragmentShader->isLoaded()); + assert(!(fragmentShader->hasCompileError())); + assert(fragmentShader->isSupported()); + + auto mat = this->Material(); + auto pass = mat->getTechnique(0u)->getPass(0); + pass->setFragmentProgram(fragmentShader->getName()); + // mat->setLightingEnabled(false); + mat->compile(); + mat->load(); + this->dataPtr->fragmentShaderPath = _path; + this->dataPtr->fragmentShaderParams.reset(new ShaderParams); +} + +////////////////////////////////////////////////// +std::string Ogre2Material::FragmentShader() const +{ + return this->dataPtr->fragmentShaderPath; +} + +////////////////////////////////////////////////// +ShaderParamsPtr Ogre2Material::FragmentShaderParams() +{ + return this->dataPtr->fragmentShaderParams; +} From 572feffb9006f3a47cb644299d811a96c8c65b5d Mon Sep 17 00:00:00 2001 From: Ian Chen Date: Thu, 23 Dec 2021 16:13:52 -0800 Subject: [PATCH 2/5] ogre2 demo working Signed-off-by: Ian Chen --- .../custom_shaders_uniforms/GlutWindow.cc | 4 + examples/custom_shaders_uniforms/Main.cc | 76 +++++++++++++---- .../media/fragment_shader.glsl | 23 +++++- .../media/vertex_shader.glsl | 21 ++++- .../ignition/rendering/ogre2/Ogre2Material.hh | 11 ++- ogre2/src/Ogre2Material.cc | 81 ++++++++++++++++++- ogre2/src/Ogre2Mesh.cc | 13 ++- src/ShaderParam.cc | 2 +- 8 files changed, 203 insertions(+), 28 deletions(-) diff --git a/examples/custom_shaders_uniforms/GlutWindow.cc b/examples/custom_shaders_uniforms/GlutWindow.cc index 620125ad2..8b3b8a112 100644 --- a/examples/custom_shaders_uniforms/GlutWindow.cc +++ b/examples/custom_shaders_uniforms/GlutWindow.cc @@ -111,6 +111,8 @@ void updateCameras() //! [update uniforms] void updateUniforms() { + if (!g_shaderParams) + return; (*g_shaderParams)["u_seed"].UpdateBuffer(g_seed); (*g_shaderParams)["u_resolution"].UpdateBuffer(g_resolution); (*g_shaderParams)["u_color"].UpdateBuffer(g_color); @@ -200,6 +202,8 @@ void initUniforms() ir::VisualPtr sphere = std::dynamic_pointer_cast(node->ChildByName("box")); ir::MaterialPtr shader = sphere->Material(); + if (!shader) + return; g_shaderParams = shader->FragmentShaderParams(); (*g_shaderParams)["u_seed"].InitializeBuffer(1); diff --git a/examples/custom_shaders_uniforms/Main.cc b/examples/custom_shaders_uniforms/Main.cc index 2ee5424a6..246d4e647 100644 --- a/examples/custom_shaders_uniforms/Main.cc +++ b/examples/custom_shaders_uniforms/Main.cc @@ -43,15 +43,19 @@ using namespace ignition; using namespace rendering; -const std::string vertexShaderFile = "vertex_shader.glsl"; -const std::string fragmentShaderFile = "fragment_shader.glsl"; +const std::string vertexShaderGLSLFile = "vertex_shader.glsl"; +const std::string fragmentShaderGLSLFile = "fragment_shader.glsl"; + +const std::string vertexShaderGLSL330File = "vertex_shader_330.glsl"; +const std::string fragmentShaderGLSL330File = "fragment_shader_330.glsl"; + //! [init shaders variables] const std::string RESOURCE_PATH = ignition::common::joinPaths(std::string(PROJECT_BINARY_PATH), "media"); ////////////////////////////////////////////////// -void buildScene(ScenePtr _scene) +void buildScene(ScenePtr _scene, const std::string &_engineName) { // initialize _scene _scene->SetAmbientLight(0.3, 0.3, 0.3); @@ -64,6 +68,19 @@ void buildScene(ScenePtr _scene) light0->SetSpecularColor(0.5, 0.5, 0.5); root->AddChild(light0); + t std::string vertexShaderFile; + std::string fragmentShaderFile; + if (_engineName == "ogre2") + { + vertexShaderFile = vertexShaderGLSL330File; + fragmentShaderFile = fragmentShaderGLSL330File; + } + else + { + vertexShaderFile = vertexShaderGLSLFile; + fragmentShaderFile = fragmentShaderGLSLFile; + } + // create shader materials // path to look for vertex and fragment shader parameters std::string vertexShaderPath = ignition::common::joinPaths( @@ -103,10 +120,11 @@ void buildScene(ScenePtr _scene) } ////////////////////////////////////////////////// -CameraPtr createCamera(const std::string &_engineName) +CameraPtr createCamera(const std::string &_engineName, + const std::map& _params) { // create and populate scene - RenderEngine *engine = rendering::engine(_engineName); + RenderEngine *engine = rendering::engine(_engineName, _params); if (!engine) { std::cout << "Engine '" << _engineName @@ -114,7 +132,7 @@ CameraPtr createCamera(const std::string &_engineName) return CameraPtr(); } ScenePtr scene = engine->CreateScene("scene"); - buildScene(scene); + buildScene(scene, _engineName); // return camera sensor SensorPtr sensor = scene->SensorByName("camera"); @@ -126,22 +144,50 @@ int main(int _argc, char** _argv) { glutInit(&_argc, _argv); + + // Expose engine name to command line because we can't instantiate both + // ogre and ogre2 at the same time + std::string ogreEngineName("ogre"); + if (_argc > 1) + { + ogreEngineName = _argv[1]; + } + + GraphicsAPI graphicsApi = GraphicsAPI::OPENGL; + if (_argc > 2) + { + graphicsApi = GraphicsAPIUtils::Set(std::string(_argv[2])); + } + common::Console::SetVerbosity(4); + std::vector engineNames; std::vector cameras; - std::string engine("ogre"); + engineNames.push_back(ogreEngineName); - try + for (auto engineName : engineNames) { - CameraPtr camera = createCamera(engine); - if (camera) + try { - cameras.push_back(camera); + std::map params; + if (engineName.compare("ogre2") == 0 + && graphicsApi == GraphicsAPI::METAL) + { + // \todo(anyone) uncomment once metal shaders are available + // params["metal"] = "1"; + ignerr << "Metal shaders are not implemented yet. Using GSLS" << std::endl; + } + + CameraPtr camera = createCamera(engineName, params); + if (camera) + { + cameras.push_back(camera); + } + } + catch (...) + { + std::cerr << "Error starting up: " << engineName << std::endl; } - } - catch (...) - { - std::cerr << "Error starting up: " << engine << std::endl; } run(cameras); diff --git a/examples/custom_shaders_uniforms/media/fragment_shader.glsl b/examples/custom_shaders_uniforms/media/fragment_shader.glsl index 19f1e2a51..f4a62a908 100644 --- a/examples/custom_shaders_uniforms/media/fragment_shader.glsl +++ b/examples/custom_shaders_uniforms/media/fragment_shader.glsl @@ -1,6 +1,23 @@ +/* + * Copyright (C) 2021 Open Source Robotics Foundation + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + #define M_PI 3.1415926535897932384626433832795 -varying vec3 interpolatedPosition; +varying vec4 interpolatedPosition; uniform int u_seed; uniform vec2 u_resolution; @@ -14,9 +31,9 @@ float random(vec2 uv, float seed) { void main() { vec3 a = vec3(u_adjustments[0][0], u_adjustments[1][0], u_adjustments[2][0]); - vec2 b = vec2(distance(interpolatedPosition, a)) * u_adjustments[3][0]; + vec2 b = vec2(distance(vec3(interpolatedPosition.xyw), a)) * u_adjustments[3][0]; vec2 normalizedFragCoord = b / u_resolution; - vec3 color = vec3(random(normalizedFragCoord, u_seed)); + vec3 color = vec3(random(normalizedFragCoord, float(u_seed))); gl_FragColor = vec4(color * u_color, 1.0); } diff --git a/examples/custom_shaders_uniforms/media/vertex_shader.glsl b/examples/custom_shaders_uniforms/media/vertex_shader.glsl index 1d43a378d..14d3af760 100644 --- a/examples/custom_shaders_uniforms/media/vertex_shader.glsl +++ b/examples/custom_shaders_uniforms/media/vertex_shader.glsl @@ -1,7 +1,24 @@ -varying vec3 interpolatedPosition; +/* + * Copyright (C) 2021 Open Source Robotics Foundation + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +varying vec4 interpolatedPosition; void main() { gl_Position = ftransform(); - interpolatedPosition = gl_Position.xyz; + interpolatedPosition = gl_Position; } diff --git a/ogre2/include/ignition/rendering/ogre2/Ogre2Material.hh b/ogre2/include/ignition/rendering/ogre2/Ogre2Material.hh index 67f16a656..dcf4c9fd8 100644 --- a/ogre2/include/ignition/rendering/ogre2/Ogre2Material.hh +++ b/ogre2/include/ignition/rendering/ogre2/Ogre2Material.hh @@ -27,6 +27,7 @@ #pragma warning(push, 0) #endif #include +#include #include #ifdef _MSC_VER #pragma warning(pop) @@ -266,7 +267,6 @@ namespace ignition // \sa Material::FragmentShaderParams() public: virtual ShaderParamsPtr FragmentShaderParams() override; - /// \brief Set the texture map for this material /// \param[in] _texture Name of the texture. /// \param[in] _type Type of texture, i.e. diffuse, normal, roughness, @@ -285,6 +285,15 @@ namespace ignition // Documentation inherited. protected: virtual void Init() override; + /// \brief bind shader parameters that have changed + protected: void UpdateShaderParams(); + + /// \brief Transfer params from ign-rendering type to ogre type + /// \param[in] _params ignition rendering params + /// \param[out] _ogreParams ogre type for holding params + protected: void UpdateShaderParams(ConstShaderParamsPtr _params, + Ogre::GpuProgramParametersSharedPtr _ogreParams); + /// \brief Ogre material. Mainly used for render targets. protected: Ogre::MaterialPtr ogreMaterial; diff --git a/ogre2/src/Ogre2Material.cc b/ogre2/src/Ogre2Material.cc index 6f66b9dca..230ac7894 100644 --- a/ogre2/src/Ogre2Material.cc +++ b/ogre2/src/Ogre2Material.cc @@ -604,6 +604,75 @@ float Ogre2Material::Metalness() const ////////////////////////////////////////////////// void Ogre2Material::PreRender() { + this->UpdateShaderParams(); +} + +////////////////////////////////////////////////// +void Ogre2Material::UpdateShaderParams() +{ + if (this->dataPtr->vertexShaderParams && + this->dataPtr->vertexShaderParams->IsDirty()) + { + Ogre::GpuProgramParametersSharedPtr ogreParams; + auto mat = this->Material(); + auto pass = mat->getTechnique(0u)->getPass(0); + ogreParams = pass->getVertexProgramParameters(); + this->UpdateShaderParams(this->dataPtr->vertexShaderParams, ogreParams); + this->dataPtr->vertexShaderParams->ClearDirty(); + } + if (this->dataPtr->fragmentShaderParams && + this->dataPtr->fragmentShaderParams->IsDirty()) + { + Ogre::GpuProgramParametersSharedPtr ogreParams; + auto mat = this->Material(); + auto pass = mat->getTechnique(0u)->getPass(0); + ogreParams = pass->getFragmentProgramParameters(); + this->UpdateShaderParams(this->dataPtr->fragmentShaderParams, ogreParams); + this->dataPtr->fragmentShaderParams->ClearDirty(); + } +} + +////////////////////////////////////////////////// +void Ogre2Material::UpdateShaderParams(ConstShaderParamsPtr _params, + Ogre::GpuProgramParametersSharedPtr _ogreParams) +{ + for (const auto name_param : *_params) + { + if (ShaderParam::PARAM_FLOAT == name_param.second.Type()) + { + float value; + name_param.second.Value(&value); + _ogreParams->setNamedConstant(name_param.first, value); + } + else if (ShaderParam::PARAM_INT == name_param.second.Type()) + { + int value; + name_param.second.Value(&value); + _ogreParams->setNamedConstant(name_param.first, value); + } + else if (ShaderParam::PARAM_FLOAT_BUFFER == name_param.second.Type()) + { + std::shared_ptr buffer; + name_param.second.Buffer(buffer); + uint32_t count = name_param.second.Count(); + + // multiple other than 4 is currently only supported by GLSL + uint32_t multiple = 1; + _ogreParams->setNamedConstant(name_param.first, + reinterpret_cast(buffer.get()), count, multiple); + } + else if (ShaderParam::PARAM_INT_BUFFER == name_param.second.Type()) + { + std::shared_ptr buffer; + name_param.second.Buffer(buffer); + uint32_t count = name_param.second.Count(); + + // multiple other than 4 is currently only supported by GLSL + uint32_t multiple = 1; + _ogreParams->setNamedConstant(name_param.first, + reinterpret_cast(buffer.get()), count, multiple); + } + } } ////////////////////////////////////////////////// @@ -684,7 +753,6 @@ void Ogre2Material::SetTextureMapImpl(const std::string &_texture, // set a custom name for the rgb texture by appending ign_ prefix std::string rgbTexName = "ign_" + baseName; baseName = rgbTexName; - std::string filename = common::joinPaths(parentPath, rgbTexName); auto tex = textureMgr->findTextureNoThrow(rgbTexName); if (!tex) { @@ -936,11 +1004,17 @@ void Ogre2Material::SetVertexShader(const std::string &_path) Ogre::HighLevelGpuProgramPtr vertexShader = Ogre::HighLevelGpuProgramManager::getSingletonPtr()->createProgram( - "__ignition_rendering_vertex__" + _path, + "_ign_" + _path, Ogre::ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME, "glsl", Ogre::GpuProgramType::GPT_VERTEX_PROGRAM); vertexShader->setSourceFile(_path); + + Ogre::GpuProgramParametersSharedPtr params = + vertexShader->getDefaultParameters(); + params->setNamedAutoConstant("worldViewProj", + Ogre::GpuProgramParameters::ACT_WORLDVIEWPROJ_MATRIX); + vertexShader->load(); assert(vertexShader->isLoaded()); @@ -986,7 +1060,7 @@ void Ogre2Material::SetFragmentShader(const std::string &_path) Ogre::HighLevelGpuProgramPtr fragmentShader = Ogre::HighLevelGpuProgramManager::getSingleton().createProgram( - "__ignition_rendering_fragment__" + _path, + "_ign_" + _path, Ogre::ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME, "glsl", Ogre::GpuProgramType::GPT_FRAGMENT_PROGRAM); @@ -1000,7 +1074,6 @@ void Ogre2Material::SetFragmentShader(const std::string &_path) auto mat = this->Material(); auto pass = mat->getTechnique(0u)->getPass(0); pass->setFragmentProgram(fragmentShader->getName()); - // mat->setLightingEnabled(false); mat->compile(); mat->load(); this->dataPtr->fragmentShaderPath = _path; diff --git a/ogre2/src/Ogre2Mesh.cc b/ogre2/src/Ogre2Mesh.cc index cff027eb3..1b097fe0a 100644 --- a/ogre2/src/Ogre2Mesh.cc +++ b/ogre2/src/Ogre2Mesh.cc @@ -346,8 +346,17 @@ void Ogre2SubMesh::SetMaterialImpl(MaterialPtr _material) return; } - this->ogreSubItem->setDatablock( - static_cast(derived->Datablock())); + // low level material with custom shaders + if (!derived->FragmentShader().empty() && !derived->VertexShader().empty()) + { + this->ogreSubItem->setMaterial(derived->Material()); + } + // Pbs Hlms material + else + { + this->ogreSubItem->setDatablock( + static_cast(derived->Datablock())); + } // set cast shadows this->ogreSubItem->getParent()->setCastShadows(_material->CastShadows()); diff --git a/src/ShaderParam.cc b/src/ShaderParam.cc index 1dd4b0f6b..09faf9b89 100644 --- a/src/ShaderParam.cc +++ b/src/ShaderParam.cc @@ -37,7 +37,7 @@ class ignition::rendering::ShaderParamPrivate public: std::shared_ptr buffer; /// \brief Count of elements in buffer of parameter held - public: uint32_t count; + public: uint32_t count = 0u; }; From dc4e4c1f480e109bd9b15920300d2c9e49796f36 Mon Sep 17 00:00:00 2001 From: Ian Chen Date: Thu, 23 Dec 2021 16:16:00 -0800 Subject: [PATCH 3/5] add glsl 330 files Signed-off-by: Ian Chen --- .../media/fragment_shader_330.glsl | 43 +++++++++++++++++++ .../media/vertex_shader_330.glsl | 35 +++++++++++++++ 2 files changed, 78 insertions(+) create mode 100644 examples/custom_shaders_uniforms/media/fragment_shader_330.glsl create mode 100644 examples/custom_shaders_uniforms/media/vertex_shader_330.glsl diff --git a/examples/custom_shaders_uniforms/media/fragment_shader_330.glsl b/examples/custom_shaders_uniforms/media/fragment_shader_330.glsl new file mode 100644 index 000000000..9e7b2a7d5 --- /dev/null +++ b/examples/custom_shaders_uniforms/media/fragment_shader_330.glsl @@ -0,0 +1,43 @@ +/* + * Copyright (C) 2021 Open Source Robotics Foundation + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +#version 330 + +#define M_PI 3.1415926535897932384626433832795 + +uniform int u_seed; +uniform vec2 u_resolution; +uniform vec3 u_color; +uniform mat4 u_adjustments; + +in vec4 interpolatedPosition; + +out vec4 fragColor; + +float random(vec2 uv, float seed) { + return fract(sin(mod(dot(uv, vec2(12.9898, 78.233)) + 1113.1 * seed, M_PI)) * 43758.5453);; +} + +void main() +{ + vec3 a = vec3(u_adjustments[0][0], u_adjustments[1][0], u_adjustments[2][0]); + vec2 b = vec2(distance(vec3(interpolatedPosition.xyw), a)) * u_adjustments[3][0]; + vec2 normalizedFragCoord = b / u_resolution; + + vec3 color = vec3(random(normalizedFragCoord, float(u_seed))); + fragColor = vec4(color * u_color, 1.0); +} diff --git a/examples/custom_shaders_uniforms/media/vertex_shader_330.glsl b/examples/custom_shaders_uniforms/media/vertex_shader_330.glsl new file mode 100644 index 000000000..7fa26eb00 --- /dev/null +++ b/examples/custom_shaders_uniforms/media/vertex_shader_330.glsl @@ -0,0 +1,35 @@ +/* + * Copyright (C) 2021 Open Source Robotics Foundation + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +#version 330 + +in vec4 vertex; +uniform mat4 worldViewProj; + +out gl_PerVertex +{ + vec4 gl_Position; +}; + +out vec4 interpolatedPosition; + +void main() +{ + gl_Position = worldViewProj * vertex; + interpolatedPosition = gl_Position; +} + From 00d56a195be56f35678b5b98d164f355d4aaee4a Mon Sep 17 00:00:00 2001 From: Ian Chen Date: Thu, 23 Dec 2021 17:08:49 -0800 Subject: [PATCH 4/5] fix build Signed-off-by: Ian Chen --- examples/custom_shaders_uniforms/Main.cc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/custom_shaders_uniforms/Main.cc b/examples/custom_shaders_uniforms/Main.cc index 246d4e647..12704373b 100644 --- a/examples/custom_shaders_uniforms/Main.cc +++ b/examples/custom_shaders_uniforms/Main.cc @@ -68,7 +68,7 @@ void buildScene(ScenePtr _scene, const std::string &_engineName) light0->SetSpecularColor(0.5, 0.5, 0.5); root->AddChild(light0); - t std::string vertexShaderFile; + std::string vertexShaderFile; std::string fragmentShaderFile; if (_engineName == "ogre2") { From 9eb34219298f43f3e7d2e6a3ae4a37aad222422f Mon Sep 17 00:00:00 2001 From: Ian Chen Date: Thu, 23 Dec 2021 17:10:30 -0800 Subject: [PATCH 5/5] typo Signed-off-by: Ian Chen --- examples/custom_shaders_uniforms/Main.cc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/custom_shaders_uniforms/Main.cc b/examples/custom_shaders_uniforms/Main.cc index 12704373b..bfb37a76c 100644 --- a/examples/custom_shaders_uniforms/Main.cc +++ b/examples/custom_shaders_uniforms/Main.cc @@ -175,7 +175,7 @@ int main(int _argc, char** _argv) { // \todo(anyone) uncomment once metal shaders are available // params["metal"] = "1"; - ignerr << "Metal shaders are not implemented yet. Using GSLS" << std::endl; + ignerr << "Metal shaders are not implemented yet. Using GLSL" << std::endl; } CameraPtr camera = createCamera(engineName, params);