# Home    # nevrax.com   
Nevrax
Nevrax.org
#News
#Mailing-list
#Documentation
#CVS
#Bugs
#License
Docs
 
Documentation  
Main Page   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Namespace Members   Compound Members   File Members   Related Pages   Search  

driver_opengl.h

Go to the documentation of this file.
00001 
00007 /* Copyright, 2000 Nevrax Ltd.
00008  *
00009  * This file is part of NEVRAX NEL.
00010  * NEVRAX NEL is free software; you can redistribute it and/or modify
00011  * it under the terms of the GNU General Public License as published by
00012  * the Free Software Foundation; either version 2, or (at your option)
00013  * any later version.
00014 
00015  * NEVRAX NEL is distributed in the hope that it will be useful, but
00016  * WITHOUT ANY WARRANTY; without even the implied warranty of
00017  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
00018  * General Public License for more details.
00019 
00020  * You should have received a copy of the GNU General Public License
00021  * along with NEVRAX NEL; see the file COPYING. If not, write to the
00022  * Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
00023  * MA 02111-1307, USA.
00024  */
00025 
00026 #ifndef NL_OPENGL_H
00027 #define NL_OPENGL_H
00028 
00029 
00030 #include "nel/misc/types_nl.h"
00031 
00032 #ifdef NL_OS_WINDOWS
00033 
00034 #define WIN32_LEAN_AND_MEAN
00035 #include <windows.h>
00036 
00037 #else // NL_OS_UNIX
00038 
00039 #define GL_GLEXT_PROTOTYPES
00040 #include <GL/glx.h>
00041 
00042 #ifdef XF86VIDMODE
00043 #include <X11/extensions/xf86vmode.h>
00044 #endif //XF86VIDMODE
00045 
00046 #endif // NL_OS_UNIX
00047 
00048 #include <GL/gl.h>
00049 #include "driver_opengl_extension.h"
00050 
00051 #include "nel/3d/driver.h"
00052 #include "nel/3d/material.h"
00053 #include "nel/3d/shader.h"
00054 #include "nel/3d/vertex_buffer.h"
00055 #include "nel/misc/matrix.h"
00056 #include "nel/misc/smart_ptr.h"
00057 #include "nel/misc/rgba.h"
00058 #include "nel/misc/event_emitter.h"
00059 #include "nel/misc/bit_set.h"
00060 
00061 #ifdef NL_OS_WINDOWS
00062 #include "nel/misc/win_event_emitter.h"
00063 #elif defined (NL_OS_UNIX)
00064 #include "unix_event_emitter.h"
00065 #endif // NL_OS_UNIX
00066 
00067 
00068 namespace NL3D {
00069 
00070 using NLMISC::CMatrix;
00071 using NLMISC::CVector;
00072 
00073 // --------------------------------------------------
00074 
00075 class CTextureDrvInfosGL : public ITextureDrvInfos
00076 {
00077 public:
00078         // The GL Id.
00079         GLuint                                  ID;
00080         // Is the internal format of the texture is a compressed one?
00081         bool                                    Compressed;
00082 
00083         // The current wrap modes assigned to the texture.
00084         ITexture::TWrapMode             WrapS;
00085         ITexture::TWrapMode             WrapT;
00086         ITexture::TMagFilter    MagFilter;
00087         ITexture::TMinFilter    MinFilter;
00088 
00089         // The gl id is auto created here.
00090         CTextureDrvInfosGL(IDriver *drv, ItTexDrvInfoPtrMap it);
00091         // The gl id is auto deleted here.
00092         ~CTextureDrvInfosGL();
00093 };
00094 
00095 
00096 
00097 // --------------------------------------------------
00098 
00099 class CVBDrvInfosGL : public IVBDrvInfos
00100 {
00101 public:
00102         // Software Skinning: post-rendered vertices/normales.
00103         std::vector<CVector>    SoftSkinVertices;
00104         std::vector<CVector>    SoftSkinNormals;
00105         // Software Skinning: flags to know what vertex must be computed.
00106         std::vector<uint8>              SoftSkinFlags;
00107 
00108         CVBDrvInfosGL(IDriver *drv, ItVBDrvInfoPtrList it) : IVBDrvInfos(drv, it) {}
00109 };
00110 
00111 // --------------------------------------------------
00112 
00113 class CShaderGL : public IShader
00114 {
00115 public:
00116         GLenum          SrcBlend;
00117         GLenum          DstBlend;
00118         GLenum          ZComp;
00119 
00120         GLfloat         Emissive[4];
00121         GLfloat         Ambient[4];
00122         GLfloat         Diffuse[4];
00123         GLfloat         Specular[4];
00124 
00125         CShaderGL(IDriver *drv, ItShaderPtrList it) : IShader(drv, it) {}
00126 };
00127 
00128 // --------------------------------------------------
00129 
00130 class CDriverGL : public IDriver
00131 {
00132 public:
00133 
00134         // Some constants
00135         enum { MaxLight=8 };
00136 
00137         // Acces
00138         uint32                                  getHwnd ()
00139         {
00140 #ifdef NL_OS_WINDOWS
00141                 return (uint32)_hWnd;
00142 #else // NL_OS_WINDOWS
00143                 return 0;
00144 #endif // NL_OS_WINDOWS
00145         }
00146 
00147                                                         CDriverGL();
00148         virtual                                 ~CDriverGL() { release(); };
00149 
00150         virtual bool                    init();
00151 
00152         virtual ModeList                enumModes();
00153 
00154         virtual bool                    setDisplay(void* wnd, const GfxMode& mode) throw(EBadDisplay);
00155 
00156         virtual void*                   getDisplay()
00157         {
00158 #ifdef NL_OS_WINDOWS
00159                 return (void*)_hWnd;
00160 #else // NL_OS_WINDOWS
00161                 return NULL;
00162 #endif // NL_OS_WINDOWS
00163         }
00164 
00165         virtual bool                    activate();
00166 
00167         virtual sint                    getNbTextureStages() {return _Extensions.NbTextureStages;}
00168 
00169         virtual bool                    isTextureExist(const ITexture&tex);
00170 
00171         virtual NLMISC::IEventEmitter   *getEventEmitter() { return&_EventEmitter; };
00172 
00173         virtual bool                    clear2D(CRGBA rgba);
00174 
00175         virtual bool                    clearZBuffer(float zval=1);
00176 
00177         virtual bool                    setupTexture(ITexture& tex);
00178 
00179         virtual bool                    setupMaterial(CMaterial& mat);
00180 
00181         virtual void                    setFrustum(float left, float right, float bottom, float top, float znear, float zfar, bool perspective = true);
00182 
00183         virtual void                    setupViewMatrix(const CMatrix& mtx);
00184 
00185         virtual void                    setupModelMatrix(const CMatrix& mtx, uint8 n=0);
00186 
00187         virtual CMatrix                 getViewMatrix() const;
00188 
00189         virtual void                    setupVertexMode(uint vmode)
00190         {
00191                 _VertexMode= vmode;
00192         }
00193 
00194         virtual bool                    activeVertexBuffer(CVertexBuffer& VB);
00195 
00196         virtual bool                    activeVertexBuffer(CVertexBuffer& VB, uint first, uint end);
00197 
00198         virtual bool                    render(CPrimitiveBlock& PB, CMaterial& Mat);
00199 
00200         virtual void                    renderTriangles(CMaterial& Mat, uint32 *tri, uint32 ntris);
00201 
00202         virtual void                    renderPoints(CMaterial& Mat, uint32 numPoints) ;
00203 
00204 
00205         virtual bool                    swapBuffers();
00206 
00207         virtual uint                    getNumMatrix();
00208 
00209         virtual bool                    supportPaletteSkinning();
00210 
00211         virtual bool                    release();
00212 
00213         virtual TMessageBoxId   systemMessageBox (const char* message, const char* title, TMessageBoxType type=okType, TMessageBoxIcon icon=noIcon);
00214 
00215         virtual void                    setupScissor (const class CViewport& viewport);
00216 
00217         virtual void                    setupViewport (const class CViewport& viewport);
00218 
00219         virtual uint32                  getImplementationVersion () const
00220         {
00221                 return ReleaseVersion;
00222         }
00223 
00224         virtual const char*             getDriverInformation ()
00225         {
00226                 return "Opengl 1.2 NeL Driver";
00227         }
00228 
00229         virtual const char*             getVideocardInformation ();
00230 
00231         virtual bool                    isActive ();
00232 
00233         virtual void                    showCursor (bool b);
00234 
00235         // between 0.0 and 1.0
00236         virtual void setMousePos(float x, float y);
00237 
00238         virtual void                    setCapture (bool b);
00239 
00240         virtual void                    getWindowSize (uint32 &width, uint32 &height);
00241 
00242         virtual void                    getBuffer (CBitmap &bitmap);
00243 
00244         virtual void                    getZBuffer (std::vector<float>  &zbuffer);
00245 
00246         virtual void                    getBufferPart (CBitmap &bitmap, NLMISC::CRect &rect);
00247 
00248         virtual void                    getZBufferPart (std::vector<float>  &zbuffer, NLMISC::CRect &rect);
00249 
00250         virtual void                    setPolygonMode (TPolygonMode mode);
00251 
00252         virtual void                    setLight (uint8 num, const CLight& light);
00253 
00254         virtual void                    enableLight (uint8 num, bool enable=true);
00255 
00256         virtual void                    setAmbientColor (CRGBA color);
00257 
00259         // @{
00260         virtual bool                    fogEnabled();
00261         virtual void                    enableFog(bool enable);
00263         virtual void                    setupFog(float start, float end, CRGBA color);
00264         // @}
00265 
00266 
00267 private:
00268 
00269         // For fast vector/point multiplication.
00270         struct  CMatrix3x4
00271         {
00272                 // Order them in memory line first, for faster memory access.
00273                 float   a11, a12, a13, a14;
00274                 float   a21, a22, a23, a24;
00275                 float   a31, a32, a33, a34;
00276 
00277                 // Copy from a matrix.
00278                 void    set(const CMatrix &mat);
00279                 // mulAddvector. NB: in should be different as v!! (else don't work).
00280                 void    mulAddVector(const CVector &in, float scale, CVector &out)
00281                 {
00282                         out.x+= (a11*in.x + a12*in.y + a13*in.z) * scale;
00283                         out.y+= (a21*in.x + a22*in.y + a23*in.z) * scale;
00284                         out.z+= (a31*in.x + a32*in.y + a33*in.z) * scale;
00285                 }
00286                 // mulAddpoint. NB: in should be different as v!! (else don't work).
00287                 void    mulAddPoint(const CVector &in, float scale, CVector &out)
00288                 {
00289                         out.x+= (a11*in.x + a12*in.y + a13*in.z + a14) * scale;
00290                         out.y+= (a21*in.x + a22*in.y + a23*in.z + a24) * scale;
00291                         out.z+= (a31*in.x + a32*in.y + a33*in.z + a34) * scale;
00292                 }
00293         };
00294 
00295 
00296 private:
00297         // Version of the driver. Not the interface version!! Increment when implementation of the driver change.
00298         static const uint32             ReleaseVersion;
00299 
00300         bool                                    _FullScreen;
00301 
00302 #ifdef NL_OS_WINDOWS
00303 
00304         friend static LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam);
00305         
00306         HWND                                            _hWnd;
00307         HDC                                                     _hDC;
00308         PIXELFORMATDESCRIPTOR           _pfd;
00309     HGLRC                                               _hRC;
00310         static uint                                     _Registered;
00311         DEVMODE                                         _OldScreenMode;
00312         NLMISC::CWinEventEmitter        _EventEmitter;
00313 
00314 #elif defined (NL_OS_UNIX)
00315 
00316         Display                                         *dpy;
00317         GLXContext                                      ctx;
00318         Window                                          win;
00319         Cursor                                          cursor;
00320         NLMISC::CUnixEventEmitter       _EventEmitter;
00321 
00322 #ifdef XF86VIDMODE
00323         int                                             _OldDotClock;   // old dotclock
00324         XF86VidModeModeLine             _OldScreenMode; // old modeline
00325         int                                             _OldX, _OldY;   //Viewport settings
00326 #endif //XF86VIDMODE
00327 
00328 #endif // NL_OS_UNIX
00329 
00330         bool                                    _Initialized;
00331 
00333         // @{
00334         // OpenGL extensions Extensions.
00335         CGlExtensions                   _Extensions;
00336         // Say if palette skinning can be done in Hardware
00337         bool                                    _PaletteSkinHard;
00338         // @}
00339 
00340 
00341         // The vertex transform mode.
00342         uint                                    _VertexMode;
00343 
00344         // To know if matrix setup has been changed from last activeVertexBuffer() (any call to setupViewMatrix() / setupModelMatrix()).
00345         bool                                    _MatrixSetupDirty;
00346 
00347         // To know if view matrix setup has been changed from last activeVertexBuffer() (any call to setupViewMatrix()).
00348         bool                                    _ViewMatrixSetupDirty;
00349 
00350         // for each model matrix, a flag to know if setuped.
00351         NLMISC::CBitSet                 _ModelViewMatrixDirty;
00352         // same flag, but for palette Skinning (because they don't share same setup).
00353         NLMISC::CBitSet                 _ModelViewMatrixDirtyPaletteSkin;
00354 
00355 
00356         // Current (OpenGL basis) View matrix.
00357         CMatrix                                 _ViewMtx;
00358 
00359         // Current computed (OpenGL basis) ModelView matrix.
00360         CMatrix                                 _ModelViewMatrix[MaxModelMatrix];
00361         // For software skinning.
00362         CMatrix                                 _ModelViewMatrixNormal[MaxModelMatrix];
00363         CMatrix3x4                              _ModelViewMatrix3x4[MaxModelMatrix];
00364         CMatrix3x4                              _ModelViewMatrixNormal3x4[MaxModelMatrix];
00365 
00366 
00367         // Sofware Skinning.
00368         uint8                                   *_CurrentSoftSkinFlags;
00369         uint8                                   *_CurrentSoftSkinSrc;
00370         uint                                    _CurrentSoftSkinNormalOff;
00371         uint                                    _CurrentSoftSkinPaletteSkinOff;
00372         uint                                    _CurrentSoftSkinWeightOff;
00373         uint                                    _CurrentSoftSkinSrcStride;
00374         uint                                    _CurrentSoftSkinFirst;
00375         uint                                    _CurrentSoftSkinEnd;
00376         CVector                                 *_CurrentSoftSkinVectorDst;
00377         CVector                                 *_CurrentSoftSkinNormalDst;
00378 
00379         // Fog.
00380         bool                                    _FogEnabled;
00381 
00382         // Num lights return by GL_MAX_LIGHTS
00383         uint                                            _MaxDriverLight;
00384         bool                                            _LightEnable[MaxLight];                         // Light enable.
00385         uint                                            _LightMode[MaxLight];                           // Light mode.
00386         CVector                                         _WorldLightPos[MaxLight];                       // World position of the lights.
00387         CVector                                         _WorldLightDirection[MaxLight];         // World direction of the lights.
00388 
00389         // Prec settings, for optimisation.
00390         ITexture*                               _CurrentTexture[IDRV_MAT_MAXTEXTURES];
00391         CMaterial*                              _CurrentMaterial;
00392         CMaterial::CTexEnv              _CurrentTexEnv[IDRV_MAT_MAXTEXTURES];
00393         bool                                    _CurrentNormalize;
00394 
00395 private:
00396         bool                                    setupVertexBuffer(CVertexBuffer& VB);
00397         bool                                    activateTexture(uint stage, ITexture *tex);
00398         void                                    activateTexEnvMode(uint stage, const CMaterial::CTexEnv  &env);
00399         void                                    activateTexEnvColor(uint stage, const CMaterial::CTexEnv  &env);
00400 
00401         // Called by activeVertexBuffer when _ViewMatrixSetupDirty is true to clean the view matrix.
00402         // set _ViewMatrixSetupDirty to false;
00403         void                                    cleanViewMatrix ();
00404 
00405         // According to extensions, retrieve GL tex format of the texture.
00406         GLint                                   getGlTextureFormat(ITexture& tex, bool &compressed);
00407 
00408 
00409         // Clip the wanted rectangle with window. return true if rect is not NULL.
00410         bool                                    clipRect(NLMISC::CRect &rect);
00411 
00412 
00413         // software skinning. Use _CurrentSoftSkin* global setup.
00414         void                    computeSoftwareVertexSkinning(uint8 *pSrc, CVector *pVertexDst);
00415         void                    computeSoftwareNormalSkinning(uint8 *pSrc, CVector *pNormalDst);
00416         void                    refreshSoftwareSkinning();
00417 
00418 
00420         // @{
00422         sint                    beginMultiPass(const CMaterial &mat);
00424         void                    setupPass(const CMaterial &mat, uint pass);
00426         void                    endMultiPass(const CMaterial &mat);
00428         CVertexBuffer   *_LastVB;
00429         // @}
00430 
00431 
00433         void            setupUVPtr(uint stage, CVertexBuffer &VB, uint uvId);
00434 
00435 };
00436 
00437 } // NL3D
00438 
00439 #endif // NL_OPENGL_H