Wrong normal for triangles

  3d-rendering, c++, normals

I am making a 3D game using SFML. I want to use normals to check if the triangle need to be draw in a terrain (triangle) mesh.
Here is my code:

vec3d line1, line2, normal;
line1.x = terrain.tris[i].p[0].x - terrain.tris[i].p[1].x;
line1.y = terrain.tris[i].p[0].y - terrain.tris[i].p[1].y;
line1.z = terrain.tris[i].p[0].z - terrain.tris[i].p[1].z;

line2.x = terrain.tris[i].p[1].x - terrain.tris[i].p[2].x;
line2.y = terrain.tris[i].p[1].y - terrain.tris[i].p[2].y;
line2.z = terrain.tris[i].p[1].z - terrain.tris[i].p[2].z;

normal.x = line1.y * line2.z - line1.z * line2.y;
normal.y = line1.z * line2.x - line1.x * line2.z;
normal.z = line1.x * line2.y - line1.y * line2.x;
vec3d vCameraRay = Vector_Sub(terrain.tris[i].p[0], cam.pos);
if (Vector_DotProduct(normal, vCameraRay) < 0.0f){
    do_something();
}

Vector_DotProduct:

float Vector_DotProduct(vec3d& v1, vec3d& v2) {
    return v1.x * v2.x + v1.y * v2.y + v1.z * v2.z;
}

Vector_sub:

vec3d Vector_Sub(vec3d& v1, vec3d& v2) {
    return { v1.x - v2.x, v1.y - v2.y, v1.z - v2.z };
}

And the vec3d is just a struct that contains

float x, y, z;

But whenever I run the program, I always get this
problem
I am not sure if this is because of the way I produce the triangle grid, or something else, so I am going to put all the things that might matter here:
The code for producing triangle grid:

for (int i = 0; i < 2; i++) {
    for (int y = 0; y < wds; y++) {
        for (int x = 0; x < wds; x++) {
            if (x + 1 < wds && y + 1 < wds) {
                vec3d point[3];
                switch (i) {
                case 0:
                    point[0] = { (float)(y + 1) * scl + p.x, height * h[y + 1][x + 1], (float)(x + 1) * scl + p.z };
                    point[1] = { (float)y * scl + p.x, height * h[y][x], (float)x * scl + p.z };
                    point[2] = { (float)y * scl + p.x, height * h[y][x + 1], (float)(x + 1) * scl + p.z };
                    break;
                case 1:
                    point[0] = { (float)(y + 1) * scl + p.x, height * h[y + 1][x + 1], (float)(x + 1) * scl + p.z };
                    point[2] = { (float)y * scl + p.x, height * h[y][x], (float)x * scl + p.z };
                    point[1] = { (float)(y + 1) * scl + p.x, height * h[y + 1][x], (float)x * scl + p.z };
                    break;
                };
                triangle out = { point[0], point[1], point[2] };
                tris.push_back(out);
            }
        }
    }
}

The wds is for the size of the grid(side length), the scl is the size of per grid, the h is the height map(two dimentions), and p is the position of the upper left corner.

My 3D point to camera point code:

float mx = p.x - pos.x;
float my = p.y - pos.y;
float mz = p.z - pos.z;
float dx = cos(rot.y) * (sin(rot.z) * my + cos(rot.z) * mx) - sin(rot.y) * mz;
float dy = sin(rot.x) * (cos(rot.y) * mz + sin(rot.y) * (sin(rot.z) * my + cos(rot.z) * mx)) + cos(rot.x) * (cos(rot.z) * my + sin(rot.z) * mx);
float dz = cos(rot.x) * (cos(rot.y) * mz + sin(rot.y) * (sin(rot.z) * my + cos(rot.z) * mx)) - sin(rot.x) * (cos(rot.z) * my + sin(rot.z) * mx);
return { dx, dy, dz };

The rot is the rotation of the camera, p is the position of the camera, and the pos is the 3D point I want to transfer to camera point.
I have been working on this problem for almost a week, but nothing seems to work.
It will be a lot of help if you guys can find the problem. Thanks in advance!

Source: Windows Questions C++

LEAVE A COMMENT