Jump to content

[SOLVED] createTangents() functin got wrong tangents


photo

Recommended Posts

 

I try to create a simple box with following code, but the rendering result was not as expected.

Any idea to solve the problem?

Thanks in advance.

int AppWorldLogic::init()
{
	// Write here code to be called on world initialization: initialize resources for your world scene during the world start.
	MeshPtr box1 = Mesh::create();
	box1->addSurface("box1");
	box1->addVertex(vec3(-0.5, -0.5, -0.5), 0);
	box1->addVertex(vec3(-0.5, -0.5, 0.5), 0);
	box1->addVertex(vec3(-0.5, 0.5, -0.5), 0);
	box1->addVertex(vec3(-0.5, 0.5, 0.5), 0);
	box1->addVertex(vec3(0.5, -0.5, -0.5), 0);
	box1->addVertex(vec3(0.5, -0.5, 0.5), 0);
	box1->addVertex(vec3(0.5, 0.5, -0.5), 0);
	box1->addVertex(vec3(0.5, 0.5, 0.5), 0);

	int index[36] = { 6,7,5,5,4,6,0,1,3,3,2,0,2,3,7,7,6,2,4,5,1,1,0,4,3,1,5,5,7,3,0,2,6,6,4,0 };
	for (int i = 0; i < 36; i++)
		box1->addIndex(index[i], 0);

	box1->createBounds(0);
	box1->createTangents(0);
	box1->createNormals(0);
	
	ObjectMeshDynamicPtr BOX = ObjectMeshDynamic::create(box1);
	BOX->translate(Vec3(0, 0, 0.5));
	BOX->release();
      ...

Snipaste_2019-09-06_14-03-30.jpg.07956778353e4ab247335f6d5fee90c7.jpg

Link to comment

More likely it happens because createNormals generates smoothed normals. To avoid it you need to create normals on your side.

Something like this:

void create_box(MeshPtr &mesh, const char *name, const vec3 &size)
{
	static const vec3 vertex[8] = {
		vec3(-0.5f, -0.5f, -0.5f), vec3(0.5f, -0.5f, -0.5f), vec3(-0.5f, 0.5f, -0.5f), vec3(0.5f, 0.5f, -0.5f),
		vec3(-0.5f, -0.5f, 0.5f), vec3(0.5f, -0.5f, 0.5f), vec3(-0.5f, 0.5f, 0.5f), vec3(0.5f, 0.5f, 0.5f),
	};
	static const vec3 normals[6] = {
		vec3(1.0f, 0.0f, 0.0f), vec3(-1.0f, 0.0f, 0.0f),
		vec3(0.0f, 1.0f, 0.0f), vec3(0.0f, -1.0f, 0.0f),
		vec3(0.0f, 0.0f, 1.0f), vec3(0.0f, 0.0f, -1.0f),
	};
	static const vec2 texcoords[4] = {
		vec2(1.0f, 1.0f), vec2(0.0f, 1.0f),
		vec2(0.0f, 0.0f), vec2(1.0f, 0.0f),
	};
	static const int cindices[6][4] = {
		{3, 1, 5, 7}, {0, 2, 6, 4},
		{2, 3, 7, 6}, {1, 0, 4, 5},
		{6, 7, 5, 4}, {0, 1, 3, 2},
	};
	static const int indices[6] = {
		0, 3, 2, 2, 1, 0,
	};

	int surface = mesh->addSurface(name);

	for (int i = 0; i < 6; i++)
	{
		for (int j = 0; j < 6; j++)
		{
			int index = indices[j];
			mesh->addVertex(vertex[cindices[i][index]] * size, surface);
			mesh->addNormal(normals[i], surface);
			if (i == 4)
			{
				mesh->addTexCoord0(vec2::ONE - texcoords[index], surface);
				mesh->addTexCoord1(vec2::ONE - texcoords[index], surface);
			} else
			{
				mesh->addTexCoord0(texcoords[index], surface);
				mesh->addTexCoord1(texcoords[index], surface);
			}
		}
	}

	mesh->createIndices(surface);
	mesh->createTangents(surface);
	mesh->createBounds(surface);
}

 

How to submit a good bug report
---
FTP server for test scenes and user uploads:

Link to comment
  • silent changed the title to [SOLVED] createTangents() functin got wrong tangents
×
×
  • Create New...