Vertex normals vs face normals

I’m creating a custom cube mesh in an of3Dprimitive. I duplicated all my vertices in order to have 3 normals per vertex instead of averaging them. The vertex normals i’ve created “manually” are all oriented properly. When i use “.drawNormals(50, false)”, they all show up where thy should. But if i use “.drawNormals(50, true)” to draw the face normals, many of them are not where they should. The base for some of them are even inside my cube (like the base is inside but the tip is outside). Is this going to be a problem when i add lighting or other shader effects?? If yes, how can i correct this?. I’ve double checked, and all my triangle vertices seem to be going counterclockwise (as they should?)

Here is the code for the mesh creation:

void Cuboid::addVerticesTriangleMesh() {
	ofMesh * meshPtr = &m_facePrimitive.getMesh();
	float vertices[24][3] = {
		// front face
		{m_position.x, m_position.y, m_position.z},
		{m_position.x + m_width, m_position.y, m_position.z},
		{m_position.x + m_width, m_position.y + m_height, m_position.z},
		{m_position.x, m_position.y + m_height, m_position.z},

		// right face
		{m_position.x + m_width, m_position.y, m_position.z},
		{m_position.x + m_width, m_position.y, m_position.z - m_depth},
		{m_position.x + m_width, m_position.y + m_height, m_position.z - m_depth},
		{m_position.x + m_width, m_position.y + m_height, m_position.z},

		// back face
		{m_position.x + m_width, m_position.y, m_position.z - m_depth},
		{m_position.x, m_position.y, m_position.z - m_depth},
		{m_position.x, m_position.y + m_height, m_position.z - m_depth},
		{m_position.x + m_width, m_position.y + m_height, m_position.z - m_depth},

		// left face
		{m_position.x, m_position.y, m_position.z - m_depth},
		{m_position.x, m_position.y, m_position.z},
		{m_position.x, m_position.y + m_height, m_position.z},
		{m_position.x, m_position.y + m_height, m_position.z - m_depth},

		// top face
		{m_position.x, m_position.y, m_position.z - m_depth},
		{m_position.x + m_width, m_position.y, m_position.z - m_depth},
		{m_position.x + m_width, m_position.y, m_position.z},
		{m_position.x, m_position.y, m_position.z},

		// bottom face
		{m_position.x, m_position.y + m_height, m_position.z},
		{m_position.x + m_width, m_position.y + m_height, m_position.z},
		{m_position.x + m_width, m_position.y + m_height, m_position.z - m_depth},
		{m_position.x, m_position.y + m_height, m_position.z - m_depth}
	};

	for (int i = 0; i < 24; ++i) {
		meshPtr->addVertex(ofVec3f(vertices[i][0], vertices[i][1], vertices[i][2]));
		meshPtr->addColor(m_fillColor);
	}

	meshPtr = nullptr;
}

void Cuboid::addIndicesTriangleMesh() {
	ofMesh* meshPtr = &m_facePrimitive.getMesh();

	int indices[12][3] = {
	{1, 0, 3}, {1, 3, 2},
	{5, 4, 7}, {5, 7, 6},
	{9, 8, 11}, {9, 11, 10},
	{13, 12, 15}, {13, 15, 14},
	{17, 16, 19}, {17, 19, 18},
	{21, 20, 23}, {21, 23, 22}
	};

	for (int i = 0; i < 12; ++i) {
		meshPtr->addIndex(indices[i][0]);
		meshPtr->addIndex(indices[i][1]);
		meshPtr->addIndex(indices[i][2]);
	}
	meshPtr = nullptr;
}

void Cuboid::addNormalsTriangleMesh() {
	ofMesh* meshPtr = &m_facePrimitive.getMesh();
	std::vector<glm::vec3> vertices = meshPtr->getVertices();
	std::vector<unsigned int> indices = meshPtr->getIndices();
	std::vector<glm::vec3> normals(24, glm::vec3(0, 0, 0)) ;


	glm::vec3 v1;
	glm::vec3 v2;
	glm::vec3 v3;
	glm::vec3 faceNormal;

	for (int i = 0; i < 6; ++i) {
		v1 = vertices[(int)indices[6 * i]];
		v2 = vertices[(int)indices[6 * i + 1]];
		v3 = vertices[(int)indices[6 * i + 2]];

		faceNormal = glm::normalize(glm::cross(v1 - v2, v3 - v2));

		for (int j = 0; j < 4; ++j) {
			normals[i * 4 + j] = faceNormal;
		}
	}
	meshPtr->addNormals(normals);
	meshPtr = nullptr;
}

And here is a picture of the normals:

I dont think that the faces normals matter when illuminating, as these dont get passed to a shader rather the vertex normals. I might be wrong but that is what my gut tells me.

1 Like