Jump to content
  • Advertisement
Sign in to follow this  
Paul Griffiths

Generate tangents for GL_TRIANGLE_STRIP?

This topic is 513 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

'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)));
	}
Edited by paulgriffiths

Share this post


Link to post
Share on other sites
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.

Edited by Mike2343

Share this post


Link to post
Share on other sites

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.

Edited by paulgriffiths

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!