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; }
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); }
void OSPObjectFile::importAttributeFloat3(const tinyxml2::XMLNode *node, OSPObject parent) { //! The attribute value is encoded in a string. const char *text = node->ToElement()->GetText(); vec3f value = vec3f(0.0f); char guard[8]; //! Get the attribute value. exitOnCondition(sscanf(text, "%f %f %f %7s", &value.x, &value.y, &value.z, guard) != 3, "malformed XML element '" + std::string(node->ToElement()->Name()) + "'"); //! Set the attribute on the parent object. ospSetVec3f(parent, node->ToElement()->Name(), value); }
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); }