Skip to main content
GameDev.net gamedev.net
🔒 Locked

Help! Tangent Calculation

Started by JasonGillespi Mar 19, 2009 at 1:08 PM 0 replies 800+ views
Original Post
JasonGillespi
JasonGillespi
Hi, I'm having trouble figuring out the tangent vector for bsp code in the project I'm working in. I'm trying to rewrite my Simple Tangent Calculation using the following verts. I need help on how to write a CalculateTangent function with this vert array to get a tangent vector. // Simple Tangent Calculation Example // v[0...2] vertex position // t[0...2] texcoords // tan.xyz = (v1-v0).xyz * (t2-t0).y - (v2-v0).xyz * (t1-t0).y; // Write above into a CalculateTangent() func using // Variables described below: // #define VERTEXSIZE 9 //xyz s1t1 s2t2 s3t3 // where xyz = vert coords; s1t1 = normal tex coords; // s2t2 = lightmap tex coords; s3t2 = detail tex coords // s->polys->verts (float verts[4][9]) - variable sized (xyz s1t1 s2t2) // Additional Variables of Interest: // s->texinfo->vecs (float vecs[2][4]) - [s/t][xyz offset] // s->plane->normal (float normal[3]) - plane normal // Pass the Tangent to the Vertex Shader using an attribute 3fv: // attribute3fv("tangent", tan); // BSP Render Code:


for ( ; s; s = s->texturechain) {
	v = s->polys->verts[0];
	glBegin(GL_POLYGON);
	for (k = 0; k < s->polys->numverts; k++, v += VERTEXSIZE) {				
		glMultiTexCoord2fARB (GL_TEXTURE0_ARB, v[3], v[4]);

		if (s->flags & SURF_PLANEBACK)
			glNormal3fv(-s->plane->normal);
		else
			glNormal3fv(s->plane->normal);

		glVertex3fv (v);
	}
	glEnd ();
}



Any help is greatly appreciated :) [Edited by - JasonGillespi on March 19, 2009 9:15:50 PM]
JasonGillespi
JasonGillespi
This is the best I could come up with so far. Part of the problem I see with this method is there is often more than 3 verts per polygon of which I'm not sure how I account for. Can anyone give me some direction or let me know if I'm on the right track please.

void TangentForPoly(msurface_t *s) {	float *v;	float *v0, *v1, *v2;	float s0, s1, s2;	float t0, t1, t2;	vec3_t vec1, vec2;	vec3_t planes[3];	int i;	v = s->polys->verts[0];	v0 = v;	v1 = v+VERTEXSIZE;	v2 = v+(VERTEXSIZE*2);	s0 = v[3];	t0 = v[4];	s1 = v1[3];	t1 = v1[4];	s2 = v2[3];	t2 = v2[4];	for (i=0; i<3; i++) {		vec1[0] = v1-v0;		vec1[1] = s1-s0;		vec1[2] = t1-t0;		vec2[0] = v2-v0;		vec2[1] = s2-s0;		vec2[2] = t2-t0;		VectorNormalize(vec1);		VectorNormalize(vec2);		CrossProduct(vec1,vec2,planes);	}	s->tangent[0] = -planes[0][1]/planes[0][0];	s->tangent[1] = -planes[1][1]/planes[1][0];	s->tangent[2] = -planes[2][1]/planes[2][0];	s->binormal[0] = -planes[0][2]/planes[0][0];	s->binormal[1] = -planes[1][2]/planes[1][0];	s->binormal[2] = -planes[2][2]/planes[2][0];	VectorNormalize(s->tangent); //is this needed?	VectorNormalize(s->binormal);}// inside draw bsp functionfor ( ; s; s = s->texturechain) {	v = s->polys->verts[0];	// Calculate Tangent & Binormal Space for polygon	TangentForPoly(s);	glBegin(GL_POLYGON);	for (k = 0; k < s->polys->numverts; k++, v += VERTEXSIZE) {						glMultiTexCoord2fARB (GL_TEXTURE0_ARB, v[3], v[4]);		if (s->flags & SURF_PLANEBACK)			glNormal3fv(-s->plane->normal);		else			glNormal3fv(s->plane->normal);		glVertex3fv (v);	}	glEnd ();}

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.