Merge branch 'main/rendor-staging' into fixes
[ryzomcore.git] / nel / src / 3d / nelu.cpp
blob7807a5ef75b8ad5c3afbce84ab9a59cf057c06ce
1 // NeL - MMORPG Framework <http://dev.ryzom.com/projects/nel/>
2 // Copyright (C) 2010 Winch Gate Property Limited
3 //
4 // This program is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU Affero General Public License as
6 // published by the Free Software Foundation, either version 3 of the
7 // License, or (at your option) any later version.
8 //
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU Affero General Public License for more details.
14 // You should have received a copy of the GNU Affero General Public License
15 // along with this program. If not, see <http://www.gnu.org/licenses/>.
17 #include "std3d.h"
19 #include "nel/misc/path.h"
20 #include "nel/misc/file.h"
22 #include "nel/3d/nelu.h"
23 #include "nel/3d/dru.h"
24 #include "nel/3d/camera.h"
25 #include "nel/3d/register_3d.h"
26 #include "nel/3d/init_3d.h"
27 #include "nel/3d/vertex_stream_manager.h"
28 #include "nel/misc/debug.h"
30 using namespace std;
31 using namespace NLMISC;
33 #ifdef DEBUG_NEW
34 #define new DEBUG_NEW
35 #endif
37 namespace NL3D
40 const float CNELU::DefLx=0.26f;
41 const float CNELU::DefLy=0.2f;
42 const float CNELU::DefLzNear=0.15f;
43 const float CNELU::DefLzFar=1000.0f;
45 IDriver *CNELU::Driver=NULL;
46 CScene *CNELU::Scene=NULL;
47 CShapeBank *CNELU::ShapeBank=NULL;
48 CVertexStreamManager*CNELU::MeshSkinManager=NULL;
49 CRefPtr<CCamera> CNELU::Camera;
50 CEventServer CNELU::EventServer;
51 CEventListenerAsync CNELU::AsyncListener;
54 bool CNELU::initDriver (uint w, uint h, uint bpp, bool windowed, nlWindow systemWindow, bool offscreen, bool direct3d)
56 // Init debug system
57 // NLMISC::InitDebug();
59 ShapeBank = new CShapeBank;
61 CNELU::Driver = NULL;
63 // Init driver.
64 #ifdef NL_OS_WINDOWS
65 if (direct3d)
67 CNELU::Driver= CDRU::createD3DDriver();
69 #endif
71 if (!CNELU::Driver)
73 CNELU::Driver= CDRU::createGlDriver();
76 if (!CNELU::Driver)
78 nlwarning ("CNELU::initDriver: no driver found");
79 return false;
82 if (!CNELU::Driver->init())
84 nlwarning ("CNELU::initDriver: init() failed");
85 return false;
87 if (!CNELU::Driver->setDisplay(systemWindow, GfxMode(uint16(w), uint16(h), uint8(bpp), windowed, offscreen)))
89 nlwarning ("CNELU::initDriver: setDisplay() failed");
90 return false;
92 if (!CNELU::Driver->activate())
94 nlwarning ("CNELU::initDriver: activate() failed");
95 return false;
98 // Create a skin manager
99 MeshSkinManager = new CVertexStreamManager;
101 return true;
105 void CNELU::initScene(CViewport viewport)
107 // Register basic csene.
108 CScene::registerBasics();
110 if (CNELU::Scene == NULL)
111 CNELU::Scene = new CScene(false);
113 // init default Roots.
114 CNELU::Scene->initDefaultRoots();
116 // Set driver.
117 CNELU::Scene->setDriver(CNELU::Driver);
119 // Set viewport
120 CNELU::Scene->setViewport (viewport);
122 // init QuadGridClipManager
123 CNELU::Scene->initQuadGridClipManager ();
125 // Create/link a camera.
126 CNELU::Camera= (CCamera*)CNELU::Scene->createModel(NL3D::CameraId);
127 CNELU::Scene->setCam(CNELU::Camera);
128 CNELU::Camera->setFrustum(DefLx, DefLy, DefLzNear, DefLzFar);
130 // Link to the shape bank
131 CNELU::Scene->setShapeBank(CNELU::ShapeBank);
133 // set the MeshSkin Vertex Streams
134 CNELU::Scene->getRenderTrav().setMeshSkinManager(MeshSkinManager);
138 void CNELU::initEventServer()
140 CNELU::AsyncListener.reset ();
141 CNELU::EventServer.addEmitter(CNELU::Driver->getEventEmitter());
142 CNELU::AsyncListener.addToServer(CNELU::EventServer);
146 void CNELU::releaseEventServer()
148 CNELU::AsyncListener.removeFromServer(CNELU::EventServer);
149 if (CNELU::Driver != NULL)
151 CNELU::EventServer.removeEmitter(CNELU::Driver->getEventEmitter());
156 void CNELU::releaseScene()
158 // Release the camera.
159 CNELU::Camera= NULL;
161 // "Release" the Scene.
162 CNELU::Scene->setDriver(NULL);
163 CNELU::Scene->release();
167 void CNELU::releaseDriver()
169 if (MeshSkinManager)
170 delete MeshSkinManager;
172 // "Release" the driver.
173 if (CNELU::Driver != NULL)
175 CNELU::Driver->release();
176 delete CNELU::Driver;
177 CNELU::Driver = NULL;
179 if( CNELU::ShapeBank != NULL )
181 delete CNELU::ShapeBank;
182 CNELU::ShapeBank = NULL;
186 bool CNELU::init (uint w, uint h, CViewport viewport, uint bpp, bool windowed, nlWindow systemWindow, bool offscreen, bool direct3d)
188 NL3D::registerSerial3d();
189 if (initDriver(w,h,bpp,windowed,systemWindow,offscreen,direct3d))
191 initScene(viewport);
192 initEventServer();
193 return true;
195 else
196 return false;
199 void CNELU::release()
201 releaseEventServer();
202 releaseScene();
203 releaseDriver();
206 void CNELU::screenshot()
208 if (AsyncListener.isKeyPushed(KeyF12))
210 CBitmap btm;
211 CNELU::Driver->getBuffer(btm);
212 string filename = CFile::findNewFile ("screenshot.tga");
213 COFile fs(filename);
214 btm.writeTGA (fs,24);
215 nlinfo("Screenshot '%s' saved", filename.c_str());
220 void CNELU::clearBuffers(CRGBA col)
222 CNELU::Driver->clear2D(col);
223 CNELU::Driver->clearZBuffer();
226 void CNELU::swapBuffers()
228 CNELU::Driver->swapBuffers();
233 } // NL3D