Normal map not displaying correctly

If you are a new Irrlicht Engine user, and have a newbie-question, this is the forum for you. You may also post general programming questions here.
Post Reply
ErUs
Posts: 165
Joined: Thu Oct 07, 2004 6:13 pm

Normal map not displaying correctly

Post by ErUs »

I think i'm getting my vertex normal angles wrong.
Could someone correct me please?

Code: Select all

#include <irr/irrlicht.h>

using namespace irr;

using namespace core;
using namespace scene;
using namespace video;
using namespace io;
using namespace gui;

class SimpleLandBodyNode : public scene::ISceneNode {
	core::aabbox3d<f32> Box;
	video::S3DVertex Vertices[24];
	video::SMaterial *mat;

public:
	SimpleLandBodyNode(scene::ISceneManager* mgr, video::SMaterial *m)
			: scene::ISceneNode(mgr->getRootSceneNode(), mgr, -1) {
				mat = m;

				// Front
				Vertices[0] = video::S3DVertex(0,0,1, 0,0,1
                                video::SColor(255,255,255,255), 0, 1);
                Vertices[1] = video::S3DVertex(1,0,1, 0,0,1
                                video::SColor(255,255,255,255), 1, 1);
                Vertices[2] = video::S3DVertex(1,1,1, 0,0,1
                                video::SColor(255,255,255,255), 1, 0);
                Vertices[3] = video::S3DVertex(0,1,1, 0,0,1
                                video::SColor(2255,255,255,255), 0, 0);

				// Back
				Vertices[4] = video::S3DVertex(0,0,0, 0,0,-1,
                                video::SColor(255,255,255,255), 1, 1);
                Vertices[5] = video::S3DVertex(0,1,0, 0,0,-1,
                                video::SColor(255,255,255,255), 0, 1);
                Vertices[6] = video::S3DVertex(1,1,0, 0,0,-1,
                                video::SColor(255,255,255,255), 0, 0);
                Vertices[7] = video::S3DVertex(1,0,0, 0,0,-1,
                                video::SColor(255,255,255,255), 1, 0);

				// Top
				Vertices[8] = video::S3DVertex(0,1,0, 0,1,0,
                                video::SColor(255,255,255,255), 1, 1);
                Vertices[9] = video::S3DVertex(0,1,1, 0,1,0,
                                video::SColor(255,255,255,255), 0, 1);
                Vertices[10] = video::S3DVertex(1,1,1, 0,1,0,
                                video::SColor(255,255,255,255), 0, 0);
                Vertices[11] = video::S3DVertex(1,1,0, 0,1,0,
                                video::SColor(255,255,255,255), 1, 0);

				// Bottom
				Vertices[12] = video::S3DVertex(0,0,0, 0,-1,0,
                                video::SColor(255,255,255,255), 1, 1);
                Vertices[13] = video::S3DVertex(1,0,0, 0,-1,0,
                                video::SColor(255,255,255,255), 0, 1);
                Vertices[14] = video::S3DVertex(1,0,1, 0,-1,0,
                                video::SColor(255,255,255,255), 0, 0);
                Vertices[15] = video::S3DVertex(0,0,1, 0,-1,0,
                                video::SColor(255,255,255,255), 1, 0);

				// Right
				Vertices[16] = video::S3DVertex(1,0,0, 1,0,0,
                                video::SColor(255,255,255,255), 1, 1);
                Vertices[17] = video::S3DVertex(1,1,0, 1,0,0,
                                video::SColor(255,255,255,255), 0, 1);
                Vertices[18] = video::S3DVertex(1,1,1, 1,0,0,
                                video::SColor(255,255,255,255), 0, 0);
                Vertices[19] = video::S3DVertex(1,0,1, 1,0,0,
                                video::SColor(255,255,255,255), 1, 0);

				// Left
				Vertices[20] = video::S3DVertex(0,0,0, -1,0,0,
                                video::SColor(255,255,255,255), 1, 1);
                Vertices[21] = video::S3DVertex(0,0,1, -1,0,0,
                                video::SColor(255,255,255,255), 0, 1);
                Vertices[22] = video::S3DVertex(0,1,1, -1,0,0,
                                video::SColor(255,255,255,255), 0, 0);
                Vertices[23] = video::S3DVertex(0,1,0, -1,0,0,
                                video::SColor(255,255,255,255), 1, 0);

				Box.reset(Vertices[0].Pos);

				for (s32 i=1; i<24; ++i)
					Box.addInternalPoint(Vertices[i].Pos);
	}

	virtual void OnRegisterSceneNode() {
		if (IsVisible)
			SceneManager->registerNodeForRendering(this);

		ISceneNode::OnRegisterSceneNode();
	}

	virtual void render() {
		u16 indices[] = {
			0,  1,  2,      0,  2,  3,    // front
			4,  5,  6,      4,  6,  7,    // back
			8,  9,  10,     8,  10, 11,   // top
			12, 13, 14,     12, 14, 15,   // bottom
			16, 17, 18,     16, 18, 19,   // right
			20, 21, 22,     20, 22, 23    // left
		};
		video::IVideoDriver* driver = SceneManager->getVideoDriver();

		driver->setMaterial(*mat);
		driver->setTransform(video::ETS_WORLD, AbsoluteTransformation);
		driver->drawVertexPrimitiveList(&Vertices[0], 24, &indices[0], 12, 				video::EVT_STANDARD, scene::EPT_TRIANGLES, video::EIT_16BIT);
	}

	virtual const core::aabbox3d<f32>& getBoundingBox() const {
		return Box;
	}

	virtual u32 getMaterialCount() const {
		return 1;
	}

	virtual video::SMaterial& getMaterial(u32 i) {
		return *mat;
	}       
};

int main(int arc, char **argv) {

	IrrlichtDevice *device =
		createDevice(video::EDT_OPENGL, dimension2d<u32>(640, 480), 16,
			false, false, false, 0);

	if (!device)
		return 1;

	device->setWindowCaption(L"Hello world!");

	IVideoDriver* driver = device->getVideoDriver();
	ISceneManager* smgr = device->getSceneManager();
	IGUIEnvironment* guienv = device->getGUIEnvironment();

	ISceneNode *cam = smgr->addCameraSceneNodeFPS(0, 100, 0.05);
	cam->setPosition(vector3df(0.5,0.5,-3));

	//-- setup material

	video::ITexture* normalMap =
		driver->getTexture("rockwall_height.bmp");

	driver->makeNormalMapTexture(normalMap, 9.0f);

	video::SMaterial rock;
	rock.Wireframe = false;
	rock.Lighting = true;
	rock.setTexture(0, driver->getTexture("test.bmp"));
	rock.setTexture(1, normalMap);
	rock.MaterialType = video::EMT_PARALLAX_MAP_SOLID;

	// Add light

	SimpleLandBodyNode *n =
		new SimpleLandBodyNode(smgr, &rock);

	scene::ILightSceneNode* light1 =
                smgr->addLightSceneNode(0, core::vector3df(0,0,0),
                video::SColorf(1.0f, 1.0f, 1.0f, 1.0f), 800.0f);

	light1->setParent(cam);
	

	// Add scene nodes

	scene::ISceneNodeAnimator* anim =
		smgr->createRotationAnimator(core::vector3df(0.2f, 0, 0));

	n->addAnimator(anim);
	
	while(device->run()) {
		driver->beginScene(true, true, SColor(255,100,101,140));

		smgr->drawAll();
		guienv->drawAll();

		driver->endScene();
	}
}
Post Reply