home *** CD-ROM | disk | FTP | other *** search
/ Hackers Magazine 57 / CdHackersMagazineNr57.iso / Software / Multimedia / k3d-setup-0.7.11.0.exe / include / k3d / k3dsdk / renderable_gl.h < prev    next >
C/C++ Source or Header  |  2008-11-07  |  4KB  |  155 lines

  1. #ifndef K3DSDK_RENDERABLE_GL_H
  2. #define K3DSDK_RENDERABLE_GL_H
  3.  
  4. // K-3D
  5. // Copyright (c) 1995-2006, Timothy M. Shead
  6. //
  7. // Contact: tshead@k-3d.com
  8. //
  9. // This program is free software; you can redistribute it and/or
  10. // modify it under the terms of the GNU General Public
  11. // License as published by the Free Software Foundation; either
  12. // version 2 of the License, or (at your option) any later version.
  13. //
  14. // This program is distributed in the hope that it will be useful,
  15. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  17. // General Public License for more details.
  18. //
  19. // You should have received a copy of the GNU General Public
  20. // License along with this program; if not, write to the Free Software
  21. // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  22.  
  23. /** \file
  24.         \author Tim Shead (tshead@k-3d.com)
  25. */
  26.  
  27. #include "data.h"
  28. #include "gl.h"
  29. #include "irender_viewport_gl.h"
  30. #include "irenderable_gl.h"
  31. #include "k3d-i18n-config.h"
  32. #include "render_state_gl.h"
  33. #include "utility_gl.h"
  34.  
  35. namespace k3d
  36. {
  37.  
  38. class bounding_box3;
  39. class idocument;
  40. class plane;
  41.  
  42. namespace gl
  43. {
  44.  
  45. class selection_state;
  46.     
  47. /**    \brief Provides a boilerplate implementation of k3d::gl::irenderable
  48.     \param base_t Must derive from k3d::transformable
  49. */
  50. template<typename base_t>
  51. class renderable :
  52.     public base_t,
  53.     public irenderable
  54. {
  55. public:
  56.     renderable(iplugin_factory& Factory, idocument& Document) :
  57.         base_t(Factory, Document),
  58.         m_visible(init_owner(*this) + init_name("viewport_visible") + init_label(_("Viewport Visible")) + init_description(_("Controls whether this node will be visibile in the viewport.")) + init_value(true)),
  59.         m_nurbs_renderer(0)
  60.     {
  61.         m_visible.changed_signal().connect(make_async_redraw_slot());
  62.     }
  63.  
  64.     ~renderable()
  65.     {
  66.         if(m_nurbs_renderer)
  67.             gluDeleteNurbsRenderer(m_nurbs_renderer);
  68.     }
  69.  
  70.     void gl_draw(const render_state& State)
  71.     {
  72.         if(!m_visible.pipeline_value())
  73.             return;
  74.  
  75.         store_attributes attributes;
  76.  
  77.         push_draw();
  78.         on_gl_draw(State);
  79.         pop_draw();
  80.     }
  81.  
  82.     void gl_select(const render_state& State, const selection_state& SelectState)
  83.     {
  84.         if(!m_visible.pipeline_value())
  85.             return;
  86.  
  87.         store_attributes attributes;
  88.  
  89.         push_draw();
  90.         on_gl_select(State, SelectState);
  91.         pop_draw();
  92.     }
  93.  
  94. protected:
  95.     sigc::slot<void, ihint*> make_async_redraw_slot()
  96.     {
  97.         return sigc::mem_fun(*this, &renderable<base_t>::async_redraw);
  98.     }
  99.  
  100.     void async_redraw(ihint*)
  101.     {
  102.         redraw_all(base_t::document(), irender_viewport::ASYNCHRONOUS);
  103.     }
  104.  
  105.     typedef GLUnurbsObj* nurbs_renderer_t;
  106.     nurbs_renderer_t nurbs_renderer(const render_state& State)
  107.     {
  108.         if(!m_nurbs_renderer)
  109.         {
  110.             m_nurbs_renderer = gluNewNurbsRenderer();
  111.  
  112.             // Important!  We load our own matrices for efficiency (saves round-trips to the server) and to prevent problems with selection
  113.             gluNurbsProperty(m_nurbs_renderer, GLU_AUTO_LOAD_MATRIX, GL_FALSE);
  114.             gluNurbsProperty(m_nurbs_renderer, GLU_CULLING, GL_TRUE);
  115.         }
  116.  
  117.         GLfloat gl_modelview_matrix[16];
  118.         glGetFloatv(GL_MODELVIEW_MATRIX, gl_modelview_matrix);
  119.         gluLoadSamplingMatrices(m_nurbs_renderer, gl_modelview_matrix, State.gl_projection_matrix, State.gl_viewport);
  120.  
  121.         return m_nurbs_renderer;
  122.     }
  123.  
  124. protected:
  125.     /// Set to true iff the object should be visible in OpenGL viewports
  126.     k3d_data(bool, data::immutable_name, data::change_signal, data::with_undo, data::local_storage, data::no_constraint, data::writable_property, data::with_serialization) m_visible;
  127.  
  128. private:
  129.     void push_draw()
  130.     {
  131.         glMatrixMode(GL_MODELVIEW);
  132.         glPushMatrix();
  133.         push_matrix(base_t::matrix());
  134.     }
  135.  
  136.     void pop_draw()
  137.     {
  138.         glMatrixMode(GL_MODELVIEW);
  139.         glPopMatrix();
  140.     }
  141.  
  142.     virtual void on_gl_draw(const render_state& State) = 0;
  143.     virtual void on_gl_select(const render_state& State, const selection_state& SelectState) = 0;
  144.  
  145.     /// OpenGL NURBS renderer
  146.     nurbs_renderer_t m_nurbs_renderer;
  147. };
  148.  
  149. } // namespace gl
  150.  
  151. } // namespace k3d
  152.  
  153. #endif // !K3DSDK_RENDERABLE_GL_H
  154.  
  155.