Skip to content

Commit 0e75ee7

Browse files
committed
Working on ospray tutorial example with latvols
1 parent 2fa1361 commit 0e75ee7

File tree

3 files changed

+311
-4
lines changed

3 files changed

+311
-4
lines changed

src/CMake/Modules/FindOSPRay.cmake

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
## ======================================================================================= ##
2+
## Copyright 2014-2015 Texas Advanced Computing Center, The University of Texas at Austin ##
3+
## All rights reserved. ##
4+
## ##
5+
## Licensed under the BSD 3-Clause License, (the "License"); you may not use this file ##
6+
## except in compliance with the License. ##
7+
## A copy of the License is included with this software in the file LICENSE. ##
8+
## If your copy does not contain the License, you may obtain a copy of the License at: ##
9+
## ##
10+
## http://opensource.org/licenses/BSD-3-Clause ##
11+
## ##
12+
## Unless required by applicable law or agreed to in writing, software distributed under ##
13+
## the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY ##
14+
## KIND, either express or implied. ##
15+
## See the License for the specific language governing permissions and limitations under ##
16+
## limitations under the License. ##
17+
## ======================================================================================= ##
18+
19+
20+
###############################################################################
21+
# Find OSPRay
22+
# defines:
23+
# OSPRAY_FOUND
24+
# OSPRAY_INCLUDE_DIRS
25+
# OSPRAY_LIBRARIES
26+
27+
set(OSPRAY_INSTALL_DIR "" CACHE PATH "install location of ospray")
28+
mark_as_advanced(OSPRAY_INSTALL_DIR)
29+
set(OSPRAY_BUILD_DIR "" CACHE PATH "build location of ospray")
30+
mark_as_advanced(OSPRAY_BUILD_DIR)
31+
if (OSPRAY_INSTALL_DIR AND OSPRAY_BUILD_DIR)
32+
message("Ignoring the ospray build location in favor of the intall location.")
33+
endif()
34+
35+
if (OSPRAY_INSTALL_DIR)
36+
37+
find_package(ospray CONFIG REQUIRED HINTS ${OSPRAY_INSTALL_DIR})
38+
39+
else()
40+
41+
if (OSPRAY_BUILD_DIR)
42+
#find corresponding source directory
43+
load_cache(${OSPRAY_BUILD_DIR} READ_WITH_PREFIX OSP_
44+
CMAKE_HOME_DIRECTORY
45+
embree_DIR
46+
)
47+
set(OSPRAY_SOURCE_DIR ${OSP_CMAKE_HOME_DIRECTORY})
48+
49+
set(OSPRAY_INCLUDE_DIRS
50+
${OSPRAY_BUILD_DIR}
51+
${OSPRAY_BUILD_DIR}/include
52+
${OSPRAY_SOURCE_DIR}
53+
${OSPRAY_SOURCE_DIR}/ospray/include
54+
)
55+
56+
# message(${OSP_embree_DIR})
57+
# message(${OSP_embree_DIR}/bin)
58+
# set(LIB_OSPRAY_EMBREE LIB_OSPRAY_EMBREE-NOTFOUND)
59+
# find_library(LIB_OSPRAY_EMBREE NAMES ospray_embree embree
60+
# PATHS ${OSPRAY_BUILD_DIR} ${OSPRAY_BUILD_DIR}/Release
61+
# ${OSP_embree_DIR} ${OSP_embree_DIR}/bin NO_DEFAULT_PATH)
62+
# mark_as_advanced(LIB_OSPRAY_EMBREE)
63+
64+
set(LIB_OSPRAY_COMMON LIB_OSPRAY_COMMON-NOTFOUND)
65+
find_library(LIB_OSPRAY_COMMON ospray_common
66+
PATHS ${OSPRAY_BUILD_DIR} ${OSPRAY_BUILD_DIR}/Release NO_DEFAULT_PATH)
67+
mark_as_advanced(LIB_OSPRAY_COMMON)
68+
69+
set(LIB_OSPRAY LIB_OSPRAY-NOTFOUND)
70+
find_library(LIB_OSPRAY ospray PATHS ${OSPRAY_BUILD_DIR} ${OSPRAY_BUILD_DIR}/Release NO_DEFAULT_PATH)
71+
mark_as_advanced(LIB_OSPRAY)
72+
73+
set(OSPRAY_LIBRARIES ${LIB_OSPRAY_COMMON} ${LIB_OSPRAY})
74+
75+
else()
76+
77+
message("Supply OSPRAY_INSTALL_DIR or OSPRAY_BUILD_DIR to find OSPRay")
78+
79+
endif()
80+
81+
endif()

