Beispiel #1
0
void VolumeViewer::setIsovalues(std::vector<float> isovalues)
{
  OSPData isovaluesData = ospNewData(isovalues.size(), OSP_FLOAT, &isovalues[0]);

  // Remove existing isosurface geometries from models.
  for(size_t i=0; i<modelStates.size(); i++) {
    for(size_t j=0; j<modelStates[i].isosurfaces.size(); j++)
      ospRemoveGeometry(modelStates[i].model, modelStates[i].isosurfaces[j]);

    modelStates[i].isosurfaces.clear();
  }

  // Add new isosurfaces for each volume of each model. Later we can do this only for the active model on time step change...
  for(size_t i=0; i<modelStates.size(); i++) {

    if(isovalues.size() > 0) {
      for(size_t j=0; j<modelStates[i].volumes.size(); j++) {

        OSPGeometry isosurfacesGeometry = ospNewGeometry("isosurfaces");
        ospSetData(isosurfacesGeometry, "isovalues", isovaluesData);
        ospSetObject(isosurfacesGeometry, "volume", modelStates[i].volumes[j]);
        ospCommit(isosurfacesGeometry);

        ospAddGeometry(modelStates[i].model, isosurfacesGeometry);

        modelStates[i].isosurfaces.push_back(isosurfacesGeometry);
      }
    }

    ospCommit(modelStates[i].model);
  }

  render();
}
Beispiel #2
0
    void Geometry::postCommit(RenderContext &)
    {
      auto ospGeometry = valueAs<OSPGeometry>();

      if (hasChild("material") && !hasChild("materialList")) {
        // XXX FIXME never happens
        ospSetMaterial(ospGeometry, child("material").valueAs<OSPMaterial>());
      }

      auto materialListNode = child("materialList").nodeAs<MaterialList>();
      const auto &materialList = materialListNode->nodes;
      if (!materialList.empty()) {
        std::vector<OSPObject> mats;
        for (auto mat : materialList) {
          auto m = mat->valueAs<OSPObject>();
          if (m)
            mats.push_back(m);
        }
        auto ospMaterialList = ospNewData(mats.size(), OSP_OBJECT, mats.data());
        ospCommit(ospMaterialList);
        ospSetData(valueAs<OSPObject>(), "materialList", ospMaterialList);
        ospRelease(ospMaterialList);
      }

      ospCommit(ospGeometry);
    }
