diff options
author | Tavian Barnes <tavianator@gmail.com> | 2009-07-06 16:37:17 +0000 |
---|---|---|
committer | Tavian Barnes <tavianator@gmail.com> | 2009-07-06 16:37:17 +0000 |
commit | 4922d448896abe5330f106f21fd6a3b0651ae9eb (patch) | |
tree | 50dc450e044059388406769c59e2418175266e8b | |
parent | e2c40b8d6cabc68eda634e103d0824fb58d5908b (diff) | |
download | dimension-4922d448896abe5330f106f21fd6a3b0651ae9eb.tar.xz |
New C++ Raytracer type.
-rw-r--r-- | libdimensionxx/Makefile.am | 5 | ||||
-rw-r--r-- | libdimensionxx/dimensionxx.hpp | 1 | ||||
-rw-r--r-- | libdimensionxx/dimensionxx/raytrace.hpp | 50 | ||||
-rw-r--r-- | libdimensionxx/dimensionxx/scene.hpp | 4 | ||||
-rw-r--r-- | libdimensionxx/raytrace.cpp | 68 | ||||
-rw-r--r-- | libdimensionxx/scene.cpp | 13 | ||||
-rw-r--r-- | tests/raytracexx.cpp | 3 |
7 files changed, 125 insertions, 19 deletions
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 <dimensionxx/camera.hpp> #include <dimensionxx/cameras.hpp> #include <dimensionxx/scene.hpp> +#include <dimensionxx/raytrace.hpp> #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 <tavianator@gmail.com> * + * * + * 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 * + * <http://www.gnu.org/licenses/>. * + *************************************************************************/ + +// C++ wrapper for libdimension raytracing + +#ifndef DIMENSIONXX_RAYTRACE_HPP +#define DIMENSIONXX_RAYTRACE_HPP + +#include <istream> +#include <ostream> + +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 <tavianator@gmail.com> * + * * + * 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 * + * <http://www.gnu.org/licenses/>. * + *************************************************************************/ + +#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* diff --git a/tests/raytracexx.cpp b/tests/raytracexx.cpp index e3d55cf..c04e525 100644 --- a/tests/raytracexx.cpp +++ b/tests/raytracexx.cpp @@ -59,7 +59,8 @@ main() { // Render the scene { - Progress rprogress = scene.raytrace_async(); + Raytracer raytracer(scene); + Progress rprogress = raytracer.render_async(); std::cout << "Raytracing scene: " << rprogress << std::endl; } |