src/Modules/Visualization/Tests/CMakeLists.txt

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,16 +34,17 @@ SET(Modules_Visualization_Tests_SRCS
3434
ShowFieldTests.cc
3535
ShowMeshTests.cc
3636
ShowStringTests.cc
37+
OsprayFieldRenderTests.cc
3738
)
3839

39-
#SET(Engine_Network_Tests_HEADERS
40-
# NetworkEditorControllerTests_.h
41-
#)
42-
4340
SCIRUN_ADD_UNIT_TEST(Modules_Visualization_Tests
4441
${Modules_Visualization_Tests_SRCS}
4542
)
4643

44+
INCLUDE(${CMAKE_SOURCE_DIR}/CMake/Modules/FindOSPRay.cmake)
45+
46+
INCLUDE_DIRECTORIES(${OSPRAY_INCLUDE_DIRS})
47+
4748
TARGET_LINK_LIBRARIES(Modules_Visualization_Tests
4849
Modules_Visualization
4950
Modules_Factory
@@ -56,4 +57,5 @@ TARGET_LINK_LIBRARIES(Modules_Visualization_Tests
5657
gtest_main
5758
gtest
5859
gmock
60+
${OSPRAY_LIBRARIES}
5961
)
Lines changed: 224 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,224 @@
1+
/*
2+
For more information, please see: http://software.sci.utah.edu
3+
4+
The MIT License
5+
6+
Copyright (c) 2015 Scientific Computing and Imaging Institute,
7+
University of Utah.
8+
9+
License for the specific language governing rights and limitations under
10+
Permission is hereby granted, free of charge, to any person obtaining a
11+
copy of this software and associated documentation files (the "Software"),
12+
to deal in the Software without restriction, including without limitation
13+
the rights to use, copy, modify, merge, publish, distribute, sublicense,
14+
and/or sell copies of the Software, and to permit persons to whom the
15+
Software is furnished to do so, subject to the following conditions:
16+
17+
The above copyright notice and this permission notice shall be included
18+
in all copies or substantial portions of the Software.
19+
20+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
21+
OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
23+
THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
25+
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
26+
DEALINGS IN THE SOFTWARE.
27+
*/
28+
29+
#include <Testing/ModuleTestBase/ModuleTestBase.h>
30+
#include <Modules/Visualization/ShowField.h>
31+
#include <Core/Datatypes/Legacy/Field/Field.h>
32+
#include <Core/Algorithms/Base/AlgorithmVariableNames.h>
33+
#include <Core/Utils/Exception.h>
34+
#include <Core/Logging/Log.h>
35+
#include <Core/Datatypes/ColorMap.h>
36+
37+
#include <ospray/ospray.h>
38+
39+
using namespace SCIRun::Testing;
40+
using namespace SCIRun::TestUtils;
41+
using namespace SCIRun::Core::Datatypes;
42+
using namespace SCIRun::Dataflow::Networks;
43+
using namespace SCIRun::Core::Algorithms;
44+
using namespace SCIRun::Core::Algorithms::Visualization;
45+
using namespace SCIRun::Modules::Visualization;
46+
using namespace SCIRun::Core;
47+
using namespace SCIRun;
48+
using namespace SCIRun::Core::Logging;
49+
using ::testing::Values;
50+
using ::testing::Combine;
51+
using ::testing::Range;
52+
53+
class OsprayFieldRenderTest : public ParameterizedModuleTest<int>
54+
{
55+
protected:
56+
virtual void SetUp()
57+
{
58+
Log::get().setVerbose(false);
59+
auto size = GetParam();
60+
latVol = CreateEmptyLatVol(size, size, size);
61+
Log::get() << INFO << "Setting up ShowField with size " << size << "^3 latvol" << std::endl;
62+
}
63+
64+
UseRealModuleStateFactory f;
65+
ModuleHandle showField;
66+
FieldHandle latVol;
67+
};
68+
69+
70+
namespace osprayImpl
71+
{
72+
73+
// helper function to write the rendered image as PPM file
74+
void writePPM(const char *fileName,
75+
const osp::vec2i &size,
76+
const uint32_t *pixel)
77+
{
78+
FILE *file = fopen(fileName, "wb");
79+
fprintf(file, "P6\n%i %i\n255\n", size.x, size.y);
80+
unsigned char *out = (unsigned char *)alloca(3 * size.x);
81+
for (int y = 0; y < size.y; y++) {
82+
const unsigned char *in = (const unsigned char *)&pixel[(size.y - 1 - y)*size.x];
83+
for (int x = 0; x < size.x; x++) {
84+
out[3 * x + 0] = in[4 * x + 0];
85+
out[3 * x + 1] = in[4 * x + 1];
86+
out[3 * x + 2] = in[4 * x + 2];
87+
}
88+
fwrite(out, 3 * size.x, sizeof(char), file);
89+
}
90+
fprintf(file, "\n");
91+
fclose(file);
92+
std::cout << "wrote file " << fileName << std::endl;
93+
}
94+
95+
96+
int mainFunc()
97+
{
98+
std::cout << "hello ospray" << std::endl;
99+
// image size
100+
osp::vec2i imgSize;
101+
imgSize.x = 1024; // width
102+
imgSize.y = 768; // height
103+
104+
// camera
105+
float cam_pos[] = { 0.f, 0.f, 0.f };
106+
float cam_up[] = { 0.f, 1.f, 0.f };
107+
float cam_view[] = { 0.1f, 0.f, 1.f };
108+
109+
// triangle mesh data
110+
float vertex[] = { -1.0f, -1.0f, 3.0f, 0.f,
111+
-1.0f, 1.0f, 3.0f, 0.f,
112+
1.0f, -1.0f, 3.0f, 0.f,
113+
0.1f, 0.1f, 0.3f, 0.f };
114+
float color[] = { 0.9f, 0.5f, 0.5f, 1.0f,
115+
0.8f, 0.8f, 0.8f, 1.0f,
116+
0.8f, 0.8f, 0.8f, 1.0f,
117+
0.5f, 0.9f, 0.5f, 1.0f };
118+
int32_t index[] = { 0, 1, 2,
119+
1, 2, 3 };
120+
121+
122+
// initialize OSPRay; OSPRay parses (and removes) its commandline parameters, e.g. "--osp:debug"
123+
const char* argv[] = {""};
124+
int argc = 0;
125+
int init_error = ospInit(&argc, argv);
126+
if (init_error != OSP_NO_ERROR)
127+
return init_error;
128+
129+
// create and setup camera
130+
OSPCamera camera = ospNewCamera("perspective");
131+
ospSetf(camera, "aspect", imgSize.x / (float)imgSize.y);
132+
ospSet3fv(camera, "pos", cam_pos);
133+
ospSet3fv(camera, "dir", cam_view);
134+
ospSet3fv(camera, "up", cam_up);
135+
ospCommit(camera); // commit each object to indicate modifications are done
136+
137+
138+
// create and setup model and mesh
139+
OSPGeometry mesh = ospNewGeometry("triangles");
140+
OSPData data = ospNewData(4, OSP_FLOAT3A, vertex); // OSP_FLOAT3 format is also supported for vertex positions
141+
ospCommit(data);
142+
ospSetData(mesh, "vertex", data);
143+
144+
data = ospNewData(4, OSP_FLOAT4, color);
145+
ospCommit(data);
146+
ospSetData(mesh, "vertex.color", data);
147+
148+
data = ospNewData(2, OSP_INT3, index); // OSP_INT4 format is also supported for triangle indices
149+
ospCommit(data);
150+
ospSetData(mesh, "index", data);
151+
152+
ospCommit(mesh);
153+
154+
155+
OSPModel world = ospNewModel();
156+
ospAddGeometry(world, mesh);
157+
ospCommit(world);
158+
159+
160+
// create renderer
161+
OSPRenderer renderer = ospNewRenderer("scivis"); // choose Scientific Visualization renderer
162+
163+
// create and setup light for Ambient Occlusion
164+
OSPLight light = ospNewLight(renderer, "ambient");
165+
ospCommit(light);
166+
OSPData lights = ospNewData(1, OSP_LIGHT, &light);
167+
ospCommit(lights);
168+
169+
// complete setup of renderer
170+
ospSet1i(renderer, "aoSamples", 1);
171+
ospSet1f(renderer, "bgColor", 1.0f); // white, transparent
172+
ospSetObject(renderer, "model", world);
173+
ospSetObject(renderer, "camera", camera);
174+
ospSetObject(renderer, "lights", lights);
175+
ospCommit(renderer);
176+
177+
178+
// create and setup framebuffer
179+
OSPFrameBuffer framebuffer = ospNewFrameBuffer(imgSize, OSP_FB_SRGBA, OSP_FB_COLOR | /*OSP_FB_DEPTH |*/ OSP_FB_ACCUM);
180+
ospFrameBufferClear(framebuffer, OSP_FB_COLOR | OSP_FB_ACCUM);
181+
182+
// render one frame
183+
ospRenderFrame(framebuffer, renderer, OSP_FB_COLOR | OSP_FB_ACCUM);
184+
185+
// access framebuffer and write its content as PPM file
186+
const uint32_t * fb = (uint32_t*)ospMapFrameBuffer(framebuffer, OSP_FB_COLOR);
187+
writePPM("firstFrame.ppm", imgSize, fb);
188+
ospUnmapFrameBuffer(fb, framebuffer);
189+
190+
191+
// render 10 more frames, which are accumulated to result in a better converged image
192+
for (int frames = 0; frames < 10; frames++)
193+
ospRenderFrame(framebuffer, renderer, OSP_FB_COLOR | OSP_FB_ACCUM);
194+
195+
fb = (uint32_t*)ospMapFrameBuffer(framebuffer, OSP_FB_COLOR);
196+
writePPM("accumulatedFrame.ppm", imgSize, fb);
197+
ospUnmapFrameBuffer(fb, framebuffer);
198+
199+
return 0;
200+
}
201+
202+
}
203+
204+
TEST_P(OsprayFieldRenderTest, RenderLatVolWithOspray)
205+
{
206+
Log::get() << INFO << "Start ShowField::execute" << std::endl;
207+
208+
209+
osprayImpl::mainFunc();
210+
211+
FAIL() << "todo";
212+
213+
214+
Log::get() << INFO << "End ShowField::execute" << std::endl;
215+
}
216+
217+
INSTANTIATE_TEST_CASE_P(
218+
RenderLatVolWithOspray,
219+
OsprayFieldRenderTest,
220+
Values(20//, 40, 60, 80
221+
//, 100, 120, 150//, //200 //to speed up make test
222+
//, 256 // probably runs out of memory
223+
)
224+
);

0 commit comments

Comments
 (0)