Beispiel #3
0
void 
Renderer::CommitVolume()
{
	volume.commit();

	OSPModel model = ospNewModel();
	ospAddVolume(model, volume.getOSPVolume());
	ospCommit(model);
	ospSetObject(getRenderer(), "model", model);
	ospCommit(getRenderer());
}
Beispiel #4
0
void VolumeViewer::initObjects(const std::string &renderer_type)
{
  // Create an OSPRay renderer.
  renderer = ospNewRenderer(renderer_type.c_str());
  exitOnCondition(renderer == NULL, "could not create OSPRay renderer object");

  // Set renderer defaults (if not using 'aoX' renderers)
  if (renderer_type[0] != 'a' && renderer_type[1] != 'o')
  {
    ospSet1i(renderer, "aoSamples", 1);
    ospSet1i(renderer, "shadowsEnabled", 1);
  }

  // Create OSPRay ambient and directional lights. GUI elements will modify their parameters.
  ambientLight = ospNewLight(renderer, "AmbientLight");
  exitOnCondition(ambientLight == NULL, "could not create ambient light");
  ospCommit(ambientLight);

  directionalLight = ospNewLight(renderer, "DirectionalLight");
  exitOnCondition(directionalLight == NULL, "could not create directional light");
  ospCommit(directionalLight);

  // Set the light sources on the renderer.
  std::vector<OSPLight> lights;
  lights.push_back(ambientLight);
  lights.push_back(directionalLight);

  ospSetData(renderer, "lights", ospNewData(lights.size(), OSP_OBJECT, &lights[0]));

  // Create an OSPRay transfer function.
  transferFunction = ospNewTransferFunction("piecewise_linear");
  exitOnCondition(transferFunction == NULL, "could not create OSPRay transfer function object");
  ospCommit(transferFunction);

  // Load OSPRay objects from files.
  for (size_t i=0 ; i < objectFileFilenames.size() ; i++)
    importObjectsFromFile(objectFileFilenames[i]);

  // Get the bounding box of all volumes of the first model.
  if(modelStates.size() > 0 && modelStates[0].volumes.size() > 0) {
    ospGetVec3f(modelStates[0].volumes[0], "boundingBoxMin", (osp::vec3f*)&boundingBox.lower);
    ospGetVec3f(modelStates[0].volumes[0], "boundingBoxMax", (osp::vec3f*)&boundingBox.upper);

    for (size_t i=1; i<modelStates[0].volumes.size(); i++) {
      ospcommon::box3f volumeBoundingBox;
      ospGetVec3f(modelStates[0].volumes[i], "boundingBoxMin", (osp::vec3f*)&volumeBoundingBox.lower);
      ospGetVec3f(modelStates[0].volumes[i], "boundingBoxMax", (osp::vec3f*)&volumeBoundingBox.upper);

      boundingBox.extend(volumeBoundingBox);
    }
  }
}
Beispiel #5
0
    void FrameBuffer::postCommit(RenderContext &)
    {
      bool removeToneMapper = false;
      bool toneMapperEnabled = false;
      if (hasChild("toneMapper")) {
        toneMapperEnabled = child("toneMapper").child("enabled").valueAs<bool>();
        if (toneMapperActive && !toneMapperEnabled)
          removeToneMapper = true;
      }
      // Avoid clearing the framebuffer when only tonemapping parameters have been changed
      if (lastModified() >= lastCommitted()
          || child("size").lastModified() >= lastCommitted()
          || child("displayWall").lastModified() >= lastCommitted()
          || child("useAccumBuffer").lastModified() >= lastCommitted()
          || child("useVarianceBuffer").lastModified() >= lastCommitted()
#ifdef OSPRAY_APPS_ENABLE_DENOISER
          || child("useDenoiser").lastModified() >= lastCommitted()
#endif
          || child("colorFormat").lastModified() >= lastCommitted()
          || removeToneMapper)
      {
        std::string displayWall = child("displayWall").valueAs<std::string>();
        this->displayWallStream = displayWall;

        updateFB();

        if (displayWall != "") {
          // TODO move into own sg::Node
          ospLoadModule("displayWald");
          OSPPixelOp pixelOp = ospNewPixelOp("display_wald");
          ospSetString(pixelOp, "streamName", displayWall.c_str());
          ospCommit(pixelOp);
          ospSetPixelOp(ospFrameBuffer, pixelOp);
          std::cout << "-------------------------------------------------------"
                    << std::endl;
          std::cout << "this is the display wall framebuffer .. size is "
                    << size() << std::endl;
          std::cout << "added display wall pixel op ..." << std::endl;

          std::cout << "created display wall pixelop, and assigned to frame buffer!"
                  << std::endl;
        }
        ospCommit(ospFrameBuffer);
      }

      if (toneMapperEnabled && !toneMapperActive) {
        ospSetPixelOp(ospFrameBuffer, child("toneMapper").valueAs<OSPPixelOp>());
        toneMapperActive = true;
      }
    }
