2 * Terrain, weather, lights (Horde3D renderer)
4 * Copyright (C) 2009 Mathias Gottschlag
6 * This file is part of OpenStranded
8 * OpenStranded is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation, either version 3 of the License, or
11 * (at your option) any later version.
13 * OpenStranded is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with OpenStranded. If not, see <http://www.gnu.org/licenses/>.
22 #include "eal/horde3d/environment.hh"
23 #include "eal/horde3d/engine.hh"
24 #include "eal/horde3d/extensions/extensions.hh"
25 #include "eal/horde3d/camera.hh"
27 #include <horde3d/Horde3D.h>
28 #include <horde3d/Horde3DUtils.h>
34 Horde3DEnvironment::Horde3DEnvironment()
42 Horde3DEnvironment::~Horde3DEnvironment()
47 Horde3DEnvironment::setSize(int size
)
58 Horde3DEnvironment::getSize()
64 Horde3DEnvironment::setHeight (const Vector2
&pos
, double height
)
73 Horde3DEnvironment::getHeight (const Vector2
&pos
)
82 Horde3DEnvironment::setHeightData(float *heightdata
)
87 float *currentdata
= horde3d::Island::getData(terrain
);
88 for (int i
= 0; i
< (terrainsize
+ 1) * (terrainsize
+ 1); i
++)
90 currentdata
[i
] = heightdata
[i
];
92 horde3d::Island::invalidate(terrain
, 0, 0, terrainsize
+ 1, terrainsize
+ 1);
96 Horde3DEnvironment::setColor (const Vector2
&pos
, const Color
&color
)
105 Horde3DEnvironment::getColor (const Vector2
&pos
)
109 void Horde3DEnvironment::setColorMap (unsigned char *colormap
, int size
)
114 // Convert color map to 32 bit array
115 uint32_t *colordata
= new uint32_t[size
* size
];
116 for (int i
= 0; i
< size
* size
; i
++)
118 colordata
[i
] = ((int)colormap
[i
* 3] << 16)
119 + ((int)colormap
[i
* 3 + 1] << 8)
120 + (int)colormap
[i
* 3 + 2];
124 Horde3D::unloadResource(this->colormap
);
126 this->colormap
= Horde3D::createTexture2D("environment_colormap2", ResourceFlags::NoTexMipmaps
, size
, size
, false);
128 this->colormap
= Horde3D::createTexture2D("environment_colormap1", ResourceFlags::NoTexMipmaps
, size
, size
, false);
129 texturename
= 1 - texturename
;
130 Horde3D::updateResourceData(this->colormap
, TextureResParams::PixelData
, colordata
, size
* size
* 4);
131 Horde3D::setResourceParamItemi(material
, MaterialResParams::Sampler
, "colormap", this->colormap
);
134 void Horde3DEnvironment::setColorMap (std::string path
)
139 Horde3DEnvironment::setSkyCube(std::string sky
)
141 // Delete old sky cube
144 Horde3D::removeNode(this->sky
);
147 // Pack skybox images
148 Horde3DConverter
*converter
= ((Horde3DEngine
*)Engine::get())->getConverter();
149 if (converter
->convertSkybox(sky
))
152 std::string filename
= std::string("models/skies/") + sky
+ ".scene.xml";
153 int skybox
= Horde3D::addResource(ResourceTypes::SceneGraph
, filename
.c_str(), 0);
154 ((Horde3DEngine
*)Engine::get())->loadResources();
155 this->sky
= Horde3D::addNodes(((Horde3DCamera
*)Engine::get()->getCamera())->getSceneNode(), skybox
);
156 Horde3D::setNodeTransform(this->sky
, 0, 0, 0, 0, 0, 0, 2000, 2000, 2000);
161 Horde3DEnvironment::setFog(const Color
&fogcolor
)
166 void Horde3DEnvironment::createTerrain()
168 material
= Horde3D::addResource(ResourceTypes::Material
, "materials/terrain.material.xml", 0 );
169 ResHandle lightMatRes
= Horde3D::addResource( ResourceTypes::Material
, "materials/light.material.xml", 0 );
170 ((Horde3DEngine
*)Engine::get())->loadResources();
172 //int skybox = Horde3D::addResource(ResourceTypes::SceneGraph, "models/skybox/skybox.scene.xml", 0);
173 //((Horde3DEngine*)Engine::get())->loadResources();
174 //sky = Horde3D::addNodes(((Horde3DCamera*)Engine::get()->getCamera())->getSceneNode(), skybox);
175 //Horde3D::setNodeTransform(sky, 0, 0, 0, 0, 0, 0, 2000, 2000, 2000);
177 terrain
= horde3d::Island::addIslandNode(RootNode
, "terrain", terrainsize
, terrainsize
, material
);
178 Horde3D::setNodeTransform(terrain
, -32 * terrainsize
, -1600, -32 * terrainsize
, 0, 0, 0, 64, 3200, 64);
179 colormap
= Horde3D::createTexture2D("environment_colormap", ResourceFlags::NoTexMipmaps
, 128, 128, false);
180 unsigned char texturedata
[128 * 128 * 4];
181 memset(texturedata
, 128, 128 * 128 * 4);
182 Horde3D::updateResourceData(colormap
, TextureResParams::PixelData
, texturedata
, 128 * 128 * 4);
183 Horde3D::setResourceParamItemi(material
, MaterialResParams::Sampler
, "colormap", colormap
);
186 sun
= Horde3D::addLightNode(RootNode
, "Light1", lightMatRes
, "LIGHTING", "");
187 Horde3D::setNodeTransform(sun
, 0, 2000, 1000, -90, 0, 0, 1, 1, 1);
188 Horde3D::setNodeParamf(sun
, LightNodeParams::Radius
, 3000 );
189 Horde3D::setNodeParami(sun
, LightNodeParams::ShadowMapCount
, 3);
190 Horde3D::setNodeParamf(sun
, LightNodeParams::ShadowSplitLambda
, 0.9f
);
191 Horde3D::setNodeParamf(sun
, LightNodeParams::ShadowMapBias
, 0.001f
);
192 Horde3D::setNodeParamf(sun
, LightNodeParams::FOV
, 120);
193 Horde3D::setNodeParamf(sun
, LightNodeParams::Col_R
, 0.9f
);
194 Horde3D::setNodeParamf(sun
, LightNodeParams::Col_G
, 0.7f
);
195 Horde3D::setNodeParamf(sun
, LightNodeParams::Col_B
, 0.75f
);
196 ((Horde3DCamera
*)Engine::get()->getCamera())->setSun(sun
);