Main Page   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members   File Members  

panda/src/glgsg/glGraphicsStateGuardian.h

Go to the documentation of this file.
00001 // Filename: glGraphicsStateGuardian.h
00002 // Created by:  drose (02Feb99)
00003 //
00004 ////////////////////////////////////////////////////////////////////
00005 //
00006 // PANDA 3D SOFTWARE
00007 // Copyright (c) 2001, Disney Enterprises, Inc.  All rights reserved
00008 //
00009 // All use of this software is subject to the terms of the Panda 3d
00010 // Software license.  You should have received a copy of this license
00011 // along with this source code; you will also find a current copy of
00012 // the license at http://www.panda3d.org/license.txt .
00013 //
00014 // To contact the maintainers of this program write to
00015 // panda3d@yahoogroups.com .
00016 //
00017 ////////////////////////////////////////////////////////////////////
00018 
00019 #ifndef GLGRAPHICSSTATEGUARDIAN_H
00020 #define GLGRAPHICSSTATEGUARDIAN_H
00021 
00022 //#define GSG_VERBOSE 1
00023 
00024 #include "pandabase.h"
00025 
00026 #include "graphicsStateGuardian.h"
00027 #include "geomprimitives.h"
00028 #include "texture.h"
00029 #include "pixelBuffer.h"
00030 #include "displayRegion.h"
00031 #include "material.h"
00032 #include "depthTestAttrib.h"
00033 #include "textureApplyAttrib.h"
00034 #include "pointerToArray.h"
00035 #include "fog.h"
00036 
00037 #ifdef WIN32_VC
00038 // Must include windows.h before gl.h on NT
00039 #define WIN32_LEAN_AND_MEAN
00040 #include <windows.h>
00041 #undef WIN32_LEAN_AND_MEAN
00042 #endif
00043 
00044 #include <GL/gl.h>
00045 
00046 
00047 class PlaneNode;
00048 class Light;
00049 
00050 #if !defined(WIN32) && defined(GSG_VERBOSE)
00051 ostream &output_gl_enum(ostream &out, GLenum v);
00052 INLINE ostream &operator << (ostream &out, GLenum v) {
00053   return output_gl_enum(out, v);
00054 }
00055 #endif
00056 
00057 
00058 ////////////////////////////////////////////////////////////////////
00059 //       Class : GLGraphicsStateGuardian
00060 // Description : A GraphicsStateGuardian specialized for rendering
00061 //               into OpenGL contexts.  There should be no GL calls
00062 //               outside of this object.
00063 ////////////////////////////////////////////////////////////////////
00064 class EXPCL_PANDAGL GLGraphicsStateGuardian : public GraphicsStateGuardian {
00065 public:
00066   GLGraphicsStateGuardian(const FrameBufferProperties &properties);
00067   virtual ~GLGraphicsStateGuardian();
00068 
00069   virtual void reset();
00070 
00071   virtual void do_clear(const RenderBuffer &buffer);
00072 
00073   virtual void prepare_display_region();
00074   virtual bool prepare_lens();
00075 
00076   virtual bool begin_frame();
00077   virtual void end_frame();
00078 
00079   virtual void draw_point(GeomPoint *geom, GeomContext *gc);
00080   virtual void draw_line(GeomLine *geom, GeomContext *gc);
00081   virtual void draw_linestrip(GeomLinestrip *geom, GeomContext *gc);
00082   virtual void draw_sprite(GeomSprite *geom, GeomContext *gc);
00083   virtual void draw_polygon(GeomPolygon *geom, GeomContext *gc);
00084   virtual void draw_quad(GeomQuad *geom, GeomContext *gc);
00085   virtual void draw_tri(GeomTri *geom, GeomContext *gc);
00086   virtual void draw_tristrip(GeomTristrip *geom, GeomContext *gc);
00087   virtual void draw_trifan(GeomTrifan *geom, GeomContext *gc);
00088   virtual void draw_sphere(GeomSphere *geom, GeomContext *gc);
00089 
00090   virtual TextureContext *prepare_texture(Texture *tex);
00091   virtual void apply_texture(TextureContext *tc);
00092   virtual void release_texture(TextureContext *tc);
00093 
00094   virtual GeomNodeContext *prepare_geom_node(GeomNode *node);
00095   virtual void draw_geom_node(GeomNode *node, const RenderState *state,
00096                               GeomNodeContext *gnc);
00097   virtual void release_geom_node(GeomNodeContext *gnc);
00098 
00099   virtual void copy_texture(TextureContext *tc, const DisplayRegion *dr);
00100   virtual void copy_texture(TextureContext *tc, const DisplayRegion *dr,
00101                             const RenderBuffer &rb);
00102 
00103   virtual void texture_to_pixel_buffer(TextureContext *tc, PixelBuffer *pb);
00104   virtual void texture_to_pixel_buffer(TextureContext *tc, PixelBuffer *pb,
00105                                        const DisplayRegion *dr);
00106 
00107   virtual void copy_pixel_buffer(PixelBuffer *pb, const DisplayRegion *dr);
00108   virtual void copy_pixel_buffer(PixelBuffer *pb, const DisplayRegion *dr,
00109                                  const RenderBuffer &rb);
00110 
00111   virtual void apply_material(const Material *material);
00112   void apply_fog(Fog *fog);
00113 
00114   virtual void issue_transform(const TransformState *transform);
00115   virtual void issue_tex_matrix(const TexMatrixAttrib *attrib);
00116   virtual void issue_texture(const TextureAttrib *attrib);
00117   virtual void issue_material(const MaterialAttrib *attrib);
00118   virtual void issue_render_mode(const RenderModeAttrib *attrib);
00119   virtual void issue_texture_apply(const TextureApplyAttrib *attrib);
00120   virtual void issue_color_write(const ColorWriteAttrib *attrib);
00121   virtual void issue_depth_test(const DepthTestAttrib *attrib);
00122   virtual void issue_alpha_test(const AlphaTestAttrib *attrib);
00123   virtual void issue_depth_write(const DepthWriteAttrib *attrib);
00124   virtual void issue_cull_face(const CullFaceAttrib *attrib);
00125   virtual void issue_fog(const FogAttrib *attrib);
00126   virtual void issue_depth_offset(const DepthOffsetAttrib *attrib);
00127   //  virtual void issue_tex_gen(const TexGenAttrib *attrib);
00128   //  virtual void issue_stencil(const StencilAttrib *attrib);
00129 
00130   virtual void bind_light(PointLight *light, int light_id);
00131   virtual void bind_light(DirectionalLight *light, int light_id);
00132   virtual void bind_light(Spotlight *light, int light_id);
00133 
00134   virtual bool wants_normals(void) const;
00135   virtual bool wants_texcoords(void) const;
00136 
00137   virtual bool depth_offset_decals();
00138 
00139   virtual CoordinateSystem get_internal_coordinate_system() const;
00140   virtual float compute_distance_to(const LPoint3f &point) const;
00141 
00142   void print_gfx_visual();
00143 
00144   //For those interested in what the guardian thinks is the current
00145   //enabled/disable GL State compared to what GL says it is
00146   void dump_state(void);
00147 
00148   void issue_transformed_color(const Colorf &color) const;
00149 
00150   INLINE static void report_errors(int line, const char *source_file);
00151 
00152 protected:
00153   static void report_errors_loop(int line, const char *source_file, 
00154                                  GLenum error_code);
00155   virtual bool slot_new_light(int light_id);
00156   virtual void enable_lighting(bool enable);
00157   virtual void set_ambient_light(const Colorf &color);
00158   virtual void enable_light(int light_id, bool enable);
00159   virtual void begin_bind_lights();
00160   virtual void end_bind_lights();
00161 
00162   virtual bool slot_new_clip_plane(int plane_id);
00163   virtual void enable_clip_plane(int plane_id, bool enable);
00164   virtual void begin_bind_clip_planes();
00165   virtual void bind_clip_plane(PlaneNode *plane, int plane_id);
00166   virtual void end_bind_clip_planes();
00167 
00168   virtual void set_blend_mode(ColorWriteAttrib::Mode color_write_mode,
00169                               ColorBlendAttrib::Mode color_blend_mode,
00170                               TransparencyAttrib::Mode transparency_mode);
00171 
00172   virtual void free_pointers();
00173   virtual PT(SavedFrameBuffer) save_frame_buffer(const RenderBuffer &buffer,
00174                                                  CPT(DisplayRegion) dr);
00175   virtual void restore_frame_buffer(SavedFrameBuffer *frame_buffer);
00176 
00177   INLINE void call_glClearColor(GLclampf red, GLclampf green, GLclampf blue,
00178                                 GLclampf alpha);
00179   INLINE void call_glClearDepth(GLclampd depth);
00180   INLINE void call_glClearStencil(GLint s);
00181   INLINE void call_glClearAccum(GLclampf red, GLclampf green, GLclampf blue,
00182                                 GLclampf alpha);
00183   INLINE void call_glDrawBuffer(GLenum mode);
00184   INLINE void call_glReadBuffer(GLenum mode);
00185   INLINE void call_glShadeModel(GLenum mode);
00186   INLINE void call_glBlendFunc(GLenum sfunc, GLenum dfunc);
00187   INLINE void call_glCullFace(GLenum mode);
00188   INLINE void call_glScissor(GLint x, GLint y, GLsizei width, GLsizei height);
00189   INLINE void call_glViewport(GLint x, GLint y, GLsizei width, GLsizei height);
00190   INLINE void call_glLightModelLocal(GLboolean local);
00191   INLINE void call_glLightModelTwoSide(GLboolean twoside);
00192   INLINE void call_glStencilFunc(GLenum func,GLint refval,GLuint mask);
00193   INLINE void call_glStencilOp(GLenum fail,GLenum zfail,GLenum pass);
00194   INLINE void call_glClipPlane(GLenum plane, const double equation[4]);
00195   INLINE void call_glLineWidth(GLfloat width);
00196   INLINE void call_glPointSize(GLfloat size);
00197   INLINE void call_glFogMode(GLint mode);
00198   INLINE void call_glFogStart(GLfloat start);
00199   INLINE void call_glFogEnd(GLfloat end);
00200   INLINE void call_glFogDensity(GLfloat density);
00201   INLINE void call_glFogColor(const Colorf &color);
00202   INLINE void call_glAlphaFunc(GLenum func, GLclampf ref);
00203   INLINE void call_glPolygonMode(GLenum mode);
00204 
00205   INLINE void set_pack_alignment(GLint alignment);
00206   INLINE void set_unpack_alignment(GLint alignment);
00207 
00208   INLINE void enable_multisample(bool val);
00209   INLINE void enable_line_smooth(bool val);
00210   INLINE void enable_point_smooth(bool val);
00211   INLINE void enable_texturing(bool val);
00212   INLINE void enable_scissor(bool val);
00213   INLINE void enable_stencil_test(bool val);
00214   INLINE void enable_multisample_alpha_one(bool val);
00215   INLINE void enable_multisample_alpha_mask(bool val);
00216   INLINE void enable_blend(bool val);
00217   INLINE void enable_depth_test(bool val);
00218   INLINE void enable_fog(bool val);
00219   INLINE void enable_alpha_test(bool val);
00220   INLINE void enable_polygon_offset(bool val);
00221 
00222   INLINE GLenum get_light_id(int index) const;
00223   INLINE GLenum get_clip_plane_id(int index) const;
00224 
00225   INLINE void issue_scene_graph_color();
00226 
00227   void set_draw_buffer(const RenderBuffer &rb);
00228   void set_read_buffer(const RenderBuffer &rb);
00229 
00230   void bind_texture(TextureContext *tc);
00231   void specify_texture(Texture *tex);
00232   bool apply_texture_immediate(Texture *tex);
00233 
00234   void draw_texture(TextureContext *tc, const DisplayRegion *dr);
00235   void draw_texture(TextureContext *tc, const DisplayRegion *dr, 
00236                     const RenderBuffer &rb);
00237   void draw_pixel_buffer(PixelBuffer *pb, const DisplayRegion *dr);
00238   void draw_pixel_buffer(PixelBuffer *pb, const DisplayRegion *dr,
00239                          const RenderBuffer &rb);
00240 
00241   GLenum get_texture_wrap_mode(Texture::WrapMode wm);
00242   GLenum get_texture_filter_type(Texture::FilterType ft);
00243   GLenum get_image_type(PixelBuffer::Type type);
00244   GLenum get_external_image_format(PixelBuffer::Format format);
00245   GLenum get_internal_image_format(PixelBuffer::Format format);
00246   GLint get_texture_apply_mode_type(TextureApplyAttrib::Mode am) const;
00247   GLenum get_fog_mode_type(Fog::Mode m) const;
00248 
00249   static CPT(RenderState) get_untextured_state();
00250 
00251 #ifndef NDEBUG
00252   void build_phony_mipmaps(Texture *tex);
00253   void build_phony_mipmap_level(int level, int xsize, int ysize);
00254   void save_mipmap_images(Texture *tex);
00255 #endif
00256 
00257   GLclampf _clear_color_red, _clear_color_green, _clear_color_blue,
00258     _clear_color_alpha;
00259   GLclampd _clear_depth;
00260   GLint _clear_stencil;
00261   GLclampf _clear_accum_red, _clear_accum_green, _clear_accum_blue,
00262     _clear_accum_alpha;
00263   GLenum _draw_buffer_mode;
00264   GLenum _read_buffer_mode;
00265   GLenum _shade_model_mode;
00266   GLint _scissor_x;
00267   GLint _scissor_y;
00268   GLsizei _scissor_width;
00269   GLsizei _scissor_height;
00270   GLint _viewport_x;
00271   GLint _viewport_y;
00272   GLsizei _viewport_width;
00273   GLsizei _viewport_height;
00274   GLboolean _lmodel_local;
00275   GLboolean _lmodel_twoside;
00276   GLfloat _line_width;
00277   GLfloat _point_size;
00278   GLenum _blend_source_func;
00279   GLenum _blend_dest_func;
00280   GLboolean _depth_mask;
00281   GLint _fog_mode;
00282   GLfloat _fog_start;
00283   GLfloat _fog_end;
00284   GLfloat _fog_density;
00285   Colorf _fog_color;
00286   GLenum _alpha_func;
00287   GLclampf _alpha_func_ref;
00288   GLenum _polygon_mode;
00289 
00290   GLint _pack_alignment;
00291   GLint _unpack_alignment;
00292 
00293   bool _multisample_enabled;
00294   bool _line_smooth_enabled;
00295   bool _point_smooth_enabled;
00296   bool _scissor_enabled;
00297   bool _texturing_enabled;
00298   bool _stencil_test_enabled;
00299   bool _multisample_alpha_one_enabled;
00300   bool _multisample_alpha_mask_enabled;
00301   bool _blend_enabled;
00302   bool _depth_test_enabled;
00303   bool _fog_enabled;
00304   bool _alpha_test_enabled;
00305   bool _polygon_offset_enabled;
00306   int _decal_level;
00307 
00308   bool _dithering_enabled;
00309 
00310   int _max_lights;
00311   int _max_clip_planes;
00312 
00313   LMatrix4f _current_projection_mat;
00314   int _projection_mat_stack_count;
00315 
00316   CPT(DisplayRegion) _actual_display_region;
00317 
00318   int _pass_number;
00319 
00320 public:
00321   static GraphicsStateGuardian *
00322   make_GlGraphicsStateGuardian(const FactoryParams &params);
00323 
00324   static TypeHandle get_class_type(void);
00325   static void init_type(void);
00326   virtual TypeHandle get_type(void) const;
00327   virtual TypeHandle force_init_type() {init_type(); return get_class_type();}
00328 
00329   static PStatCollector _vertices_display_list_pcollector;
00330 
00331 private:
00332   static TypeHandle _type_handle;
00333 };
00334 
00335 #ifdef DO_PSTATS
00336 #define DO_PSTATS_STUFF(XX) XX;
00337 #else
00338 #define DO_PSTATS_STUFF(XX)
00339 #endif
00340 
00341 #define ISPOW2(X) (((X) & ((X)-1))==0)
00342 
00343 #ifndef NDEBUG
00344 #define report_gl_errors() \
00345   GLGraphicsStateGuardian::report_errors(__LINE__, __FILE__)
00346 #else
00347 #define report_gl_errors()
00348 #endif
00349 
00350 #include "glGraphicsStateGuardian.I"
00351 
00352 #endif
00353 

Generated on Fri May 2 00:39:08 2003 for Panda by doxygen1.3