Beispiel #6
0
void VolumeViewer::addGeometry(std::string filename)
{
  // For now we assume PLY geometry files. Later we can support other geometry formats.

  // Get filename if not specified.
  if(filename.empty())
    filename = QFileDialog::getOpenFileName(this, tr("Load geometry"), ".", "Geometry files (*.ply *.dds)").toStdString();

  if(filename.empty())
    return;

  // Attempt to load the geometry through the TriangleMeshFile loader.
  OSPGeometry triangleMesh = ospNewGeometry("trianglemesh");

  // If successful, commit the triangle mesh and add it to all models.
  if(TriangleMeshFile::importTriangleMesh(filename, triangleMesh) != NULL) {

    // For now: if this is a DDS geometry, assume it is a horizon and its color should be mapped through the first volume's transfer function.
    if(QString(filename.c_str()).endsWith(".dds") && modelStates.size() > 0 && modelStates[0].volumes.size() > 0) {

      OSPMaterial material = ospNewMaterial(renderer, "default");
      ospSet3f(material, "Kd", 1,1,1);
      ospSetObject(material, "volume", modelStates[0].volumes[0]);
      ospCommit(material);

      ospSetMaterial(triangleMesh, material);
    }

    ospCommit(triangleMesh);

    // Create an instance of the geometry and add the instance to the main model(s)--this prevents the geometry
    // from being rebuilt every time the main model is committed (e.g. when slices / isosurfaces are manipulated)
    OSPModel modelInstance = ospNewModel();
    ospAddGeometry(modelInstance, triangleMesh);
    ospCommit(modelInstance);

    ospcommon::affine3f xfm = ospcommon::one;
    OSPGeometry triangleMeshInstance = ospNewInstance(modelInstance, (osp::affine3f&)xfm);
    ospCommit(triangleMeshInstance);

    for(size_t i=0; i<modelStates.size(); i++) {
      ospAddGeometry(modelStates[i].model, triangleMeshInstance);
      ospCommit(modelStates[i].model);
    }

    // Force render.
    render();
  }
}
Beispiel #7
0
void
Renderer::Render(std::string fname) 
{ 
	ospCommit(getRenderer());
  getWindow()->render(getRenderer()); 
	getWindow()->save(fname);
}
Beispiel #8
0
void GLFWOSPRayWindow::reshape(const ospcommon::vec2i &newWindowSize)
{
  windowSize = newWindowSize;

  // release the current frame buffer, if it exists
  if (framebuffer)
    ospRelease(framebuffer);

  // create new frame buffer
  framebuffer = ospNewFrameBuffer(*reinterpret_cast<osp::vec2i *>(&windowSize),
                                  OSP_FB_SRGBA,
                                  OSP_FB_COLOR | OSP_FB_ACCUM);

  // reset OpenGL viewport and orthographic projection
  glViewport(0, 0, windowSize.x, windowSize.y);

  glMatrixMode(GL_PROJECTION);
  glLoadIdentity();
  glOrtho(0.0, windowSize.x, 0.0, windowSize.y, -1.0, 1.0);

  // update camera
  arcballCamera->updateWindowSize(windowSize);

  ospSetf(camera, "aspect", windowSize.x / float(windowSize.y));
  ospCommit(camera);
}
Beispiel #9
0
void VolumeViewer::setModel(size_t index)
{
  modelIndex = index;

  // Set current model on the OSPRay renderer.
  ospSetObject(renderer, "model", modelStates[index].model);
  ospCommit(renderer);
  rendererInitialized = true;

  // Update transfer function and isosurface editor data value range with the voxel range of the current model's first volume.
  ospcommon::vec2f voxelRange(0.f); 
  OSPVolume volume = modelStates[index].volumes[0];
#ifdef OSPRAY_VOLUME_VOXELRANGE_IN_APP
  voxelRange = VolumeFile::voxelRangeOf[volume];
#else
  ospGetVec2f(modelStates[index].volumes[0], "voxelRange", (osp::vec2f*)&voxelRange);
#endif

  if(voxelRange != ospcommon::vec2f(0.f)) {
    transferFunctionEditor->setDataValueRange(voxelRange);
    isosurfaceEditor->setDataValueRange(voxelRange);
  }

  // Update active volume on probe widget.
  probeWidget->setVolume(modelStates[index].volumes[0]);

  // Update current filename information label.
  if (ownModelPerObject)
    currentFilenameInfoLabel.setText("<b>Timestep " + QString::number(index) + QString("</b>: Data value range: [") + QString::number(voxelRange.x) + ", " + QString::number(voxelRange.y) + "]");
  else
    currentFilenameInfoLabel.setText("<b>Timestep " + QString::number(index) + QString("</b>: ") + QString(objectFileFilenames[index].c_str()).split('/').back() + ". Data value range: [" + QString::number(voxelRange.x) + ", " + QString::number(voxelRange.y) + "]");

  // Enable rendering on the OSPRay window.
  osprayWindow->setRenderingEnabled(true);
}
Beispiel #10
0
Renderer::Renderer(int width, int height)
{
	renderer = ospNewRenderer("vis_renderer");
	camera.setRenderer(renderer);
	renderProperties.setRenderer(renderer);

	float clips[12] = 
		{
			 1.0,  0.0,  0.0, -0.9,
			 1.0, -1.0,  0.0,  0.0,
			-1.0,  0.0,  0.0,  0.1
		};

	ospSetData(renderer, "clips", ospNewData(sizeof(clips) / (4 * sizeof(float)), OSP_FLOAT4, clips));

	float slice[4] = {5.0, 6.0, 7.0, 8.0};
	ospSetData(renderer, "slices", ospNewData(1, OSP_FLOAT4, slice));

	OSPModel dmodel = ospNewModel();
	ospCommit(dmodel);
	ospSetObject(renderer, "dynamic_model", dmodel);

	window = new CinemaWindow(width, height);
	camera.setAspect(((float)height) / width);
}
Beispiel #11
0
void VolumeViewer::importObjectsFromFile(const std::string &filename)
{
  if (!ownModelPerObject)
  // Create an OSPRay model and its associated model state.
  modelStates.push_back(ModelState(ospNewModel()));

  // Load OSPRay objects from a file.
  OSPObject *objects = ObjectFile::importObjects(filename.c_str());

  // Iterate over the objects contained in the object list.
  for (size_t i=0 ; objects[i] ; i++) {
    if (ownModelPerObject)
      modelStates.push_back(ModelState(ospNewModel()));

    OSPDataType type;
    ospGetType(objects[i], NULL, &type);

    if (type == OSP_GEOMETRY) {

      // Commit the geometry.
      ospCommit(objects[i]);

      // Add the loaded geometry to the model.
      ospAddGeometry(modelStates.back().model, (OSPGeometry) objects[i]);

    } else if (type == OSP_VOLUME) {

      // For now we set the same transfer function on all volumes.
      ospSetObject(objects[i], "transferFunction", transferFunction);
      ospCommit(objects[i]);

      // Add the loaded volume(s) to the model.
      ospAddVolume(modelStates.back().model, (OSPVolume) objects[i]);

      // Add to volumes vector for the current model.
      modelStates.back().volumes.push_back((OSPVolume) objects[i]);
    }

    if (ownModelPerObject)
      ospCommit(modelStates.back().model);
  }

  if (!ownModelPerObject)
  // Commit the model.
  ospCommit(modelStates.back().model);
}
Beispiel #12
0
void GLFWOSPRayWindow::motion(const ospcommon::vec2f &position)
{
  static ospcommon::vec2f previousMouse(-1);

  const ospcommon::vec2f mouse(position.x, position.y);
  if (previousMouse != ospcommon::vec2f(-1)) {
    const bool leftDown =
        glfwGetMouseButton(glfwWindow, GLFW_MOUSE_BUTTON_LEFT) == GLFW_PRESS;
    const bool rightDown =
        glfwGetMouseButton(glfwWindow, GLFW_MOUSE_BUTTON_RIGHT) == GLFW_PRESS;
    const bool middleDown =
        glfwGetMouseButton(glfwWindow, GLFW_MOUSE_BUTTON_MIDDLE) == GLFW_PRESS;
    const ospcommon::vec2f prev = previousMouse;

    bool cameraChanged = leftDown || rightDown || middleDown;

    if (leftDown) {
      const ospcommon::vec2f mouseFrom(
          ospcommon::clamp(prev.x * 2.f / windowSize.x - 1.f, -1.f, 1.f),
          ospcommon::clamp(prev.y * 2.f / windowSize.y - 1.f, -1.f, 1.f));
      const ospcommon::vec2f mouseTo(
          ospcommon::clamp(mouse.x * 2.f / windowSize.x - 1.f, -1.f, 1.f),
          ospcommon::clamp(mouse.y * 2.f / windowSize.y - 1.f, -1.f, 1.f));
      arcballCamera->rotate(mouseFrom, mouseTo);
    } else if (rightDown) {
      arcballCamera->zoom(mouse.y - prev.y);
    } else if (middleDown) {
      arcballCamera->pan(ospcommon::vec2f(mouse.x - prev.x, prev.y - mouse.y));
    }

    if (cameraChanged) {
      ospFrameBufferClear(framebuffer, OSP_FB_COLOR | OSP_FB_ACCUM);

      ospSetf(camera, "aspect", windowSize.x / float(windowSize.y));
      ospSetVec3f(camera,
                  "pos",
                  osp::vec3f{arcballCamera->eyePos().x,
                             arcballCamera->eyePos().y,
                             arcballCamera->eyePos().z});
      ospSetVec3f(camera,
                  "dir",
                  osp::vec3f{arcballCamera->lookDir().x,
                             arcballCamera->lookDir().y,
                             arcballCamera->lookDir().z});
      ospSetVec3f(camera,
                  "up",
                  osp::vec3f{arcballCamera->upDir().x,
                             arcballCamera->upDir().y,
                             arcballCamera->upDir().z});

      ospCommit(camera);
    }
  }

  previousMouse = mouse;
}
Beispiel #13
0
void VolumeViewer::setGradientShadingEnabled(bool value)
{
  for(size_t i=0; i<modelStates.size(); i++)
    for(size_t j=0; j<modelStates[i].volumes.size(); j++) {
      ospSet1i(modelStates[i].volumes[j], "gradientShadingEnabled", value);
      ospCommit(modelStates[i].volumes[j]);
    }

  render();
}
Beispiel #14
0
void VolumeViewer::setSlices(std::vector<SliceParameters> sliceParameters)
{
  // Provide the slices to OSPRay as the coefficients (a,b,c,d) of the plane equation ax + by + cz + d = 0.
  std::vector<ospcommon::vec4f> planes;

  for(size_t i=0; i<sliceParameters.size(); i++)
    planes.push_back(ospcommon::vec4f(sliceParameters[i].normal.x,
                                sliceParameters[i].normal.y,
                                sliceParameters[i].normal.z,
                                -dot(sliceParameters[i].origin, sliceParameters[i].normal)));

  OSPData planesData = ospNewData(planes.size(), OSP_FLOAT4, &planes[0].x);

  // Remove existing slice geometries from models.
  for(size_t i=0; i<modelStates.size(); i++) {
    for(size_t j=0; j<modelStates[i].slices.size(); j++)
      ospRemoveGeometry(modelStates[i].model, modelStates[i].slices[j]);

    modelStates[i].slices.clear();
  }

  // Add new slices for each volume of each model. Later we can do this only for the active model on time step change...
  for(size_t i=0; i<modelStates.size(); i++) {

    if(planes.size() > 0) {
      for(size_t j=0; j<modelStates[i].volumes.size(); j++) {

        OSPGeometry slicesGeometry = ospNewGeometry("slices");
        ospSetData(slicesGeometry, "planes", planesData);
        ospSetObject(slicesGeometry, "volume", modelStates[i].volumes[j]);
        ospCommit(slicesGeometry);

        ospAddGeometry(modelStates[i].model, slicesGeometry);

        modelStates[i].slices.push_back(slicesGeometry);
      }
    }

    ospCommit(modelStates[i].model);
  }

  render();
}
Beispiel #15
0
void VolumeViewer::setSamplingRate(double value)
{
  for(size_t i=0; i<modelStates.size(); i++)
    for(size_t j=0; j<modelStates[i].volumes.size(); j++) {
      ospSet1f(modelStates[i].volumes[j], "samplingRate", value);
      ospCommit(modelStates[i].volumes[j]);
    }

  render();
}
 void PerspectiveCamera::commit() 
 {
   if (!ospCamera) create(); 
   
   ospSetVec3f(ospCamera,"pos",from);
   ospSetVec3f(ospCamera,"dir",at - from);
   ospSetVec3f(ospCamera,"up",up);
   ospSetf(ospCamera,"aspect",aspect);
   ospSetf(ospCamera,"fovy",fovy);
   ospCommit(ospCamera);      
 }
