1 // NeL - MMORPG Framework <http://dev.ryzom.com/projects/nel/>
2 // Copyright (C) 2010 Winch Gate Property Limited
4 // This source file has been modified by the following contributors:
5 // Copyright (C) 2014 Jan BOON (Kaetemi) <jan.boon@kaetemi.be>
7 // This program is free software: you can redistribute it and/or modify
8 // it under the terms of the GNU Affero General Public License as
9 // published by the Free Software Foundation, either version 3 of the
10 // License, or (at your option) any later version.
12 // This program is distributed in the hope that it will be useful,
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 // GNU Affero General Public License for more details.
17 // You should have received a copy of the GNU Affero General Public License
18 // along with this program. If not, see <http://www.gnu.org/licenses/>.
25 #include <nel/misc/path.h>
26 #include <nel/misc/file.h>
27 #include <nel/misc/common.h>
28 #include <nel/3d/u_scene.h>
29 #include <nel/3d/u_light.h>
30 #include <nel/3d/u_camera.h>
31 #include <nel/3d/u_driver.h>
32 #include <nel/3d/u_instance.h>
39 #endif // NL_OS_WINDOWS
41 using namespace NLMISC
;
44 // function to split a string into several substrings delimited by specified characters
45 void split(const std::string
&str
, std::vector
<std::string
> &tokens
, const std::string
&delimiters
)
47 // Skip delimiters at beginning.
48 std::string::size_type lastPos
= str
.find_first_not_of(delimiters
, 0);
49 // Find first "non-delimiter".
50 std::string::size_type pos
= str
.find_first_of(delimiters
, lastPos
);
52 while (std::string::npos
!= pos
|| std::string::npos
!= lastPos
)
54 // Found a token, add it to the vector.
55 tokens
.push_back(str
.substr(lastPos
, pos
- lastPos
));
56 // Skip delimiters. Note the "not_of"
57 lastPos
= str
.find_first_not_of(delimiters
, pos
);
58 // Find next "non-delimiter"
59 pos
= str
.find_first_of(delimiters
, lastPos
);
64 sint WINAPI
WinMain(HINSTANCE
/* hInstance */, HINSTANCE
/* hPrevInstance */, LPSTR cmdline
, int /* nCmdShow */)
66 // we can specify several shapes on command line
67 // so we need to process it
68 std::vector
<std::string
> argv
;
70 // argv[0] is the fullpath to the executable but since it's not used, we can let it empty
73 // split the whole cmdline into args
74 split(cmdline
, argv
, " ");
77 int argc
= (int)argv
.size();
80 sint
main(int argc
, char **argv
)
83 NLMISC::CApplicationContext myApplicationContext
;
87 // create OpenGL driver
88 NL3D::UDriver
*Driver
= UDriver::createDriver();
91 // create a window in 800x600
92 Driver
->setDisplay(UDriver::CMode(800, 600, 32, true));
95 Driver
->setWindowTitle(ucstring("NeL shape viewer"));
97 // can use both dds and tga textures for shapes
98 CPath::remapExtension ("dds", "tga", true);
101 ULight
*Light
= ULight::createLight();
104 // set mode of the light
105 Light
->setMode(ULight::DirectionalLight
);
107 // set position of the light
108 Light
->setPosition(CVector(-20.f
, 30.f
, 10.f
));
111 Light
->setAmbiant(CRGBA(255, 255, 255));
113 // set and enable the light
114 Driver
->setLight(0, *Light
);
115 Driver
->enableLight(0);
118 NL3D::UScene
*Scene
= Driver
->createScene(true);
122 UCamera Camera
= Scene
->getCam();
123 if (Camera
.empty()) throw 5;
125 Camera
.setTransformMode (UTransformable::DirectMatrix
);
126 Camera
.setPerspective ((float)Pi
/2.f
, 1.33f
, 0.1f
, 1000);
128 // camera will look at entities
129 Camera
.lookAt (CVector(-10.f
, 10.f
, 0.f
), CVector(0.f
, 0.f
, 0.f
));
132 std::vector
<UInstance
> Entities
;
134 // create each entity
135 for(int i
= 1; i
< argc
; ++i
)
137 // use the path of the shape to find its textures
138 CPath::addSearchPath(CFile::getPath(argv
[i
]), true, false);
140 // add an entity to the scene
141 UInstance Entity
= Scene
->createInstance(argv
[i
]);
143 // if we can't create entity, skip it
144 if (Entity
.empty()) continue;
146 // we will rotate it later so use Euler rotation transform mode
147 Entity
.setTransformMode(UTransformable::RotEuler
);
149 // add entity to the vector
150 Entities
.push_back(Entity
);
157 while (Driver
->isActive())
159 Driver
->EventServer
.pump();
161 // the background is black
162 Driver
->clearBuffers(CRGBA(0, 0, 0));
164 // increase the angle
167 if (angle
>= NLMISC::Pi
*2) angle
= 0.f
;
169 // rotate all entities
170 for(size_t i
= 0; i
< Entities
.size(); ++i
)
172 Entities
[i
].setRotEuler(0.f
, 0.f
, angle
);
176 Scene
->animate(NLMISC::CTime::getLocalTime() / 1000.0);
182 Driver
->swapBuffers();
184 // escape will leave the program
185 if (Driver
->AsyncListener
.isKeyPushed(KeyESCAPE
))
189 // F3 will change the render mode
190 else if (Driver
->AsyncListener
.isKeyPushed(KeyF3
))
192 UDriver::TPolygonMode p
= Driver
->getPolygonMode();
193 p
= UDriver::TPolygonMode(((int)p
+1)%3);
194 Driver
->setPolygonMode(p
);
196 // F12 will take a screenshot
197 else if (Driver
->AsyncListener
.isKeyPushed(KeyF12
))
200 Driver
->getBuffer(btm
);
201 COFile
fs(CFile::findNewFile("screenshot.png"));
206 // we are leaving the program
208 // delete all entities
209 for(size_t i
= 0; i
< Entities
.size(); ++i
)
211 Scene
->deleteInstance(Entities
[i
]);
215 Driver
->deleteScene(Scene
);
220 // release all textures and others elements