Ogre Mesh Normals

A place for users of OGRE to discuss ideas and experiences of utilitising OGRE in their games / demos / applications.
Post Reply
Nexxxy
Gnoblar
Posts: 1
Joined: Tue Jun 23, 2015 11:44 am

Ogre Mesh Normals

Post by Nexxxy » Tue Jun 23, 2015 12:07 pm

Hi Users :)

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.
Image

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)
inside AddQuadFace ill calculate Normals for each Vertex:

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);
with Vector operator overloading:

// 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);
}
I dont use the TCoords atm since they have no effect to this LightProblem.

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);
inside AddVertex ill iterate through the Vertex list searching for an equal point with same pos and color conditions. If i find one ill sum up the normals and return his index. Elsewise i create a new item in this VertexList.

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);
Next step would be the index Buffer .. just build 2 tris:

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);
last but not least ..
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();
    }
with 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;
}
I tryed to search for in this form as well as i did many hours of research with google to find a solution. .. Mb i searched too long to find the right solution .. I rly hope that someone out there can help me. Where is my mistake ?

Greetings
Nexxy
0 x

User avatar
Kojack
OGRE Moderator
OGRE Moderator
Posts: 7147
Joined: Sun Jan 25, 2004 7:35 am
Location: Brisbane, Australia
x 16

Re: Ogre Mesh Normals

Post by Kojack » Wed Jun 24, 2015 1:43 pm

Nexxxy wrote:inside AddVertex ill iterate through the Vertex list searching for an equal point with same pos and color conditions. If i find one ill sum up the normals and return his index. Elsewise i create a new item in this VertexList.
That's probably the problem. Merging vertices and summing normals is only for smooth surfaces. Sharp edges like on cubes shouldn't be merged. For example, a cube should have 24 vertices, each corner needs 3 vertices with unique normals.
0 x

Post Reply