From 5f04e8e41f287b8aa1c7298654b9217f98df46d1 Mon Sep 17 00:00:00 2001 From: Stijn Buys Date: Mon, 13 Feb 2012 21:48:42 +0000 Subject: Added support for globes to ui::ModelView. --- src/ui/modelview.cc | 151 ++++++++++++++++++++++++++++++++++++++++++++++++---- src/ui/modelview.h | 31 ++++++++--- 2 files changed, 165 insertions(+), 17 deletions(-) (limited to 'src/ui') diff --git a/src/ui/modelview.cc b/src/ui/modelview.cc index be56856..79544bb 100755 --- a/src/ui/modelview.cc +++ b/src/ui/modelview.cc @@ -21,14 +21,12 @@ namespace ui GLenum modelview_gllight = GL_LIGHT0 + 7; -ModelView::ModelView(Widget *parent, const char *modelname) : Widget(parent) +ModelView::ModelView(Widget *parent) : Widget(parent) { set_border(false); set_background(false); set_label("modelview"); - set_modelname(modelname); - modelview_radius = 0.75f; modelview_zoom = 1.0f; modelview_dragging = false; @@ -36,6 +34,8 @@ ModelView::ModelView(Widget *parent, const char *modelname) : Widget(parent) modelview_axis.clear(); modelview_axis.change_direction(180); modelview_axis.change_pitch(-15); + + modelview_mode = Model; } ModelView::~ModelView() @@ -47,19 +47,33 @@ void ModelView::print(const size_t indent) const con_print << aux::pad_left(marker, indent*2) << label() << " \"" << modelname() << "\"" << std::endl; } -void ModelView::set_modelname(const char *modelname) +void ModelView::set_globetexturename(const std::string & texturename, const bool bright, const std::string & coronaname) { - if (modelname) - modelview_modelname.assign(modelname); - else - modelview_modelname.clear(); + modelview_globetexturename.assign(texturename); + modelview_globecoronaname.assign(coronaname); + modelview_globebright = bright; set_background(false); + modelview_axis.clear(); + modelview_axis.change_direction(180); + modelview_axis.change_pitch(-15); + modelview_zoom = 1.0f; + modelview_modelname.clear(); +} + +void ModelView::set_modelname(const std::string & modelname) +{ + modelview_modelname.assign(modelname); + + set_background(false); modelview_axis.clear(); modelview_axis.change_direction(180); modelview_axis.change_pitch(-15); modelview_zoom = 1.0f; + modelview_globetexturename.clear(); + modelview_globecoronaname.clear(); + modelview_globebright = false; } void ModelView::set_colors(const math::Color & color_primary, const math::Color & color_secondary) @@ -141,11 +155,126 @@ void ModelView::draw_background() void ModelView::draw() { - if (!modelview_modelname.size()) { + if (!core::application()->connected() || !core::game()->time()) { return; } - if (!core::application()->connected() || !core::game()->time()) { + switch (mode()) { + + case Model: + draw_model(); + break; + case Globe: + draw_globe(); + break; + } +} + +void ModelView::draw_globe() +{ + math::Vector2f center(global_location() + size() * 0.5f); + + const float minscreen(math::min(root()->width(),root()->height())); + const float minwidget(math::min(width(), height())); + const float reference_radius = radius() * minwidget / minscreen; + + // gl 3d mode + render::Camera::frustum_default(modelview_zoom, center.x(), center.y()); + + // reset lighting + render::pass_reset_lights(); + + // enable lighting + gl::enable(GL_LIGHTING); + + gl::disable(GL_BLEND); + gl::depthmask(GL_TRUE); // enable writing to the depth buffer + gl::enable(GL_DEPTH_TEST); // enable depth test + + gl::enable(GL_CULL_FACE); // enable culling + gl::enable(GL_COLOR_MATERIAL); // enable color tracking + + // clear depth buffer + gl::clear(GL_DEPTH_BUFFER_BIT); + + // enable vertex arrays + glEnableClientState(GL_VERTEX_ARRAY); + glEnableClientState(GL_TEXTURE_COORD_ARRAY); + glEnableClientState(GL_NORMAL_ARRAY); + + // we set up the light in camera space + for (size_t i = 0; i < 8; i++) { + // check if a light is available + if (!glIsEnabled(GL_LIGHT0 + i)) { + modelview_gllight = GL_LIGHT0 + i; + break; + } + } + + GLfloat modelview_light[] = { -10.0f * reference_radius, 0, 0, 1.0f }; + GLfloat ambient_light[] = { 0.1f, 0.1f, 0.1f, 1.0f }; + GLfloat diffuse_light[] = { 0.75f, 0.75f, 0.75f, 1.0f }; + GLfloat specular_light[] = { 0.75f, 0.75f, 0.75f, 1.0f }; + + glLightfv(modelview_gllight, GL_POSITION, modelview_light); + glLightfv(modelview_gllight, GL_AMBIENT, ambient_light); + glLightfv(modelview_gllight, GL_DIFFUSE, diffuse_light); + glLightfv(modelview_gllight, GL_SPECULAR, specular_light); + + glLightf(modelview_gllight, GL_LINEAR_ATTENUATION, 0.05f); + + gl::enable(modelview_gllight); + + render::State::set_normalize(true); + + // push transformation matrix to stack + gl::push(); + + if (modelview_globebright) { + gl::disable(GL_LIGHTING); + + if (modelview_globecoronaname.size()) { + size_t corona_id = render::Textures::load("textures/corona/" + modelview_globecoronaname); + render::draw_globe_corona(math::Vector3f(0.0f, 0.0f, 0.0f), modelview_color_primary, reference_radius, corona_id); + } + } + + gl::multmatrix(modelview_axis); + + if (modelview_globetexturename.size()) { + // textured globe + render::Textures::bind("textures/" + modelview_globetexturename); + gl::enable(GL_TEXTURE_2D); + } + + render::draw_sphere(modelview_color_primary, reference_radius); + + gl::pop(); + + render::State::set_normalize(false); + gl::disable(modelview_gllight); + + glDisableClientState(GL_VERTEX_ARRAY); + glDisableClientState(GL_NORMAL_ARRAY); + glDisableClientState(GL_TEXTURE_COORD_ARRAY); + + gl::enable(GL_BLEND); + gl::disable(GL_TEXTURE_2D); + gl::disable(GL_COLOR_MATERIAL); // disable color tracking + gl::disable(GL_CULL_FACE); // disable culling + gl::depthmask(GL_FALSE); // enable depth buffer writing + gl::disable(GL_DEPTH_TEST); // disable depth buffer testing + gl::disable(GL_LIGHTING); + + // gl 2d mode + render::Camera::ortho(); + + +} + +void ModelView::draw_model() +{ + if (!modelview_modelname.size()) { return; } @@ -157,7 +286,7 @@ void ModelView::draw() return; } - math ::Vector2f center(global_location() + size() * 0.5f); + math::Vector2f center(global_location() + size() * 0.5f); const float minscreen(math::min(root()->width(),root()->height())); const float minwidget(math::min(width(), height())); diff --git a/src/ui/modelview.h b/src/ui/modelview.h index f63dfa8..5596271 100755 --- a/src/ui/modelview.h +++ b/src/ui/modelview.h @@ -16,13 +16,19 @@ namespace ui class ModelView : public Widget { public: - ModelView(Widget *parent, const char *modelname = 0); + ModelView(Widget *parent); ~ModelView(); + + enum Mode { Model = 0, Globe = 1 }; inline std::string const &modelname() const { return modelview_modelname; } + inline Mode mode() const { + return modelview_mode; + } + inline float zoom() const { return modelview_zoom; } @@ -31,11 +37,9 @@ public: return modelview_radius; } - void set_modelname(const char *modelname); - - inline void set_modelname(const std::string & modelname) { - set_modelname(modelname.c_str()); - } + void set_globetexturename(const std::string & texturename, const bool bright, const std::string & coronaname); + + void set_modelname(const std::string & modelname); void set_colors(const math::Color & color_primary, const math::Color & color_secondary); @@ -53,6 +57,10 @@ public: inline void set_radius(const float radius) { modelview_radius = radius; } + + inline void set_mode(const Mode mode) { + modelview_mode = mode; + } protected: /// draw the widget @@ -76,9 +84,20 @@ protected: virtual void on_mousemove(const math::Vector2f &cursor); private: + void draw_model(); + + void draw_globe(); + + Mode modelview_mode; + std::string modelview_modelname; + std::string modelview_globetexturename; + std::string modelview_globecoronaname; + bool modelview_globebright; + math::Color modelview_color_primary; math::Color modelview_color_secondary; + float modelview_zoom; float modelview_radius; -- cgit v1.2.3