ivideo/graph3d.h
Go to the documentation of this file.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 __CS_IVIDEO_GRAPH3D_H__ 00021 #define __CS_IVIDEO_GRAPH3D_H__ 00022 00031 #include "csutil/refarr.h" 00032 #include "csutil/scf.h" 00033 #include "csgeom/plane3.h" 00034 #include "csgeom/vector2.h" 00035 #include "csgeom/transfrm.h" 00036 #include "csgeom/tri.h" 00037 #include "csutil/cscolor.h" 00038 #include "ivideo/rndbuf.h" 00039 #include "ivideo/polyrender.h" 00040 00041 struct csCoreRenderMesh; 00042 struct csRenderMeshModes; 00043 class csMatrix3; 00044 class csVector4; 00045 class csVector3; 00046 class csVector2; 00047 class csPlane3; 00048 class csRect; 00049 class csReversibleTransform; 00050 class csShaderVariable; 00051 00052 struct iGraphics2D; 00053 struct iPolygonBuffer; 00054 struct iPolygonRenderer; 00055 struct iVertexBuffer; 00056 struct iVertexBufferManager; 00057 struct iTextureManager; 00058 struct iTextureHandle; 00059 struct iMaterialHandle; 00060 struct iMaterial; 00061 struct iClipper2D; 00062 struct iHalo; 00063 struct iRendererLightmap; 00064 struct csRGBpixel; 00065 struct csPixelFormat; 00066 struct csPolyTextureMapping; 00067 struct iRenderBuffer; 00068 struct iRenderBufferManager; 00069 struct iLightingManager; 00070 struct iShader; 00071 struct iShaderVariableContext; 00072 00073 00076 00077 #define CSDRAW_2DGRAPHICS 0x00000001 00078 00079 #define CSDRAW_3DGRAPHICS 0x00000002 00080 00081 #define CSDRAW_CLEARZBUFFER 0x00000010 00082 00083 #define CSDRAW_CLEARSCREEN 0x00000020 00084 00091 #define CS_CLIPPER_NONE -1 00092 00095 #define CS_CLIPPER_OPTIONAL 0 00096 00099 #define CS_CLIPPER_TOPLEVEL 1 00100 00103 #define CS_CLIPPER_REQUIRED 2 00104 00112 #define CS_CLIP_NOT 0 00113 00118 #define CS_CLIP_NEEDED 1 00119 00124 struct csFog 00125 { 00127 bool enabled; 00129 float density; 00131 float red; 00133 float green; 00135 float blue; 00136 }; 00137 00139 enum csZBufMode 00140 { 00141 // values below are sometimes used as bit masks, so don't change them! 00143 CS_ZBUF_NONE = 0x00000000, 00145 CS_ZBUF_FILL = 0x00000001, 00147 CS_ZBUF_TEST = 0x00000002, 00149 CS_ZBUF_USE = 0x00000003, 00151 CS_ZBUF_FILLONLY = 0x00000004, 00153 CS_ZBUF_EQUAL = 0x00000005, 00155 CS_ZBUF_INVERT = 0x00000006, 00157 CS_ZBUF_MESH = 0x00000007, 00166 CS_ZBUF_MESH2 = 0x00000008 00167 }; 00168 00169 // @@@ Keep in sync with values below 00170 // @@@ Document me better! 00171 #define CS_VATTRIB_GENERIC_FIRST 0 00172 #define CS_VATTRIB_GENERIC_LAST 15 00173 #define CS_VATTRIB_SPECIFIC_FIRST 100 00174 #define CS_VATTRIB_SPECIFIC_LAST (CS_VATTRIB_SPECIFIC_FIRST + 15) 00175 00176 #define CS_VATTRIB_IS_GENERIC(va) \ 00177 ((va >= CS_VATTRIB_GENERIC_FIRST) && (va <=CS_VATTRIB_GENERIC_LAST)) 00178 #define CS_VATTRIB_IS_SPECIFIC(va) \ 00179 ((va >= CS_VATTRIB_SPECIFIC_FIRST) && (va <=CS_VATTRIB_SPECIFIC_LAST)) 00180 00185 enum csVertexAttrib 00186 { 00188 00191 CS_VATTRIB_0 = 0, 00192 CS_VATTRIB_1 = 1, 00193 CS_VATTRIB_2 = 2, 00194 CS_VATTRIB_3 = 3, 00195 CS_VATTRIB_4 = 4, 00196 CS_VATTRIB_5 = 5, 00197 CS_VATTRIB_6 = 6, 00198 CS_VATTRIB_7 = 7, 00199 CS_VATTRIB_8 = 8, 00200 CS_VATTRIB_9 = 9, 00201 CS_VATTRIB_10 = 10, 00202 CS_VATTRIB_11 = 11, 00203 CS_VATTRIB_12 = 12, 00204 CS_VATTRIB_13 = 13, 00205 CS_VATTRIB_14 = 14, 00206 CS_VATTRIB_15 = 15, 00208 00209 CS_VATTRIB_POSITION = CS_VATTRIB_SPECIFIC_FIRST + 0, 00211 CS_VATTRIB_WEIGHT = CS_VATTRIB_SPECIFIC_FIRST + 1, 00213 CS_VATTRIB_NORMAL = CS_VATTRIB_SPECIFIC_FIRST + 2, 00215 CS_VATTRIB_COLOR = CS_VATTRIB_SPECIFIC_FIRST + 3, 00217 CS_VATTRIB_PRIMARY_COLOR = CS_VATTRIB_SPECIFIC_FIRST + 3, 00219 CS_VATTRIB_SECONDARY_COLOR = CS_VATTRIB_SPECIFIC_FIRST + 4, 00221 CS_VATTRIB_FOGCOORD = CS_VATTRIB_SPECIFIC_FIRST + 5, 00223 CS_VATTRIB_TEXCOORD = CS_VATTRIB_SPECIFIC_FIRST + 8, 00225 CS_VATTRIB_TEXCOORD0 = CS_VATTRIB_SPECIFIC_FIRST + 8, 00227 CS_VATTRIB_TEXCOORD1 = CS_VATTRIB_SPECIFIC_FIRST + 9, 00229 CS_VATTRIB_TEXCOORD2 = CS_VATTRIB_SPECIFIC_FIRST + 10, 00231 CS_VATTRIB_TEXCOORD3 = CS_VATTRIB_SPECIFIC_FIRST + 11, 00233 CS_VATTRIB_TEXCOORD4 = CS_VATTRIB_SPECIFIC_FIRST + 12, 00235 CS_VATTRIB_TEXCOORD5 = CS_VATTRIB_SPECIFIC_FIRST + 13, 00237 CS_VATTRIB_TEXCOORD6 = CS_VATTRIB_SPECIFIC_FIRST + 14, 00239 CS_VATTRIB_TEXCOORD7 = CS_VATTRIB_SPECIFIC_FIRST + 15, 00240 }; 00241 00243 enum G3D_FOGMETHOD 00244 { 00245 G3DFOGMETHOD_NONE = 0x00, 00246 G3DFOGMETHOD_ZBUFFER = 0x01, 00247 G3DFOGMETHOD_VERTEX = 0x02 00248 }; 00249 00253 00254 #define CS_FX_MASK_MIXMODE 0xF0000000 00255 00256 #define CS_FX_COPY 0x00000000 00257 00258 #define CS_FX_MULTIPLY 0x10000000 00259 00260 #define CS_FX_MULTIPLY2 0x20000000 00261 00262 #define CS_FX_ADD 0x30000000 00263 00264 #define CS_FX_ALPHA 0x40000000 00265 00266 #define CS_FX_TRANSPARENT 0x50000000 00267 00268 #define CS_FX_DESTALPHAADD 0x60000000 00269 00270 #define CS_FX_SRCALPHAADD 0x70000000 00271 00275 #define CS_FX_MESH 0xf0000000 00276 00277 #define CS_FX_KEYCOLOR 0x08000000 00278 00279 #define CS_FX_FLAT 0x04000000 00280 00281 #define CS_FX_TILING 0x02000000 00282 00283 #define CS_FX_MASK_ALPHA 0x000000FF 00284 00286 #define CS_FX_SETALPHA(alpha) \ 00287 (CS_FX_ALPHA | uint (alpha * CS_FX_MASK_ALPHA)) 00288 00289 #define CS_FX_SETALPHA_INT(alpha) \ 00290 (CS_FX_ALPHA | uint (alpha & CS_FX_MASK_ALPHA)) 00291 00296 struct csAlphaMode 00297 { 00299 enum AlphaType 00300 { 00302 alphaNone = 1, 00304 alphaBinary, 00306 alphaSmooth 00307 }; 00309 bool autoAlphaMode; 00310 union 00311 { 00313 AlphaType alphaType; 00315 csStringID autoModeTexture; 00316 }; 00317 }; 00318 00321 00322 #define CS_LIGHTPARAM_POSITION 0 00323 00324 #define CS_LIGHTPARAM_DIFFUSE 1 00325 00326 #define CS_LIGHTPARAM_SPECULAR 2 00327 00328 #define CS_LIGHTPARAM_ATTENUATION 3 00329 00333 00334 #define CS_SHADOW_VOLUME_BEGIN 1 00335 00336 #define CS_SHADOW_VOLUME_PASS1 2 00337 00338 #define CS_SHADOW_VOLUME_PASS2 3 00339 00340 #define CS_SHADOW_VOLUME_FAIL1 4 00341 00342 #define CS_SHADOW_VOLUME_FAIL2 5 00343 00344 #define CS_SHADOW_VOLUME_USE 6 00345 00346 #define CS_SHADOW_VOLUME_FINISH 7 00347 00349 00350 enum G3D_RENDERSTATEOPTION 00351 { 00353 G3DRENDERSTATE_ZBUFFERMODE, 00355 G3DRENDERSTATE_DITHERENABLE, 00357 G3DRENDERSTATE_BILINEARMAPPINGENABLE, 00359 G3DRENDERSTATE_TRILINEARMAPPINGENABLE, 00361 G3DRENDERSTATE_TRANSPARENCYENABLE, 00363 G3DRENDERSTATE_MIPMAPENABLE, 00365 G3DRENDERSTATE_TEXTUREMAPPINGENABLE, 00367 G3DRENDERSTATE_LIGHTINGENABLE, 00369 G3DRENDERSTATE_INTERLACINGENABLE, 00371 G3DRENDERSTATE_MMXENABLE, 00373 G3DRENDERSTATE_INTERPOLATIONSTEP, 00375 G3DRENDERSTATE_MAXPOLYGONSTODRAW, 00377 G3DRENDERSTATE_GOURAUDENABLE, 00379 G3DRENDERSTATE_EDGES 00380 }; 00381 00383 struct csGraphics3DCaps 00384 { 00385 bool CanClip; 00386 int minTexHeight, minTexWidth; 00387 int maxTexHeight, maxTexWidth; 00388 G3D_FOGMETHOD fog; 00389 bool NeedsPO2Maps; 00390 int MaxAspectRatio; 00391 bool SupportsPointSprites; 00392 }; 00393 00394 //=========================================================================== 00395 // Not for new renderer! @@@NR@@@ 00396 // The stuff below is either to be ported to new renderer or else 00397 // it will be removed. 00398 //=========================================================================== 00399 00400 #define CS_FOG_FRONT 0 00401 #define CS_FOG_BACK 1 00402 #define CS_FOG_VIEW 2 00403 00404 //====================================================================== 00405 // For vertex based fog the following defines are used: 00406 #define CS_FOGTABLE_SIZE 64 00407 00408 // Each texel in the fog table holds the fog alpha value at a certain 00409 // (distance*density). The median distance parameter determines the 00410 // (distance*density) value represented by the texel at the center of 00411 // the fog table. The fog calculation is: 00412 // alpha = 1.0 - exp( -(density*distance) / CS_FOGTABLE_MEDIANDISTANCE) 00413 #define CS_FOGTABLE_MEDIANDISTANCE 10.0f 00414 #define CS_FOGTABLE_MAXDISTANCE (CS_FOGTABLE_MEDIANDISTANCE * 2.0f) 00415 #define CS_FOGTABLE_DISTANCESCALE (1.0f / CS_FOGTABLE_MAXDISTANCE) 00416 00417 // Fog (distance*density) is mapped to a texture coordinate and then 00418 // clamped. This determines the clamp value. Some drivers don't 00419 // like clamping textures so we must do it ourself 00420 #define CS_FOGTABLE_CLAMPVALUE 0.85f 00421 #define CS_FOG_MAXVALUE (CS_FOGTABLE_MAXDISTANCE * CS_FOGTABLE_CLAMPVALUE) 00422 //====================================================================== 00423 00425 class G3DFogInfo 00426 { 00427 public: 00429 float r, g, b; 00436 float intensity; 00437 float intensity2; 00438 }; 00439 00441 class G3DCam2TextureTransform 00442 { 00443 public: 00445 csMatrix3* m_cam2tex; 00447 csVector3* v_cam2tex; 00448 }; 00449 00451 struct G3DPolygonDPFX 00452 { 00454 int num; 00456 csVector2 vertices[100]; 00458 float z[100]; 00460 csVector2 texels[100]; 00462 csColor colors[100]; 00463 00465 G3DFogInfo fog_info[100]; 00467 bool use_fog; 00468 00470 iMaterialHandle *mat_handle; 00472 uint mixmode; 00473 00475 00476 uint8 flat_color_r; 00477 uint8 flat_color_g; 00478 uint8 flat_color_b; 00480 00481 // A dummy constructor to appease NextStep compiler which otherwise 00482 // complains that it is unable to create this object. This happens when 00483 // a subcomponent such as csVector2 has a constructor. 00484 G3DPolygonDPFX() {} 00485 }; 00486 00488 struct G3DPolygonDFP 00489 { 00491 int num; 00493 csVector2 vertices[100]; 00494 00496 csPlane3 normal; 00497 }; 00498 00500 struct G3DPolygonDP : public G3DPolygonDFP 00501 { 00503 G3DFogInfo fog_info[100]; 00505 bool use_fog; 00506 00508 iMaterialHandle* mat_handle; 00509 00511 G3DCam2TextureTransform cam2tex; 00512 00514 csPolyTextureMapping* texmap; 00515 iRendererLightmap* rlm; 00516 00518 bool do_fullbright; 00519 00521 uint mixmode; 00522 00524 float z_value; 00525 00526 iTextureHandle* txt_handle; 00527 }; 00528 00530 typedef G3DPolygonDP G3DPolygonDPF; 00531 00543 struct G3DTriangleMesh 00544 { 00545 enum 00546 { 00548 MAX_VERTEXPOOL = 2 00549 }; 00550 00552 int num_vertices_pool; 00553 00555 int num_triangles; 00557 csTriangle* triangles; 00558 00560 int clip_portal; 00562 int clip_plane; 00564 int clip_z_plane; 00565 00567 bool use_vertex_color; 00568 00570 bool do_fog; 00572 bool do_mirror; 00574 bool do_morph_texels; 00576 bool do_morph_colors; 00577 00579 enum VertexMode 00580 { 00582 VM_WORLDSPACE, 00584 VM_VIEWSPACE 00585 }; 00586 00588 VertexMode vertex_mode; 00589 00591 uint mixmode; 00592 float morph_factor; 00597 iVertexBuffer* buffers[MAX_VERTEXPOOL]; 00598 iMaterialHandle* mat_handle; 00600 G3DFogInfo* vertex_fog; 00601 00602 // TODO : store information required for lighting calculation 00603 }; 00604 00615 struct G3DPolygonMesh 00616 { 00618 iPolygonBuffer* polybuf; 00619 00620 // Apply fogging? 00621 bool do_fog; 00622 00624 uint mixmode; 00625 00627 int clip_portal; 00629 int clip_plane; 00631 int clip_z_plane; 00632 00634 bool do_mirror; 00635 00637 enum VertexMode 00638 { 00640 VM_WORLDSPACE, 00642 VM_VIEWSPACE 00643 }; 00644 00646 VertexMode vertex_mode; 00647 00649 G3DFogInfo* vertex_fog; 00650 }; 00651 00653 enum csRenderMeshType 00654 { 00656 CS_MESHTYPE_TRIANGLES, 00658 CS_MESHTYPE_QUADS, 00668 CS_MESHTYPE_TRIANGLESTRIP, 00674 CS_MESHTYPE_TRIANGLEFAN, 00678 CS_MESHTYPE_POINTS, 00684 CS_MESHTYPE_POINT_SPRITES, 00688 CS_MESHTYPE_LINES, 00693 CS_MESHTYPE_LINESTRIP, 00700 CS_MESHTYPE_POLYGON, 00701 }; 00702 00706 enum csSimpleMeshFlags 00707 { 00713 csSimpleMeshScreenspace = 0x01, 00714 }; 00715 00719 struct csSimpleRenderMesh 00720 { 00722 csRenderMeshType meshtype; 00723 00725 uint indexCount; 00727 const uint* indices; 00728 00730 uint vertexCount; 00736 const csVector3* vertices; 00738 const csVector2* texcoords; 00744 const csVector4* colors; 00749 iTextureHandle* texture; 00750 00752 iShader* shader; 00754 iShaderVariableContext* dynDomain; 00756 csAlphaMode alphaType; 00758 csZBufMode z_buf_mode; 00760 uint mixmode; 00769 csReversibleTransform object2camera; 00770 00771 csSimpleRenderMesh () : colors(0), texture (0), shader (0), dynDomain (0), 00772 z_buf_mode (CS_ZBUF_NONE), mixmode (CS_FX_COPY) 00773 { 00774 alphaType.autoAlphaMode = true; 00775 alphaType.autoModeTexture = csInvalidStringID; 00776 }; 00777 }; 00778 00779 SCF_VERSION (iGraphics3D, 5, 4, 1); 00780 00798 struct iGraphics3D : public iBase 00799 { 00801 virtual bool Open () = 0; 00803 virtual void Close () = 0; 00804 00812 virtual iGraphics2D *GetDriver2D () = 0; 00813 00820 virtual iTextureManager *GetTextureManager () = 0; 00821 00823 virtual void SetDimensions (int width, int height) = 0; 00825 virtual int GetWidth () const = 0; 00827 virtual int GetHeight () const = 0; 00828 00833 virtual const csGraphics3DCaps *GetCaps () const = 0; 00834 00839 virtual void SetPerspectiveCenter (int x, int y) = 0; 00840 00842 virtual void GetPerspectiveCenter (int& x, int& y) const = 0; 00843 00847 virtual void SetPerspectiveAspect (float aspect) = 0; 00848 00850 virtual float GetPerspectiveAspect () const = 0; 00851 00856 virtual void SetObjectToCamera (csReversibleTransform* o2c) = 0; 00857 00861 virtual const csReversibleTransform& GetObjectToCamera () = 0; 00862 00876 virtual void SetRenderTarget (iTextureHandle* handle, 00877 bool persistent = false) = 0; 00878 00882 virtual iTextureHandle* GetRenderTarget () const = 0; 00883 00885 virtual bool BeginDraw (int DrawFlags) = 0; 00886 00888 virtual void FinishDraw () = 0; 00889 00895 virtual void Print (csRect const* area) = 0; 00896 00911 virtual void DrawPixmap (iTextureHandle *hTex, int sx, int sy, 00912 int sw, int sh, int tx, int ty, int tw, int th, uint8 Alpha = 0) = 0; 00913 00918 virtual void DrawLine (const csVector3& v1, const csVector3& v2, 00919 float fov, int color) = 0; 00920 00926 virtual void SetClipper (iClipper2D* clipper, int cliptype) = 0; 00927 00931 virtual iClipper2D* GetClipper () = 0; 00932 00936 virtual int GetClipType () const = 0; 00937 00942 virtual void SetNearPlane (const csPlane3& pl) = 0; 00943 00947 virtual void ResetNearPlane () = 0; 00948 00952 virtual const csPlane3& GetNearPlane () const = 0; 00953 00957 virtual bool HasNearPlane () const = 0; 00958 00960 virtual bool SetRenderState (G3D_RENDERSTATEOPTION op, long val) = 0; 00961 00963 virtual long GetRenderState (G3D_RENDERSTATEOPTION op) const = 0; 00964 00973 virtual csPtr<iRenderBuffer> CreateRenderBuffer (int size, 00974 csRenderBufferType type, csRenderBufferComponentType componentType, 00975 int componentCount, bool index) = 0; 00976 00983 virtual void CreateInterleavedRenderBuffers (int size, 00984 csRenderBufferType type, int count, csArray<iRenderBuffer*> &buffers) = 0; 00985 00990 virtual void SetBufferState (csVertexAttrib* attribs, 00991 iRenderBuffer** buffers, int count) = 0; 00992 00997 virtual void SetTextureState (int* units, iTextureHandle** textures, 00998 int count) = 0; 00999 01001 virtual void DrawMesh (const csCoreRenderMesh* mymesh, 01002 const csRenderMeshModes& modes, 01003 const csArray< csArray<csShaderVariable*> > &stacks) = 0; 01004 01006 virtual void SetWriteMask (bool red, bool green, bool blue, bool alpha) = 0; 01007 01009 virtual void GetWriteMask (bool &red, bool &green, bool &blue, 01010 bool &alpha) const = 0; 01011 01013 virtual void SetZMode (csZBufMode mode) = 0; 01014 01016 virtual void EnableZOffset () = 0; 01017 01019 virtual void DisableZOffset () = 0; 01020 01022 virtual void SetShadowState (int state) = 0; 01023 01024 //========================================================================= 01025 // Below this line are all functions that are not yet implemented by 01026 // the new renderer or are not going to be implemented ever. In the 01027 // last case they will be removed as soon as we permanently switch 01028 // to the new renderer. @@@NR@@@ 01029 //========================================================================= 01031 virtual uint32 *GetZBuffAt (int x, int y) = 0; 01032 01034 virtual float GetZBuffValue (int x, int y) = 0; 01035 01037 virtual void DrawPolygon (G3DPolygonDP& poly) = 0; 01038 01045 virtual void DrawPolygonDebug (G3DPolygonDP& poly) = 0; 01046 01065 virtual void DrawPolygonFX (G3DPolygonDPFX& poly) = 0; 01066 01070 virtual void DrawTriangleMesh (G3DTriangleMesh& mesh) = 0; 01071 01075 virtual void DrawPolygonMesh (G3DPolygonMesh& mesh) = 0; 01076 01084 virtual void OpenFogObject (CS_ID id, csFog* fog) = 0; 01085 01097 virtual void DrawFogPolygon (CS_ID id, G3DPolygonDFP& poly, int fogtype) = 0; 01098 01104 virtual void CloseFogObject (CS_ID id) = 0; 01105 01115 virtual void OpenPortal (size_t numVertices, const csVector2* vertices, 01116 const csPlane3& normal) = 0; 01117 01121 virtual void ClosePortal () = 0; 01122 01124 virtual iHalo *CreateHalo (float iR, float iG, float iB, 01125 unsigned char *iAlpha, int iWidth, int iHeight) = 0; 01126 01128 virtual void DumpCache () = 0; 01129 01131 virtual void ClearCache () = 0; 01132 01138 virtual void RemoveFromCache (iRendererLightmap* rlm) = 0; 01139 01144 virtual iVertexBufferManager* GetVertexBufferManager () = 0; 01145 01146 //========================================================================= 01147 // Here ends the zone of unimplemented methods. 01148 //========================================================================= 01149 01154 virtual bool IsLightmapOK (int lmw, int lmh, 01155 int lightCellSize) = 0; 01156 01157 virtual csPtr<iPolygonRenderer> CreatePolygonRenderer () = 0; 01158 01159 /* 01160 @@@ Needed for SW poly drawing ATM. 01161 */ 01162 virtual void SetWorldToCamera (csReversibleTransform* w2c) = 0; 01163 01175 virtual void DrawSimpleMesh (const csSimpleRenderMesh& mesh, 01176 uint flags = 0) = 0; 01177 01179 virtual csZBufMode GetZMode () = 0; 01180 }; 01181 01184 #endif // __CS_IVIDEO_GRAPH3D_H__ 01185
Generated for Crystal Space by doxygen 1.2.18