I do not know about the moving between textures,also i have stucked with the blending
between skys texture and the sun texture.
any way here is the new class that i have adjusted to include sun,the movement does
not implemented yet.
take a look at the render function.
Code: Select all
/************************************
Magic library
day/night cycle for skybox Demo
it is krama757 Idea
*************************************/
#include <irrlicht.h>
#include <Magic2d.hpp>
using namespace irr;
using namespace core;
using namespace scene;
using namespace video;
using namespace io;
using namespace gui;
// global declration of Irrlicht interfaces
IrrlichtDevice* device;
IVideoDriver* driver;
ISceneManager* smgr;
IGUIEnvironment* guienv;
const s32 MaxSkyMaterial = 4;
//! day/night cycle skybox class
class TSkyBoxSceneNode : public ISceneNode
{
private:
core::aabbox3d<f32> Box;
video::SMaterial Material[6][MaxSkyMaterial];
TFadeInOut* Fade;
s32 Src,Dst;
u16 Indices[6];
S3DVertex Vertices[4*6];
s32 MaxSkys;
video::SMaterial Sun;
public:
//! constructor
TSkyBoxSceneNode(video::ITexture* top, video::ITexture* bottom, video::ITexture* left,
video::ITexture* right, video::ITexture* front, video::ITexture* back,
ISceneNode* parent, ISceneManager* mgr, s32 id): ISceneNode(parent, mgr, id)
{
AutomaticCullingEnabled = false;
// create indices
Indices[0] = 0;
Indices[1] = 1;
Indices[2] = 2;
Indices[3] = 0;
Indices[4] = 2;
Indices[5] = 3;
// create material
video::SMaterial mat;
mat.Lighting = false;
mat.ZBuffer = false;
mat.ZWriteEnable = false;
mat.BilinearFilter = true;
f32 onepixel = 0.0f;
video::ITexture* tex = front;
if (!tex) tex = left;
if (!tex) tex = back;
if (!tex) tex = right;
if (!tex) tex = top;
if (!tex) tex = bottom;
if (tex)
{
core::dimension2d<s32> dim = tex->getSize();
onepixel = 1.0f / (dim.Width * 1.5f);
}
f32 l = 10.0f;
f32 t = 1.0f - onepixel;
f32 o = 0.0f + onepixel;
Material[0][0] = mat;
Material[0][0].Texture1 = front;
Vertices[0] = video::S3DVertex(-l,-l,-l, 0,0,1, video::SColor(255,255,255,255), o, t);
Vertices[1] = video::S3DVertex( l,-l,-l, 0,0,1, video::SColor(255,255,255,255), t, t);
Vertices[2] = video::S3DVertex( l, l,-l, 0,0,1, video::SColor(255,255,255,255), t, o);
Vertices[3] = video::S3DVertex(-l, l,-l, 0,0,1, video::SColor(255,255,255,255), o, o);
// create left side
Material[1][0] = mat;
Material[1][0].Texture1 = left;
Vertices[4] = video::S3DVertex( l,-l,-l, -1,0,0, video::SColor(255,255,255,255), o, t);
Vertices[5] = video::S3DVertex( l,-l, l, -1,0,0, video::SColor(255,255,255,255), t, t);
Vertices[6] = video::S3DVertex( l, l, l, -1,0,0, video::SColor(255,255,255,255), t, o);
Vertices[7] = video::S3DVertex( l, l,-l, -1,0,0, video::SColor(255,255,255,255), o, o);
// create back side
Material[2][0] = mat;
Material[2][0].Texture1 = back;
Vertices[8] = video::S3DVertex( l,-l, l, 0,0,-1, video::SColor(255,255,255,255), o, t);
Vertices[9] = video::S3DVertex(-l,-l, l, 0,0,-1, video::SColor(255,255,255,255), t, t);
Vertices[10] = video::S3DVertex(-l, l, l, 0,0,-1, video::SColor(255,255,255,255), t, o);
Vertices[11] = video::S3DVertex( l, l, l, 0,0,-1, video::SColor(255,255,255,255), o, o);
// create right side
Material[3][0] = mat;
Material[3][0].Texture1 = right;
Vertices[12] = video::S3DVertex(-l,-l, l, 1,0,0, video::SColor(255,255,255,255), o, t);
Vertices[13] = video::S3DVertex(-l,-l,-l, 1,0,0, video::SColor(255,255,255,255), t, t);
Vertices[14] = video::S3DVertex(-l, l,-l, 1,0,0, video::SColor(255,255,255,255), t, o);
Vertices[15] = video::S3DVertex(-l, l, l, 1,0,0, video::SColor(255,255,255,255), o, o);
// create top side
Material[4][0] = mat;
Material[4][0].Texture1 = top;
Vertices[16] = video::S3DVertex( l, l, l, 0,-1,0, video::SColor(255,255,255,255), o, o);
Vertices[17] = video::S3DVertex(-l, l, l, 0,-1,0, video::SColor(255,255,255,255), o, t);
Vertices[18] = video::S3DVertex(-l, l,-l, 0,-1,0, video::SColor(255,255,255,255), t, t);
Vertices[19] = video::S3DVertex( l, l,-l, 0,-1,0, video::SColor(255,255,255,255), t, o);
// create bottom side
Material[5][0] = mat;
Material[5][0].Texture1 = bottom;
Vertices[20] = video::S3DVertex(-l,-l, l, 0,1,0, video::SColor(255,255,255,255), o, o);
Vertices[21] = video::S3DVertex( l,-l, l, 0,1,0, video::SColor(255,255,255,255), o, t);
Vertices[22] = video::S3DVertex( l,-l,-l, 0,1,0, video::SColor(255,255,255,255), t, t);
Vertices[23] = video::S3DVertex(-l,-l,-l, 0,1,0, video::SColor(255,255,255,255), t, o);
Fade = new TFadeInOut();
}
//! adding second SkyBox Materials
void AddSecondSkyBox(int MatNum,video::ITexture* top, video::ITexture* bottom, video::ITexture* left,
video::ITexture* right, video::ITexture* front, video::ITexture* back)
{
video::SMaterial mat;
mat.Lighting = false;
mat.ZBuffer = false;
mat.ZWriteEnable = false;
mat.BilinearFilter = true;
Material[0][MatNum] = mat;
Material[0][MatNum].Texture1 = front;
Material[1][MatNum] = mat;
Material[1][MatNum].Texture1 = left;
Material[2][MatNum] = mat;
Material[2][MatNum].Texture1 = back;
Material[3][MatNum] = mat;
Material[3][MatNum].Texture1 = right;
Material[4][MatNum] = mat;
Material[4][MatNum].Texture1 = top;
Material[5][MatNum] = mat;
Material[5][MatNum].Texture1 = bottom;
Src = 0;
Dst = 1;
MaxSkys=MatNum;
}
void AddSun(video::ITexture* sun)
{
Sun.Lighting = false;
Sun.ZBuffer = false;
Sun.ZWriteEnable = false;
Sun.BilinearFilter = true;
Sun.Texture1 = sun;
}
//! destructor
virtual ~TSkyBoxSceneNode(){};
virtual void OnPreRender()
{
if (IsVisible)
SceneManager->registerNodeForRendering(this, ESNRP_SKY_BOX);
ISceneNode::OnPreRender();
}
//! renders the node.
virtual void render()
{
SetBlend(ALPHABLEND);
SetColor(255,255,255);
Fade->FadeIn(4000);
if(Fade->GetFactor()==1.0){Src=Dst; Dst++; Fade->SetFactor(0.0);}
if(Dst>MaxSkys)Dst=0;
video::IVideoDriver* driver = SceneManager->getVideoDriver();
scene::ICameraSceneNode* camera = SceneManager->getActiveCamera();
if (!camera || !driver) return;
core::matrix4 mat;
mat.setTranslation(camera->getAbsolutePosition());
driver->setTransform(video::ETS_WORLD, mat);
for(int i=0;i<23;i++)Vertices[i].Color.setAlpha(Fade->GetFactor()*255);
for (s32 i=0; i<6; ++i)
{
driver->setMaterial(Material[i][Dst]);
driver->drawIndexedTriangleList(&Vertices[i*4], 4, Indices, 2);
}
for(int i=0;i<23;i++)Vertices[i].Color.setAlpha((1.0-Fade->GetFactor())*255);
for (s32 i=0; i<6; ++i)
{
driver->setMaterial(Material[i][Src]);
driver->drawIndexedTriangleList(&Vertices[i*4], 4, Indices, 2);
}
SetBlend(LIGHTBLEND);
driver->setMaterial(Sun);
driver->drawIndexedTriangleList(&Vertices[4*4], 4, Indices, 2);
// SetBlend(ALPHABLEND);
SetBlend(SOLIDBLEND);
}
//! returns the axis aligned bounding box of this node
virtual const core::aabbox3d<f32>& getBoundingBox() const
{
return Box;
}
virtual video::SMaterial& getMaterial(s32 i)
{
return Material[i][0];
}
//! returns amount of materials used by this scene node.
virtual s32 getMaterialCount()
{
return 6;
}
};
int main()
{
device = createDevice(EDT_OPENGL, dimension2d<s32>(640, 480), 32);
bool rslt = InitMagic(device);
if(rslt == false)
printf("Magic Library will only work with OpenGL driver");
device->setWindowCaption(L"Hello World! - Magic 2d library for Irrlicht");
driver = device->getVideoDriver();
smgr = device->getSceneManager();
guienv = device->getGUIEnvironment();
scene::ICameraSceneNode* cam = smgr->addCameraSceneNodeFPS(0,100.0f,1200.0f);
// create skybox
driver->setTextureCreationFlag(video::ETCF_CREATE_MIP_MAPS, false);
TSkyBoxSceneNode* myNode=new TSkyBoxSceneNode(
driver->getTexture("../../media/irrlicht2_up.jpg"),
driver->getTexture("../../media/irrlicht2_dn.jpg"),
driver->getTexture("../../media/irrlicht2_lf.jpg"),
driver->getTexture("../../media/irrlicht2_rt.jpg"),
driver->getTexture("../../media/irrlicht2_ft.jpg"),
driver->getTexture("../../media/irrlicht2_bk.jpg"),
smgr->getRootSceneNode(),smgr,1);
myNode->AddSecondSkyBox(1,
driver->getTexture("../../media/n_irrlicht2_up.jpg"),
driver->getTexture("../../media/irrlicht2_dn.jpg"),
driver->getTexture("../../media/n_irrlicht2_lf.jpg"),
driver->getTexture("../../media/n_irrlicht2_rt.jpg"),
driver->getTexture("../../media/n_irrlicht2_ft.jpg"),
driver->getTexture("../../media/n_irrlicht2_bk.jpg"));
myNode->AddSecondSkyBox(2,
driver->getTexture("../../media/Skyboxtop.Jpg"),
driver->getTexture("../../media/irrlicht2_dn.jpg"),
driver->getTexture("../../media/Skyboxleft.Jpg"),
driver->getTexture("../../media/Skyboxright.Jpg"),
driver->getTexture("../../media/Skyboxfront.Jpg"),
driver->getTexture("../../media/Skyboxback.Jpg"));
// adding Sun texture
myNode->AddSun(driver->getTexture("../../media/light1.png"));
driver->setTextureCreationFlag(video::ETCF_CREATE_MIP_MAPS, true);
int lastFPS = -1;
while(device->run())
{
driver->beginScene(true, true, SColor(0,0,0,0));
smgr->drawAll();
guienv->drawAll();
driver->endScene();
int fps = driver->getFPS();
if (lastFPS != fps)
{
wchar_t tmp[1024];
swprintf(tmp, 1024, L"Hello World! - Magic 2d library for Irrlicht (%s)(fps:%d)",driver->getName(), fps);
device->setWindowCaption(tmp);
lastFPS = fps;
}
}
device->drop();
return 0;
}