I startet a fun project with Ogre 2.1 some time ago and faced into a Problem generating Meshes out of Octrees.

My problem is that the lighting isnt working in that way i want it to.

It looks like the Normal Vector of an Vertex doesnt fit.

To get you into this i want to show you my idea of calculating Normals for a QuadFace from one mesh:

First i got a DTO called MeshData which contains an Index and Vertex Buffer holding the Vertices (Pos (Vector3D) , Normal (Vector3Df), TCoords(Vector2D), Color(OgreColor))

MeshData has an function AddQuadFace witch adds 4 independent Vertices into the Mesh.

(Asuming that the left bottom corner = 0, right bottom corner = 1, right top corner = 2, left top corner = 3)

Code: Select all

`void MeshData::AddQuadFace(Vertex& v0, Vertex& v1, Vertex& v2, Vertex& v3)`

Code: Select all

```
v0.normal = ((v3.pos - v0.pos) * (v2.pos - v0.pos)) + ((v2.pos - v0.pos) * (v1.pos - v0.pos));
v1.normal = (v0.pos - v1.pos) * (v2.pos - v1.pos);
v2.normal = ((v3.pos - v2.pos) * (v0.pos - v2.pos)) + ((v0.pos - v2.pos) * (v1.pos - v2.pos));
v3.normal = (v2.pos - v3.pos) * (v0.pos - v3.pos);
```

// Cross *

Code: Select all

```
Vector3D Vector3D::operator*(const Vector3D& v1) const
{
return (Vector3D(this->y * v1.z - this->z * v1.y, this->z*v1.x - this->x * v1.z, this->x*v1.y - this->y * v1.x));
}
Vector3D Vector3D::operator-(Vector3D const& _v1) const
{
return (Vector3D(this->x - _v1.x, this->y - _v1.y, this->z - _v1.z));
}
Vector3Df& Vector3Df::operator=(Vector3D const& _v1)
{
this->x = _v1.x;
this->y = _v1.y;
this->z = _v1.z;
return (*this);
}
```

After that calculation ill add this 4 Points into an std::List:

Code: Select all

```
// now add them into the Vertex list and save the VertexIndex
int p0 = this->AddVertex(v0);
int p1 = this->AddVertex(v1);
int p2 = this->AddVertex(v2);
int p3 = this->AddVertex(v3);
```

Code: Select all

```
int index = 0;
for (auto it = this->m_vertexList.begin(), end = this->m_vertexList.end(); it != end; it++)
{
if (it->pos == _vertex.pos && it->color == _vertex.color)
{
it->normal = it->normal + _vertex.normal;
return index;
}
index++;
}
this->m_vertexList.push_back(Vertex(_vertex));
return (this->m_vertexList.size()-1);
```

Code: Select all

```
// generate face 1
this->m_indexList.push_back(p0);
this->m_indexList.push_back(p3);
this->m_indexList.push_back(p2);
// generate face 2
this->m_indexList.push_back(p0);
this->m_indexList.push_back(p2);
this->m_indexList.push_back(p1);
```

if the whole mesh is finished. Ill iterate trough the whole vertex list and normalize the normals (length = 1)

Code: Select all

```
for(std::vector<Vertex>::iterator curVertex = this->m_vertexList.begin(); curVertex != this->m_vertexList.end(); ++curVertex)
{
curVertex->normal.normalize();
}
```

Code: Select all

```
void Vector3Df::normalize()
{
double length = this->getLength();
this->x = this->x / length;
this->y = this->y / length;
this->z = this->z / length;
}
```

Greetings

Nexxy