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

[OGRE 1.x] Uniform buffer shader support #294

Merged
merged 27 commits into from
May 15, 2021
Merged
Show file tree
Hide file tree
Changes from 17 commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
f09108e
Added example for custom shaders with different uniform types
WilliamLewww Mar 25, 2021
22f06aa
Added buffer shader parameter
WilliamLewww Mar 25, 2021
61a59cf
Added ogre uniform buffer update
WilliamLewww Mar 26, 2021
5531c66
Fixed uninitialized variables
WilliamLewww Mar 26, 2021
5c47586
Added matrix uniform update
WilliamLewww Mar 29, 2021
d9189fa
Fixed shader paths
WilliamLewww Mar 29, 2021
62a8cbf
Added command line argument to pick version of Ogre (#277)
WilliamLewww Mar 31, 2021
956a1e0
Merge branch 'ign-rendering4' into wlew/extend_shader_support
WilliamLewww Mar 31, 2021
c46e579
Fixed C-style cast and formatting issues
WilliamLewww Mar 31, 2021
db1c36d
Changed raw pointer buffer to shared pointer buffer
WilliamLewww Mar 31, 2021
478af43
Specified uniform buffer to be a word buffer
WilliamLewww Mar 31, 2021
a09cd40
Fixed codecheck errors
WilliamLewww Mar 31, 2021
50b1026
Added documentation for word buffer
WilliamLewww Mar 31, 2021
a255418
Added tests for word buffer shader parameter
WilliamLewww Mar 31, 2021
53c1a67
Fixed updating int uniform buffer
WilliamLewww Apr 1, 2021
53c2639
Added tests for int and float uniform buffers
WilliamLewww Apr 1, 2021
137f336
Updated example to show different types of uniforms
WilliamLewww Apr 1, 2021
b6f42e3
Apply suggestions from code review
WilliamLewww Apr 1, 2021
024efe3
Apply style suggestions
WilliamLewww Apr 1, 2021
f359fe5
Removed support for engines other than OGRE1
WilliamLewww Apr 1, 2021
55a900e
Changed "WordBuffer" to "Buffer"
WilliamLewww Apr 1, 2021
72fae87
Removed raw pointer when getting buffer
WilliamLewww Apr 1, 2021
ecbdd78
Merge branch 'ign-rendering4' into wlew/extend_shader_support
WilliamLewww Apr 19, 2021
1b2d49b
Added windows support
WilliamLewww Apr 20, 2021
8e71ce7
Merge branch 'wlew/extend_shader_support' of github.com:WilliamLewww/…
WilliamLewww Apr 20, 2021
10f3e18
Merge branch 'ign-rendering4' into wlew/extend_shader_support
WilliamLewww May 10, 2021
9209eab
Fixed code check formatting issues
WilliamLewww May 11, 2021
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
40 changes: 40 additions & 0 deletions examples/custom_shaders_uniforms/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
cmake_minimum_required(VERSION 3.10.2 FATAL_ERROR)
project(ignition-rendering-custom_shaders_uniforms)

include_directories(SYSTEM
${PROJECT_BINARY_DIR}
)

find_package(ignition-rendering4)

find_package(GLUT REQUIRED)
include_directories(SYSTEM ${GLUT_INCLUDE_DIRS})
link_directories(${GLUT_LIBRARY_DIRS})

find_package(OpenGL REQUIRED)
include_directories(SYSTEM ${OpenGL_INCLUDE_DIRS})
link_directories(${OpenGL_LIBRARY_DIRS})

if (NOT APPLE)
find_package(GLEW REQUIRED)
include_directories(SYSTEM ${GLEW_INCLUDE_DIRS})
link_directories(${GLEW_LIBRARY_DIRS})
endif()

configure_file (example_config.hh.in ${PROJECT_BINARY_DIR}/example_config.hh)

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wno-deprecated-declarations")

add_executable(custom_shaders_uniforms Main.cc GlutWindow.cc)

target_link_libraries(custom_shaders_uniforms
${GLUT_LIBRARIES}
${OPENGL_LIBRARIES}
${GLEW_LIBRARIES}
${IGNITION-RENDERING_LIBRARIES}
)
WilliamLewww marked this conversation as resolved.
Show resolved Hide resolved

add_custom_command(TARGET custom_shaders_uniforms POST_BUILD
COMMAND ${CMAKE_COMMAND} -E copy_directory
${CMAKE_SOURCE_DIR}/media
$<TARGET_FILE_DIR:custom_shaders_uniforms>/media)
248 changes: 248 additions & 0 deletions examples/custom_shaders_uniforms/GlutWindow.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,248 @@
/*
* Copyright (C) 2015 Open Source Robotics Foundation
WilliamLewww marked this conversation as resolved.
Show resolved Hide resolved
*
* 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.
*
*/

#if __APPLE__
WilliamLewww marked this conversation as resolved.
Show resolved Hide resolved
#include <OpenGL/gl.h>
#include <OpenGL/OpenGL.h>
#include <GLUT/glut.h>
#else
#include <GL/glew.h>
#include <GL/gl.h>
#include <GL/glut.h>
#endif

#if !defined(__APPLE__) && !defined(_WIN32)
#include <GL/glx.h>
#endif

#include <mutex>

#include <ignition/common/Console.hh>
#include <ignition/rendering.hh>
#include <ignition/rendering/Camera.hh>
#include <ignition/rendering/Image.hh>
#include <ignition/rendering/Scene.hh>

#include "GlutWindow.hh"

#define KEY_ESC 27
#define KEY_TAB 9

//////////////////////////////////////////////////
unsigned int imgw = 0;
unsigned int imgh = 0;

std::vector<ir::CameraPtr> g_cameras;
ir::CameraPtr g_camera;
ir::CameraPtr g_currCamera;
unsigned int g_cameraIndex = 0;
ir::ImagePtr g_image;

bool g_initContext = false;

#if __APPLE__
CGLContextObj g_context;
CGLContextObj g_glutContext;
#elif _WIN32
#else
GLXContext g_context;
Display *g_display;
GLXDrawable g_drawable;
GLXContext g_glutContext;
Display *g_glutDisplay;
GLXDrawable g_glutDrawable;
#endif

double g_offset = 0.0;

int g_seed[1] = {0};
float g_resolution[2] = {400, 200};
float g_color[3] = {1.0, 1.0, 1.0};
float g_adjustments[16] = {
0, 0, 0, 0.0005,
0, 0, 0, 0,
0, 0, 0, 0,
0, 0, 0, 0
};

//////////////////////////////////////////////////
//! [update camera]
void updateCameras()

WilliamLewww marked this conversation as resolved.
Show resolved Hide resolved
{
double angle = g_offset / 2 * M_PI;
double x = sin(angle) * 3.0 + 3.0;
double y = cos(angle) * 3.0;
for (ir::CameraPtr camera : g_cameras)
{
camera->SetLocalPosition(x, y, 0.0);
}

g_offset += 0.0005;
}
//! [update camera]

//////////////////////////////////////////////////
//! [update uniforms]
void updateUniforms()

WilliamLewww marked this conversation as resolved.
Show resolved Hide resolved
{
ir::NodePtr node = g_camera->Parent();
ir::VisualPtr sphere = std::dynamic_pointer_cast<ir::Visual>(node->ChildByName("box"));
ir::MaterialPtr shader = sphere->Material();
ir::ShaderParamsPtr shaderParams = shader->FragmentShaderParams();

(*shaderParams)["u_seed"].InitializeWordBuffer(1);
WilliamLewww marked this conversation as resolved.
Show resolved Hide resolved
(*shaderParams)["u_seed"].UpdateWordBuffer(g_seed);

(*shaderParams)["u_resolution"].InitializeWordBuffer(2);
(*shaderParams)["u_resolution"].UpdateWordBuffer(g_resolution);

(*shaderParams)["u_color"].InitializeWordBuffer(3);
(*shaderParams)["u_color"].UpdateWordBuffer(g_color);

(*shaderParams)["u_adjustments"].InitializeWordBuffer(16);
(*shaderParams)["u_adjustments"].UpdateWordBuffer(g_adjustments);
}
//! [update uniforms]

//////////////////////////////////////////////////
void displayCB()
{
#if __APPLE__
CGLSetCurrentContext(g_context);
#elif _WIN32
#else
if (g_display)
{
glXMakeCurrent(g_display, g_drawable, g_context);
}
#endif

g_cameras[g_cameraIndex]->Capture(*g_image);

#if __APPLE__
CGLSetCurrentContext(g_glutContext);
#elif _WIN32
#else
glXMakeCurrent(g_glutDisplay, g_glutDrawable, g_glutContext);
#endif

unsigned char *data = g_image->Data<unsigned char>();

glClearColor(0.5, 0.5, 0.5, 1);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glPixelZoom(1, -1);
glRasterPos2f(-1, 1);
glDrawPixels(imgw, imgh, GL_RGB, GL_UNSIGNED_BYTE, data);

glutSwapBuffers();
updateCameras();
updateUniforms();
}

//////////////////////////////////////////////////
void idleCB()
{
glutPostRedisplay();
}

//////////////////////////////////////////////////
void keyboardCB(unsigned char _key, int, int)
{
if (_key == KEY_ESC || _key == 'q' || _key == 'Q')
{
exit(0);
}
else if (_key == 'o')
{
g_adjustments[3] -= 0.0001;
}
else if (_key == 'p')
{
g_adjustments[3] += 0.0001;
}
}

//////////////////////////////////////////////////
void initCamera(ir::CameraPtr _camera)
{
g_camera = _camera;
imgw = g_camera->ImageWidth();
imgh = g_camera->ImageHeight();
ir::Image image = g_camera->CreateImage();
g_image = std::make_shared<ir::Image>(image);
g_camera->Capture(*g_image);
}

//////////////////////////////////////////////////
void initContext()
{
glutInitDisplayMode(GLUT_DOUBLE);
glutInitWindowPosition(0, 0);
glutInitWindowSize(imgw, imgh);
glutCreateWindow("Custom Shaders Uniforms");
glutDisplayFunc(displayCB);
glutIdleFunc(idleCB);
glutKeyboardFunc(keyboardCB);
}

//////////////////////////////////////////////////
void printUsage()
{
std::cout << "===============================" << std::endl;
std::cout << " ESC - Exit " << std::endl;
std::cout << " " << std::endl;
std::cout << " o - Decrease Frequency " << std::endl;
std::cout << " p - Increase Frequency " << std::endl;
std::cout << "===============================" << std::endl;
}

//////////////////////////////////////////////////
void run(std::vector<ir::CameraPtr> _cameras)
{
if (_cameras.empty())
{
ignerr << "No cameras found. Scene will not be rendered" << std::endl;
return;
}

#if __APPLE__
g_context = CGLGetCurrentContext();
#elif _WIN32
#else
g_context = glXGetCurrentContext();
g_display = glXGetCurrentDisplay();
g_drawable = glXGetCurrentDrawable();
#endif

g_cameras = _cameras;
initCamera(_cameras[0]);
initContext();
printUsage();

#if __APPLE__
g_glutContext = CGLGetCurrentContext();
#elif _WIN32
#else
g_glutDisplay = glXGetCurrentDisplay();
g_glutDrawable = glXGetCurrentDrawable();
g_glutContext = glXGetCurrentContext();
#endif

glutMainLoop();
}
29 changes: 29 additions & 0 deletions examples/custom_shaders_uniforms/GlutWindow.hh
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
* Copyright (C) 2015 Open Source Robotics Foundation
WilliamLewww marked this conversation as resolved.
Show resolved Hide resolved
*
* 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.
*
*/
#ifndef IGNITION_RENDERING_EXAMPLES_CUSTOM_SHADERS_UNIFORMS_DEMO_GLUTWINDOW_HH_
#define IGNITION_RENDERING_EXAMPLES_CUSTOM_SHADERS_UNIFORMS_DEMO_GLUTWINDOW_HH_

#include <vector>
#include "ignition/rendering/RenderTypes.hh"

namespace ir = ignition::rendering;

/// \brief Run the demo and render the scene from the cameras
/// \param[in] _cameras Cameras in the scene
void run(std::vector<ir::CameraPtr> _cameras);

#endif
Loading