Calculate normal

General Programming help in C, C++ or ASM, Compiling / Debugging, and R3000A Central Processing Unit (CPU) information
Post Reply
masmorra
Interested PSXDEV User
Interested PSXDEV User
Posts: 8
Joined: October 15th, 2020, 5:50 am

Calculate normal

Post by masmorra » November 29th, 2020, 1:15 am

Hi guys, I'm making some terrain using perlin noise and my normals for lighting are not looking right, can someone help me find the error?

Code: Select all

const auto x1y1 = perlin.noise2d(x1, y1),
           x0y0 = perlin.noise2d(x0, y0),
           ...;
                
mesh[meshIdx]   = {X+1  , x1y1, Z+1, 0};
mesh[meshIdx+1] = {X    , x0y0, Z  , 0};
mesh[meshIdx+2] = {X    , x0y1, Z+1, 0};

//calculate side2 - side1 for cross
const SVECTOR side1 = {mesh[meshIdx+1].vx - mesh[meshIdx].vx, mesh[meshIdx+1].vy - mesh[meshIdx].vy, mesh[meshIdx+1].vz - mesh[meshIdx].vz };
const SVECTOR side2 = {mesh[meshIdx+1].vx - mesh[meshIdx+2].vx, mesh[meshIdx+1].vy - mesh[meshIdx+2].vy, mesh[meshIdx+1].vz - mesh[meshIdx+2].vz };
                
normals[0] = MathUtils::cross(side1, side2);

mesh[meshIdx+3] = {X  , x0y0, Z,   0};
mesh[meshIdx+4] = {X+1, x1y1, Z+1, 0};
mesh[meshIdx+5] = {X+1, x1y0, Z,   0};

//calculate side4 - side3 for cross
const SVECTOR side3 = {mesh[meshIdx+4].vx - mesh[meshIdx+3].vx, mesh[meshIdx+4].vy - mesh[meshIdx+3].vy, mesh[meshIdx+4].vz - mesh[meshIdx+3].vz };
const SVECTOR side4 = {mesh[meshIdx+4].vx - mesh[meshIdx+5].vx, mesh[meshIdx+4].vy - mesh[meshIdx+5].vy, mesh[meshIdx+4].vz - mesh[meshIdx+5].vz };

normals[1] = MathUtils::cross(side3, side4);

and the code for cross:

Code: Select all

static constexpr SVECTOR cross(const SVECTOR& left, const SVECTOR& right)
{
    return {((left.vy*right.vz)-(left.vz*right.vy))>>12,
            ((left.vz*right.vx)-(left.vx*right.vz))>>12,
            ((left.vx*right.vy)-(left.vy*right.vx))>>12
    };
}
In the model that I created in blender have the nomals correct, they are generated by blender, but my manual mesh does not look good. The faces behind the hills are darker, but the faces in front are not lighter like in the model.
map.png
map2.png
I'm using psn00bsdk + gcc 10.2

Thanks for the help.
You do not have the required permissions to view the files attached to this post.

masmorra
Interested PSXDEV User
Interested PSXDEV User
Posts: 8
Joined: October 15th, 2020, 5:50 am

Re: Calculate normal

Post by masmorra » November 29th, 2020, 12:19 pm

I got it!
The base color of the terrain was too dark to see the ligth changes. It was gray (128, 128, 128) :oops:
Now it looks ok! :roll:

EDIT: It was not what I stated above. My error is that I was not normalizing the cross, now I'm using the VectorNormalS function.
map3.PNG
You do not have the required permissions to view the files attached to this post.

Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest