2009-02-10 00:30:59 -05:00
|
|
|
// $Id: irr_driver.cpp 694 2006-08-29 07:42:36Z hiker $
|
2009-02-03 19:31:40 -05:00
|
|
|
//
|
|
|
|
// SuperTuxKart - a fun racing game with go-kart
|
|
|
|
// Copyright (C) 2009 Joerg Henrichs
|
|
|
|
//
|
|
|
|
// This program is free software; you can redistribute it and/or
|
|
|
|
// modify it under the terms of the GNU General Public License
|
|
|
|
// as published by the Free Software Foundation; either version 3
|
|
|
|
// of the License, or (at your option) any later version.
|
|
|
|
//
|
|
|
|
// This program is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU General Public License
|
|
|
|
// along with this program; if not, write to the Free Software
|
|
|
|
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
|
|
|
|
|
|
|
#include "graphics/irr_driver.hpp"
|
|
|
|
|
2009-04-29 10:48:18 -04:00
|
|
|
#ifdef HAVE_GLUT
|
|
|
|
# ifdef __APPLE__
|
|
|
|
# include <GLUT/glut.h>
|
|
|
|
# else
|
|
|
|
# include <GL/glut.h>
|
|
|
|
# endif
|
|
|
|
#endif
|
|
|
|
|
2009-06-11 06:00:43 -04:00
|
|
|
#include "config/user_config.hpp"
|
|
|
|
#include "graphics/material_manager.hpp"
|
2009-03-26 19:31:00 -04:00
|
|
|
#include "gui/engine.hpp"
|
2009-06-11 06:00:43 -04:00
|
|
|
#include "gui/font.hpp"
|
2009-04-23 19:27:06 -04:00
|
|
|
#include "gui/state_manager.hpp"
|
2009-03-11 23:49:31 -04:00
|
|
|
#include "io/file_manager.hpp"
|
2009-04-23 19:27:06 -04:00
|
|
|
#include "items/item_manager.hpp"
|
|
|
|
#include "items/powerup_manager.hpp"
|
|
|
|
#include "items/attachment_manager.hpp"
|
|
|
|
#include "items/projectile_manager.hpp"
|
|
|
|
#include "karts/kart_properties_manager.hpp"
|
2009-04-29 10:48:18 -04:00
|
|
|
#include "modes/world.hpp"
|
2009-04-23 19:27:06 -04:00
|
|
|
|
2009-02-03 19:31:40 -05:00
|
|
|
IrrDriver *irr_driver = NULL;
|
|
|
|
|
|
|
|
IrrDriver::IrrDriver()
|
|
|
|
{
|
2009-02-10 00:30:59 -05:00
|
|
|
file_manager->dropFileSystem();
|
2009-04-23 19:27:06 -04:00
|
|
|
|
|
|
|
initDevice();
|
|
|
|
} // IrrDriver
|
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
IrrDriver::~IrrDriver()
|
|
|
|
{
|
|
|
|
m_device->drop();
|
|
|
|
} // ~IrrDriver
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
void IrrDriver::initDevice()
|
|
|
|
{
|
2009-05-10 20:23:40 -04:00
|
|
|
static bool firstTime = true;
|
2009-04-23 19:27:06 -04:00
|
|
|
|
2009-05-10 20:23:40 -04:00
|
|
|
// ---- the first time, get a list of available video modes
|
|
|
|
if(firstTime)
|
2009-04-23 20:14:04 -04:00
|
|
|
{
|
2009-05-10 20:23:40 -04:00
|
|
|
m_device = createDevice(EDT_NULL);
|
|
|
|
|
|
|
|
video::IVideoModeList* modes = m_device->getVideoModeList();
|
|
|
|
const int count = modes->getVideoModeCount();
|
2009-06-07 11:20:51 -04:00
|
|
|
//std::cout << "--------------\n allowed modes \n--------------\n";
|
|
|
|
//std::cout << "Desktop depth : " << modes->getDesktopDepth() << std::endl;
|
|
|
|
//std::cout << "Desktop resolution : " << modes->getDesktopResolution().Width << "," << modes->getDesktopResolution().Height << std::endl;
|
2009-05-10 20:23:40 -04:00
|
|
|
|
2009-06-07 11:20:51 -04:00
|
|
|
//std::cout << "Found " << count << " valid modes\n";
|
2009-05-10 20:23:40 -04:00
|
|
|
for(int i=0; i<count; i++)
|
2009-05-09 16:20:16 -04:00
|
|
|
{
|
2009-05-10 20:23:40 -04:00
|
|
|
// only consider 32-bit resolutions for now
|
|
|
|
if(modes->getVideoModeDepth(i) >= 24)
|
|
|
|
{
|
|
|
|
VideoMode mode;
|
|
|
|
mode.width = modes->getVideoModeResolution(i).Width;
|
|
|
|
mode.height = modes->getVideoModeResolution(i).Height;
|
|
|
|
m_modes.push_back( mode );
|
|
|
|
}
|
|
|
|
|
2009-06-07 11:20:51 -04:00
|
|
|
//std::cout <<
|
|
|
|
//"bits : " << modes->getVideoModeDepth(i) <<
|
|
|
|
//" resolution=" << modes->getVideoModeResolution(i).Width <<
|
|
|
|
//"x" << modes->getVideoModeResolution(i).Height << std::endl;
|
2009-05-09 16:20:16 -04:00
|
|
|
}
|
2009-05-10 20:23:40 -04:00
|
|
|
m_device->closeDevice();
|
|
|
|
|
|
|
|
firstTime = false;
|
|
|
|
} // end if firstTime
|
|
|
|
|
|
|
|
// ---- open device
|
2009-02-03 19:31:40 -05:00
|
|
|
// Try different drivers: start with opengl, then DirectX
|
|
|
|
for(int driver_type=0; driver_type<3; driver_type++)
|
|
|
|
{
|
2009-05-09 20:55:46 -04:00
|
|
|
video::E_DRIVER_TYPE type = driver_type==0
|
|
|
|
? video::EDT_OPENGL
|
|
|
|
: (driver_type==1
|
|
|
|
? video::EDT_DIRECT3D9
|
2009-04-23 19:27:06 -04:00
|
|
|
: video::EDT_DIRECT3D8);
|
2009-05-10 20:09:40 -04:00
|
|
|
// Try 32 and, upon failure, 24 then 16 bit per pixels
|
2009-05-09 20:55:46 -04:00
|
|
|
for(int bits=32; bits>15; bits -=8)
|
2009-02-03 19:31:40 -05:00
|
|
|
{
|
2009-02-28 19:46:47 -05:00
|
|
|
m_device = createDevice(type,
|
2009-06-20 21:10:43 -04:00
|
|
|
core::dimension2d<s32>(UserConfigParams::m_width,
|
|
|
|
UserConfigParams::m_height ),
|
2009-02-28 19:46:47 -05:00
|
|
|
bits, //bits per pixel
|
2009-06-20 21:10:43 -04:00
|
|
|
UserConfigParams::m_fullscreen,
|
2009-02-28 19:46:47 -05:00
|
|
|
false, // stencil buffers
|
|
|
|
false, // vsync
|
|
|
|
this // event receiver
|
|
|
|
);
|
|
|
|
if(m_device) break;
|
|
|
|
} // for bits=24, 16
|
|
|
|
if(m_device) break;
|
|
|
|
} // for edt_types
|
|
|
|
if(!m_device)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "Couldn't initialise irrlicht device. Quitting.\n");
|
|
|
|
exit(-1);
|
|
|
|
}
|
2009-05-09 20:55:46 -04:00
|
|
|
|
2009-03-29 11:51:55 -04:00
|
|
|
m_device->getVideoDriver()->setTextureCreationFlag(video::ETCF_CREATE_MIP_MAPS,true);
|
2009-05-09 20:55:46 -04:00
|
|
|
|
2009-02-28 19:46:47 -05:00
|
|
|
// Stores the new file system pointer.
|
2009-02-10 00:30:59 -05:00
|
|
|
file_manager->setDevice(m_device);
|
|
|
|
m_device->setWindowCaption(L"SuperTuxKart");
|
|
|
|
m_scene_manager = m_device->getSceneManager();
|
2009-03-11 01:10:56 -04:00
|
|
|
m_gui_env = m_device->getGUIEnvironment();
|
2009-03-12 01:21:13 -04:00
|
|
|
const std::string &font = file_manager->getFontFile("DomesticManners.xml");
|
2009-03-11 01:10:56 -04:00
|
|
|
m_race_font = m_gui_env->getFont(font.c_str());
|
2009-04-23 19:27:06 -04:00
|
|
|
}
|
2009-02-03 19:31:40 -05:00
|
|
|
|
2009-05-02 20:50:09 -04:00
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
void IrrDriver::showPointer()
|
|
|
|
{
|
|
|
|
this->getDevice()->getCursorControl()->setVisible(true);
|
|
|
|
} // showPointer
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
void IrrDriver::hidePointer()
|
|
|
|
{
|
|
|
|
this->getDevice()->getCursorControl()->setVisible(true);
|
|
|
|
} // hidePointer
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
2009-04-23 19:27:06 -04:00
|
|
|
void IrrDriver::changeResolution()
|
2009-02-03 19:31:40 -05:00
|
|
|
{
|
2009-04-23 19:34:41 -04:00
|
|
|
// show black before resolution switch so we don't see OpenGL's buffer garbage during switch
|
|
|
|
m_device->getVideoDriver()->beginScene(true, true, video::SColor(255,100,101,140));
|
|
|
|
m_device->getVideoDriver()->draw2DRectangle( SColor(255, 0, 0, 0),
|
|
|
|
core::rect<s32>(0, 0,
|
2009-06-20 21:10:43 -04:00
|
|
|
UserConfigParams::m_prev_width,
|
|
|
|
UserConfigParams::m_prev_height) );
|
2009-04-23 19:34:41 -04:00
|
|
|
m_device->getVideoDriver()->endScene();
|
2009-05-09 20:55:46 -04:00
|
|
|
|
2009-04-23 19:27:06 -04:00
|
|
|
// startScreen -> removeTextures();
|
|
|
|
attachment_manager -> removeTextures();
|
|
|
|
projectile_manager -> removeTextures();
|
|
|
|
item_manager -> removeTextures();
|
|
|
|
kart_properties_manager -> removeTextures();
|
|
|
|
powerup_manager -> removeTextures();
|
2009-05-27 23:15:43 -04:00
|
|
|
GUIEngine::cleanUp();
|
2009-05-09 20:55:46 -04:00
|
|
|
|
2009-04-23 19:27:06 -04:00
|
|
|
m_device->closeDevice();
|
2009-02-03 19:31:40 -05:00
|
|
|
m_device->drop();
|
2009-04-23 19:27:06 -04:00
|
|
|
initDevice();
|
2009-05-09 20:55:46 -04:00
|
|
|
|
2009-04-23 19:27:06 -04:00
|
|
|
material_manager->reInit();
|
2009-05-09 20:55:46 -04:00
|
|
|
|
2009-04-23 19:27:06 -04:00
|
|
|
powerup_manager -> loadPowerups();
|
|
|
|
kart_properties_manager -> loadKartData();
|
|
|
|
item_manager -> loadDefaultItems();
|
|
|
|
projectile_manager -> loadData();
|
|
|
|
attachment_manager -> loadModels();
|
2009-05-09 20:55:46 -04:00
|
|
|
|
2009-04-23 19:27:06 -04:00
|
|
|
//FIXME: the font reinit funcs should be inside the font class
|
|
|
|
//Reinit fonts
|
|
|
|
delete_fonts();
|
|
|
|
init_fonts();
|
2009-05-09 20:55:46 -04:00
|
|
|
|
2009-04-23 19:27:06 -04:00
|
|
|
StateManager::initGUI();
|
|
|
|
GUIEngine::reshowCurrentScreen();
|
|
|
|
// startScreen -> installMaterial();
|
2009-05-09 20:55:46 -04:00
|
|
|
|
2009-04-23 19:27:06 -04:00
|
|
|
}
|
2009-02-10 00:30:59 -05:00
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
/** Loads an animated mesh and returns a pointer to it.
|
|
|
|
* \param filename File to load.
|
|
|
|
*/
|
|
|
|
scene::IAnimatedMesh *IrrDriver::getAnimatedMesh(const std::string &filename)
|
|
|
|
{
|
2009-03-26 19:31:00 -04:00
|
|
|
scene::IAnimatedMesh *m = m_scene_manager->getMesh(filename.c_str());
|
2009-03-27 08:34:52 -04:00
|
|
|
if(m) setAllMaterialFlags(m);
|
2009-03-26 19:31:00 -04:00
|
|
|
return m;
|
2009-02-10 00:30:59 -05:00
|
|
|
} // getAnimatedMesh
|
|
|
|
|
2009-02-10 06:11:39 -05:00
|
|
|
// ----------------------------------------------------------------------------
|
2009-03-29 11:51:55 -04:00
|
|
|
|
2009-02-10 06:11:39 -05:00
|
|
|
/** Loads a non-animated mesh and returns a pointer to it.
|
|
|
|
* \param filename File to load.
|
|
|
|
*/
|
|
|
|
scene::IMesh *IrrDriver::getMesh(const std::string &filename)
|
|
|
|
{
|
|
|
|
scene::IAnimatedMesh *m = m_scene_manager->getMesh(filename.c_str());
|
|
|
|
if(!m) return NULL;
|
2009-03-26 19:31:00 -04:00
|
|
|
setAllMaterialFlags(m);
|
2009-02-10 06:11:39 -05:00
|
|
|
return m->getMesh(0);
|
|
|
|
} // getMesh
|
|
|
|
|
2009-03-26 19:31:00 -04:00
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
/** Sets the material flags in this mesh depending on the settings in
|
|
|
|
* material_manager.
|
|
|
|
* \param mesh The mesh to change the settings in.
|
|
|
|
*/
|
|
|
|
void IrrDriver::setAllMaterialFlags(scene::IAnimatedMesh *mesh) const
|
|
|
|
{
|
|
|
|
unsigned int n=mesh->getMeshBufferCount();
|
|
|
|
for(unsigned int i=0; i<n; i++)
|
|
|
|
{
|
|
|
|
scene::IMeshBuffer *mb = mesh->getMeshBuffer(i);
|
|
|
|
video::SMaterial &irr_material=mb->getMaterial();
|
|
|
|
for(unsigned int j=0; j<video::MATERIAL_MAX_TEXTURES; j++)
|
|
|
|
{
|
|
|
|
video::ITexture* t=irr_material.getTexture(j);
|
|
|
|
if(!t) continue;
|
|
|
|
material_manager->setAllMaterialFlags(t, mb);
|
|
|
|
} // for j<MATERIAL_MAX_TEXTURES
|
|
|
|
} // for i<getMeshBufferCount()
|
|
|
|
} // setAllMaterialFlags
|
|
|
|
|
2009-03-23 19:50:50 -04:00
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
/** Converts the mesh into a water scene node.
|
|
|
|
* \param mesh The mesh which is converted into a water scene node.
|
|
|
|
* \param wave_height Height of the water waves.
|
|
|
|
* \param wave_speed Speed of the water waves.
|
|
|
|
* \param wave_length Lenght of a water wave.
|
|
|
|
*/
|
|
|
|
scene::ISceneNode* IrrDriver::addWaterNode(scene::IMesh *mesh,
|
|
|
|
float wave_height,
|
|
|
|
float wave_speed,
|
|
|
|
float wave_length)
|
|
|
|
{
|
|
|
|
return m_scene_manager->addWaterSurfaceSceneNode(mesh,
|
2009-05-09 20:55:46 -04:00
|
|
|
wave_height, wave_speed,
|
2009-03-23 19:50:50 -04:00
|
|
|
wave_length);
|
|
|
|
} // addWaterNode
|
|
|
|
|
2009-02-10 00:30:59 -05:00
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
/** Adds a mesh that will be optimised using an oct tree.
|
|
|
|
* \param mesh Mesh to add.
|
|
|
|
*/
|
|
|
|
scene::ISceneNode *IrrDriver::addOctTree(scene::IMesh *mesh)
|
|
|
|
{
|
|
|
|
return m_scene_manager->addOctTreeSceneNode(mesh);
|
|
|
|
} // addOctTree
|
|
|
|
|
2009-03-16 22:52:58 -04:00
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
/** Adds a particle scene node.
|
|
|
|
*/
|
|
|
|
scene::IParticleSystemSceneNode *IrrDriver::addParticleNode(bool default_emitter)
|
|
|
|
{
|
|
|
|
return m_scene_manager->addParticleSystemSceneNode(default_emitter);
|
|
|
|
} // addParticleNode
|
|
|
|
|
2009-02-10 00:30:59 -05:00
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
/** Adds a static mesh to scene. This should be used for smaller objects,
|
|
|
|
* since the node is not optimised.
|
|
|
|
* \param mesh The mesh to add.
|
|
|
|
*/
|
|
|
|
scene::ISceneNode *IrrDriver::addMesh(scene::IMesh *mesh)
|
|
|
|
{
|
|
|
|
return m_scene_manager->addMeshSceneNode(mesh);
|
|
|
|
} // addMesh
|
|
|
|
|
2009-06-25 21:19:57 -04:00
|
|
|
void IrrDriver::renderToTexture(ptr_vector<scene::IMesh, REF>& mesh, std::vector<Vec3>& mesh_location, ITexture* target, float angle)
|
|
|
|
{
|
|
|
|
scene::ISceneNode* main_node = NULL;
|
|
|
|
|
|
|
|
const int mesh_amount = mesh.size();
|
|
|
|
for(int n=0; n<mesh_amount; n++)
|
|
|
|
{
|
|
|
|
scene::ISceneNode* node = addMesh(mesh.get(n));
|
|
|
|
|
|
|
|
if(main_node == NULL)
|
|
|
|
{
|
|
|
|
main_node = node;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
node->setParent(main_node);
|
|
|
|
}
|
|
|
|
|
|
|
|
node->setPosition( mesh_location[n].toIrrVector() );
|
|
|
|
node->updateAbsolutePosition();
|
|
|
|
}
|
|
|
|
|
|
|
|
main_node->setScale( core::vector3df(50.0f, 50.0f, 50.0f) );
|
|
|
|
main_node->setRotation( core::vector3df(0, angle, 0) );
|
2009-06-24 19:01:45 -04:00
|
|
|
|
2009-06-24 20:50:58 -04:00
|
|
|
//vector3d< f32 > modelsize = mesh->getBoundingBox().getExtent();
|
|
|
|
//std::cout << "box size " << modelsize.X*50.0 << ", " << modelsize.Y*50.0 << ", " << modelsize.Z*50.0 << std::endl;
|
2009-06-24 19:01:45 -04:00
|
|
|
|
2009-06-24 20:50:58 -04:00
|
|
|
getSceneManager()->setAmbientLight(video::SColor(255, 120, 120, 120));
|
|
|
|
|
|
|
|
const core::vector3df &sun_pos = core::vector3df( 0, 200, 100.0f );
|
|
|
|
scene::ILightSceneNode* light = getSceneManager()->addLightSceneNode(NULL, sun_pos, video::SColorf(1.0f,1.0f,1.0f), 10000.0f /* radius */);
|
|
|
|
//light->setLightType(ELT_DIRECTIONAL); // ELT_DIRECTIONAL , ELT_POINT
|
|
|
|
//light->getLightData().AmbientColor = irr::video::SColorf(0.5f, 0.5f, 0.5f, 1.0f);
|
|
|
|
//light->getLightData().Attenuation = core::vector3df(10, 10, 10);
|
|
|
|
light->getLightData().DiffuseColor = irr::video::SColorf(1.0f, 1.0f, 1.0f, 1.0f);
|
|
|
|
light->getLightData().SpecularColor = irr::video::SColorf(1.0f, 1.0f, 1.0f, 1.0f);
|
|
|
|
|
2009-06-25 21:19:57 -04:00
|
|
|
main_node->setMaterialFlag(EMF_GOURAUD_SHADING , true);
|
|
|
|
main_node->setMaterialFlag(EMF_LIGHTING, true);
|
2009-06-24 20:50:58 -04:00
|
|
|
// node->setMaterialFlag(EMF_LIGHTING, true);
|
|
|
|
|
2009-06-25 21:19:57 -04:00
|
|
|
const int materials = main_node->getMaterialCount();
|
2009-06-24 20:50:58 -04:00
|
|
|
for(int n=0; n<materials; n++)
|
|
|
|
{
|
2009-06-25 21:19:57 -04:00
|
|
|
main_node->getMaterial(n).setFlag(EMF_LIGHTING, true);
|
2009-06-24 20:50:58 -04:00
|
|
|
|
2009-06-25 21:19:57 -04:00
|
|
|
main_node->getMaterial(n).Shininess = 200.0f; // set size of specular highlights
|
|
|
|
main_node->getMaterial(n).SpecularColor.set(255,150,150,150);
|
|
|
|
main_node->getMaterial(n).DiffuseColor.set(255,150,150,150);
|
2009-06-24 20:50:58 -04:00
|
|
|
|
|
|
|
//node->getMaterial(n).setFlag(EMF_NORMALIZE_NORMALS , true);
|
2009-06-25 21:19:57 -04:00
|
|
|
main_node->getMaterial(n).setFlag(EMF_GOURAUD_SHADING , true);
|
|
|
|
main_node->getMaterial(n).GouraudShading = true;
|
2009-06-24 20:50:58 -04:00
|
|
|
}
|
2009-06-24 19:01:45 -04:00
|
|
|
|
|
|
|
ICameraSceneNode* camera = m_scene_manager->addCameraSceneNode();
|
|
|
|
|
2009-07-04 13:24:09 -04:00
|
|
|
camera->setPosition( core::vector3df(0.0, 30.0f, 70.0f) );
|
2009-06-24 19:01:45 -04:00
|
|
|
camera->setUpVector( core::vector3df(0.0, -1.0, 0.0) );
|
2009-07-04 13:24:09 -04:00
|
|
|
camera->setTarget( core::vector3df(0, 10, 0.0f) );
|
2009-06-24 19:01:45 -04:00
|
|
|
camera->updateAbsolutePosition();
|
|
|
|
|
|
|
|
m_device->getVideoDriver()->setRenderTarget(target);
|
|
|
|
//m_device->getVideoDriver()->beginScene(true, true, video::SColor(0,0,0,0));
|
|
|
|
m_scene_manager->drawAll();
|
|
|
|
//m_device->getVideoDriver()->endScene();
|
|
|
|
|
2009-06-25 21:19:57 -04:00
|
|
|
removeNode(main_node);
|
2009-06-24 19:01:45 -04:00
|
|
|
removeNode(camera);
|
|
|
|
removeNode(light);
|
|
|
|
|
|
|
|
m_device->getVideoDriver()->setRenderTarget(0, false, false);
|
|
|
|
}
|
|
|
|
|
2009-03-23 19:50:50 -04:00
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
/** Creates a quad mesh buffer and adds it to the scene graph.
|
|
|
|
*/
|
2009-05-18 23:24:56 -04:00
|
|
|
scene::IMesh *IrrDriver::createQuadMesh(const video::SMaterial *material)
|
2009-03-23 19:50:50 -04:00
|
|
|
{
|
|
|
|
scene::SMeshBuffer *buffer = new scene::SMeshBuffer();
|
|
|
|
video::S3DVertex v;
|
|
|
|
v.Pos = core::vector3df(0,0,0);
|
2009-05-18 23:24:56 -04:00
|
|
|
|
2009-03-23 19:50:50 -04:00
|
|
|
// Add the vertices
|
|
|
|
// ----------------
|
|
|
|
buffer->Vertices.push_back(v);
|
|
|
|
buffer->Vertices.push_back(v);
|
|
|
|
buffer->Vertices.push_back(v);
|
|
|
|
buffer->Vertices.push_back(v);
|
|
|
|
|
|
|
|
// Define the indices for the triangles
|
|
|
|
// ------------------------------------
|
|
|
|
buffer->Indices.push_back(0);
|
|
|
|
buffer->Indices.push_back(1);
|
|
|
|
buffer->Indices.push_back(2);
|
|
|
|
|
|
|
|
buffer->Indices.push_back(0);
|
|
|
|
buffer->Indices.push_back(2);
|
|
|
|
buffer->Indices.push_back(3);
|
|
|
|
|
|
|
|
// Set the normals
|
|
|
|
// ---------------
|
|
|
|
core::vector3df n(1/sqrt(2.0f), 1/sqrt(2.0f), 0);
|
|
|
|
buffer->Vertices[0].Normal = n;
|
|
|
|
buffer->Vertices[1].Normal = n;
|
|
|
|
buffer->Vertices[2].Normal = n;
|
|
|
|
buffer->Vertices[3].Normal = n;
|
2009-05-18 23:24:56 -04:00
|
|
|
if(material)
|
|
|
|
buffer->Material = *material;
|
2009-03-23 19:50:50 -04:00
|
|
|
SMesh *mesh = new SMesh();
|
|
|
|
mesh->addMeshBuffer(buffer);
|
|
|
|
mesh->recalculateBoundingBox();
|
|
|
|
buffer->drop();
|
|
|
|
return mesh;
|
|
|
|
} // createQuadMesh
|
|
|
|
|
2009-03-11 01:10:56 -04:00
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
/** Removes a scene node from the scene.
|
|
|
|
* \param node The scene node to remove.
|
|
|
|
*/
|
|
|
|
void IrrDriver::removeNode(scene::ISceneNode *node)
|
|
|
|
{
|
|
|
|
node->remove();
|
|
|
|
} // removeMesh
|
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
/** Removes a mesh from the mesh cache, freeing the memory.
|
|
|
|
* \param mesh The mesh to remove.
|
|
|
|
*/
|
|
|
|
void IrrDriver::removeMesh(scene::IMesh *mesh)
|
|
|
|
{
|
|
|
|
m_scene_manager->getMeshCache()->removeMesh(mesh);
|
|
|
|
} // removeMesh
|
|
|
|
|
2009-02-10 00:30:59 -05:00
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
/** Adds an animated mesh to the scene.
|
|
|
|
* \param mesh The animated mesh to add.
|
|
|
|
*/
|
2009-04-05 08:53:17 -04:00
|
|
|
scene::IAnimatedMeshSceneNode *IrrDriver::addAnimatedMesh(scene::IAnimatedMesh *mesh)
|
2009-02-10 00:30:59 -05:00
|
|
|
{
|
|
|
|
return m_scene_manager->addAnimatedMeshSceneNode(mesh);
|
|
|
|
} // addAnimatedMesh
|
|
|
|
|
2009-03-16 22:52:58 -04:00
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
/** Adds a sky dome. Documentation from irrlicht:
|
|
|
|
* A skydome is a large (half-) sphere with a panoramic texture on the inside
|
|
|
|
* and is drawn around the camera position.
|
|
|
|
* \param texture: Texture for the dome.
|
|
|
|
* \param horiRes: Number of vertices of a horizontal layer of the sphere.
|
|
|
|
* \param vertRes: Number of vertices of a vertical layer of the sphere.
|
2009-05-09 20:55:46 -04:00
|
|
|
* \param texturePercentage: How much of the height of the texture is used.
|
2009-03-16 22:52:58 -04:00
|
|
|
* Should be between 0 and 1.
|
2009-05-09 20:55:46 -04:00
|
|
|
* \param spherePercentage: How much of the sphere is drawn. Value should be
|
|
|
|
* between 0 and 2, where 1 is an exact half-sphere and 2 is a full
|
2009-03-16 22:52:58 -04:00
|
|
|
* sphere.
|
|
|
|
*/
|
2009-05-09 20:55:46 -04:00
|
|
|
scene::ISceneNode *IrrDriver::addSkyDome(const std::string &texture_name,
|
|
|
|
int hori_res, int vert_res,
|
|
|
|
float texture_percent,
|
2009-03-16 22:52:58 -04:00
|
|
|
float sphere_percent)
|
|
|
|
{
|
|
|
|
ITexture *texture = getTexture(texture_name);
|
|
|
|
return m_scene_manager->addSkyDomeSceneNode(texture, hori_res, vert_res,
|
2009-05-09 20:55:46 -04:00
|
|
|
texture_percent,
|
2009-03-16 22:52:58 -04:00
|
|
|
sphere_percent);
|
|
|
|
} // addSkyDome
|
|
|
|
|
2009-03-23 19:50:50 -04:00
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
/** Adds a skybox using. Irrlicht documentation:
|
2009-05-09 20:55:46 -04:00
|
|
|
* A skybox is a big cube with 6 textures on it and is drawn around the camera
|
2009-03-23 19:50:50 -04:00
|
|
|
* position.
|
|
|
|
* \param top: Texture for the top plane of the box.
|
|
|
|
* \param bottom: Texture for the bottom plane of the box.
|
|
|
|
* \param left: Texture for the left plane of the box.
|
|
|
|
* \param right: Texture for the right plane of the box.
|
|
|
|
* \param front: Texture for the front plane of the box.
|
|
|
|
* \param back: Texture for the back plane of the box.
|
|
|
|
*/
|
|
|
|
scene::ISceneNode *IrrDriver::addSkyBox(const std::vector<std::string> &texture_names)
|
|
|
|
{
|
|
|
|
std::vector<video::ITexture*> t;
|
|
|
|
for(unsigned int i=0; i<texture_names.size(); i++)
|
|
|
|
{
|
|
|
|
t.push_back(getTexture(texture_names[i]));
|
|
|
|
}
|
|
|
|
return m_scene_manager->addSkyBoxSceneNode(t[0], t[1], t[2], t[3], t[4], t[5]);
|
|
|
|
} // addSkyBox
|
|
|
|
|
2009-02-10 00:30:59 -05:00
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
/** Adds a camera to the scene.
|
|
|
|
*/
|
|
|
|
scene::ICameraSceneNode *IrrDriver::addCamera()
|
|
|
|
{
|
|
|
|
return m_scene_manager->addCameraSceneNode();
|
|
|
|
} // addCamera
|
|
|
|
|
2009-03-11 01:10:56 -04:00
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
/** Loads a texture from a file and returns the texture object.
|
|
|
|
* \param filename File name of the texture to load.
|
|
|
|
*/
|
|
|
|
video::ITexture *IrrDriver::getTexture(const std::string &filename)
|
|
|
|
{
|
|
|
|
return m_scene_manager->getVideoDriver()->getTexture(filename.c_str());
|
|
|
|
} // getTexture
|
|
|
|
|
2009-03-15 21:45:49 -04:00
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
/** Sets the ambient light.
|
2009-05-09 20:55:46 -04:00
|
|
|
* \param light The colour of the light to set.
|
2009-03-15 21:45:49 -04:00
|
|
|
*/
|
|
|
|
void IrrDriver::setAmbientLight(const video::SColor &light)
|
|
|
|
{
|
|
|
|
m_scene_manager->setAmbientLight(light);
|
|
|
|
} // setAmbientLight
|
|
|
|
|
2009-02-10 00:30:59 -05:00
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
/** Update, called once per frame.
|
|
|
|
* \param dt Time since last update
|
|
|
|
*/
|
|
|
|
void IrrDriver::update(float dt)
|
|
|
|
{
|
2009-04-25 13:55:39 -04:00
|
|
|
if(!m_device->run()) return;
|
2009-06-24 19:01:45 -04:00
|
|
|
|
2009-02-10 00:30:59 -05:00
|
|
|
m_device->getVideoDriver()->beginScene(true, true, video::SColor(255,100,101,140));
|
2009-06-24 19:01:45 -04:00
|
|
|
|
|
|
|
if(!StateManager::isGameState())
|
|
|
|
{
|
|
|
|
// this code needs to go outside beginScene() / endScene() since
|
|
|
|
// the model view widget will do off-screen rendering there
|
|
|
|
const int updateAmount = GUIEngine::needsUpdate.size();
|
|
|
|
for(int n=0; n<updateAmount; n++)
|
|
|
|
{
|
|
|
|
GUIEngine::needsUpdate[n].update(dt);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-04-29 10:48:18 -04:00
|
|
|
#ifdef HAVE_GLUT
|
2009-06-20 21:10:43 -04:00
|
|
|
if(UserConfigParams::m_bullet_debug && race_manager->raceIsActive())
|
2009-04-29 10:48:18 -04:00
|
|
|
{
|
|
|
|
// Use bullets debug drawer
|
|
|
|
GLfloat light_ambient[] = { 0.0, 0.0, 0.0, 1.0 };
|
|
|
|
GLfloat light_diffuse[] = { 1.0, 1.0, 1.0, 1.0 };
|
|
|
|
GLfloat light_specular[] = { 1.0, 1.0, 1.0, 1.0 };
|
|
|
|
/* light_position is NOT default value */
|
|
|
|
GLfloat light_position0[] = { 1.0, 1.0, 1.0, 0.0 };
|
|
|
|
GLfloat light_position1[] = { -1.0, -1.0, -1.0, 0.0 };
|
|
|
|
|
|
|
|
glLightfv(GL_LIGHT0, GL_AMBIENT, light_ambient);
|
|
|
|
glLightfv(GL_LIGHT0, GL_DIFFUSE, light_diffuse);
|
|
|
|
glLightfv(GL_LIGHT0, GL_SPECULAR, light_specular);
|
|
|
|
glLightfv(GL_LIGHT0, GL_POSITION, light_position0);
|
|
|
|
|
|
|
|
glLightfv(GL_LIGHT1, GL_AMBIENT, light_ambient);
|
|
|
|
glLightfv(GL_LIGHT1, GL_DIFFUSE, light_diffuse);
|
|
|
|
glLightfv(GL_LIGHT1, GL_SPECULAR, light_specular);
|
|
|
|
glLightfv(GL_LIGHT1, GL_POSITION, light_position1);
|
|
|
|
|
|
|
|
glEnable(GL_LIGHTING);
|
|
|
|
glEnable(GL_LIGHT0);
|
|
|
|
glEnable(GL_LIGHT1);
|
|
|
|
|
|
|
|
glShadeModel(GL_SMOOTH);
|
|
|
|
glEnable(GL_DEPTH_TEST);
|
|
|
|
glDepthFunc(GL_LESS);
|
|
|
|
|
|
|
|
glClearColor(0.8f,0.8f,0.8f,0);
|
|
|
|
|
2009-05-03 08:33:26 -04:00
|
|
|
glCullFace(GL_BACK);
|
2009-04-29 10:48:18 -04:00
|
|
|
glMatrixMode(GL_PROJECTION);
|
|
|
|
glLoadIdentity();
|
|
|
|
float f=2.0f;
|
|
|
|
glFrustum(-f, f, -f, f, 1.0, 1000.0);
|
|
|
|
|
|
|
|
Vec3 xyz = RaceManager::getKart(race_manager->getNumKarts()-1)->getXYZ();
|
|
|
|
gluLookAt(xyz.getX(), xyz.getY()-5.f, xyz.getZ()+4,
|
2009-05-03 08:33:26 -04:00
|
|
|
xyz.getX(), xyz.getY(), xyz.getZ(),
|
|
|
|
0.0f, 0.0f, 1.0f);
|
2009-04-29 10:48:18 -04:00
|
|
|
glMatrixMode(GL_MODELVIEW);
|
|
|
|
|
|
|
|
for (unsigned int i = 0 ; i < race_manager->getNumKarts(); ++i)
|
|
|
|
{
|
|
|
|
Kart *kart=RaceManager::getKart((int)i);
|
|
|
|
if(!kart->isEliminated()) kart->draw();
|
|
|
|
}
|
|
|
|
RaceManager::getWorld()->getPhysics()->draw();
|
2009-03-14 21:34:21 -04:00
|
|
|
|
2009-04-29 10:48:18 -04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
#endif
|
|
|
|
{
|
|
|
|
m_scene_manager->drawAll();
|
|
|
|
GUIEngine::render(dt);
|
|
|
|
}
|
2009-06-24 19:01:45 -04:00
|
|
|
|
2009-06-28 21:40:15 -04:00
|
|
|
// draw FPS if enabled
|
|
|
|
if ( UserConfigParams::m_display_fps )
|
|
|
|
{
|
|
|
|
// TODO : don't re-create string every frame
|
|
|
|
video::IVideoDriver* driver = irr_driver->getDevice()->getVideoDriver();
|
|
|
|
gui::IGUIFont* font = irr_driver->getRaceFont();
|
|
|
|
const int fps = driver->getFPS();
|
|
|
|
|
|
|
|
static char buffer[32];
|
|
|
|
sprintf(buffer, "FPS : %i", fps);
|
|
|
|
|
|
|
|
core::stringw fpsString = buffer;
|
|
|
|
|
|
|
|
//std::cout << "===== Drawing FPS " << fpsString.c_str() << "=====\n";
|
|
|
|
|
|
|
|
static video::SColor fpsColor = video::SColor(255, 255, 0, 0);
|
|
|
|
font->draw( fpsString.c_str(), core::rect< s32 >(0,0,600,200), fpsColor, true );
|
|
|
|
}
|
|
|
|
|
|
|
|
m_device->getVideoDriver()->endScene();
|
2009-06-24 19:01:45 -04:00
|
|
|
|
2009-02-10 00:30:59 -05:00
|
|
|
} // update
|
|
|
|
|
2009-02-03 19:31:40 -05:00
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
// Irrlicht Event handler.
|
2009-02-28 19:46:47 -05:00
|
|
|
bool IrrDriver::OnEvent(const irr::SEvent &event)
|
|
|
|
{
|
2009-05-09 20:55:46 -04:00
|
|
|
switch (event.EventType)
|
2009-02-28 19:46:47 -05:00
|
|
|
{
|
|
|
|
case irr::EET_KEY_INPUT_EVENT: {
|
|
|
|
printf("key input event\n");
|
|
|
|
break;
|
|
|
|
} // keyboard
|
|
|
|
case irr::EET_GUI_EVENT: {
|
|
|
|
return false; }
|
|
|
|
case irr::EET_MOUSE_INPUT_EVENT: {
|
|
|
|
return false; }
|
|
|
|
case irr::EET_LOG_TEXT_EVENT:
|
|
|
|
{
|
|
|
|
// Ignore 'normal' messages
|
2009-03-11 01:10:56 -04:00
|
|
|
if(event.LogEvent.Level>0)
|
2009-02-28 19:46:47 -05:00
|
|
|
{
|
2009-05-09 20:55:46 -04:00
|
|
|
printf("Level %d: %s\n",
|
2009-02-28 19:46:47 -05:00
|
|
|
event.LogEvent.Level,event.LogEvent.Text);
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
printf("Event: %d -> ",event.EventType);
|
|
|
|
return false;
|
|
|
|
} // switch
|
|
|
|
return false;
|
|
|
|
} // OnEvent
|
|
|
|
|
2009-02-03 19:31:40 -05:00
|
|
|
// ----------------------------------------------------------------------------
|