include irrlicht irrlicht include irrlicht driverChoice include Realis

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
#include <irrlicht/irrlicht.h>
#include <irrlicht/driverChoice.h>
#include "RealisticWater.h"
using namespace irr;
#ifdef _MSC_VER
#pragma comment(lib, "Irrlicht.lib")
#endif
using namespace irr;
using namespace core;
using namespace scene;
using namespace video;
class MyEventReceiver : public IEventReceiver
{
public:
MyEventReceiver(scene::ISceneNode* terrain, scene::ISceneNode* skybox, scene::ICameraSceneNode* camera) :
Terrain(terrain), Skybox(skybox), showBox(true), Camera(camera), showDebug(false)
{
Skybox->setVisible(showBox);
}
bool OnEvent(const SEvent& event)
{
// check if user presses the key 'W' or 'D'
if (event.EventType == irr::EET_KEY_INPUT_EVENT && !event.KeyInput.PressedDown)
{
switch (event.KeyInput.Key)
{
case irr::KEY_KEY_W: // switch wire frame mode
Terrain->setMaterialFlag(video::EMF_WIREFRAME,
!Terrain->getMaterial(0).Wireframe);
Terrain->setMaterialFlag(video::EMF_POINTCLOUD, false);
return true;
case irr::KEY_KEY_P: // switch wire frame mode
Terrain->setMaterialFlag(video::EMF_POINTCLOUD,
!Terrain->getMaterial(0).PointCloud);
Terrain->setMaterialFlag(video::EMF_WIREFRAME, false);
return true;
case irr::KEY_KEY_D: // toggle detail map
Terrain->setMaterialType(
Terrain->getMaterial(0).MaterialType == video::EMT_SOLID ?
video::EMT_DETAIL_MAP : video::EMT_SOLID);
return true;
case irr::KEY_KEY_X: // toggle debug information
showDebug=!showDebug;
Terrain->setDebugDataVisible(showDebug?scene::EDS_BBOX_ALL:scene::EDS_OFF);
return true;
default:
break;
}
}
return false;
}
private:
scene::ISceneNode* Terrain;
scene::ISceneNode* Skybox;
scene::ICameraSceneNode* Camera;
bool showBox;
bool showDebug;
};
int main()
{
// ask user for driver
video::E_DRIVER_TYPE driverType=EDT_OPENGL; //driverChoiceConsole();
if (driverType==video::EDT_COUNT)
return 1;
// create device with full flexibility over creation parameters
// you can add more parameters if desired, check irr::SIrrlichtCreationParameters
irr::SIrrlichtCreationParameters params;
params.DriverType=driverType;
params.Stencilbuffer = true;
params.WindowSize=core::dimension2d<u32>(640, 480);
IrrlichtDevice* device = createDeviceEx(params);
if (device == 0)
return 1; // could not create selected driver.
video::IVideoDriver* driver = device->getVideoDriver();
scene::ISceneManager* smgr = device->getSceneManager();
gui::IGUIEnvironment* env = device->getGUIEnvironment();
driver->setTextureCreationFlag(video::ETCF_ALWAYS_32_BIT, true);
// // add irrlicht logo
// env->addImage(driver->getTexture("data/irrlichtlogo2.png"),
// core::position2d<s32>(10,10));
//set other font
env->getSkin()->setFont(env->getFont("data/fontlucida.png"));
// add some help text
env->addStaticText(
L"Press 'W' to change wireframe mode\nPress 'D' to toggle detail map\nPress 'S' to toggle skybox/skydome",
core::rect<s32>(10,421,250,475), true, true, 0, -1, true);
// add camera
scene::ICameraSceneNode* camera =
smgr->addCameraSceneNodeFPS(0,
100.0f,
1.2f,
-1,
0,
0,
false,
3.f
);
camera->setPosition(core::vector3df(2700*2,255*2,2600*2));
camera->setTarget(core::vector3df(2397*2,343*2,2700*2));
camera->setFarValue(42000.0f);
// disable mouse cursor
device->getCursorControl()->setVisible(false);
// add terrain scene node
scene::ITerrainSceneNode* terrain = smgr->addTerrainSceneNode(
"data/terrain-heightmap.bmp",
0, // parent node
-1, // node id
core::vector3df(0.f, 0.f, 0.f), // position
core::vector3df(0.f, 0.f, 0.f), // rotation
core::vector3df(40.f, 4.4f, 40.f), // scale
video::SColor ( 255, 255, 255, 255 ), // vertexColor
5, // maxLOD
scene::ETPS_17, // patchSize
4 // smoothFactor
);
terrain->setMaterialFlag(video::EMF_LIGHTING, false);
terrain->setMaterialTexture(0,
driver->getTexture("data/terrain-texture.jpg"));
terrain->setMaterialTexture(1,
driver->getTexture("data/detailmap3.jpg"));
terrain->setMaterialType(video::EMT_DETAIL_MAP);
terrain->scaleTexture(1.0f, 20.0f);
scene::ITriangleSelector* selector
= smgr->createTerrainTriangleSelector(terrain, 0);
terrain->setTriangleSelector(selector);
scene::CDynamicMeshBuffer* buffer = new scene::CDynamicMeshBuffer(video::EVT_2TCOORDS, video::EIT_16BIT);
terrain->getMeshBufferForLOD(*buffer, 0);
video::S3DVertex2TCoords* data = (video::S3DVertex2TCoords*)buffer->getVertexBuffer().getData();
// Work on data or get the IndexBuffer with a similar call.
buffer->drop(); // When done drop the buffer again.
scene::ISceneNodeAnimator* anim = smgr->createCollisionResponseAnimator(
selector, camera, core::vector3df(60,100,60),
core::vector3df(0,-10,0), // gravi
core::vector3df(0,50,0));
selector->drop();
camera->addAnimator(anim);
anim->drop();
// create skybox and skydome
driver->setTextureCreationFlag(video::ETCF_CREATE_MIP_MAPS, false);
scene::ISceneNode* skybox=smgr->addSkyBoxSceneNode(
driver->getTexture("data/irrlicht2_up.jpg"),
driver->getTexture("data/irrlicht2_dn.jpg"),
driver->getTexture("data/irrlicht2_lf.jpg"),
driver->getTexture("data/irrlicht2_rt.jpg"),
driver->getTexture("data/irrlicht2_ft.jpg"),
driver->getTexture("data/irrlicht2_bk.jpg"));
driver->setTextureCreationFlag(video::ETCF_CREATE_MIP_MAPS, true);
// create event receiver
MyEventReceiver receiver(terrain, skybox, camera);
device->setEventReceiver(&receiver);
int lastFPS = -1;
smgr->setAmbientLight(video::SColorf(0.8,0.8,0.8,1));
scene::IAnimatedMesh* plane = smgr->addHillPlaneMesh("plane", // Name of mesh
core::dimension2d<f32>(20,20), // Size of a tile of the mesh. (10.0f, 10.0f) would be a good value to start, for example.
core::dimension2d<u32>(40,40), 0, 0, // Specifies how much tiles there will be. If you specifiy for example that a tile has the size (10.0f, 10.0f) and the tileCount is (10,10), than you get a field of 100 tiles which has the dimension 100.0fx100.0f.
core::dimension2d<f32>(0,0), //material
core::dimension2d<f32>(10,10)); //countHills
// virtual ISceneNode * addWaterSurfaceSceneNode (IMesh *mesh, f32 waveHeight=2.0f, f32 waveSpeed=300.0f, f32 waveLength=10.0f, ISceneNode *parent=0, s32 id=-1, const core::vector3df &position=core::vector3df(0, 0, 0), const core::vector3df &rotation=core::vector3df(0, 0, 0), const core::vector3df &scale=core::vector3df(1.0f, 1.0f, 1.0f))=0
/* scene::ISceneNode* sea = new RealisticWaterSceneNode(smgr,409600, 409600, "", irr::core::dimension2du(0,0),
smgr->getRootSceneNode());*/
scene::ISceneNode* sea = smgr->addWaterSurfaceSceneNode(plane->getMesh(0),
0.5f, //Wave height
200.0f, //Wave speed
5.0f, //Wavelen
0, -1, core::vector3df(5000.f, 500.f, 5000.f), //POS
core::vector3df(0.f, 0.f, 0.f), // ROTATION
core::vector3df(12.f, 12.f, 12.f)// SCALE
);
video::ITexture *water = driver->getTexture("data/water.jpg");
// sea->setMaterialTexture(1, driver->getTexture("data/stones.jpg"));
sea->setMaterialTexture(0, water);
sea->setMaterialFlag(EMF_LIGHTING, true);
sea->setMaterialType(video::EMT_TRANSPARENT_ADD_COLOR);
// sea->setPosition( core::vector3df(0.f, 35.f, 0.f) );
// if(nodePosition.Z > 1024 || nodePosition.Z<0)
// nodePosition.Z = 0 ;
// if(nodePosition.X > 1024 || nodePosition.X<0)
// nodePosition.X = 0 ;
while(device->run())
if (device->isWindowActive())
{
driver->beginScene(true, true, 0 );
smgr->drawAll();
env->drawAll();
driver->endScene();
// display frames per second in window title
int fps = driver->getFPS();
core::vector3df nodePosition = camera->getPosition();
// if(nodePosition.Z > 1024 || nodePosition.Z<0)
// nodePosition.Z = 0 ;
// if(nodePosition.X > 1024 || nodePosition.X<0)
// nodePosition.X = 0 ;
camera->setPosition(nodePosition);
//if (lastFPS != fps)
{
core::stringw str = L"[ pos x:";
str+= nodePosition.X;
str+=" z: ";
str+=nodePosition.Z;
str += driver->getName();
str += "] FPS:";
str += fps;
// Also print terrain height of current camera position
// We can use camera position because terrain is located at coordinate origin
str += " Height: ";
str += terrain->getHeight(camera->getAbsolutePosition().X,
camera->getAbsolutePosition().Z);
device->setWindowCaption(str.c_str());
lastFPS = fps;
}
}
device->drop();
return 0;
}