Hi All.
Code: Select all
#include <irrlicht.h>
using namespace irr;
#ifdef _MSC_VER
#pragma comment(lib, "Irrlicht.lib")
#endif
/// Class to create a 1 pixel BMP in memory
class CSingleColorBitmap {
public:
CSingleColorBitmap(int r, int g, int b) {
unsigned char bmpInit[58] = {0x42, 0x4d, 0x3a, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x36, 0x00, 0x00, 0x00,
0x28, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01,
0x00, 0x18, 0x00, 0x00, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,0x00, 0x00,
0x4d, 0x48, 0xf0, 0x00 };
(void) memcpy(bmp_, bmpInit, 58);
bmp_[54] = (unsigned char)b;
bmp_[55] = (unsigned char)g;
bmp_[56] = (unsigned char)r;
}
void* Data() const {
return (void*)bmp_;
}
int Size() const {
return 58;
}
private:
unsigned char bmp_[58];
};
int main()
{
auto driverType = video::E_DRIVER_TYPE::EDT_OPENGL;
// create device
auto device = createDevice(driverType,
core::dimension2d<u32>(640, 480), 16, false);
if (device == nullptr)
return 1; // could not create driver.
device->setWindowCaption(L"Lighting issues");
auto driver = device->getVideoDriver();
auto smgr = device->getSceneManager();
smgr->addCameraSceneNode(nullptr, core::vector3df(60,100,-30), core::vector3df(60,0,-30));
auto topLight = smgr->addLightSceneNode(nullptr,
core::vector3df(60, 50, -30),
video::SColorf(0.05f, 0.05f, 0.05f),
1000);
smgr->setAmbientLight(video::SColorf(0.0f, 0.0f, 0.0f));
auto normal = irr::core::vector3df(0.0, 1.0, 0.0);
auto color = irr::video::SColor(255, 255, 255, 255);
// Create a mesh.
auto mesh = new irr::scene::SMesh();
auto buffer = new irr::scene::SMeshBuffer();
mesh->addMeshBuffer(buffer);
buffer->drop();
// Add vertices
buffer->Vertices.push_back(irr::video::S3DVertex(irr::core::vector3df(121.919998f, 0.0f, 0.0f),
normal, color,
irr::core::vector2df(6.09600019f, 0.0f)));
buffer->Vertices.push_back(irr::video::S3DVertex(irr::core::vector3df(121.919998f, 0.0f, -60.9599991f),
normal, color,
irr::core::vector2df(6.09600019f, 3.04800010f)));
buffer->Vertices.push_back(irr::video::S3DVertex(irr::core::vector3df(0.0f, 0.0f, -60.9599991f),
normal, color,
irr::core::vector2df(0.0f, 3.04800010f)));
buffer->Vertices.push_back(irr::video::S3DVertex(irr::core::vector3df(0.0f, 0.0f, 0.0f),
normal, color,
irr::core::vector2df(0.0f, 0.0f)));
buffer->Vertices.push_back(irr::video::S3DVertex(irr::core::vector3df(34.3258286f, 0.0f, -26.0343609f),
normal, color,
irr::core::vector2df(1.71629155f, 1.30171800f)));
buffer->Vertices.push_back(irr::video::S3DVertex(irr::core::vector3df(57.3258286f, 0.0f, -26.0343609f),
normal, color,
irr::core::vector2df(2.86629152f, 1.30171800f)));
buffer->Vertices.push_back(irr::video::S3DVertex(irr::core::vector3df(34.3258286f, 0.0f, -30.0573101f),
normal, color,
irr::core::vector2df(1.71629155f, 1.50286555f)));
buffer->Vertices.push_back(irr::video::S3DVertex(irr::core::vector3df(57.3258286f, 0.0f, -30.0573101f),
normal, color,
irr::core::vector2df(2.86629152f, 1.50286555f)));
// Add triangles
buffer->Indices.push_back(0);
buffer->Indices.push_back(1);
buffer->Indices.push_back(2);
buffer->Indices.push_back(2);
buffer->Indices.push_back(3);
buffer->Indices.push_back(4);
buffer->Indices.push_back(5);
buffer->Indices.push_back(4);
buffer->Indices.push_back(3);
buffer->Indices.push_back(2);
buffer->Indices.push_back(4);
buffer->Indices.push_back(6);
buffer->Indices.push_back(5);
buffer->Indices.push_back(3);
buffer->Indices.push_back(0);
buffer->Indices.push_back(2);
buffer->Indices.push_back(6);
buffer->Indices.push_back(7);
buffer->Indices.push_back(7);
buffer->Indices.push_back(5);
buffer->Indices.push_back(0);
buffer->Indices.push_back(0);
buffer->Indices.push_back(2);
buffer->Indices.push_back(7);
buffer->Indices.push_back(4);
buffer->Indices.push_back(5);
buffer->Indices.push_back(7);
buffer->Indices.push_back(7);
buffer->Indices.push_back(6);
buffer->Indices.push_back(4);
buffer->recalculateBoundingBox();
mesh->recalculateBoundingBox();
irr::scene::IMeshSceneNode* node(smgr->addMeshSceneNode(mesh));
auto bitmap = CSingleColorBitmap(0, 255, 0);
irr::io::IReadFile* textureFile(device->getFileSystem()->createMemoryReadFile(bitmap.Data(), bitmap.Size(), "bitmap"));
irr::video::ITexture* texture(device->getVideoDriver()->getTexture(textureFile));
textureFile->drop();
node->setMaterialTexture(0, texture);
node->setMaterialFlag(irr::video::EMF_GOURAUD_SHADING, false);
node->setDebugDataVisible(irr::scene::EDS_MESH_WIRE_OVERLAY);
/*
Now draw everything and finish.
*/
u32 frames=0;
while(device->run())
{
driver->beginScene(true, true, video::SColor(0,100,100,100));
smgr->drawAll();
driver->endScene();
if (++frames==100)
{
core::stringw str = L"Irrlicht Engine [";
str += driver->getName();
str += L"] FPS: ";
str += (s32)driver->getFPS();
device->setWindowCaption(str.c_str());
frames=0;
}
}
device->drop();
return 0;
}
If anyone knows why the triangles are lighted so differently, or even how to solve it
David.