glRenderer.h

Go to the documentation of this file.
00001 // -*- Mode: C++; tab-width: 2; -*-
00002 // vi: set ts=2:
00003 //
00004 // $Id: glRenderer.h,v 1.36.16.1 2007/03/25 21:26:15 oliver Exp $
00005 //
00006 
00007 #ifndef BALL_VIEW_RENDERING_GLRENDERER_H
00008 #define BALL_VIEW_RENDERING_GLRENDERER_H
00009 
00010 #ifndef BALL_VIEW_RENDERING_RENDERER_H
00011 # include <BALL/VIEW/RENDERING/renderer.h>
00012 #endif
00013 
00014 #ifndef BALL_MATHS_QUATERNION_H
00015 # include <BALL/MATHS/quaternion.h>
00016 #endif
00017 
00018 #ifndef BALL_VIEW_DATATYPE_COLORRGBA_H
00019 # include <BALL/VIEW/DATATYPE/colorRGBA.h>
00020 #endif
00021 
00022 #ifndef BALL_VIEW_KERNEL_GEOMETRICOBJECT_H
00023 # include <BALL/VIEW/KERNEL/geometricObject.h>
00024 #endif
00025 
00026 #ifndef BALL_VIEW_KERNEL_STAGE_H
00027 # include <BALL/VIEW/KERNEL/stage.h>
00028 #endif
00029 
00030 #ifdef BALL_HAS_GLEW
00031 # include <GL/glew.h>
00032 #endif
00033 
00034 #ifndef BALL_VIEW_RENDERING_GLDISPLAYLIST_H
00035 # include <BALL/VIEW/RENDERING/glDisplayList.h>
00036 #endif
00037 
00038 #ifndef BALL_DATATYPE_REGULARDATA3D_H
00039 # include <BALL/DATATYPE/regularData3D.h>
00040 #endif
00041 
00042 #ifndef APIENTRY
00043 #define APIENTRY
00044 #endif
00045 
00046 class QFont;
00047 
00048 namespace BALL
00049 {
00050 // defines the maximal number of GL-objects, which can be selected in picking mode
00051 // a number as big as 100.000 is needed for large molecules, just to be sure we use a million
00052 #define BALL_GLRENDERER_PICKING_NUMBER_OF_MAX_OBJECTS 1000000
00053   namespace VIEW
00054   {
00055     class Scene;
00056     class GLDisplayList;
00057     class MeshBuffer;
00058     class ColorMap;
00059     class RenderTarget;
00060 
00065     class BALL_VIEW_EXPORT GLRenderer
00066       : public Renderer
00067     {
00068       friend class Scene;
00069       public:
00070 
00072       enum StereoMode
00073       {
00074         NO_STEREO = 0,
00075 
00077         ACTIVE_STEREO,
00078 
00080         DUAL_VIEW_STEREO,
00081 
00083         DUAL_VIEW_DIFFERENT_DISPLAY_STEREO
00084       };
00085 
00087       enum RenderMode
00088       {
00090         RENDER_MODE_UNDEFINED = 0,
00091 
00093         RENDER_MODE_SOLID,
00094 
00096         RENDER_MODE_TRANSPARENT,
00097 
00099         RENDER_MODE_ALWAYS_FRONT
00100       };
00101 
00102 
00104       enum BufferMode
00105       {
00107         DIRECT_RENDERING = 0,
00108 
00110         DISPLAY_LISTS_RENDERING,
00111 
00113         REBUILD_DISPLAY_LISTS
00114       };
00115 
00116 
00122       class BALL_VIEW_EXPORT WrongModes:  public Exception::GeneralException
00123       {
00124         public:
00125 
00126         WrongModes(const char* file, int line, int mode, int precision);
00127       };
00128 
00130       typedef unsigned int Name;
00131 
00133       GLRenderer();
00134 
00136       virtual ~GLRenderer();
00137 
00139       virtual void clear();
00140 
00142       void dump(std::ostream& s, Size depth) const;
00143 
00145       inline Name getName(const GeometricObject& object);
00146 
00148       GeometricObject* getObject(GLRenderer::Name name) const;
00149 
00150       virtual bool init(Scene& scene);
00151 
00153       virtual bool init(const Stage& stage, float width, float height);
00154 
00156       virtual void setLights(bool reset_all = false);
00157 
00160       virtual void setSmoothLines(bool smooth_lines);
00161       virtual bool getSmoothLines();
00162 
00166       Vector3 mapViewportTo3D(Position x, Position y);
00167 
00171       Vector2 map3DToViewport(const Vector3& vec);
00172 
00176       void pickObjects1(Position x1, Position y1, Position x2, Position y2);
00177 
00182       void pickObjects2(List<GeometricObject*>& objects);
00183 
00185       void enterPickingMode();
00186 
00188       void exitPickingMode();
00189 
00191       void setSize(float width, float height);
00192 
00194       float getXScale() const;
00195 
00197       float getYScale() const;
00198 
00200       virtual void updateBackgroundColor();
00201 
00202       // Initialise transparent rendering
00203       void initTransparent();
00204 
00205       // Initialise solid rendering
00206       void initSolid();
00207 
00208       // Initialise always front rendering
00209       void initAlwaysFront();
00210 
00212       void setAntialiasing(bool state);
00213 
00215       void removeRepresentation(const Representation& rep);
00216 
00218       void bufferRepresentation(const Representation& rep);
00219 
00221       void drawBuffered(const Representation& rep);
00222 
00224       bool hasDisplayListFor(const Representation& rep) const;
00225 
00227       void setStereoMode(StereoMode state);
00228 
00230       StereoMode getStereoMode() const;
00231 
00233       RenderMode getRenderMode() const;
00234 
00236       void setRenderMode(RenderMode mode) { render_mode_ = mode;}
00237 
00242       virtual void renderToBuffer(RenderTarget* renderTarget, BufferMode);
00243 
00245       virtual bool render(const Representation& representation, bool for_display_list = false);
00246 
00247       virtual void bufferingDependentRender_(const Representation& repr, BufferMode mode);
00248 
00252       bool isExtensionSupported(const String& extension) const;
00253 
00255       void clearVertexBuffersFor(Representation& rep);
00256 
00258       bool vertexBuffersSupported() const;
00259 
00261       String getVendor();
00262 
00264       String getRenderer();
00265 
00267       String getOpenGLVersion();
00268 
00270       vector<String> getExtensions();
00271 
00273       bool enableVertexBuffers(bool state);
00274 
00276       bool vertexBuffersEnabled() const;
00277 
00279       DrawingMode getDrawingMode() const;
00280 
00282       void initPerspective();
00283 
00284       //_
00285       void setColorRGBA_(const ColorRGBA& color);
00286 
00287       //_
00288       void vertexVector3_(const Vector3& v);
00289 
00290       //
00291       void updateCamera(const Camera* camera = 0);
00292 
00293       //
00294       void setupStereo(float eye_separation, float focal_length);
00295 
00296       Position createTextureFromGrid(const RegularData3D& grid, const ColorMap& map);
00297       void removeTextureFor_(const RegularData3D& grid);
00298 
00299       void getFrustum(float& near, float& far, float& left, float& right, float& top, float& bottom);
00300 
00301   protected:
00302 
00306       bool mapViewplaneToScreen_();
00307 
00308       void renderRepresentation_(const Representation& representation, bool for_display_list);
00309 
00311       void renderRepresentations_(BufferMode mode);
00312 
00314       virtual void renderLabel_(const Label& /*label*/);
00315 
00317       virtual void renderLine_(const Line& /*line*/);
00318 
00320       virtual void renderMultiLine_(const MultiLine& line);
00321 
00323       virtual void renderMesh_(const Mesh& /*mesh*/);
00324 
00326       virtual void renderQuadMesh_(const QuadMesh& /*mesh*/);
00327 
00329       void initDrawingMeshes_();
00330 
00332       void initDrawingOthers_();
00333 
00341       virtual void renderRuler();
00342 
00344       virtual void renderPoint_(const Point& /*point*/);
00345 
00347       virtual void renderSimpleBox_(const SimpleBox& /*box*/);
00348 
00350       virtual void renderBox_(const Box& /*box*/);
00351 
00353       virtual void renderSphere_(const Sphere& /*sphere*/);
00354 
00356       virtual void renderDisc_(const Disc& /*disc*/);
00357 
00359       virtual void renderTube_(const Tube& /*tube*/);
00360 
00362       virtual void renderTwoColoredLine_(const TwoColoredLine& /*two_colored_line*/);
00363 
00365       virtual void renderTwoColoredTube_(const TwoColoredTube& /*two_colored_tube*/);
00366 
00368       virtual void renderClippingPlane_(const ClippingPlane& plane);
00369 
00371       virtual void renderGridVisualisation_(const GridVisualisation& vol);
00372 
00373       //_
00374       void setColor4ub_(const GeometricObject& object);
00375 
00376       //_
00377       void createSpheres_();
00378 
00379       //_
00380       void createTubes_();
00381 
00382       //_
00383       void createBoxes_();
00384 
00385       //_
00386       void createDottedSphere_(int precision);
00387 
00388       //_
00389       void subdivideTriangle_(Vector3& v1, Vector3& v2, Vector3& v3, int precision);
00390 
00391       //_
00392       void createLineBox_();
00393 
00394       //_
00395       void createDotBox_();
00396 
00397       //_
00398       void createSolidBox_();
00399 
00400       //_
00401       void clearNames_();
00402 
00403       //_
00404       void normalVector3_(const Vector3& v);
00405 
00406 
00407       //_
00408       void translateVector3_(const Vector3& v);
00409 
00410       //_
00411       void texCoordVector3_(const Vector3& v)
00412         { glTexCoord3f(v.x, v.y, v.z); }
00413 
00414       //_
00415       void scaleVector3_(const Vector3& v);
00416 
00417       //_
00418       void rotateVector3Angle_(const Vector3& v, Real angle);
00419 
00420       //_
00421       void scale_(float f);
00422 
00423 
00424       void initGLU_(DrawingMode mode);
00425 
00426       //_
00427       void generateIlluminationTexture_(float ka, float kd, float kr, float shininess);
00428 
00429       inline Position getTextureIndex_(Position x, Position y, Position z, Size width, Size height);
00430       void setupGridClipPlanes_(const GridVisualisation& slice);
00431 
00433       DrawingMode           drawing_mode_;
00434 
00436       Index                 drawing_precision_;
00437 
00438       //_
00439       float                 near_;
00440       //_
00441       float                 far_;
00442       //_
00443       float                 left_;
00444       //_
00445       float                 right_;
00446       //_
00447       float                 top_;
00448       //_
00449       float                 bottom_;
00450       
00451       //_
00452       float                 x_scale_;
00453 
00454       //_
00455       float                 y_scale_;
00456 
00457       GLDisplayList*        GL_spheres_list_;
00458       GLDisplayList*        GL_tubes_list_;
00459       GLDisplayList*        GL_boxes_list_;
00460       GLDisplayList*        sphere_list_;
00461       GLDisplayList         line_list_;
00462       GLuint                line_texture_bind_;
00463       GLubyte               line_tex_[128][128][4];
00464 
00465       // naming of geometric objects
00466       typedef HashMap<const GeometricObject*, Name> NameHashMap;
00467       typedef HashMap<Name, const GeometricObject*> GeometricObjectHashMap;
00468       typedef HashMap<const Representation*, GLDisplayList*> DisplayListHashMap;
00469       typedef HashMap<const Representation*, vector<MeshBuffer*> > MeshBufferHashMap;
00470 
00471       GeometricObjectHashMap  name_to_object_;
00472       NameHashMap             object_to_name_;
00473       DisplayListHashMap      display_lists_;
00474       MeshBufferHashMap       rep_to_buffers_;
00475       Name                    all_names_;
00476       GLuint                  object_buffer_[BALL_GLRENDERER_PICKING_NUMBER_OF_MAX_OBJECTS];
00477       Vector3                 normal_vector_;
00478 
00479       StereoMode              stereo_;
00480       RenderMode              render_mode_;
00481 
00482       bool                    use_vertex_buffer_;
00483       bool smooth_lines_;
00484       bool                    picking_mode_;
00485       ModelType               model_type_;
00486       Position                display_lists_index_;
00487       bool                    single_pick_;
00488       bool                    drawed_other_object_;
00489       bool                    drawed_mesh_;
00490       GLUquadricObj*  GLU_quadric_obj_;
00491       HashMap<const RegularData3D*, Position> grid_to_texture_;
00492       GLuint                  cel_texture_;
00493     };
00494 
00495 # ifndef BALL_NO_INLINE_FUNCTIONS
00496 #   include <BALL/VIEW/RENDERING/glRenderer.iC>
00497 # endif
00498 
00499   } // namespace VIEW
00500 } // namespace BALL
00501 
00502 #endif // BALL_VIEW_RENDERING_GLRENDERER_H