00001
00007 00008 00009 00010 00011 00012 00013 00014 00015 00016 00017 00018 00019 00020 00021 00022 00023 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
00079 GLuint ID;
00080
00081 bool Compressed;
00082
00083
00084 ITexture::TWrapMode WrapS;
00085 ITexture::TWrapMode WrapT;
00086 ITexture::TMagFilter MagFilter;
00087 ITexture::TMinFilter MinFilter;
00088
00089
00090 CTextureDrvInfosGL(IDriver *drv, ItTexDrvInfoPtrMap it);
00091
00092 ~CTextureDrvInfosGL();
00093 };
00094
00095
00096
00097
00098
00099 class CVBDrvInfosGL : public IVBDrvInfos
00100 {
00101 public:
00102
00103 std::vector<CVector> SoftSkinVertices;
00104 std::vector<CVector> SoftSkinNormals;
00105
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
00135 enum { MaxLight=8 };
00136
00137
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
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
00270 struct CMatrix3x4
00271 {
00272
00273 float a11, a12, a13, a14;
00274 float a21, a22, a23, a24;
00275 float a31, a32, a33, a34;
00276
00277
00278 void set(const CMatrix &mat);
00279
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
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
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;
00324 XF86VidModeModeLine _OldScreenMode;
00325 int _OldX, _OldY;
00326 #endif //XF86VIDMODE
00327
00328 #endif // NL_OS_UNIX
00329
00330 bool _Initialized;
00331
00333
00334
00335 CGlExtensions _Extensions;
00336
00337 bool _PaletteSkinHard;
00338
00339
00340
00341
00342 uint _VertexMode;
00343
00344
00345 bool _MatrixSetupDirty;
00346
00347
00348 bool _ViewMatrixSetupDirty;
00349
00350
00351 NLMISC::CBitSet _ModelViewMatrixDirty;
00352
00353 NLMISC::CBitSet _ModelViewMatrixDirtyPaletteSkin;
00354
00355
00356
00357 CMatrix _ViewMtx;
00358
00359
00360 CMatrix _ModelViewMatrix[MaxModelMatrix];
00361
00362 CMatrix _ModelViewMatrixNormal[MaxModelMatrix];
00363 CMatrix3x4 _ModelViewMatrix3x4[MaxModelMatrix];
00364 CMatrix3x4 _ModelViewMatrixNormal3x4[MaxModelMatrix];
00365
00366
00367
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
00380 bool _FogEnabled;
00381
00382
00383 uint _MaxDriverLight;
00384 bool _LightEnable[MaxLight];
00385 uint _LightMode[MaxLight];
00386 CVector _WorldLightPos[MaxLight];
00387 CVector _WorldLightDirection[MaxLight];
00388
00389
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
00402
00403 void cleanViewMatrix ();
00404
00405
00406 GLint getGlTextureFormat(ITexture& tex, bool &compressed);
00407
00408
00409
00410 bool clipRect(NLMISC::CRect &rect);
00411
00412
00413
00414 void computeSoftwareVertexSkinning(uint8 *pSrc, CVector *pVertexDst);
00415 void computeSoftwareNormalSkinning(uint8 *pSrc, CVector *pNormalDst);
00416 void refreshSoftwareSkinning();
00417
00418
00420 00422 00424 00426 00428
00429
00430
00431
00433 void setupUVPtr(uint stage, CVertexBuffer &VB, uint uvId);
00434
00435 };
00436
00437 }
00438
00439 #endif // NL_OPENGL_H