Beispiel #17
0
void VolumeViewer::setVolumeClippingBox(ospcommon::box3f value)
{
  for(size_t i=0; i<modelStates.size(); i++)
    for(size_t j=0; j<modelStates[i].volumes.size(); j++) {
      ospSet3fv(modelStates[i].volumes[j], "volumeClippingBoxLower", &value.lower.x);
      ospSet3fv(modelStates[i].volumes[j], "volumeClippingBoxUpper", &value.upper.x);
      ospCommit(modelStates[i].volumes[j]);
    }

  render();
}
Beispiel #18
0
void GLFWOSPRayWindow::setModel(OSPModel newModel)
{
  model = newModel;

  // set the model on the renderer
  ospSetObject(renderer, "model", model);

  // commit the renderer
  ospCommit(renderer);

  // clear frame buffer
  ospFrameBufferClear(framebuffer, OSP_FB_COLOR | OSP_FB_ACCUM);
}
Beispiel #19
0
    void World::render(RenderContext &ctx)
    {
      ctx.world = this;

      if (ospModel)
        throw std::runtime_error("World::ospModel alrady exists!?");
      ospModel = ospNewModel();
      affine3f xfm = embree::one;
      for (size_t i=0;i<node.size();i++) {
        node[i]->render(ctx);
      }
      ospCommit(ospModel);
    }
