Main Page   Class Hierarchy   Compound List   File List   Compound Members  

graph3d.h

00001 /*
00002     Copyright (C) 1998-2001 by Jorrit Tyberghein
00003     Written by Jorrit Tyberghein, Dan Ogles, and Gary Clark.
00004 
00005     This library is free software; you can redistribute it and/or
00006     modify it under the terms of the GNU Library General Public
00007     License as published by the Free Software Foundation; either
00008     version 2 of the License, or (at your option) any later version.
00009 
00010     This library is distributed in the hope that it will be useful,
00011     but WITHOUT ANY WARRANTY; without even the implied warranty of
00012     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013     Library General Public License for more details.
00014 
00015     You should have received a copy of the GNU Library General Public
00016     License along with this library; if not, write to the Free
00017     Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
00018 */
00019 
00020 #ifndef __IVIDEO_GRAPH3D_H__
00021 #define __IVIDEO_GRAPH3D_H__
00022 
00023 #include "csutil/scf.h"
00024 #include "csgeom/plane3.h"
00025 #include "csgeom/vector2.h"
00026 
00027 class csMatrix3;
00028 class csVector3;
00029 class csVector2;
00030 class csPlane3;
00031 class csRect;
00032 class csReversibleTransform;
00033 class csColor;
00034 
00035 struct iGraphics2D;
00036 struct iPolygonTexture;
00037 struct iPolygonBuffer;
00038 struct iVertexBuffer;
00039 struct iVertexBufferManager;
00040 struct iTextureManager;
00041 struct iTextureHandle;
00042 struct iMaterialHandle;
00043 struct iClipper2D;
00044 struct iHalo;
00045 struct csRGBpixel;
00046 struct csPixelFormat;
00047 
00048 #define CS_FOG_FRONT            0
00049 #define CS_FOG_BACK             1
00050 #define CS_FOG_VIEW             2
00051 
00052 //======================================================================
00053 // For vertex based fog the following defines are used:
00054 #define CS_FOGTABLE_SIZE 64
00055 
00056 // Each texel in the fog table holds the fog alpha value at a certain
00057 // (distance*density).  The median distance parameter determines the
00058 // (distance*density) value represented by the texel at the center of
00059 // the fog table.  The fog calculation is:
00060 // alpha = 1.0 - exp( -(density*distance) / CS_FOGTABLE_MEDIANDISTANCE)
00061 #define CS_FOGTABLE_MEDIANDISTANCE 10.0
00062 #define CS_FOGTABLE_MAXDISTANCE (CS_FOGTABLE_MEDIANDISTANCE * 2.0)
00063 #define CS_FOGTABLE_DISTANCESCALE (1.0 / CS_FOGTABLE_MAXDISTANCE)
00064 
00065 // Fog (distance*density) is mapped to a texture coordinate and then
00066 // clamped.  This determines the clamp value.  Some drivers don't
00067 // like clamping textures so we must do it ourself
00068 #define CS_FOGTABLE_CLAMPVALUE 0.85
00069 #define CS_FOG_MAXVALUE (CS_FOGTABLE_MAXDISTANCE * CS_FOGTABLE_CLAMPVALUE)
00070 //======================================================================
00071 
00076 #define CS_FX_MASK_MIXMODE 0xF0000000 // SRC/DST mixing mode mask
00077 #define CS_FX_COPY         0x00000000 // =SRC
00078 #define CS_FX_MULTIPLY     0x10000000 // =SRC*DST
00079 #define CS_FX_MULTIPLY2    0x20000000 // =2*SRC*DST
00080 #define CS_FX_ADD          0x30000000 // =SRC+DST
00081 #define CS_FX_ALPHA        0x40000000 // =(1-alpha)*SRC + alpha*DST
00082 #define CS_FX_TRANSPARENT  0x50000000 // =DST
00083 #define CS_FX_KEYCOLOR     0x08000000 // color 0 is transparent
00084 #define CS_FX_GOURAUD      0x04000000 // Gouraud shading
00085 #define CS_FX_TILING       0x02000000 // Tiling
00086 #define CS_FX_MASK_ALPHA   0x000000FF // alpha = 0..FF (opaque..transparent)
00087 
00089 #define CS_FX_SETALPHA(alpha) \
00090   (CS_FX_ALPHA | uint (alpha * CS_FX_MASK_ALPHA))
00091 
00092 #define CS_FX_SETALPHA_INT(alpha) \
00093   (CS_FX_ALPHA | uint (alpha & CS_FX_MASK_ALPHA))
00094 
00096 class G3DTexturedVertex : public csVector2
00097 {
00098 public:
00100   float z;
00101   
00102   // Texture coordinates
00103   float u, v; 
00104 
00105   // Lighting info (Used only with Gouraud shading (between 0 and 1))
00106   float r, g, b;
00107 };
00108 
00110 class G3DFogInfo
00111 {
00112 public:
00114   float r, g, b;
00121   float intensity;
00122   float intensity2;
00123 };
00124 
00126 class G3DTexturePlane
00127 {
00128 public:
00130   csMatrix3* m_cam2tex;
00132   csVector3* v_cam2tex;
00133 };
00134 
00136 struct G3DPolygonDPFX
00137 {
00139   int num;
00141   G3DTexturedVertex vertices[100];
00143   G3DFogInfo fog_info[100];
00145   bool use_fog;
00146 
00148   iMaterialHandle *mat_handle;
00150   UInt mixmode;
00151 
00153   UByte flat_color_r;
00154   UByte flat_color_g;
00155   UByte flat_color_b;
00156 
00157   // A dummy constructor to appease NextStep compiler which otherwise
00158   // complains that it is unable to create this object.  This happens when
00159   // a subcomponent such as G3DTexturedVertex has a constructor.
00160   G3DPolygonDPFX() {}
00161 };
00162 
00164 struct G3DPolygonDFP
00165 {
00167   int num;
00169   csVector2 vertices[100];
00170 
00172   csPlane3 normal;
00173 };
00174 
00176 struct G3DPolygonDP : public G3DPolygonDFP
00177 {
00179   G3DFogInfo fog_info[100];
00181   bool use_fog;
00182 
00184   iMaterialHandle* mat_handle;
00185 
00187   G3DTexturePlane plane;
00188 
00190   iPolygonTexture* poly_texture;
00191 
00193   UInt mixmode;
00194 
00196   float z_value;
00197 
00198 #ifdef DO_HW_UVZ
00199   csVector3* uvz;
00200   bool mirror;
00201 #endif
00202 };
00203 
00205 typedef G3DPolygonDP G3DPolygonDPF;
00206 
00212 enum csZBufMode
00213 {
00214   CS_ZBUF_NONE     = 0x00000000,
00215   CS_ZBUF_FILL     = 0x00000001,
00216   CS_ZBUF_TEST     = 0x00000002,
00217   CS_ZBUF_USE      = 0x00000003,
00218   CS_ZBUF_FILLONLY = 0x00000004,
00219   CS_ZBUF_EQUAL    = 0x00000005
00220 };
00221 
00223 enum G3D_RENDERSTATEOPTION
00224 {
00226   G3DRENDERSTATE_ZBUFFERMODE,
00228   G3DRENDERSTATE_DITHERENABLE,
00230   G3DRENDERSTATE_BILINEARMAPPINGENABLE,
00232   G3DRENDERSTATE_TRILINEARMAPPINGENABLE,
00234   G3DRENDERSTATE_TRANSPARENCYENABLE,
00236   G3DRENDERSTATE_MIPMAPENABLE,
00238   G3DRENDERSTATE_TEXTUREMAPPINGENABLE,
00240   G3DRENDERSTATE_LIGHTINGENABLE,
00242   G3DRENDERSTATE_INTERLACINGENABLE,
00244   G3DRENDERSTATE_MMXENABLE,
00246   G3DRENDERSTATE_INTERPOLATIONSTEP,
00248   G3DRENDERSTATE_MAXPOLYGONSTODRAW,
00250   G3DRENDERSTATE_GOURAUDENABLE,
00252   G3DRENDERSTATE_GAMMACORRECTION,
00254   G3DRENDERSTATE_EDGES
00255 };
00256 
00257 // Bit flags for iGraphics3D::BeginDraw ()
00259 #define CSDRAW_2DGRAPHICS   0x00000001
00261 #define CSDRAW_3DGRAPHICS   0x00000002
00263 #define CSDRAW_CLEARZBUFFER 0x00000010
00265 #define CSDRAW_CLEARSCREEN  0x00000020
00266 
00268 enum G3D_FOGMETHOD
00269 {
00270   G3DFOGMETHOD_NONE = 0x00,
00271   G3DFOGMETHOD_ZBUFFER = 0x01,
00272   G3DFOGMETHOD_VERTEX = 0x02
00273 };
00274 
00276 struct csGraphics3DCaps
00277 {
00278   bool CanClip;
00279   int minTexHeight, minTexWidth;
00280   int maxTexHeight, maxTexWidth;
00281   G3D_FOGMETHOD fog;
00282   bool NeedsPO2Maps;
00283   int MaxAspectRatio;
00284 };
00285 
00291 struct csTriangle
00292 {
00293   int a, b, c;
00294 };
00295 
00300 #define CS_CLIPPER_NONE -1
00301 
00306 #define CS_CLIPPER_OPTIONAL 0
00307 
00312 #define CS_CLIPPER_TOPLEVEL 1
00313 
00318 #define CS_CLIPPER_REQUIRED 2
00319 
00320 
00326 #define CS_CLIP_NOT 0
00327 
00335 #define CS_CLIP_NEEDED 1
00336 
00343 #define CS_CLIP_TOPLEVEL 2
00344 
00356 struct G3DTriangleMesh
00357 {
00358   enum
00359   {
00361     MAX_VERTEXPOOL = 2
00362   };
00363 
00365   int num_vertices_pool;
00366 
00368   int num_triangles;
00370   csTriangle* triangles;
00371 
00373   int clip_portal;
00375   int clip_plane;
00377   int clip_z_plane;
00378 
00380   bool use_vertex_color;
00381   
00383   bool do_fog;
00385   bool do_mirror;
00387   bool do_morph_texels;
00389   bool do_morph_colors;
00390 
00392   enum
00393   {
00395     VM_WORLDSPACE,
00397     VM_VIEWSPACE
00398   } vertex_mode;
00399 
00401   UInt mixmode;
00402   float morph_factor;
00407   iVertexBuffer* buffers[MAX_VERTEXPOOL];
00408   iMaterialHandle* mat_handle;
00410   G3DFogInfo* vertex_fog;
00411 
00412   // TODO : store information required for lighting calculation
00413 };
00414 
00425 struct G3DPolygonMesh
00426 {
00428   iPolygonBuffer* polybuf;
00429 
00430   // Apply fogging?
00431   bool do_fog;
00432 
00434   UInt mixmode;
00435 
00437   int clip_portal;
00439   int clip_plane;
00441   int clip_z_plane;
00442 
00444   bool do_mirror;
00445 
00447   enum
00448   {
00450     VM_WORLDSPACE,
00452     VM_VIEWSPACE
00453   } vertex_mode;
00454 
00456   G3DFogInfo* vertex_fog;
00457 };
00458 
00462 struct csFog
00463 {
00465   bool enabled;
00467   float density;
00469   float red;
00471   float green;
00473   float blue;
00474 };
00475 
00476 SCF_VERSION (iGraphics3D, 5, 0, 2);
00477 
00484 struct iGraphics3D : public iBase
00485 {
00487   virtual bool Open () = 0;
00489   virtual void Close () = 0;
00490 
00492   virtual iGraphics2D *GetDriver2D () = 0;
00493 
00495   virtual void SetDimensions (int width, int height) = 0;
00496 
00498   virtual int GetWidth () = 0;
00500   virtual int GetHeight () = 0;
00501 
00506   virtual csGraphics3DCaps *GetCaps () = 0;
00507 
00512   virtual void SetPerspectiveCenter (int x, int y) = 0;
00513 
00515   virtual void GetPerspectiveCenter (int& x, int& y) = 0;
00516 
00520   virtual void SetPerspectiveAspect (float aspect) = 0;
00521 
00523   virtual float GetPerspectiveAspect () = 0;
00524 
00529   virtual void SetObjectToCamera (csReversibleTransform* o2c) = 0;
00530 
00534   virtual const csReversibleTransform& GetObjectToCamera () = 0;
00535 
00541   virtual void SetClipper (iClipper2D* clipper, int cliptype) = 0;
00542 
00546   virtual iClipper2D* GetClipper () = 0;
00547 
00551   virtual int GetClipType () = 0;
00552 
00557   virtual void SetNearPlane (const csPlane3& pl) = 0;
00558 
00562   virtual void ResetNearPlane () = 0;
00563 
00567   virtual const csPlane3& GetNearPlane () = 0;
00568 
00572   virtual bool HasNearPlane () = 0;
00573 
00575   virtual uint32 *GetZBuffAt (int x, int y) = 0;
00576 
00578   virtual float GetZBuffValue (int x, int y) = 0;
00579 
00581   virtual bool BeginDraw (int DrawFlags) = 0;
00582 
00584   virtual void FinishDraw () = 0;
00585 
00591   virtual void Print (csRect *area) = 0;
00592 
00594   virtual bool SetRenderState (G3D_RENDERSTATEOPTION op, long val) = 0;
00595 
00597   virtual long GetRenderState (G3D_RENDERSTATEOPTION op) = 0;
00598 
00600   virtual void DrawPolygon (G3DPolygonDP& poly) = 0;
00601 
00608   virtual void DrawPolygonDebug (G3DPolygonDP& poly) = 0;
00609 
00628   virtual void DrawPolygonFX (G3DPolygonDPFX& poly) = 0;
00629 
00633   virtual void DrawTriangleMesh (G3DTriangleMesh& mesh) = 0;
00634 
00638   virtual void DrawPolygonMesh (G3DPolygonMesh& mesh) = 0;
00639 
00647   virtual void OpenFogObject (CS_ID id, csFog* fog) = 0;
00648 
00660   virtual void DrawFogPolygon (CS_ID id, G3DPolygonDFP& poly, int fogtype) = 0;
00661 
00667   virtual void CloseFogObject (CS_ID id) = 0;
00668 
00670   virtual void DrawLine (const csVector3& v1, const csVector3& v2,
00671         float fov, int color) = 0;
00672 
00674   virtual iHalo *CreateHalo (float iR, float iG, float iB,
00675     unsigned char *iAlpha, int iWidth, int iHeight) = 0;
00676 
00691   virtual void DrawPixmap (iTextureHandle *hTex, int sx, int sy,
00692     int sw, int sh, int tx, int ty, int tw, int th, uint8 Alpha = 0) = 0;
00693 
00695   virtual iTextureManager *GetTextureManager () = 0;
00696 
00698   virtual void DumpCache () = 0;
00699 
00701   virtual void ClearCache () = 0;
00702 
00708   virtual void RemoveFromCache (iPolygonTexture* poly_texture) = 0;
00709 
00714   virtual iVertexBufferManager* GetVertexBufferManager () = 0;
00715 };
00716 
00717 #endif // __IVIDEO_GRAPH3D_H__

Generated for Crystal Space by doxygen 1.2.5 written by Dimitri van Heesch, ©1997-2000