From d6645d0494f55509c868dadcd91058e0cdc7fc03 Mon Sep 17 00:00:00 2001 From: Nick Gasson Date: Sat, 5 Dec 2009 15:58:04 +0000 Subject: [PATCH] Add new 3D preview window --- include/gui/Canvas3D.hpp | 37 ++++++++++++++++++++ src/gui/Canvas3D.cpp | 75 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 112 insertions(+) create mode 100644 include/gui/Canvas3D.hpp create mode 100644 src/gui/Canvas3D.cpp diff --git a/include/gui/Canvas3D.hpp b/include/gui/Canvas3D.hpp new file mode 100644 index 0000000..90eac87 --- /dev/null +++ b/include/gui/Canvas3D.hpp @@ -0,0 +1,37 @@ +// +// Copyright (C) 2009 Nick Gasson +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// This program 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 General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program. If not, see . +// + +#ifndef INC_CANVAS3D_HPP +#define INC_CANVAS3D_HPP + +#include "Platform.hpp" +#include "gui/Widget.hpp" + +namespace gui { + + // A canvas which arbitrary 3D scenes can be rendered on + class Canvas3D : public Widget { + public: + Canvas3D(const AttributeSet& attrs); + + void render(RenderContext& rc) const; + private: + bool clear; + }; +} + +#endif diff --git a/src/gui/Canvas3D.cpp b/src/gui/Canvas3D.cpp new file mode 100644 index 0000000..e574c0c --- /dev/null +++ b/src/gui/Canvas3D.cpp @@ -0,0 +1,75 @@ +// +// Copyright (C) 2009 Nick Gasson +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// This program 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 General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program. If not, see . +// + +#include "gui/Canvas3D.hpp" +#include "ILogger.hpp" + +#include +#include + +#include + +using namespace gui; +using namespace boost; + +Canvas3D::Canvas3D(const AttributeSet& attrs) + : Widget(attrs), + clear(attrs.get("clear", true)) +{ + +} + +void Canvas3D::render(RenderContext& rc) const +{ + glPushAttrib(GL_ALL_ATTRIB_BITS); + glPushMatrix(); + + glMatrixMode(GL_PROJECTION); + glPushMatrix(); + glLoadIdentity(); + + const GLfloat wf = static_cast(width()); + const GLfloat hf = static_cast(height()); + gluPerspective(45.0f, wf/hf, 0.1f, 50.0f); + + glMatrixMode(GL_MODELVIEW); + glLoadIdentity(); + + if (clear) + glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); + + glEnable(GL_LIGHTING); + glEnable(GL_DEPTH_TEST); + + const_cast(this)->raise(SIG_RENDER); + + glMatrixMode(GL_PROJECTION); + glPopMatrix(); + + glMatrixMode(GL_MODELVIEW); + glPopMatrix(); + + glPopAttrib(); + + GLenum error = glGetError(); + if (error != GL_NO_ERROR) { + throw runtime_error + ("OpenGL error: " + lexical_cast(gluErrorString(error))); + } +} + + -- 2.39.2