Beispiel #20
0
    void ToneMapper::postCommit(RenderContext &)
    {
      if (!child("enabled").valueAs<bool>())
        return;

      auto toneMapper = valueAs<OSPPixelOp>();

      float exposure = child("exposure").valueAs<float>();
      float linearExposure = exp2(exposure);
      ospSet1f(toneMapper, "exposure", linearExposure);

      ospCommit(toneMapper);
    }
Beispiel #21
0
    void Model::postCommit(RenderContext &ctx)
    {
      auto model = valueAs<OSPModel>();
      ctx.currentOSPModel = model;

      //instancegroup caches render calls in commit.
      for (auto &child : properties.children)
        child.second->finalize(ctx);

      ospCommit(model);
      ctx.currentOSPModel = stashedModel;

      // reset bounding box
      child("bounds") = box3f(empty);
      computeBounds();
    }
Beispiel #22
0
void OSPRayMaterial::commit()
{
    // Do nothing until this material is instanced for a specific renderer
    if (!_ospMaterial || !isModified())
        return;

    if (getCurrentType() == "simulation")
        osphelper::set(_ospMaterial, "apply_simulation", 1);
    else
        ospRemoveParam(_ospMaterial, "apply_simulation");

    osphelper::set(_ospMaterial, "kd", Vector3f(_diffuseColor));
    osphelper::set(_ospMaterial, "ks", Vector3f(_specularColor));
    osphelper::set(_ospMaterial, "ns", static_cast<float>(_specularExponent));
    osphelper::set(_ospMaterial, "d", static_cast<float>(_opacity));
    osphelper::set(_ospMaterial, "refraction",
                   static_cast<float>(_refractionIndex));
    osphelper::set(_ospMaterial, "reflection",
                   static_cast<float>(_reflectionIndex));
    osphelper::set(_ospMaterial, "a", static_cast<float>(_emission));
    osphelper::set(_ospMaterial, "glossiness", static_cast<float>(_glossiness));
    osphelper::set(_ospMaterial, "skybox", _isBackGroundMaterial);

    // Properties
    toOSPRayProperties(*this, _ospMaterial);

    // Textures
    for (const auto& textureType : textureTypeMaterialAttribute)
        ospSetObject(_ospMaterial, textureType.attribute.c_str(), nullptr);

    for (const auto& textureDescriptor : _textureDescriptors)
    {
        const auto texType = textureDescriptor.first;
        auto texture = getTexture(texType);
        if (texture)
        {
            auto ospTexture = _createOSPTexture2D(texture);
            const auto str =
                textureTypeMaterialAttribute[texType].attribute.c_str();
            ospSetObject(_ospMaterial, str, ospTexture);
            ospRelease(ospTexture);
        }
    }

    ospCommit(_ospMaterial);
    resetModified();
}
Beispiel #23
0
void LightEditor::alphaBetaSliderValueChanged()
{
  // Get alpha value in [-180, 180] degrees.
  float alpha = -180.0f + float(alphaSlider.value() - alphaSlider.minimum()) / float(alphaSlider.maximum() - alphaSlider.minimum()) * 360.0f;

  // Get beta value in [-90, 90] degrees.
  float beta = -90.0f + float(betaSlider.value() - betaSlider.minimum()) / float(betaSlider.maximum() - betaSlider.minimum()) * 180.0f;

  // Compute unit vector.
  float lightX = cos(alpha * M_PI/180.0f) * cos(beta * M_PI/180.0f);
  float lightY = sin(alpha * M_PI/180.0f) * cos(beta * M_PI/180.0f);
  float lightZ = sin(beta * M_PI/180.0f);

  // Update OSPRay light direction.
  ospSet3f(light, "direction", lightX, lightY, lightZ);

  // Commit and emit signal.
  ospCommit(light);
  emit lightChanged();
}
Beispiel #24
0
OSPTexture OSPRayMaterial::_createOSPTexture2D(Texture2DPtr texture)
{
    OSPTextureFormat type = OSP_TEXTURE_R8; // smallest valid type as default
    if (texture->getDepth() == 1)
    {
        if (texture->getNbChannels() == 1)
            type = OSP_TEXTURE_R8;
        if (texture->getNbChannels() == 3)
            type = OSP_TEXTURE_RGB8;
        if (texture->getNbChannels() == 4)
            type = OSP_TEXTURE_RGBA8;
    }
    else if (texture->getDepth() == 4)
    {
        if (texture->getNbChannels() == 1)
            type = OSP_TEXTURE_R32F;
        if (texture->getNbChannels() == 3)
            type = OSP_TEXTURE_RGB32F;
        if (texture->getNbChannels() == 4)
            type = OSP_TEXTURE_RGBA32F;
    }

    BRAYNS_DEBUG << "Creating OSPRay texture from " << texture->getFilename()
                 << ": " << texture->getWidth() << "x" << texture->getHeight()
                 << "x" << (int)type << std::endl;

    OSPTexture ospTexture = ospNewTexture("texture2d");

    const Vector2i size{int(texture->getWidth()), int(texture->getHeight())};

    osphelper::set(ospTexture, "type", static_cast<int>(type));
    osphelper::set(ospTexture, "size", size);
    auto textureData =
        ospNewData(texture->getSizeInBytes(), OSP_RAW, texture->getRawData(),
                   OSP_DATA_SHARED_BUFFER);
    ospSetObject(ospTexture, "data", textureData);
    ospRelease(textureData);
    ospCommit(ospTexture);

    return ospTexture;
}
Beispiel #25
0
int main(int ac, const char **av) {
  // image size
  osp_vec2i imgSize;
  imgSize.x = 1024; // width
  imgSize.y = 768; // height

  // camera
  float cam_pos[] = {0.f, 0.f, 0.f};
  float cam_up [] = {0.f, 1.f, 0.f};
  float cam_view [] = {0.1f, 0.f, 1.f};

  // triangle mesh data
  float vertex[] = { -1.0f, -1.0f, 3.0f, 0.f,
                     -1.0f,  1.0f, 3.0f, 0.f,
                      1.0f, -1.0f, 3.0f, 0.f,
                      0.1f,  0.1f, 0.3f, 0.f };
  float color[] =  { 0.9f, 0.5f, 0.5f, 1.0f,
                     0.8f, 0.8f, 0.8f, 1.0f,
                     0.8f, 0.8f, 0.8f, 1.0f,
                     0.5f, 0.9f, 0.5f, 1.0f };
  int32_t index[] = { 0, 1, 2,
                      1, 2, 3 };


  // initialize OSPRay; OSPRay parses (and removes) its commandline parameters, e.g. "--osp:debug"
  ospInit(&ac, av);

  // create and setup camera
  OSPCamera camera = ospNewCamera("perspective");
  ospSetf(camera, "aspect", imgSize.x/(float)imgSize.y);
  ospSet3fv(camera, "pos", cam_pos);
  ospSet3fv(camera, "dir", cam_view);
  ospSet3fv(camera, "up",  cam_up);
  ospCommit(camera); // commit each object to indicate modifications are done


  // create and setup model and mesh
  OSPGeometry mesh = ospNewGeometry("triangles");
  OSPData data = ospNewData(4, OSP_FLOAT3A, vertex, 0); // OSP_FLOAT3 format is also supported for vertex positions (currently not on MIC)
  ospCommit(data);
  ospSetData(mesh, "vertex", data);

  data = ospNewData(4, OSP_FLOAT4, color, 0);
  ospCommit(data);
  ospSetData(mesh, "vertex.color", data);

  data = ospNewData(2, OSP_INT3, index, 0); // OSP_INT4 format is also supported for triangle indices
  ospCommit(data);
  ospSetData(mesh, "index", data);

  ospCommit(mesh);


  OSPModel world = ospNewModel();
  ospAddGeometry(world, mesh);
  ospCommit(world);


  // create and setup renderer
  OSPRenderer renderer = ospNewRenderer("scivis"); // choose Scientific Visualization renderer
  ospSet1f(renderer, "aoWeight", 1.0f);            // with full Ambient Occlusion
  ospSet1i(renderer, "aoSamples", 1);
  ospSetObject(renderer, "model",  world);
  ospSetObject(renderer, "camera", camera);
  ospCommit(renderer);


  // create and setup framebuffer
  OSPFrameBuffer framebuffer = ospNewFrameBuffer(&imgSize, OSP_FB_SRGBA, OSP_FB_COLOR | /*OSP_FB_DEPTH |*/ OSP_FB_ACCUM);
  ospFrameBufferClear(framebuffer, OSP_FB_COLOR | OSP_FB_ACCUM);

  // render one frame
  ospRenderFrame(framebuffer, renderer, OSP_FB_COLOR | OSP_FB_ACCUM);

  // access framebuffer and write its content as PPM file
  const uint32_t * fb = (uint32_t*)ospMapFrameBuffer(framebuffer, OSP_FB_COLOR);
  writePPM("firstFrameC.ppm", &imgSize, fb);
  ospUnmapFrameBuffer(fb, framebuffer);


  // render 10 more frames, which are accumulated to result in a better converged image
  for (int frames = 0; frames < 10; frames++)
    ospRenderFrame(framebuffer, renderer, OSP_FB_COLOR | OSP_FB_ACCUM);

  fb = (uint32_t*)ospMapFrameBuffer(framebuffer, OSP_FB_COLOR);
  writePPM("accumulatedFrameC.ppm", &imgSize, fb);
  ospUnmapFrameBuffer(fb, framebuffer);

  return 0;
}
Beispiel #26
0
void VolumeViewer::commitVolumes()
{
  for(size_t i=0; i<modelStates.size(); i++)
    for(size_t j=0; j<modelStates[i].volumes.size(); j++)
      ospCommit(modelStates[i].volumes[j]);
}
Beispiel #27
0
GLFWOSPRayWindow::GLFWOSPRayWindow(const ospcommon::vec2i &windowSize,
                                   const ospcommon::box3f &worldBounds,
                                   OSPModel model,
                                   OSPRenderer renderer)
    : windowSize(windowSize),
      worldBounds(worldBounds),
      model(model),
      renderer(renderer)
{
  if (activeWindow != nullptr)
    throw std::runtime_error("Cannot create more than one GLFWOSPRayWindow!");

  activeWindow = this;

  // initialize GLFW
  if (!glfwInit())
    throw std::runtime_error("Failed to initialize GLFW!");

  // create GLFW window
  glfwWindow = glfwCreateWindow(
      windowSize.x, windowSize.y, "OSPRay Tutorial", NULL, NULL);

  if (!glfwWindow) {
    glfwTerminate();
    throw std::runtime_error("Failed to create GLFW window!");
  }

  // make the window's context current
  glfwMakeContextCurrent(glfwWindow);

  ImGui_ImplGlfwGL3_Init(glfwWindow, true);

  // set initial OpenGL state
  glEnable(GL_TEXTURE_2D);
  glDisable(GL_LIGHTING);

  // create OpenGL frame buffer texture
  glGenTextures(1, &framebufferTexture);
  glEnable(GL_TEXTURE_2D);
  glBindTexture(GL_TEXTURE_2D, framebufferTexture);
  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);

  // set GLFW callbacks
  glfwSetFramebufferSizeCallback(
      glfwWindow, [](GLFWwindow *, int newWidth, int newHeight) {
        activeWindow->reshape(ospcommon::vec2i{newWidth, newHeight});
      });

  glfwSetCursorPosCallback(glfwWindow, [](GLFWwindow *, double x, double y) {
    ImGuiIO &io = ImGui::GetIO();
    if (!io.WantCaptureMouse)
      activeWindow->motion(ospcommon::vec2f{float(x), float(y)});
  });

  glfwSetKeyCallback(glfwWindow,
                     [](GLFWwindow *, int key, int, int action, int) {
                       if (action == GLFW_PRESS) {
                         switch (key) {
                         case GLFW_KEY_G:
                           activeWindow->showUi = !(activeWindow->showUi);
                           break;
                         }
                       }
                     });

  // OSPRay setup

  // set the model on the renderer
  ospSetObject(renderer, "model", model);

  // create the arcball camera model
  arcballCamera = std::unique_ptr<ArcballCamera>(
      new ArcballCamera(worldBounds, windowSize));

  // create camera
  camera = ospNewCamera("perspective");
  ospSetf(camera, "aspect", windowSize.x / float(windowSize.y));

  ospSetVec3f(camera,
              "pos",
              osp::vec3f{arcballCamera->eyePos().x,
                         arcballCamera->eyePos().y,
                         arcballCamera->eyePos().z});
  ospSetVec3f(camera,
              "dir",
              osp::vec3f{arcballCamera->lookDir().x,
                         arcballCamera->lookDir().y,
                         arcballCamera->lookDir().z});
  ospSetVec3f(camera,
              "up",
              osp::vec3f{arcballCamera->upDir().x,
                         arcballCamera->upDir().y,
                         arcballCamera->upDir().z});

  ospCommit(camera);

  // set camera on the renderer
  ospSetObject(renderer, "camera", camera);

  // finally, commit the renderer
  ospCommit(renderer);

  // trigger window reshape events with current window size
  glfwGetFramebufferSize(glfwWindow, &this->windowSize.x, &this->windowSize.y);
  reshape(this->windowSize);
}
Beispiel #28
0
    void Material::render(RenderContext &ctx)
    {
      if (ospMaterial) return;
      
      ospMaterial = ospNewMaterial(ctx.integrator->getOSPHandle(), type.c_str());
      
      //We failed to create a material of the given type, handle it
      if (!ospMaterial) {
        std::cerr << "Warning: Could not create material type '" << type << "'. Replacing with default material." << std::endl;
        //Replace with default
        static OSPMaterial defaultMaterial = NULL;
        if (defaultMaterial) {
          ospMaterial = defaultMaterial;
          return;
        }
        defaultMaterial = ospNewMaterial(ctx.integrator->getOSPHandle(), "OBJMaterial");
        vec3f kd(.7f);
        vec3f ks(.3f);
        ospSet3fv(defaultMaterial, "Kd", &kd.x);
        ospSet3fv(defaultMaterial, "Ks", &ks.x);
        ospSet1f(defaultMaterial, "Ns", 99.f);
        ospCommit(defaultMaterial);
        return;
      }

      for(size_t i = 0; i < textures.size(); i++) {
        textures[i]->render(ctx);
      }
      
      //Forward all params on to the ospMaterial...
      for (ParamMap::const_iterator itr = param.begin(); itr != param.end(); ++itr) {
        switch(itr->second->getOSPDataType()) {
        case OSP_INT:
        case OSP_UINT:
          {
            ParamT<int> *p = (ParamT<int>*)itr->second.ptr;
            if(itr->second->getName().find("map_") != std::string::npos) {
              //Handle textures!
              assert(textures[p->value]->ospTexture != NULL && "Texture should not be null at this point.");
              ospSetObject(ospMaterial, itr->second->getName().c_str(), textures[p->value]->ospTexture);
            } else {
              ospSet1i(ospMaterial, itr->second->getName().c_str(), p->value);
            }
          }
          break;
        case OSP_INT3:
        case OSP_UINT3:
          {
            ParamT<vec3i> *p = (ParamT<vec3i>*)itr->second.ptr;
            ospSet3i(ospMaterial, itr->second->getName().c_str(), p->value.x, p->value.y, p->value.z);
          }
          break;
        case OSP_FLOAT:
          {
            ParamT<float> *p = (ParamT<float>*)itr->second.ptr;
            ospSet1f(ospMaterial, itr->second->getName().c_str(), p->value);
          }
          break;
        case OSP_FLOAT2:
          {
            ParamT<vec2f> *p = (ParamT<vec2f>*)itr->second.ptr;
            ospSet2fv(ospMaterial, itr->second->getName().c_str(), &p->value.x);
          }
          break;
        case OSP_FLOAT3:
          {
            ParamT<vec3f> *p = (ParamT<vec3f>*)itr->second.ptr;
            ospSet3fv(ospMaterial, itr->second->getName().c_str(), &p->value.x);
          }
          break;
        default: //Catch not yet implemented data types
          std::cerr << "Warning: parameter '" << itr->second->getName() << "' of material '" << name << "' had an invalid data type and will be ignored." << std::endl;
        }
      }
        

      ospCommit(ospMaterial);
    }
Beispiel #29
0
void VolumeViewer::setSubsamplingInteractionEnabled(bool value)
{
  ospSet1i(renderer, "spp", value ? -1 : 1);
  if(rendererInitialized)
    ospCommit(renderer);
}