From 4922d448896abe5330f106f21fd6a3b0651ae9eb Mon Sep 17 00:00:00 2001 From: Tavian Barnes Date: Mon, 6 Jul 2009 16:37:17 +0000 Subject: New C++ Raytracer type. --- libdimensionxx/Makefile.am | 5 ++- libdimensionxx/dimensionxx.hpp | 1 + libdimensionxx/dimensionxx/raytrace.hpp | 50 ++++++++++++++++++++++++ libdimensionxx/dimensionxx/scene.hpp | 4 -- libdimensionxx/raytrace.cpp | 68 +++++++++++++++++++++++++++++++++ libdimensionxx/scene.cpp | 13 ------- 6 files changed, 123 insertions(+), 18 deletions(-) create mode 100644 libdimensionxx/dimensionxx/raytrace.hpp create mode 100644 libdimensionxx/raytrace.cpp (limited to 'libdimensionxx') diff --git a/libdimensionxx/Makefile.am b/libdimensionxx/Makefile.am index 7f4ad85..9c22cd5 100644 --- a/libdimensionxx/Makefile.am +++ b/libdimensionxx/Makefile.am @@ -28,7 +28,9 @@ nobase_include_HEADERS = dimensionxx.hpp \ dimensionxx/object.hpp \ dimensionxx/objects.hpp \ dimensionxx/png.hpp \ - dimensionxx/progress.hpp + dimensionxx/progress.hpp \ + dimensionxx/raytrace.hpp \ + dimensionxx/scene.hpp INCLUDES = -I../libdimension @@ -44,6 +46,7 @@ libdimensionxx_la_SOURCES = $(nobase_include_HEADERS) \ objects.cpp \ png.cpp \ progress.cpp \ + raytrace.cpp \ scene.cpp if FOPENCOOKIE diff --git a/libdimensionxx/dimensionxx.hpp b/libdimensionxx/dimensionxx.hpp index e8a7636..bc52580 100644 --- a/libdimensionxx/dimensionxx.hpp +++ b/libdimensionxx/dimensionxx.hpp @@ -37,5 +37,6 @@ #include #include #include +#include #endif /* DIMENSIONXX_HPP */ diff --git a/libdimensionxx/dimensionxx/raytrace.hpp b/libdimensionxx/dimensionxx/raytrace.hpp new file mode 100644 index 0000000..bfd7e1c --- /dev/null +++ b/libdimensionxx/dimensionxx/raytrace.hpp @@ -0,0 +1,50 @@ +/************************************************************************* + * Copyright (C) 2008 Tavian Barnes * + * * + * This file is part of The Dimension Library. * + * * + * The Dimension Library is free software; you can redistribute it and/ * + * or modify it under the terms of the GNU Lesser General Public License * + * as published by the Free Software Foundation; either version 3 of the * + * License, or (at your option) any later version. * + * * + * The Dimension Library is distributed in the hope that it will be * + * useful, but WITHOUT ANY WARRANTY; without even the implied warranty * + * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * + * Lesser General Public License for more details. * + * * + * You should have received a copy of the GNU Lesser General Public * + * License along with this program. If not, see * + * . * + *************************************************************************/ + +// C++ wrapper for libdimension raytracing + +#ifndef DIMENSIONXX_RAYTRACE_HPP +#define DIMENSIONXX_RAYTRACE_HPP + +#include +#include + +namespace Dimension +{ + class Raytracer + { + public: + Raytracer(Scene& scene); + ~Raytracer(); + + void render(); + Progress render_async(); + + private: + // Copying prohibited + Raytracer(const Raytracer&); + Raytracer& operator=(const Raytracer&); + + Scene* m_scene; + bool m_rendered; + }; +} + +#endif /* DIMENSIONXX_RAYTRACE_HPP */ diff --git a/libdimensionxx/dimensionxx/scene.hpp b/libdimensionxx/dimensionxx/scene.hpp index 4f906ef..256de29 100644 --- a/libdimensionxx/dimensionxx/scene.hpp +++ b/libdimensionxx/dimensionxx/scene.hpp @@ -46,10 +46,6 @@ namespace Dimension // Add objects void push_object(Object& object); - // Render it! - void raytrace(); - Progress raytrace_async(); - // Access the wrapped C object. dmnsn_scene* dmnsn(); const dmnsn_scene* dmnsn() const; diff --git a/libdimensionxx/raytrace.cpp b/libdimensionxx/raytrace.cpp new file mode 100644 index 0000000..263fe6d --- /dev/null +++ b/libdimensionxx/raytrace.cpp @@ -0,0 +1,68 @@ +/************************************************************************* + * Copyright (C) 2008 Tavian Barnes * + * * + * This file is part of The Dimension Library. * + * * + * The Dimension Library is free software; you can redistribute it and/ * + * or modify it under the terms of the GNU Lesser General Public License * + * as published by the Free Software Foundation; either version 3 of the * + * License, or (at your option) any later version. * + * * + * The Dimension Library is distributed in the hope that it will be * + * useful, but WITHOUT ANY WARRANTY; without even the implied warranty * + * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * + * Lesser General Public License for more details. * + * * + * You should have received a copy of the GNU Lesser General Public * + * License along with this program. If not, see * + * . * + *************************************************************************/ + +#include "dimensionxx.hpp" + +namespace Dimension +{ + Raytracer::Raytracer(Scene& scene) + : m_scene(&scene), m_rendered(false) { } + + // Call render() if we've never rendered the scene + Raytracer::~Raytracer() + { + if (!m_rendered) { + try { + render(); + } catch (...) { + dmnsn_error(SEVERITY_MEDIUM, + "Rendering scene failed in Raytracer destructor."); + } + } + } + + // Render the scene + void Raytracer::render() + { + // Raytrace the scene + if (dmnsn_raytrace_scene(m_scene->dmnsn()) != 0) { + // The rendering operation failed + throw Dimension_Error("Raytracing scene failed."); + } + + m_rendered = true; // Don't render the scene again in the destructor + } + + // Render a scene in the background + Progress + Raytracer::render_async() + { + m_rendered = true; // Don't render the scene again in the destructor + + // Start the asynchronous task + dmnsn_progress *progress = dmnsn_raytrace_scene_async(m_scene->dmnsn()); + if (!progress) { + throw Dimension_Error("Starting background raytrace failed."); + } + + // Return the Progress object + return Progress(progress); + } +} diff --git a/libdimensionxx/scene.cpp b/libdimensionxx/scene.cpp index 5719207..f5e4ea8 100644 --- a/libdimensionxx/scene.cpp +++ b/libdimensionxx/scene.cpp @@ -85,19 +85,6 @@ namespace Dimension dmnsn_array_push(m_scene->objects, &cobject); } - // Render it! - - void Scene::raytrace() - { - dmnsn_raytrace_scene(m_scene); - } - - Progress - Scene::raytrace_async() - { - return Progress(dmnsn_raytrace_scene_async(m_scene)); - } - // Access the wrapped C object. dmnsn_scene* -- cgit v1.2.3