newspeoplefor developersdocumentationdownloads

ngfxserver2.h

Go to the documentation of this file.
00001 #ifndef N_GFXSERVER2_H
00002 #define N_GFXSERVER2_H
00003 //------------------------------------------------------------------------------
00014 #include "kernel/nroot.h"
00015 #include "gfx2/ncamera2.h"
00016 #include "kernel/nautoref.h"
00017 #include "mathlib/matrix.h"
00018 #include "gfx2/ntexture2.h"
00019 #include "mathlib/rectangle.h"
00020 #include "gfx2/nmousecursor.h"
00021 #include "gfx2/ndisplaymode2.h"
00022 #include "gfx2/nfont2.h"
00023 #include "gfx2/ninstancestream.h"
00024 #include "gfx2/nlight.h"
00025 #include "util/nfixedarray.h"
00026 #include "mathlib/rectangle.h"
00027 #include "mathlib/plane.h"
00028 #if __NEBULA_STATS__
00029 #include "kernel/nprofiler.h"
00030 #endif
00031 
00032 //------------------------------------------------------------------------------
00033 class nMesh2;
00034 class nShader2;
00035 class nResourceServer;
00036 class nFontDesc;
00037 class nMeshArray;
00038 class nOcclusionQuery;
00039 class nViewport
00040 {
00041 public:
00042     float x;
00043     float y;
00044     float width;
00045     float height;
00046     float nearz;
00047     float farz;
00048 };
00049 
00050 class nGfxServer2 : public nRoot
00051 {
00052 public:
00054     enum TransformType
00055     {
00056         Model = 0,              
00057         View,                   
00058         Projection,             
00059         InvModel,               
00060         InvView,                
00061         ModelView,              
00062         InvModelView,           
00063         ModelViewProjection,    
00064         ViewProjection,         
00065         Texture0,               
00066         Texture1,
00067         Texture2,
00068         Texture3,
00069         Light,                  
00070         ModelLight,
00071         InvModelLight,
00072         ShadowProjection,
00073 
00074         NumTransformTypes
00075     };
00076 
00078     enum LightingType
00079     {
00080         Off = 0,                
00081         FFP,                    
00082         Shader,                 
00083     };
00084 
00086     enum BufferType
00087     {
00088         NoBuffer      = 0,
00089         ColorBuffer   = (1<<0),
00090         DepthBuffer   = (1<<1),
00091         StencilBuffer = (1<<2),
00092         AllBuffers    = (ColorBuffer | DepthBuffer | StencilBuffer),
00093     };
00094 
00096     enum PrimitiveType
00097     {
00098         PointList,
00099         LineList,
00100         LineStrip,
00101         TriangleList,
00102         TriangleStrip,
00103         TriangleFan,
00104     };
00105 
00107     enum IndexType
00108     {
00109         Index16,
00110         Index32,
00111     };
00112 
00114     enum ShapeType
00115     {
00116         Box = 0,
00117         Cylinder,
00118         Sphere,
00119         Torus,
00120         Teapot,
00121 
00122         NumShapeTypes,
00123     };
00124 
00126     enum FeatureSet
00127     {
00128         DX7 = 0,                    
00129         DX8,                        
00130         DX8SB,                      
00131         DX9,                        
00132         DX9FLT,                     
00133 
00134         NumFeatureSets,             
00135         InvalidFeatureSet,          
00136     };
00137 
00139     enum CursorVisibility
00140     {
00141         None,               
00142         System,             
00143         Custom,             
00144         Gui,                
00145     };
00146 
00147     enum
00148     {
00149         MaxVertexStreams = 16,
00150         MaxTextureStages = 4,
00151         MaxTransformStackDepth = 4,
00152         MaxRenderTargets = 4,
00153         MaxLights = 8,
00154     };
00155 
00157     enum DeviceIdentifier       // list of devices that are know to cause bugs
00158     {
00159         GenericDevice,
00160         Intel_82865G,           // shadow bugs
00161         SiS_630,                // crap
00162         SiS_741,                // crap
00163     };
00164 
00165     enum Hint
00166     {
00167         MvpOnly = (1<<0),       // only update the ModelViewProjection matrix in shaders
00168         CountStats = (1<<1),    // statistics counting currently active?
00169     };
00170 
00172     nGfxServer2();
00174     virtual ~nGfxServer2();
00176     static nGfxServer2* Instance();
00177 
00179     virtual nMesh2* NewMesh(const nString& rsrcName);
00181     virtual nMeshArray* NewMeshArray(const nString& rsrcName);
00183     virtual nTexture2* NewTexture(const nString& rsrcName);
00185     virtual nShader2* NewShader(const nString& rsrcName);
00187     virtual nFont2* NewFont(const nString& rsrcName, const nFontDesc& fontDesc);
00189     virtual nTexture2* NewRenderTarget(const nString& rsrcName, int width, int height, nTexture2::Format format, int usageFlags);
00191     virtual nInstanceStream* NewInstanceStream(const nString& rsrcName);
00193     virtual nOcclusionQuery* NewOcclusionQuery();
00194 
00196     virtual void SetDisplayMode(const nDisplayMode2& mode);
00198     virtual const nDisplayMode2& GetDisplayMode() const;
00200     virtual void SetWindowTitle(const char* title);
00202     void SetFontScale(float s);
00204     float GetFontScale() const;
00206     void SetMinFontHeight(int s);
00208     int GetMinFontHeight() const;
00210     virtual void SetCamera(nCamera2& cam);
00212     void SetFeatureSetOverride(FeatureSet f);
00214     virtual FeatureSet GetFeatureSet();
00216     virtual bool AreVertexShadersEmulated();
00218     nCamera2& GetCamera();
00220     virtual void SetViewport(nViewport& vp);
00222     virtual nViewport& GetViewport();
00224     virtual bool OpenDisplay();
00226     virtual void CloseDisplay();
00228     virtual bool Trigger();
00230     virtual int GetNumStencilBits() const;
00232     virtual int GetNumDepthBits() const;
00234     virtual void SetScissorRect(const rectangle& rect);
00236     const rectangle& GetScissorRect() const;
00238     virtual void SetClipPlanes(const nArray<plane>& planes);
00240     const nArray<plane>& GetClipPlanes() const;
00242     void SetHint(Hint hint, bool enable);
00244     bool GetHint(Hint hint) const;
00245 
00247     virtual void SetRenderTarget(int index, nTexture2* tex);
00249     nTexture2* GetRenderTarget(int index) const;
00250 
00252     virtual bool BeginFrame();
00254     virtual bool BeginScene();
00256     virtual void EndScene();
00258     virtual void PresentScene();
00260     virtual void EndFrame();
00262     bool InBeginScene() const;
00264     virtual void Clear(int bufferTypes, float red, float green, float blue, float alpha, float z, int stencil);
00265 
00267     void SetLightingType(LightingType t);
00269     LightingType GetLightingType() const;
00271     virtual void ClearLights();
00273     virtual void ClearPointLights();
00275     virtual void ClearLight(int index);
00277     virtual int AddLight(const nLight& light);
00279     const nArray<nLight>& GetLightArray() const;
00281     virtual void SetMesh(nMesh2* vbMesh, nMesh2* ibMesh);
00283     nMesh2* GetMesh() const;
00285     virtual void SetMeshArray(nMeshArray* meshArray);
00287     nMeshArray* GetMeshArray() const;
00289     virtual void SetShader(nShader2* shader);
00291     nShader2* GetShader() const;
00293     virtual void SetFont(nFont2* font);
00295     nFont2* GetFont() const;
00297     void SetInstanceStream(nInstanceStream* stream);
00299     nInstanceStream* GetInstanceStream() const;
00301     virtual void SetTransform(TransformType type, const matrix44& matrix);
00303     const matrix44& GetTransform(TransformType type) const;
00305     void PushTransform(TransformType type, const matrix44& matrix);
00307     const matrix44& PopTransform(TransformType type);
00309     void SetVertexRange(int firstVertex, int numVertices);
00311     void SetIndexRange(int firstIndex, int numIndices);
00312 
00314     virtual void DrawIndexed(PrimitiveType primType);
00316     virtual void Draw(PrimitiveType primType);
00318     virtual void DrawIndexedNS(PrimitiveType primType);
00320     virtual void DrawNS(PrimitiveType primType);
00322     virtual void DrawText(const nString& text, const vector4& color, const rectangle& rect, uint flags, bool immediate=true);
00324     virtual vector2 GetTextExtent(const nString& text);
00326     void BreakLines(const nString& text, const rectangle& rect, nString& outString);
00327 
00329     virtual void Text(const nString& text, const vector4& color, float xPos, float yPos);
00331     virtual void DrawTextBuffer();
00332 
00334     virtual void SetMouseCursor(const nMouseCursor& cursor);
00336     virtual const nMouseCursor& GetMouseCursor() const;
00338     virtual void SetCursorVisibility(CursorVisibility type);
00340     virtual CursorVisibility GetCursorVisibility() const;
00341 
00343     virtual void EnterDialogBoxMode();
00345     virtual void LeaveDialogBoxMode();
00347     bool InDialogBoxMode() const;
00348 
00350     virtual bool SaveScreenshot(const char* filename, nTexture2::FileFormat fileFormat);
00351 
00353     static FeatureSet StringToFeatureSet(const char* str);
00355     static const char* FeatureSetToString(FeatureSet f);
00356 
00358     static const vector4 GetShadowLightIndexVector(int shadowLightIndex, float value);
00359 
00361     DeviceIdentifier GetDeviceIdentifier() const;
00363     line3 ComputeWorldMouseRay(const vector2& mousePos, float length);
00364 
00366     void SetGamma(float g);
00368     void SetBrightness(float b);
00370     void SetContrast(float c);
00372     float GetGamma() const;
00374     float GetBrightness() const;
00376     float GetContrast() const;
00377 
00379     virtual void AdjustGamma();
00381     virtual void RestoreGamma();
00383     virtual void SetSkipMsgLoop(bool skip);
00384 
00386     virtual void BeginShapes();
00388     virtual void DrawShape(ShapeType type, const matrix44& model, const vector4& color);
00390     virtual void DrawShapeNS(ShapeType type, const matrix44& model);
00392     virtual void DrawShapePrimitives(PrimitiveType type, int numPrimitives, const vector3* vertexList, int vertexWidth, const matrix44& model, const vector4& color);
00394     virtual void DrawShapeIndexedPrimitives(PrimitiveType type, int numPrimitives, const vector3* vertexList, int numVertices, int vertexWidth, void* indices, IndexType indexType, const matrix44& model, const vector4& color);
00396     virtual void EndShapes();
00397 
00399     virtual void BeginLines();
00401     virtual void DrawLines3d(const vector3* vertexList, int numVertices, const vector4& color);
00403     virtual void DrawLines2d(const vector2* vertexList, int numVertices, const vector4& color);
00405     virtual void EndLines();
00406 
00407 private:
00408     static nGfxServer2* Singleton;
00409 
00410 protected:
00411     bool displayOpen;
00412     bool inBeginFrame;
00413     bool inBeginScene;
00414     bool inBeginLines;
00415     bool inBeginShapes;
00416 
00417     nAutoRef<nResourceServer> refResource;
00418     nCamera2 camera;
00419     nViewport viewport;
00420 
00421     nFixedArray<nRef<nTexture2> >   refRenderTargets;
00422     nRef<nMesh2>                    refVbMesh;
00423     nRef<nMesh2>                    refIbMesh;
00424     nRef<nMeshArray>                refMeshArray;
00425     nRef<nFont2>                    refFont;
00426     nRef<nShader2>                  refShader;
00427     nRef<nInstanceStream>           refInstanceStream;
00428     nMouseCursor curMouseCursor;
00429     int vertexRangeFirst;
00430     int vertexRangeNum;
00431     int indexRangeFirst;
00432     int indexRangeNum;
00433     FeatureSet featureSetOverride;
00434 
00435     matrix44 transform[NumTransformTypes];
00436     int transformTopOfStack[NumTransformTypes];
00437     matrix44 transformStack[NumTransformTypes][MaxTransformStackDepth];
00438     bool cursorDirty;
00439     bool inDialogBoxMode;
00440 
00441     nArray<nLight> lightArray;
00442 
00443     float gamma;
00444     float brightness;
00445     float contrast;
00446     float fontScale;
00447     int fontMinHeight;
00448 
00449     DeviceIdentifier deviceIdentifier;
00450     LightingType lightingType;
00451     rectangle scissorRect;
00452 
00453     uint hints;         // currently set hints
00454     nArray<plane> clipPlanes;
00455 
00456 #if __NEBULA_STATS__
00457     nProfiler profGUIBreakLines;
00458     nProfiler profGUIGetTextExtent;
00459     nProfiler profGUIDrawText;
00460 #endif
00461 public:
00463     CursorVisibility cursorVisibility;
00464 };
00465 
00466 //------------------------------------------------------------------------------
00469 inline
00470 nGfxServer2*
00471 nGfxServer2::Instance()
00472 {
00473     n_assert(Singleton);
00474     return Singleton;
00475 }
00476 
00477 //------------------------------------------------------------------------------
00480 inline
00481 void
00482 nGfxServer2::SetHint(Hint hint, bool enable)
00483 {
00484     if (enable) this->hints |= hint;
00485     else        this->hints &= ~hint;
00486 }
00487 
00488 //------------------------------------------------------------------------------
00491 inline
00492 bool
00493 nGfxServer2::GetHint(Hint hint) const
00494 {
00495     return (this->hints & hint) != 0;
00496 }
00497 
00498 //------------------------------------------------------------------------------
00501 inline
00502 void
00503 nGfxServer2::SetMinFontHeight(int s)
00504 {
00505     this->fontMinHeight = s;
00506 }
00507 
00508 //------------------------------------------------------------------------------
00511 inline
00512 int
00513 nGfxServer2::GetMinFontHeight() const
00514 {
00515     return this->fontMinHeight;
00516 }
00517 
00518 //------------------------------------------------------------------------------
00521 inline
00522 void
00523 nGfxServer2::SetFontScale(float s)
00524 {
00525     this->fontScale = s;
00526 }
00527 
00528 //------------------------------------------------------------------------------
00531 inline
00532 float
00533 nGfxServer2::GetFontScale() const
00534 {
00535     return this->fontScale;
00536 }
00537 
00538 //------------------------------------------------------------------------------
00541 inline
00542 const nArray<nLight>&
00543 nGfxServer2::GetLightArray() const
00544 {
00545     return this->lightArray;
00546 }
00547 
00548 //------------------------------------------------------------------------------
00552 inline
00553 nGfxServer2::FeatureSet
00554 nGfxServer2::StringToFeatureSet(const char* str)
00555 {
00556     n_assert(str);
00557     if (0 == strcmp(str, "dx7"))        return DX7;
00558     if (0 == strcmp(str, "dx8"))        return DX8;
00559     if (0 == strcmp(str, "dx8sb"))      return DX8SB;
00560     if (0 == strcmp(str, "dx9"))        return DX9;
00561     if (0 == strcmp(str, "dx9flt"))     return DX9FLT;
00562     return InvalidFeatureSet;
00563 }
00564 
00565 //------------------------------------------------------------------------------
00569 inline
00570 const char*
00571 nGfxServer2::FeatureSetToString(FeatureSet f)
00572 {
00573     switch (f)
00574     {
00575         case nGfxServer2::DX7:          return "dx7";
00576         case nGfxServer2::DX8:          return "dx8";
00577         case nGfxServer2::DX8SB:        return "dx8sb";
00578         case nGfxServer2::DX9:          return "dx9";
00579         case nGfxServer2::DX9FLT:       return "dx9flt";
00580         default:                        return "invalid";
00581     }
00582 }
00583 
00584 //------------------------------------------------------------------------------
00588 inline
00589 nViewport&
00590 nGfxServer2::GetViewport()
00591 {
00592     return this->viewport;
00593 }
00594 
00595 //------------------------------------------------------------------------------
00598 inline
00599 void
00600 nGfxServer2::SetFeatureSetOverride(FeatureSet f)
00601 {
00602     this->featureSetOverride = f;
00603     n_printf("nGfxServer2: set feature set override to '%s'\n", FeatureSetToString(f));
00604 }
00605 
00606 //------------------------------------------------------------------------------
00610 inline
00611 nCamera2&
00612 nGfxServer2::GetCamera()
00613 {
00614     return this->camera;
00615 }
00616 
00617 //------------------------------------------------------------------------------
00625 inline
00626 nTexture2*
00627 nGfxServer2::GetRenderTarget(int index) const
00628 {
00629     return this->refRenderTargets[index].get_unsafe();
00630 }
00631 
00632 //------------------------------------------------------------------------------
00638 inline
00639 nInstanceStream*
00640 nGfxServer2::GetInstanceStream() const
00641 {
00642     return this->refInstanceStream.isvalid() ? this->refInstanceStream.get() : 0;
00643 }
00644 
00645 //------------------------------------------------------------------------------
00651 inline
00652 nMeshArray*
00653 nGfxServer2::GetMeshArray() const
00654 {
00655     return this->refMeshArray.isvalid() ? this->refMeshArray.get() : 0;
00656 }
00657 
00658 //------------------------------------------------------------------------------
00664 inline
00665 nShader2*
00666 nGfxServer2::GetShader() const
00667 {
00668     return this->refShader.isvalid() ? this->refShader.get() : 0;
00669 }
00670 
00671 //------------------------------------------------------------------------------
00677 inline
00678 nFont2*
00679 nGfxServer2::GetFont() const
00680 {
00681     return this->refFont.isvalid() ? this->refFont.get() : 0;
00682 }
00683 
00684 //------------------------------------------------------------------------------
00691 inline
00692 const matrix44&
00693 nGfxServer2::GetTransform(TransformType type) const
00694 {
00695     n_assert(type < NumTransformTypes);
00696     return this->transform[type];
00697 }
00698 
00699 //------------------------------------------------------------------------------
00706 inline
00707 void
00708 nGfxServer2::SetVertexRange(int firstVertex, int numVertices)
00709 {
00710     this->vertexRangeFirst = firstVertex;
00711     this->vertexRangeNum   = numVertices;
00712 }
00713 
00714 //------------------------------------------------------------------------------
00721 inline
00722 void
00723 nGfxServer2::SetIndexRange(int firstIndex, int numIndices)
00724 {
00725     this->indexRangeFirst = firstIndex;
00726     this->indexRangeNum   = numIndices;
00727 }
00728 
00729 //------------------------------------------------------------------------------
00737 inline
00738 void
00739 nGfxServer2::SetGamma(float g)
00740 {
00741     this->gamma = g;
00742 }
00743 
00744 //------------------------------------------------------------------------------
00752 inline
00753 void
00754 nGfxServer2::SetBrightness(float b)
00755 {
00756     this->brightness = b;
00757 }
00758 
00759 //------------------------------------------------------------------------------
00767 inline
00768 void
00769 nGfxServer2::SetContrast(float c)
00770 {
00771     this->contrast = c;
00772 }
00773 
00774 //------------------------------------------------------------------------------
00780 inline
00781 float nGfxServer2::GetGamma() const
00782 {
00783     return this->gamma;
00784 }
00785 
00786 //------------------------------------------------------------------------------
00792 inline
00793 float nGfxServer2::GetBrightness() const
00794 {
00795     return this->brightness;
00796 }
00797 
00798 //------------------------------------------------------------------------------
00804 inline
00805 float nGfxServer2::GetContrast() const
00806 {
00807     return this->contrast;
00808 }
00809 
00810 //------------------------------------------------------------------------------
00813 inline
00814 nGfxServer2::DeviceIdentifier
00815 nGfxServer2::GetDeviceIdentifier() const
00816 {
00817     return this->deviceIdentifier;
00818 }
00819 
00820 //------------------------------------------------------------------------------
00823 inline
00824 void
00825 nGfxServer2::SetLightingType(LightingType t)
00826 {
00827     this->lightingType = t;
00828 }
00829 
00830 //------------------------------------------------------------------------------
00833 inline
00834 nGfxServer2::LightingType
00835 nGfxServer2::GetLightingType() const
00836 {
00837     return this->lightingType;
00838 }
00839 
00840 //------------------------------------------------------------------------------
00843 inline
00844 bool
00845 nGfxServer2::InBeginScene() const
00846 {
00847     return this->inBeginScene;
00848 }
00849 
00850 //------------------------------------------------------------------------------
00853 inline
00854 const rectangle&
00855 nGfxServer2::GetScissorRect() const
00856 {
00857     return this->scissorRect;
00858 }
00859 
00860 //------------------------------------------------------------------------------
00864 inline
00865 const vector4
00866 nGfxServer2::GetShadowLightIndexVector(int shadowLightIndex, float value)
00867 {
00868     // FIXME: lightIndex should be a bit field (shader must support this?),
00869     // so there can be 32 shadow casting lights at once,
00870     // or 8 shadow casting lights in a one channel map (e.g. only alpha write)
00871     vector4 indexColor;
00872     indexColor.x = (shadowLightIndex == 0) ? value : 0.0f;
00873     indexColor.y = (shadowLightIndex == 1) ? value : 0.0f;
00874     indexColor.z = (shadowLightIndex == 2) ? value : 0.0f;
00875     indexColor.w = (shadowLightIndex == 3) ? value : 0.0f;
00876     return indexColor;
00877 }
00878 
00879 //------------------------------------------------------------------------------
00882 inline
00883 const nArray<plane>&
00884 nGfxServer2::GetClipPlanes() const
00885 {
00886     return this->clipPlanes;
00887 }
00888 
00889 //------------------------------------------------------------------------------
00890 #endif

Copyright © 1999-2005 by the contributing authors. Ideas, requests, problems: Send feedback.