What I have here seems to create the proper vertices and triangles, according to the console output, but it doesn't actually render. I've been staring at it and these forums for about an hour and I can't figure it out.
Is it because I did not give the vertices texture coordinates, even though I don't actually have a texture assigned to it? And if so, what would be a good way to assign proper values to them?
Or maybe it's something else entirely?
Thanks in advance!
Terrain.h
Code: Select all
class Terrain : public irr::scene::ISceneNode
{
public:
Terrain(irr::scene::ISceneManager* mgr, int id, irr::scene::ISceneNode* parent);
~Terrain();
void recalculateNormals();
void recalculateBoundingBox();
virtual void OnRegisterSceneNode();
virtual void render();
virtual const irr::core::aabbox3d<float>& getBoundingBox() const;
virtual unsigned int getMaterialCount() const;
virtual irr::video::SMaterial& getMaterial(unsigned int i);
private:
irr::core::array<irr::video::S3DVertex> vertex;
irr::core::array<unsigned short> index;
irr::core::aabbox3d<float> Box;
irr::video::SMaterial Material;
};
terrain.cpp
Code: Select all
#include <irrlicht.h>
#include "terrain.h"
Terrain::Terrain(irr::scene::ISceneManager* mgr, int id, irr::scene::ISceneNode* parent) : irr::scene::ISceneNode(parent, mgr, id)
{
float tileSize = 100.0;
unsigned int tileX = 10;
unsigned int tileZ = 10;
unsigned int x;
unsigned int z;
for (x = 0; x <= tileX; x++)
{
for (z = 0; z <= tileZ; z++)
{
irr::video::S3DVertex point = irr::video::S3DVertex
(
x * tileSize,
0,
z * tileSize,
0,
0,
0,
irr::video::SColor(255, 255, 255, 255),
0,
0
);
vertex.push_back(point);
printf("Added point: %f x %f\n", x * tileSize, z * tileSize);
}
}
for (x = 0; x < tileX; x++)
{
for (z = 0; z < tileZ; z++)
{
unsigned int upper = z + x + (x * tileZ);
unsigned int lower = z + (x + 1) + ((x + 1) * tileZ);
index.push_back(upper);
index.push_back(upper + 1);
index.push_back(lower);
printf("Added triangle: %i, %i, %i\n", upper, upper + 1, lower);
index.push_back(lower);
index.push_back(upper + 1);
index.push_back(lower + 1);
printf("Added triangle: %i, %i, %i\n", lower, upper + 1, lower + 1);
}
}
recalculateNormals();
recalculateBoundingBox();
}
Terrain::~Terrain()
{
}
void Terrain::recalculateNormals()
{
for (int i = 0; i < index.size(); i += 3)
{
irr::core::plane3d<float> plane
(
vertex[index[i]].Pos,
vertex[index[i + 1]].Pos,
vertex[index[i + 2]].Pos
);
vertex[index[i]].Normal = plane.Normal;
vertex[index[i + 1]].Normal = plane.Normal;
vertex[index[i + 2]].Normal = plane.Normal;
}
}
void Terrain::recalculateBoundingBox()
{
for (int i = 0; i < vertex.size(); i++)
{
Box.addInternalPoint(vertex[i].Pos);
}
}
void Terrain::OnRegisterSceneNode()
{
if (IsVisible)
{
SceneManager->registerNodeForRendering(this);
}
ISceneNode::OnRegisterSceneNode();
}
void Terrain::render()
{
SceneManager->getVideoDriver()->setMaterial(Material);
SceneManager->getVideoDriver()->setTransform(irr::video::ETS_WORLD, AbsoluteTransformation);
SceneManager->getVideoDriver()->drawIndexedTriangleList(vertex.const_pointer(), vertex.size(), index.const_pointer(), index.size());
}
const irr::core::aabbox3d<float>& Terrain::getBoundingBox() const
{
return Box;
}
unsigned int Terrain::getMaterialCount() const
{
return 1;
}
irr::video::SMaterial& Terrain::getMaterial(unsigned int i)
{
return Material;
}