From e81e9c0c6834ff7753680751780c5bd55fbdee2e Mon Sep 17 00:00:00 2001 From: Nick Gasson Date: Thu, 9 Jul 2009 21:50:59 +0100 Subject: [PATCH] Add FLTK model viewer --- include/ModelViewer.hpp | 42 ++++++++++++++++++++++ src/Editor.cpp | 5 +++ src/Mesh.cpp | 2 +- src/ModelViewer.cpp | 78 +++++++++++++++++++++++++++++++++++++++++ 4 files changed, 126 insertions(+), 1 deletion(-) create mode 100644 include/ModelViewer.hpp create mode 100644 src/ModelViewer.cpp diff --git a/include/ModelViewer.hpp b/include/ModelViewer.hpp new file mode 100644 index 0000000..aac186b --- /dev/null +++ b/include/ModelViewer.hpp @@ -0,0 +1,42 @@ +// +// 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_MODELVIEWER_HPP +#define INC_MODELVIEWER_HPP + +#include "Platform.hpp" +#include "IModel.hpp" + +#include +#include + +// FLTK widget for displaying models +class ModelViewer : public Fl_Gl_Window { +public: + ModelViewer(int x, int y, int w, int h); + ~ModelViewer(); + + void setModel(IModelPtr aModel) { myModel = aModel; } + + // Fl_Gl_Window interface + void draw(); + int handle(int anEvent); +private: + IModelPtr myModel; +}; + +#endif diff --git a/src/Editor.cpp b/src/Editor.cpp index 23b6b84..2b970d5 100644 --- a/src/Editor.cpp +++ b/src/Editor.cpp @@ -22,6 +22,7 @@ #include "Maths.hpp" #include "ILight.hpp" #include "BuildingPanel.hpp" +#include "ModelViewer.hpp" #include @@ -99,6 +100,7 @@ namespace { }; Fl_Menu_Button* theToolMenu; + ModelViewer* theModelViewer; Editor* theEditor = NULL; @@ -116,6 +118,9 @@ void addEditorGUI() theToolMenu = new Fl_Menu_Button(0, 0, panelW, 32); theToolMenu->copy(theTools); + + theModelViewer = new ModelViewer(0, 40, panelW, 200); + theModelViewer->setModel(loadModel("pclass.obj")); } Editor::Editor(IMapPtr aMap, const string& aFileName) diff --git a/src/Mesh.cpp b/src/Mesh.cpp index f719545..d3e2a02 100644 --- a/src/Mesh.cpp +++ b/src/Mesh.cpp @@ -551,7 +551,7 @@ IMeshPtr makeMesh(IMeshBufferPtr aBuffer) { //aBuffer->printStats(); - // Prefer VBOs for large meshes meshes + // Prefer VBOs for large meshes if (aBuffer->vertexCount() > 100 && GLEW_ARB_vertex_buffer_object) return IMeshPtr(new VBOMesh(aBuffer)); else diff --git a/src/ModelViewer.cpp b/src/ModelViewer.cpp new file mode 100644 index 0000000..c78f6ab --- /dev/null +++ b/src/ModelViewer.cpp @@ -0,0 +1,78 @@ +// +// 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 "ModelViewer.hpp" +#include "ILight.hpp" + +#include + +#include + +#include +#include + +using namespace boost; + +ModelViewer::ModelViewer(int x, int y, int w, int h) + : Fl_Gl_Window(x, y, w, h) +{ + +} + +ModelViewer::~ModelViewer() +{ + +} + +void ModelViewer::draw() +{ + static ILightPtr sun = makeSunLight(); + + glMatrixMode(GL_PROJECTION); + glLoadIdentity(); + + const GLfloat wf = static_cast(w()); + const GLfloat hf = static_cast(h()); + gluPerspective(45.0f, wf/hf, 0.1f, 50.0f); + + glMatrixMode(GL_MODELVIEW); + glLoadIdentity(); + + glEnable(GL_LIGHTING); + glEnable(GL_DEPTH_TEST); + + sun->apply(); + + if (myModel) { + glRotatef(45.0f, 1.0f, 0.0f, 0.0f); + glRotatef(45.0f, 0.0f, 1.0f, 0.0f); + glTranslatef(5.0f, -8.0f, -4.0f); + glColor3f(1.0f, 1.0f, 1.0f); + myModel->render(); + } + + GLenum error = glGetError(); + if (error != GL_NO_ERROR) { + throw runtime_error + ("OpenGL error: " + lexical_cast(gluErrorString(error))); + } +} + +int ModelViewer::handle(int anEvent) +{ + return Fl_Gl_Window::handle(anEvent); +} -- 2.39.2