MercuryDisplay.h

Go to the documentation of this file.
00001 #ifndef MERCURYDISPLAY_H
00002 #define MERCURYDISPLAY_H
00003 
00004 #include "MercuryWindow.h"
00005 #include "MercuryLight.h"
00006 #include "MercuryTypes.h"
00007 #include "MercuryVertex.h"
00008 #include "MercuryParticle.h"
00009 #include "MercuryMesh.h"
00010 #include "MercuryMatrixStack.h"
00011 #include "MercuryTexture.h"
00012 #include "MercuryCamera.h"
00013 
00014 #include "MercuryObject.h"
00015 #include "MercuryText.h"
00016 #include "MercuryGLState.h"
00017 #include "MCQueue.h"
00018 
00019 extern MercuryMatrixStack WorldStack;
00020 extern MercuryMatrixStack TextureStack;
00021 
00022 namespace InitialDisplayProperties
00023 {
00024     const int InitialBasedWidth(640);
00025     const int InitialBasedHeight(480);
00026 }
00027 
00028 struct LookAtInternals
00029 {
00030     LookAtInternals()
00031     {}
00032     LookAtInternals(const MercuryPoint& direction, const MercuryPoint& x, const MercuryPoint& up)
00033         :m_direction(direction), m_x(x), m_up(up)
00034     {}
00035     MercuryPoint m_direction;
00036     MercuryPoint m_x;
00037     MercuryPoint m_up;
00038 };
00039 
00040 struct FrustumInfo
00041 {
00042     float m_fov;
00043     float m_aspect;
00044     float m_znear;
00045     float m_zfar;
00046 };
00047 
00048 struct Projection
00049 {
00050     MercuryMatrix m_matrix;
00051     FrustumInfo m_frustumInfo;
00052 };
00053 
00054 class RenderPacket
00055 {
00056 public:
00057     enum Type
00058     {
00059         OBJECT,
00060         SETVIEW,
00061         SWAPBUFFER,
00062         INVALID
00063     };
00064 
00065     RenderPacket(Type type = INVALID, void* data = NULL)
00066         :m_type(type), m_data(data)
00067     {}
00068 
00069     Type GetType() const { return m_type; }
00070     void* GetData() { return m_data; }
00071 private:
00072     Type m_type;
00073     void* m_data;
00074 };
00075 
00076 Projection Perspective( float fov, float aspect, float znear, float zfar );
00077 MercuryMatrix Frustum(float left, float right, float bottom, float top, float zNear, float zFar);
00078 Projection Ortho(float left, float right, float bottom, float top, float zNear, float zFar);
00079 LookAtInternals CalculateLookAtMatrix(const MercuryPoint& eye, const MercuryPoint& center,
00080                                     const MercuryPoint& up, MercuryMatrix& m);
00081 MercuryPoint PointToScreenCoord(const MercuryMatrix& proj, const MercuryMatrix& view, const MercuryPoint& p, const DisplayDimensions& bd);
00082 
00083 
00085 class MercuryDisplay
00086 {
00087 public:
00088     MercuryDisplay();
00089     virtual ~MercuryDisplay();
00090 
00091     static void* ThreadCallback(void* display);
00092     void CreateThread();
00093     void ThreadLoop();
00094 
00095     DisplayDimensions GetDimensions() const;
00096     DisplayDimensions GetBasedDimensions() const;
00097     void Resize(int width, int height);
00098     void LookAt(const MercuryPoint& eye, const MercuryPoint& center,
00099                     const MercuryPoint& up);
00100     void LookAt( float eyeX, float eyeY, float eyeZ, float centerX, float centerY, float centerZ, float upX, float upY, float upZ );
00101     void LookAt(const MQuaternion& q, const MercuryPoint& eye);
00102     void AddLight(MercuryLight* light);
00103     void RemoveLight(MercuryLight* light);
00104 
00105     void TakeScreenShot();
00106 
00108     MercuryPoint GetLastCameraPosition( );
00109     inline void CloseNextCycle() { m_bCloseNextCycle = true; }
00110 
00111     inline MercuryWindow* GetWindow() { return m_window; }
00112 
00114     bool Update(const float dTime);
00115 
00116     inline const MercuryMatrix& GetProjection() const { return m_projection.m_matrix; }
00117 
00119     virtual bool MakeWindow(const char* title, int width, int height, int bits, bool fullscreenflag);
00120     virtual bool BeginFrame() = 0;
00121     virtual bool EndFrame() = 0;
00122     virtual bool AttachWindow(MercuryWindow* window);
00123     virtual bool Init(const MString& name, unsigned int width, unsigned int height, unsigned short bpp, bool fullscreen);
00124 
00126     virtual void RendererInfo() = 0;
00127 
00128     virtual void DrawParticleField(const MercuryParticleField* field) = 0;
00129     virtual void DrawMesh(const MercuryMesh& mesh) = 0;
00130     virtual void RenderText(const MercuryText& text) = 0;
00131     virtual void DrawSprite(const MercuryMesh& sprite) = 0;
00132     virtual void GenerateVBO(MercuryMesh& mesh) = 0;
00133     virtual void DestroyVBO(MercuryMesh& mesh) = 0;
00134 
00136     virtual void SetMaterial(const MercuryMaterial* material) = 0;
00137     void ToggleUseMaterialColors(bool toggle) { m_useMaterialColors = toggle; }
00138 
00140     virtual void ClearZBuffer() = 0;
00141     virtual void ClearFrameBuffer() = 0;
00142 
00143     //The following functions are used in managers      
00144     virtual void DeleteTextures(int n, unsigned int* textureNames) = 0;
00145 
00146     virtual void UpdateTextureData(MercuryTexture* texture, unsigned int& ID) = 0;
00147     virtual void CreateCache(RawImageData* data, unsigned int& ID) = 0;
00148 
00149     inline bool IsUseLighting() { return m_currentState.GetState(MGLS_LIGHTING); }
00150 //  void ToggleLighting(bool toggle) { m_useLights = toggle; }
00151 
00152     virtual void Viewport(int x, int y, int width, int height);
00153     virtual void SetProjection(const Projection& projection);
00154     virtual void SetView(const MercuryPoint& position, const MercuryMatrix& m, const LookAtInternals& lai);
00155     inline MercuryMatrix& GetView() { return m_view; }
00156 
00157     virtual void EnableTextures(MercuryMaterial* material) = 0;
00158     virtual void EnableShaders(MercuryMaterial* material) = 0;
00159 
00160     virtual void ReadFrameBuffer(RawImageData& image) = 0;
00161 
00162     virtual void ErrorCheck() = 0;
00163     virtual void SendMatrixData(const MercuryMatrix& m) = 0;
00164     virtual void SendProjectionMatrixData() = 0;
00165 
00166     virtual void StencilOp(STENCILOPS fail, STENCILOPS zfail, STENCILOPS zpass) = 0;
00167     virtual void CullBackface(bool toggle = true) = 0;
00168     virtual void StencilFunc(TFUNCT func, int ref, unsigned int zref) = 0;
00169 
00170     void Ortho(float left, float right, float bottom, float top, float zNear, float zFar);
00171     void Perspective( float fovy, float aspect, float znear, float zfar );
00172     virtual int CompileShader(MString vertStr, MString fragStr) { return -1; }
00173     virtual void DestroyShaderProgram(int shaderID) {};
00174 
00175     bool IsVisible( const MercuryPoint & pCenter, float fRadius, const MercuryMatrix& world );
00176 
00177     virtual void DefineClippingPlanes(const MVector<MQuaternion>& planes) = 0;
00178 
00179     void ResetFrustumClippingPlanes();
00180 
00181     void AddTranslucentObject(MercuryObject* obj);
00182     virtual void RenderTranslucentObject() = 0;
00183 
00184     inline const Capabilities& GetCapabilities() const { return m_capabilities; }
00185 
00186     void FindLights(const MercuryMatrix& world, float radius);
00187     virtual MercuryGLState SetStates(MercuryGLState states);
00188 
00189     inline void EnableGLState(unsigned long mask) { m_globalState.back().Enable(mask); }
00190     inline void DisableGLState(unsigned long mask) { m_globalState.back().Disable(mask); }
00191     inline void PushGLState() { m_globalState.push_back(m_globalState.back()); }
00192     inline void PopGLState() { m_globalState.pop_back(); }
00193     inline const MercuryGLState& GetGlobalState() const { return m_globalState.back(); }
00194 
00195     void EnqueueObject(const RenderPacket& rp);
00196 protected:
00197     const MercuryLight* m_usedLights[3];
00198     MVector< MercuryLight* > m_foundLights;
00199 
00200     int Next3Lights(const MercuryLight* light[3], unsigned int& count);
00201 
00202     unsigned int m_based_width;
00203     unsigned int m_based_height;
00204 
00205     MercuryTimer m_timer;
00206     unsigned int m_framesDrawn;
00207     MercuryWindow* m_window;
00208 
00209     unsigned int m_verticesDrawn;
00210 
00211     bool    m_bLastHadFocus;
00212     bool    m_bCloseNextCycle;
00213     MercuryPoint m_pLastCameraPos;
00214 
00215     bool m_disableTextures;
00216     bool m_useMaterialColors;
00217 
00218     MVector<MercuryLight*> m_lights; //all lights
00219 //  bool m_useLights;
00220 
00221     //Clipping planes (for use with frustum culling)
00222     //These are set on Perspective() and represent the normals of the six view planes.
00223     void SetupClippingPlanes(const MercuryPoint& position, MercuryPoint F, MercuryPoint u, MercuryPoint s );
00224     MercuryPoint m_nViewPlanes[6];
00225     MercuryPoint m_pNearPoint;
00226     MercuryPoint m_pFarPoint;
00227 
00228     int m_maxTextures;
00229     int m_maxLights;
00230     int m_maxDrawBuffers;
00231     int m_maxVertexAttributes;
00232     int m_maxTextureCoordinates;
00233     int m_maxShaderVertUniformComponents;
00234     int m_maxShaderFragUniformComponents;
00235     int m_shaderLanngVersion;
00236     int m_maxAnisotropy;
00237 
00238     struct TranslucentPair
00239     {
00240         float dist;
00241         MercuryObject* obj;
00242         bool operator<(const TranslucentPair& x) const { return dist < x.dist; }
00243         bool operator>=(const TranslucentPair& x) const { return dist >= x.dist; }
00244     };
00245     MDeque<TranslucentPair> m_translucentObjects;
00246 
00247     Capabilities m_capabilities;
00248 
00249     int m_VPx, m_VPy;
00250     int m_VPwidth, m_VPheight;
00251     MercuryGLState m_currentState;
00252 
00253     Projection m_projection;
00254     MercuryMatrix m_view;
00255 private:
00256     MercuryThread m_thread;
00257     bool m_initalized;
00258     virtual void ProcessLightsInternal( const MercuryLight* light[3], unsigned int count ) = 0;
00259 
00260     struct ProjectionTypeMatrix
00261     {
00262         MercuryMatrix matrix;
00263         PROJECTIONTYPE type;
00264     };
00265 
00266     struct PickedLightInfo
00267     {
00268         PickedLightInfo() : brightness(0), light(NULL) {};
00269         float brightness;
00270         MercuryLight* light;
00271     };
00272 
00273     MDeque<MercuryGLState> m_globalState;
00274     MCQueue< RenderPacket > m_renderObjects;//(200);
00275 };
00276 
00277 MercuryDisplay* MakeDisplayDriver();
00278 
00279 extern MercuryDisplay* DISPLAY;
00280 
00281 MercuryDisplay * GetDisplay();
00282 
00283 #endif
00284 
00285 /* 
00286  * Copyright (c) 2005-2006, Joshua Allen
00287  * All rights reserved.
00288  *
00289  * Redistribution and use in source and binary forms, with or
00290  * without modification, are permitted provided that the following
00291  * conditions are met:
00292  *  -   Redistributions of source code must retain the above
00293  *      copyright notice, this list of conditions and the following disclaimer.
00294  *  -   Redistributions in binary form must reproduce the above copyright
00295  *      notice, this list of conditions and the following disclaimer in
00296  *      the documentation and/or other materials provided with the distribution.
00297  *  -   Neither the name of the Mercury Engine nor the names of its
00298  *      contributors may be used to endorse or promote products derived from
00299  *      this software without specific prior written permission.
00300  *
00301  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00302  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00303  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00304  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
00305  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00306  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
00307  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00308  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
00309  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
00310  * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00311  */

Hosted by SourceForge.net Logo