00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019 #ifndef CRGRAPHICSSTATEGUARDIAN_H
00020 #define CRGRAPHICSSTATEGUARDIAN_H
00021
00022
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
00036 #ifdef WIN32_VC
00037
00038 #define WIN32_LEAN_AND_MEAN
00039 #include <windows.h>
00040 #undef WIN32_LEAN_AND_MEAN
00041 #endif
00042
00043
00044 #include <GL/gl.h>
00045
00046 #ifdef WIN32_VC // [
00047 #define WINDOWS 1
00048 #endif //]
00049 #include "cr_glwrapper.h"
00050 #include "cr_applications.h"
00051 #include "cr_spu.h"
00052 extern SPUDispatchTable chromium;
00053
00054
00055
00056 class PlaneNode;
00057 class Light;
00058
00059 #if !defined(WIN32) && defined(GSG_VERBOSE)
00060 ostream &output_cr_enum(ostream &out, GLenum v);
00061 INLINE ostream &operator << (ostream &out, GLenum v) {
00062 return output_cr_enum(out, v);
00063 }
00064 #endif
00065
00066
00067
00068
00069
00070
00071
00072
00073 class EXPCL_PANDACR CRGraphicsStateGuardian : public GraphicsStateGuardian {
00074 public:
00075 CRGraphicsStateGuardian(GraphicsWindow *win);
00076 ~CRGraphicsStateGuardian();
00077
00078 virtual void reset();
00079
00080 virtual void clear(const RenderBuffer &buffer);
00081 virtual void clear(const RenderBuffer &buffer, const DisplayRegion* region);
00082
00083 virtual void prepare_display_region();
00084 virtual bool prepare_lens();
00085
00086 virtual void draw_point(GeomPoint *geom, GeomContext *gc);
00087 virtual void draw_line(GeomLine *geom, GeomContext *gc);
00088 virtual void draw_linestrip(GeomLinestrip *geom, GeomContext *gc);
00089 virtual void draw_sprite(GeomSprite *geom, GeomContext *gc);
00090 virtual void draw_polygon(GeomPolygon *geom, GeomContext *gc);
00091 virtual void draw_quad(GeomQuad *geom, GeomContext *gc);
00092 virtual void draw_tri(GeomTri *geom, GeomContext *gc);
00093 virtual void draw_tristrip(GeomTristrip *geom, GeomContext *gc);
00094 virtual void draw_trifan(GeomTrifan *geom, GeomContext *gc);
00095 virtual void draw_sphere(GeomSphere *geom, GeomContext *gc);
00096
00097 virtual TextureContext *prepare_texture(Texture *tex);
00098 virtual void apply_texture(TextureContext *tc);
00099 virtual void release_texture(TextureContext *tc);
00100
00101 virtual GeomNodeContext *prepare_geom_node(GeomNode *node);
00102 virtual void draw_geom_node(GeomNode *node, const RenderState *state,
00103 GeomNodeContext *gnc);
00104 virtual void release_geom_node(GeomNodeContext *gnc);
00105
00106 virtual void copy_texture(TextureContext *tc, const DisplayRegion *dr);
00107 virtual void copy_texture(TextureContext *tc, const DisplayRegion *dr,
00108 const RenderBuffer &rb);
00109
00110 virtual void texture_to_pixel_buffer(TextureContext *tc, PixelBuffer *pb);
00111 virtual void texture_to_pixel_buffer(TextureContext *tc, PixelBuffer *pb,
00112 const DisplayRegion *dr);
00113
00114 virtual void copy_pixel_buffer(PixelBuffer *pb, const DisplayRegion *dr);
00115 virtual void copy_pixel_buffer(PixelBuffer *pb, const DisplayRegion *dr,
00116 const RenderBuffer &rb);
00117 virtual void draw_pixel_buffer(PixelBuffer *pb, const DisplayRegion *dr,
00118 const NodeTransitions& na=NodeTransitions());
00119 virtual void draw_pixel_buffer(PixelBuffer *pb, const DisplayRegion *dr,
00120 const RenderBuffer &rb,
00121 const NodeTransitions& na=NodeTransitions());
00122
00123 virtual void apply_material(const Material *material);
00124 void apply_fog(Fog *fog);
00125
00126 virtual void issue_transform(const TransformState *transform);
00127 virtual void issue_tex_matrix(const TexMatrixAttrib *attrib);
00128 virtual void issue_texture(const TextureAttrib *attrib);
00129 virtual void issue_material(const MaterialAttrib *attrib);
00130 virtual void issue_render_mode(const RenderModeAttrib *attrib);
00131 virtual void issue_texture_apply(const TextureApplyAttrib *attrib);
00132 virtual void issue_color_write(const ColorWriteAttrib *attrib);
00133 virtual void issue_depth_test(const DepthTestAttrib *attrib);
00134 virtual void issue_depth_write(const DepthWriteAttrib *attrib);
00135 virtual void issue_cull_face(const CullFaceAttrib *attrib);
00136 virtual void issue_fog(const FogAttrib *attrib);
00137 virtual void issue_depth_offset(const DepthOffsetAttrib *attrib);
00138
00139
00140
00141
00142 virtual void bind_light(PointLight *light, int light_id);
00143 virtual void bind_light(DirectionalLight *light, int light_id);
00144 virtual void bind_light(Spotlight *light, int light_id);
00145
00146 virtual bool wants_normals(void) const;
00147 virtual bool wants_texcoords(void) const;
00148
00149 virtual bool depth_offset_decals();
00150
00151 virtual CoordinateSystem get_internal_coordinate_system() const;
00152 virtual float compute_distance_to(const LPoint3f &point) const;
00153
00154 void print_gfx_visual();
00155
00156
00157
00158 void dump_state(void);
00159
00160
00161 INLINE const LMatrix4f &get_current_color_mat() const;
00162 INLINE const float &get_current_alpha_offset() const;
00163 INLINE const float &get_current_alpha_scale() const;
00164
00165 void issue_transformed_color(const Colorf &color) const;
00166
00167 protected:
00168 virtual void enable_lighting(bool enable);
00169 virtual void set_ambient_light(const Colorf &color);
00170 virtual void enable_light(int light_id, bool enable);
00171 virtual void begin_bind_lights();
00172 virtual void end_bind_lights();
00173
00174 virtual void set_blend_mode(ColorWriteAttrib::Mode color_write_mode,
00175 ColorBlendAttrib::Mode color_blend_mode,
00176 TransparencyAttrib::Mode transparency_mode);
00177
00178 void free_pointers();
00179 virtual PT(SavedFrameBuffer) save_frame_buffer(const RenderBuffer &buffer,
00180 CPT(DisplayRegion) dr);
00181 virtual void restore_frame_buffer(SavedFrameBuffer *frame_buffer);
00182
00183 INLINE void activate();
00184
00185 INLINE void call_glClearColor(GLclampf red, GLclampf green, GLclampf blue,
00186 GLclampf alpha);
00187 INLINE void call_glClearDepth(GLclampd depth);
00188 INLINE void call_glClearStencil(GLint s);
00189 INLINE void call_glClearAccum(GLclampf red, GLclampf green, GLclampf blue,
00190 GLclampf alpha);
00191 INLINE void call_glDrawBuffer(GLenum mode);
00192 INLINE void call_glReadBuffer(GLenum mode);
00193 INLINE void call_glShadeModel(GLenum mode);
00194 INLINE void call_glBlendFunc(GLenum sfunc, GLenum dfunc);
00195 INLINE void call_glCullFace(GLenum mode);
00196 INLINE void call_glScissor(GLint x, GLint y, GLsizei width, GLsizei height);
00197 INLINE void call_glViewport(GLint x, GLint y, GLsizei width, GLsizei height);
00198 INLINE void call_glLightModelLocal(GLboolean local);
00199 INLINE void call_glLightModelTwoSide(GLboolean twoside);
00200 INLINE void call_glStencilFunc(GLenum func,GLint refval,GLuint mask);
00201 INLINE void call_glStencilOp(GLenum fail,GLenum zfail,GLenum pass);
00202 INLINE void call_glClipPlane(GLenum plane, const double equation[4]);
00203 INLINE void call_glLineWidth(GLfloat width);
00204 INLINE void call_glPointSize(GLfloat size);
00205 INLINE void call_glFogMode(GLint mode);
00206 INLINE void call_glFogStart(GLfloat start);
00207 INLINE void call_glFogEnd(GLfloat end);
00208 INLINE void call_glFogDensity(GLfloat density);
00209 INLINE void call_glFogColor(const Colorf &color);
00210 INLINE void call_glAlphaFunc(GLenum func, GLclampf ref);
00211 INLINE void call_glPolygonMode(GLenum mode);
00212
00213 INLINE void set_pack_alignment(GLint alignment);
00214 INLINE void set_unpack_alignment(GLint alignment);
00215
00216 INLINE void enable_multisample(bool val);
00217 INLINE void enable_line_smooth(bool val);
00218 INLINE void enable_point_smooth(bool val);
00219 INLINE void enable_texturing(bool val);
00220 INLINE void enable_scissor(bool val);
00221 INLINE void enable_dither(bool val);
00222 INLINE void enable_stencil_test(bool val);
00223 INLINE void enable_clip_plane(int clip_plane, bool val);
00224 INLINE void enable_multisample_alpha_one(bool val);
00225 INLINE void enable_multisample_alpha_mask(bool val);
00226 INLINE void enable_blend(bool val);
00227 INLINE void enable_depth_test(bool val);
00228 INLINE void enable_fog(bool val);
00229 INLINE void enable_alpha_test(bool val);
00230 INLINE void enable_polygon_offset(bool val);
00231
00232 INLINE GLenum get_light_id(int index) const;
00233 INLINE GLenum get_clip_plane_id(int index) const;
00234
00235 INLINE void issue_scene_graph_color();
00236
00237 void set_draw_buffer(const RenderBuffer &rb);
00238 void set_read_buffer(const RenderBuffer &rb);
00239
00240 void bind_texture(TextureContext *tc);
00241 void specify_texture(Texture *tex);
00242 bool apply_texture_immediate(Texture *tex);
00243
00244 GLenum get_texture_wrap_mode(Texture::WrapMode wm);
00245 GLenum get_texture_filter_type(Texture::FilterType ft);
00246 GLenum get_image_type(PixelBuffer::Type type);
00247 GLenum get_external_image_format(PixelBuffer::Format format);
00248 GLenum get_internal_image_format(PixelBuffer::Format format);
00249 GLint get_texture_apply_mode_type( TextureApplyProperty::Mode am ) const;
00250 GLint get_texture_apply_mode_type(TextureApplyAttrib::Mode am) const;
00251 GLenum get_depth_func_type(DepthTestProperty::Mode m) const;
00252 GLenum get_depth_func_type(DepthTestAttrib::Mode m) const;
00253 GLenum get_stencil_func_type(StencilProperty::Mode m) const;
00254 GLenum get_stencil_action_type(StencilProperty::Action a) const;
00255 GLenum get_fog_mode_type(Fog::Mode m) const;
00256
00257 #ifndef NDEBUG
00258 void build_phony_mipmaps(Texture *tex);
00259 void build_phony_mipmap_level(int level, int xsize, int ysize);
00260 void save_mipmap_images(Texture *tex);
00261 #endif
00262
00263 GLclampf _clear_color_red, _clear_color_green, _clear_color_blue,
00264 _clear_color_alpha;
00265 GLclampd _clear_depth;
00266 GLint _clear_stencil;
00267 GLclampf _clear_accum_red, _clear_accum_green, _clear_accum_blue,
00268 _clear_accum_alpha;
00269 GLenum _draw_buffer_mode;
00270 GLenum _read_buffer_mode;
00271 GLenum _shade_model_mode;
00272 GLint _scissor_x;
00273 GLint _scissor_y;
00274 GLsizei _scissor_width;
00275 GLsizei _scissor_height;
00276 GLint _viewport_x;
00277 GLint _viewport_y;
00278 GLsizei _viewport_width;
00279 GLsizei _viewport_height;
00280 GLboolean _lmodel_local;
00281 GLboolean _lmodel_twoside;
00282 Colorf _material_ambient;
00283 Colorf _material_diffuse;
00284 Colorf _material_specular;
00285 float _material_shininess;
00286 Colorf _material_emission;
00287 GLenum _stencil_func;
00288 GLenum _stencil_op;
00289 GLfloat _line_width;
00290 GLfloat _point_size;
00291 GLenum _blend_source_func;
00292 GLenum _blend_dest_func;
00293 GLboolean _depth_mask;
00294 GLint _fog_mode;
00295 GLfloat _fog_start;
00296 GLfloat _fog_end;
00297 GLfloat _fog_density;
00298 Colorf _fog_color;
00299 GLenum _alpha_func;
00300 GLclampf _alpha_func_ref;
00301 GLenum _polygon_mode;
00302
00303 GLint _pack_alignment;
00304 GLint _unpack_alignment;
00305
00306 bool _multisample_enabled;
00307 bool _line_smooth_enabled;
00308 bool _point_smooth_enabled;
00309 bool _scissor_enabled;
00310 bool _texturing_enabled;
00311 bool _dither_enabled;
00312 bool _stencil_test_enabled;
00313 bool _multisample_alpha_one_enabled;
00314 bool _multisample_alpha_mask_enabled;
00315 bool _blend_enabled;
00316 bool _depth_test_enabled;
00317 bool _fog_enabled;
00318 bool _dithering_enabled;
00319 bool _alpha_test_enabled;
00320 bool _polygon_offset_enabled;
00321 int _decal_level;
00322
00323 int _cur_light_id;
00324
00325 LMatrix4f _current_projection_mat;
00326 int _projection_mat_stack_count;
00327
00328 int _max_clip_planes;
00329 PTA(PlaneNode *)_available_clip_plane_ids;
00330 bool *_clip_plane_enabled;
00331 bool *_cur_clip_plane_enabled;
00332 int _cur_clip_plane_id;
00333
00334 CPT(DisplayRegion) _actual_display_region;
00335
00336 int _pass_number;
00337
00338 public:
00339 static GraphicsStateGuardian *
00340 make_GlGraphicsStateGuardian(const FactoryParams ¶ms);
00341
00342 static TypeHandle get_class_type(void);
00343 static void init_type(void);
00344 virtual TypeHandle get_type(void) const;
00345 virtual TypeHandle force_init_type() {init_type(); return get_class_type();}
00346
00347 static PStatCollector _vertices_display_list_pcollector;
00348
00349 private:
00350 static TypeHandle _type_handle;
00351 };
00352
00353 #ifdef DO_PSTATS
00354 #define DO_PSTATS_STUFF(XX) XX;
00355 #else
00356 #define DO_PSTATS_STUFF(XX)
00357 #endif
00358
00359 #include "crGraphicsStateGuardian.I"
00360
00361 #endif
00362