Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support using custom shader materials and updating uniform variables (ogre2) #520

Merged
merged 5 commits into from
Jan 4, 2022
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions examples/custom_shaders_uniforms/GlutWindow.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -200,6 +202,8 @@ void initUniforms()
ir::VisualPtr sphere =
std::dynamic_pointer_cast<ir::Visual>(node->ChildByName("box"));
ir::MaterialPtr shader = sphere->Material();
if (!shader)
return;
g_shaderParams = shader->FragmentShaderParams();

(*g_shaderParams)["u_seed"].InitializeBuffer(1);
Expand Down
76 changes: 61 additions & 15 deletions examples/custom_shaders_uniforms/Main.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -64,6 +68,19 @@ void buildScene(ScenePtr _scene)
light0->SetSpecularColor(0.5, 0.5, 0.5);
root->AddChild(light0);

t std::string vertexShaderFile;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
t std::string vertexShaderFile;
std::string vertexShaderFile;

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oops, fixed. 00d56a1

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(
Expand Down Expand Up @@ -103,18 +120,19 @@ void buildScene(ScenePtr _scene)
}

//////////////////////////////////////////////////
CameraPtr createCamera(const std::string &_engineName)
CameraPtr createCamera(const std::string &_engineName,
const std::map<std::string, std::string>& _params)
{
// create and populate scene
RenderEngine *engine = rendering::engine(_engineName);
RenderEngine *engine = rendering::engine(_engineName, _params);
if (!engine)
{
std::cout << "Engine '" << _engineName
<< "' is not supported" << std::endl;
return CameraPtr();
}
ScenePtr scene = engine->CreateScene("scene");
buildScene(scene);
buildScene(scene, _engineName);

// return camera sensor
SensorPtr sensor = scene->SensorByName("camera");
Expand All @@ -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<std::string> engineNames;
std::vector<CameraPtr> 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<std::string, std::string> 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);
Expand Down
23 changes: 20 additions & 3 deletions examples/custom_shaders_uniforms/media/fragment_shader.glsl
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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);
}
43 changes: 43 additions & 0 deletions examples/custom_shaders_uniforms/media/fragment_shader_330.glsl
Original file line number Diff line number Diff line change
@@ -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);
}
21 changes: 19 additions & 2 deletions examples/custom_shaders_uniforms/media/vertex_shader.glsl
Original file line number Diff line number Diff line change
@@ -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;
}
35 changes: 35 additions & 0 deletions examples/custom_shaders_uniforms/media/vertex_shader_330.glsl
Original file line number Diff line number Diff line change
@@ -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;
}

35 changes: 35 additions & 0 deletions ogre2/include/ignition/rendering/ogre2/Ogre2Material.hh
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
#pragma warning(push, 0)
#endif
#include <Hlms/Pbs/OgreHlmsPbsPrerequisites.h>
#include <OgreGpuProgramParams.h>
#include <OgreMaterial.h>
#ifdef _MSC_VER
#pragma warning(pop)
Expand Down Expand Up @@ -241,6 +242,31 @@ 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,
Expand All @@ -259,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;

Expand Down
Loading