Generate tangents for GL_TRIANGLE_STRIP?

Started by
2 comments, last by 21st Century Moose 7 years, 1 month ago

'm using the following code to produce tangents for GL_TRIANGLES, but how do I do it for GL_TRIANGLE_STRIP? Is it possible? It's for a terrain height mesh. I could do it out of triangles but would be inefficient.


        for (unsigned int i = 0; i<getNumVertices(); i += 3)
	{
		// Shortcuts for vertices
		glm::vec3 & v0 = getVertice(i + 0);
		glm::vec3 & v1 = getVertice(i + 1);
		glm::vec3 & v2 = getVertice(i + 2);

		// Shortcuts for UVs
		glm::vec2 & uv0 = getUV(i + 0);
		glm::vec2 & uv1 = getUV(i + 1);
		glm::vec2 & uv2 = getUV(i + 2);

		// Get normals of the polygon
		glm::vec3 &n0 = getNormal(i + 0);
		glm::vec3 &n1 = getNormal(i + 1);
		glm::vec3 &n2 = getNormal(i + 2);

		glm::vec2 deltaUV1 = uv1 - uv0;
		glm::vec2 deltaUV2 = uv2 - uv0;

		// Edges of the triangle : postion delta
		glm::vec3 deltaPos1 = v1 - v0;
		glm::vec3 deltaPos2 = v2 - v0;

		// Calculate tangent and bitangent
		float r = 1.0f / (deltaUV1.x * deltaUV2.y - deltaUV1.y * deltaUV2.x);
		glm::vec3 tangent = (deltaPos1 * deltaUV2.y - deltaPos2 * deltaUV1.y) * r;
		glm::vec3 bitangent = (deltaPos2 * deltaUV1.x - deltaPos1 * deltaUV2.x) * r;

		// Set the same tangent for all three vertices of the triangle.
		getTangents()->push_back(glm::normalize(tangent - n0 * glm::dot(n0, tangent)));
		getTangents()->push_back(glm::normalize(tangent - n1 * glm::dot(n1, tangent)));
		getTangents()->push_back(glm::normalize(tangent - n2 * glm::dot(n2, tangent)));
		// Same thing for binormals
		getBitangents()->push_back(glm::normalize(bitangent - n0 * glm::dot(n0, bitangent)));
		getBitangents()->push_back(glm::normalize(bitangent - n1 * glm::dot(n1, bitangent)));
		getBitangents()->push_back(glm::normalize(bitangent - n2 * glm::dot(n2, bitangent)));
	}
Advertisement

EDIT - So they're not depreciated, the search I did showed them as being such, my bad.

Though I still had issues with them being decently slower than regular triangles on more than a couple cards. YMMV.

"Those who would give up essential liberty to purchase a little temporary safety deserve neither liberty nor safety." --Benjamin Franklin

Not sure what version of OpenGL you're using but in OpenGL 3.0 triangle strips are deprecated and updating older code a while ago, they had speed issues on a couple cards. I would just stick with GL_TRIANGLES.

Thanks for the info. Don't want to use deprecated code so ill convert.

The only code in my engine using triangle strip is a sphere which I don't use.

Triangle strips are NOT deprecated. The only deprecated primitive type is GL_QUADS: https://www.khronos.org/opengl/wiki/Primitive#Quads

Despite that, you are still better off using GL_TRIANGLES (with indices where required) for everything as it is more likely to be better optimized by the driver, and gives better vertex reuse than strips or fans.

Direct3D has need of instancing, but we do not. We have plenty of glVertexAttrib calls.

This topic is closed to new replies.

Advertisement