|
| 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