-
Notifications
You must be signed in to change notification settings - Fork 196
Expand file tree
/
Copy pathospTutorial.cpp
More file actions
172 lines (140 loc) · 5.38 KB
/
ospTutorial.cpp
File metadata and controls
172 lines (140 loc) · 5.38 KB
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
// Copyright 2009 Intel Corporation
// SPDX-License-Identifier: Apache-2.0
/* This is a small example tutorial how to use OSPRay in an application.
*
* On Linux build it in the build_directory with
* g++ ../apps/ospTutorial/ospTutorial.cpp -I ../ospray/include \
* -I ../../rkcommon -L . -lospray -Wl,-rpath,. -o ospTutorial
* On Windows build it in the build_directory\$Configuration with
* cl ..\..\apps\ospTutorial\ospTutorial.cpp /EHsc -I ..\..\ospray\include ^
* -I ..\.. -I ..\..\..\rkcommon ospray.lib
* Above commands assume that rkcommon is present in a directory right "next
* to" the OSPRay directory. If this is not the case, then adjust the include
* path (alter "-I <path/to/rkcommon>" appropriately).
*/
#include <errno.h>
#include <stdint.h>
#include <stdio.h>
#ifdef _WIN32
#define NOMINMAX
#include <conio.h>
#include <malloc.h>
#include <windows.h>
#else
#include <alloca.h>
#endif
#include <vector>
#include "ospray/ospray_cpp.h"
#include "ospray/ospray_cpp/ext/rkcommon.h"
#include "rkcommon/utility/SaveImage.h"
using namespace rkcommon::math;
int main(int argc, const char **argv)
{
// image size
vec2i imgSize;
imgSize.x = 1024; // width
imgSize.y = 768; // height
// camera
vec3f cam_pos{0.f, 0.f, 0.f};
vec3f cam_up{0.f, 1.f, 0.f};
vec3f cam_view{0.1f, 0.f, 1.f};
// triangle mesh data
std::vector<vec3f> vertex = {vec3f(-1.0f, -1.0f, 3.0f),
vec3f(-1.0f, 1.0f, 3.0f),
vec3f(1.0f, -1.0f, 3.0f),
vec3f(0.1f, 0.1f, 0.3f)};
std::vector<vec4f> color = {vec4f(0.9f, 0.5f, 0.5f, 1.0f),
vec4f(0.8f, 0.8f, 0.8f, 1.0f),
vec4f(0.8f, 0.8f, 0.8f, 1.0f),
vec4f(0.5f, 0.9f, 0.5f, 1.0f)};
std::vector<vec3ui> index = {vec3ui(0, 1, 2), vec3ui(1, 2, 3)};
#ifdef _WIN32
bool waitForKey = false;
CONSOLE_SCREEN_BUFFER_INFO csbi;
if (GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &csbi)) {
// detect standalone console: cursor at (0,0)?
waitForKey = csbi.dwCursorPosition.X == 0 && csbi.dwCursorPosition.Y == 0;
}
#endif
// initialize OSPRay; OSPRay parses (and removes) its commandline parameters,
// e.g. "--osp:debug"
OSPError init_error = ospInit(&argc, argv);
if (init_error != OSP_NO_ERROR)
return init_error;
// use scoped lifetimes of wrappers to release everything before ospShutdown()
{
// create and setup camera
ospray::cpp::Camera camera("perspective");
camera.setParam("aspect", imgSize.x / (float)imgSize.y);
camera.setParam("position", cam_pos);
camera.setParam("direction", cam_view);
camera.setParam("up", cam_up);
camera.commit(); // commit each object to indicate modifications are done
// create and setup model and mesh
ospray::cpp::Geometry mesh("mesh");
mesh.setParam("vertex.position", ospray::cpp::CopiedData(vertex));
mesh.setParam("vertex.color", ospray::cpp::CopiedData(color));
mesh.setParam("index", ospray::cpp::CopiedData(index));
mesh.commit();
// put the mesh into a model
ospray::cpp::GeometricModel model(mesh);
model.commit();
// put the model into a group (collection of models)
ospray::cpp::Group group;
group.setParam("geometry", ospray::cpp::CopiedData(model));
group.commit();
// put the group into an instance (give the group a world transform)
ospray::cpp::Instance instance(group);
instance.commit();
// put the instance in the world
ospray::cpp::World world;
world.setParam("instance", ospray::cpp::CopiedData(instance));
// create and setup light for Ambient Occlusion
ospray::cpp::Light light("ambient");
light.commit();
world.setParam("light", ospray::cpp::CopiedData(light));
world.commit();
// create renderer, choose Scientific Visualization renderer
ospray::cpp::Renderer renderer("scivis");
// complete setup of renderer
renderer.setParam("aoSamples", 1);
renderer.setParam("backgroundColor", 1.0f); // white, transparent
renderer.commit();
// create and setup framebuffer
ospray::cpp::FrameBuffer framebuffer(
imgSize.x, imgSize.y, OSP_FB_SRGBA, OSP_FB_COLOR | OSP_FB_ACCUM);
framebuffer.clear();
// render one frame
framebuffer.renderFrame(renderer, camera, world);
// access framebuffer and write its content as PPM file
uint32_t *fb = (uint32_t *)framebuffer.map(OSP_FB_COLOR);
rkcommon::utility::writePPM("firstFrameCpp.ppm", imgSize.x, imgSize.y, fb);
framebuffer.unmap(fb);
std::cout << "rendering initial frame to firstFrameCpp.ppm" << std::endl;
// render 10 more frames, which are accumulated to result in a better
// converged image
for (int frames = 0; frames < 10; frames++)
framebuffer.renderFrame(renderer, camera, world);
fb = (uint32_t *)framebuffer.map(OSP_FB_COLOR);
rkcommon::utility::writePPM(
"accumulatedFrameCpp.ppm", imgSize.x, imgSize.y, fb);
framebuffer.unmap(fb);
std::cout << "rendering 10 accumulated frames to accumulatedFrameCpp.ppm"
<< std::endl;
ospray::cpp::PickResult res =
framebuffer.pick(renderer, camera, world, 0.5f, 0.5f);
if (res.hasHit) {
std::cout << "picked geometry [instance: " << res.instance.handle()
<< ", model: " << res.model.handle()
<< ", primitive: " << res.primID << "]" << std::endl;
}
}
ospShutdown();
#ifdef _WIN32
if (waitForKey) {
printf("\n\tpress any key to exit");
_getch();
}
#endif
return 0;
}