00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019 #ifndef DXGRAPHICSSTATEGUARDIAN7_H
00020 #define DXGRAPHICSSTATEGUARDIAN7_H
00021
00022
00023
00024 #include "dxgsg7base.h"
00025 #include "dxGeomNodeContext7.h"
00026 #include "dxTextureContext7.h"
00027
00028 #include "graphicsStateGuardian.h"
00029 #include "geomprimitives.h"
00030 #include "texture.h"
00031 #include "pixelBuffer.h"
00032 #include "displayRegion.h"
00033 #include "material.h"
00034 #include "depthTestAttrib.h"
00035 #include "renderModeAttrib.h"
00036 #include "textureApplyAttrib.h"
00037 #include "fog.h"
00038 #include "pointerToArray.h"
00039
00040 class Light;
00041
00042
00043 #ifdef _DEBUG
00044
00045
00046
00047
00048
00049 extern void dbgPrintVidMem(LPDIRECTDRAW7 pDD, LPDDSCAPS2 lpddsCaps,const char *pMsg);
00050 #define PRINTVIDMEM(pDD,pCaps,pMsg) dbgPrintVidMem(pDD,pCaps,pMsg)
00051 #else
00052 #define PRINTVIDMEM(pDD,pCaps,pMsg)
00053 #endif
00054
00055
00056
00057
00058
00059
00060
00061 class EXPCL_PANDADX DXGraphicsStateGuardian7 : public GraphicsStateGuardian {
00062 friend class wdxGraphicsWindow7;
00063 friend class wdxGraphicsPipe7;
00064 friend class wdxGraphicsWindow7Group;
00065 friend class DXTextureContext7;
00066
00067 public:
00068 DXGraphicsStateGuardian7(const FrameBufferProperties &properties);
00069 ~DXGraphicsStateGuardian7();
00070
00071 virtual void reset();
00072
00073 virtual void do_clear(const RenderBuffer &buffer);
00074
00075 virtual void prepare_display_region();
00076 virtual bool prepare_lens();
00077
00078 virtual void draw_point(GeomPoint *geom, GeomContext *gc);
00079 virtual void draw_line(GeomLine *geom, GeomContext *gc);
00080 virtual void draw_linestrip(GeomLinestrip *geom, GeomContext *gc);
00081 void draw_linestrip_base(Geom *geom, GeomContext *gc, bool bConnectEnds);
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 GeomNodeContext *prepare_geom_node(GeomNode *node);
00091 virtual void draw_geom_node(GeomNode *node, const RenderState *state,
00092 GeomNodeContext *gnc);
00093 virtual void release_geom_node(GeomNodeContext *gnc);
00094
00095 virtual TextureContext *prepare_texture(Texture *tex);
00096 virtual void apply_texture(TextureContext *tc);
00097 virtual void release_texture(TextureContext *tc);
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 virtual 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_alpha_test(const AlphaTestAttrib *attrib);
00121 virtual void issue_depth_test(const DepthTestAttrib *attrib);
00122 virtual void issue_depth_write(const DepthWriteAttrib *attrib);
00123 virtual void issue_cull_face(const CullFaceAttrib *attrib);
00124 virtual void issue_fog(const FogAttrib *attrib);
00125 virtual void issue_depth_offset(const DepthOffsetAttrib *attrib);
00126
00127 virtual void bind_light(PointLight *light, int light_id);
00128 virtual void bind_light(DirectionalLight *light, int light_id);
00129 virtual void bind_light(Spotlight *light, int light_id);
00130
00131
00132 virtual bool begin_scene();
00133 virtual void end_scene();
00134 virtual void end_frame();
00135
00136 virtual bool wants_normals(void) const;
00137 virtual bool wants_texcoords(void) const;
00138
00139 virtual bool depth_offset_decals();
00140
00141 virtual CoordinateSystem get_internal_coordinate_system() const;
00142 INLINE float compute_distance_to(const LPoint3f &point) const;
00143 virtual void set_color_clear_value(const Colorf& value);
00144
00145 public:
00146
00147 DXScreenData *_pScrn;
00148 LPDIRECT3DDEVICE7 _pD3DDevice;
00149
00150 #ifndef USE_TEXFMTVEC
00151 LPDDPIXELFORMAT _pTexPixFmts;
00152 int _cNumTexPixFmts;
00153 #endif
00154
00155 protected:
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
00160 virtual bool slot_new_clip_plane(int plane_id);
00161 virtual void enable_clip_plane(int plane_id, bool enable);
00162 virtual void bind_clip_plane(PlaneNode *plane, int plane_id);
00163
00164 virtual void set_blend_mode(ColorWriteAttrib::Mode color_write_mode,
00165 ColorBlendAttrib::Mode color_blend_mode,
00166 TransparencyAttrib::Mode transparency_mode);
00167
00168 void free_pointers();
00169 void free_dxgsg_objects(void);
00170 virtual PT(SavedFrameBuffer) save_frame_buffer(const RenderBuffer &buffer,
00171 CPT(DisplayRegion) dr);
00172 virtual void restore_frame_buffer(SavedFrameBuffer *frame_buffer);
00173
00174 void set_draw_buffer(const RenderBuffer &rb);
00175 void set_read_buffer(const RenderBuffer &rb);
00176
00177
00178 BYTE *_pCurFvfBufPtr,*_pFvfBufBasePtr;
00179 INLINE void add_to_FVFBuf(void *data, size_t bytes) ;
00180 WORD *_index_buf;
00181
00182 bool _dx_ready;
00183 HRESULT _last_testcooplevel_result;
00184
00185 HDC _front_hdc;
00186 DXTextureContext7 *_pCurTexContext;
00187
00188 bool _bTransformIssued;
00189 D3DMATRIX _SavedTransform;
00190
00191 RenderBuffer::Type _cur_read_pixel_buffer;
00192
00193 void GenerateSphere(void *pVertexSpace,DWORD dwVertSpaceByteSize,
00194 void *pIndexSpace,DWORD dwIndexSpaceByteSize,
00195 D3DVECTOR *pCenter, float fRadius,
00196 DWORD wNumRings, DWORD wNumSections, float sx, float sy, float sz,
00197 DWORD *pNumVertices,DWORD *pNumIndices,DWORD fvfFlags,DWORD dwVertSize);
00198 HRESULT RestoreAllVideoSurfaces(void);
00199 HRESULT RecreateAllVideoSurfaces(void);
00200 HRESULT DeleteAllVideoSurfaces(void);
00201
00202
00203
00204
00205
00206
00207
00208 INLINE void enable_color_material(bool val);
00209 INLINE void enable_fog(bool val);
00210 INLINE void enable_zwritemask(bool val);
00211 INLINE void set_shademode(D3DSHADEMODE val);
00212
00213 INLINE D3DTEXTUREADDRESS get_texture_wrap_mode(Texture::WrapMode wm) const;
00214 INLINE D3DFOGMODE get_fog_mode_type(Fog::Mode m) const;
00215
00216 INLINE void enable_primitive_clipping(bool val);
00217 INLINE void enable_alpha_test(bool val);
00218 INLINE void enable_line_smooth(bool val);
00219 INLINE void enable_blend(bool val);
00220 INLINE void enable_point_smooth(bool val);
00221 INLINE void enable_texturing(bool val);
00222 INLINE void call_dxLightModelAmbient(const Colorf& color);
00223 INLINE void call_dxAlphaFunc(D3DCMPFUNC func, float ref);
00224 INLINE void call_dxBlendFunc(D3DBLEND sfunc, D3DBLEND dfunc);
00225 INLINE void enable_dither(bool val);
00226 INLINE void enable_stencil_test(bool val);
00227 void report_texmgr_stats();
00228 void draw_multitri(Geom *geom, D3DPRIMITIVETYPE tri_id);
00229 void set_context(DXScreenData *pNewContextData);
00230
00231 void draw_prim_inner_loop(int nVerts, const Geom *geom, ushort perFlags);
00232 void draw_prim_inner_loop_coordtexonly(int nVerts, const Geom *geom);
00233 size_t draw_prim_setup(const Geom *geom) ;
00234
00235
00236 Normalf p_normal;
00237 TexCoordf p_texcoord;
00238 D3DCOLOR _curD3Dcolor;
00239 DWORD _curFVFflags;
00240 DWORD _perPrim,_perVertex,_perComp;
00241
00242 D3DCOLOR _scene_graph_color_D3DCOLOR;
00243 D3DCOLOR _d3dcolor_clear_value;
00244 D3DSHADEMODE _CurShadeMode;
00245
00246 bool _bDrawPrimDoSetupVertexBuffer;
00247 DXGeomNodeContext7 *_pCurrentGeomContext;
00248
00249
00250 Geom::VertexIterator vi;
00251 Geom::NormalIterator ni;
00252 Geom::TexCoordIterator ti;
00253 Geom::ColorIterator ci;
00254
00255
00256
00257 Vertexf *_coord_array,*_pCurCoord;
00258 ushort *_coordindex_array,*_pCurCoordIndex;
00259
00260 TexCoordf *_texcoord_array,*_pCurTexCoord;
00261 ushort *_texcoordindex_array,*_pCurTexCoordIndex;
00262
00263
00264
00265
00266
00267
00268
00269 Colorf _lmodel_ambient;
00270 float _material_ambient;
00271 float _material_diffuse;
00272 float _material_specular;
00273 float _material_shininess;
00274 float _material_emission;
00275
00276 typedef enum {None,
00277 PerVertexFog=D3DRENDERSTATE_FOGVERTEXMODE,
00278 PerPixelFog=D3DRENDERSTATE_FOGTABLEMODE
00279 } DxgsgFogType;
00280 DxgsgFogType _doFogType;
00281 bool _fog_enabled;
00282
00283
00284
00285
00286 float _alpha_func_refval;
00287 D3DCMPFUNC _alpha_func;
00288
00289 D3DBLEND _blend_source_func;
00290 D3DBLEND _blend_dest_func;
00291
00292 bool _line_smooth_enabled;
00293 bool* _light_enabled;
00294 bool _color_material_enabled;
00295 bool _texturing_enabled;
00296 bool _clipping_enabled;
00297 bool _dither_enabled;
00298 bool _stencil_test_enabled;
00299 bool _blend_enabled;
00300 bool _depth_test_enabled;
00301 bool _depth_write_enabled;
00302 bool _alpha_test_enabled;
00303 DWORD _clip_plane_bits;
00304
00305 RenderModeAttrib::Mode _current_fill_mode;
00306
00307
00308
00309 TextureApplyAttrib::Mode _CurTexBlendMode;
00310 D3DTEXTUREMAGFILTER _CurTexMagFilter;
00311 D3DTEXTUREMINFILTER _CurTexMinFilter;
00312 D3DTEXTUREMIPFILTER _CurTexMipFilter;
00313 DWORD _CurTexAnisoDegree;
00314 Texture::WrapMode _CurTexWrapModeU,_CurTexWrapModeV;
00315
00316 LMatrix4f _current_projection_mat;
00317 int _projection_mat_stack_count;
00318
00319 CPT(DisplayRegion) _actual_display_region;
00320
00321
00322 INLINE void transform_color(Colorf &InColor,D3DCOLOR &OutColor);
00323
00324 bool _overlay_windows_supported;
00325
00326 #if 0
00327
00328
00329
00330
00331
00332
00333 GraphicsWindow *_win;
00334 #endif
00335
00336 public:
00337 static GraphicsStateGuardian*
00338 make_DXGraphicsStateGuardian(const FactoryParams ¶ms);
00339
00340 static TypeHandle get_class_type(void);
00341 static void init_type(void);
00342 virtual TypeHandle get_type(void) const;
00343 virtual TypeHandle force_init_type() {init_type(); return get_class_type();}
00344
00345 void adjust_view_rect(int x, int y);
00346 INLINE void SetDXReady(bool stat) { _dx_ready = stat; }
00347 INLINE bool GetDXReady(void) { return _dx_ready;}
00348 void DXGraphicsStateGuardian7::SetTextureBlendMode(TextureApplyAttrib::Mode TexBlendMode,bool bJustEnable);
00349
00350 void dx_cleanup(bool bRestoreDisplayMode,bool bAtExitFnCalled);
00351
00352 #define DO_REACTIVATE_WINDOW true
00353 bool CheckCooperativeLevel(bool bDoReactivateWindow = false);
00354
00355 void dx_setup_after_resize(RECT *pViewRect);
00356 void show_frame();
00357 void show_full_screen_frame();
00358 void show_windowed_frame();
00359
00360 void support_overlay_window(bool flag);
00361
00362
00363
00364
00365
00366
00367
00368
00369
00370 void dx_init(void);
00371
00372 friend HRESULT CALLBACK EnumTexFmtsCallback( LPDDPIXELFORMAT pddpf, VOID* param );
00373
00374 private:
00375 static TypeHandle _type_handle;
00376 };
00377
00378 #include "dxGraphicsStateGuardian7.I"
00379
00380 #endif
00381