-
Notifications
You must be signed in to change notification settings - Fork 56
/
Copy pathcustom_shaders.cc
193 lines (161 loc) · 5.87 KB
/
custom_shaders.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
/*
* Copyright (C) 2017 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.
*
*/
#include <gz/common/Image.hh>
#include <gz/common/Filesystem.hh>
#include <gz/rendering.hh>
#include "example_config.hh"
void BuildScene(gz::rendering::ScenePtr _scene);
void PresentImage(gz::rendering::ImagePtr _image,
const std::string &_name);
// Global constants due to laziness
const double width = 512;
const double height = 512;
const int bytesPerPixel = 3;
//! [init shaders variables]
const std::string depth_vertex_shader_file =
"depth_vertex_shader.glsl";
const std::string depth_fragment_shader_file =
"depth_fragment_shader.glsl";
const std::string vertex_shader_file = "vertex_shader.glsl";
const std::string fragment_shader_file = "fragment_shader.glsl";
//! [init shaders variables]
const std::string RESOURCE_PATH =
gz::common::joinPaths(std::string(PROJECT_BINARY_PATH), "media");
//////////////////////////////////////////////////
int main()
{
// Initialize gz::rendering
auto *engine = gz::rendering::engine("ogre");
if (!engine)
{
std::cerr << "Failed to load ogre\n";
return 1;
}
// Create a scene and add stuff to it
gz::rendering::ScenePtr scene = engine->CreateScene("scene");
if (!scene)
{
std::cerr << "Failed to create scene." << std::endl;
return 1;
}
BuildScene(scene);
gz::rendering::VisualPtr root = scene->RootVisual();
// Create a camera
gz::rendering::CameraPtr camera;
camera = scene->CreateCamera("example_custom_shaders");
camera->SetImageWidth(width);
camera->SetImageHeight(height);
camera->SetHFOV(1.05);
camera->SetAntiAliasing(2);
camera->SetAspectRatio(width / height);
camera->SetImageFormat(gz::rendering::PF_R8G8B8);
root->AddChild(camera);
// Create a camera for depth image
gz::rendering::CameraPtr depthCamera;
depthCamera = scene->CreateCamera("example_custom_shaders_depth");
depthCamera->SetImageWidth(width);
depthCamera->SetImageHeight(height);
depthCamera->SetHFOV(1.05);
depthCamera->SetAntiAliasing(2);
depthCamera->SetAspectRatio(width / height);
depthCamera->SetImageFormat(gz::rendering::PF_R8G8B8);
root->AddChild(depthCamera);
//! [Get shader path]
// path to look for vertex and fragment shaders
std::string depth_vertex_shader_path = gz::common::joinPaths(
RESOURCE_PATH, depth_vertex_shader_file);
std::string depth_fragment_shader_path = gz::common::joinPaths(
RESOURCE_PATH, depth_fragment_shader_file);
//! [Get shader path]
//! [add shader to camera]
// create shader material
gz::rendering::MaterialPtr depthMat = scene->CreateMaterial();
depthMat->SetVertexShader(depth_vertex_shader_path);
depthMat->SetFragmentShader(depth_fragment_shader_path);
// apply shader to camera
depthCamera->SetMaterial(depthMat);
//! [add shader to camera]
gz::rendering::ImagePtr image =
std::make_shared<gz::rendering::Image>(camera->CreateImage());
gz::rendering::ImagePtr depthImage =
std::make_shared<gz::rendering::Image>(depthCamera->CreateImage());
depthCamera->Capture(*depthImage);
PresentImage(depthImage, "depth.png");
camera->Capture(*image);
PresentImage(image, "regular.png");
return 0;
}
//////////////////////////////////////////////////
void PresentImage(gz::rendering::ImagePtr _image,
const std::string &_name)
{
// Present the data
unsigned char *data = _image->Data<unsigned char>();
gz::common::Image image;
image.SetFromData(data, width, height, gz::common::Image::RGB_INT8);
image.SavePNG(_name);
std::cout << "Image saved: " << _name << std::endl;
}
//////////////////////////////////////////////////
void BuildScene(gz::rendering::ScenePtr _scene)
{
// initialize _scene
_scene->SetAmbientLight(0.3, 0.3, 0.3);
gz::rendering::VisualPtr root = _scene->RootVisual();
// create directional light
gz::rendering::DirectionalLightPtr light0 =
_scene->CreateDirectionalLight();
light0->SetDirection(-0.5, 0.5, -1);
light0->SetDiffuseColor(0.5, 0.5, 0.5);
light0->SetSpecularColor(0.5, 0.5, 0.5);
root->AddChild(light0);
// create white material
gz::rendering::MaterialPtr grey = _scene->CreateMaterial();
grey->SetAmbient(0.5, 0.5, 0.5);
grey->SetDiffuse(0.8, 0.8, 0.8);
grey->SetReceiveShadows(true);
grey->SetReflectivity(0);
// create plane visual
gz::rendering::VisualPtr plane = _scene->CreateVisual();
auto geom = _scene->CreatePlane();
plane->AddGeometry(geom);
plane->SetLocalScale(5, 8, 1);
plane->SetLocalPosition(3, 0, -0.5);
plane->SetMaterial(grey);
root->AddChild(plane);
// create shader materials
// path to look for vertex and fragment shader parameters
std::string vertex_shader_path = gz::common::joinPaths(
RESOURCE_PATH, vertex_shader_file);
std::string fragment_shader_path = gz::common::joinPaths(
RESOURCE_PATH, fragment_shader_file);
//! [add shader to visual]
// create shader material
gz::rendering::MaterialPtr shader = _scene->CreateMaterial();
shader->SetVertexShader(vertex_shader_path);
shader->SetFragmentShader(fragment_shader_path);
// create box visual and apply shader
gz::rendering::VisualPtr box = _scene->CreateVisual();
box->AddGeometry(_scene->CreateBox());
box->SetOrigin(0.0, 0.5, 0.0);
box->SetLocalPosition(3, 0, 0);
box->SetLocalRotation(GZ_PI / 4, 0, GZ_PI / 3);
box->SetLocalScale(1, 2.5, 1);
box->SetMaterial(shader);
//! [add shader to visual]
root->AddChild(box);
}