Skip to content

Instantly share code, notes, and snippets.

@StillManic
Created July 27, 2015 00:00
Show Gist options
  • Save StillManic/08f60813211279d00478 to your computer and use it in GitHub Desktop.
Save StillManic/08f60813211279d00478 to your computer and use it in GitHub Desktop.
Vector3f v1 = new Vector3f(this.verts[0].getPosition().x, this.verts[0].getPosition().y, this.verts[0].getPosition().z);
Vector3f v2 = new Vector3f(this.verts[1].getPosition().x, this.verts[1].getPosition().y, this.verts[1].getPosition().z);
Vector3f v3 = new Vector3f(this.verts[2].getPosition().x, this.verts[2].getPosition().y, this.verts[2].getPosition().z);
Vector3f v4 = this.verts.length > 3 ? new Vector3f(this.verts[3].getPosition().x, this.verts[3].getPosition().y, this.verts[3].getPosition().z) : null;
if (v4 == null)
{
Vector3f v2c = new Vector3f(v2.x, v2.y, v2.z);
Vector3f v1c = new Vector3f(v1.x, v1.y, v1.z);
v1c.sub(v2c);
Vector3f v3c = new Vector3f(v3.x, v3.y, v3.z);
v3c.sub(v2c);
Vector3f c = new Vector3f();
c.cross(v1c, v3c);
c.normalize();
Normal normal = new Normal(c);
return normal;
}
else
{
Vector3f v2c = new Vector3f(v2.x, v2.y, v2.z);
Vector3f v1c = new Vector3f(v1.x, v1.y, v1.z);
v1c.sub(v2c);
Vector3f v3c = new Vector3f(v3.x, v3.y, v3.z);
v3c.sub(v2c);
Vector3f c = new Vector3f();
c.cross(v1c, v3c);
c.normalize();
v1c = new Vector3f(v1.x, v1.y, v1.z);
v3c = new Vector3f(v3.x, v3.y, v3.z);
Vector3f v4c = new Vector3f(v4.x, v4.y, v4.z);
v1c.sub(v4c);
v3c.sub(v4c);
Vector3f d = new Vector3f();
d.cross(v1c, v3c);
d.normalize();
Vector3f avg = new Vector3f();
avg.x = (c.x + d.x) * 0.5f;
avg.y = (c.y + d.y) * 0.5f;
avg.z = (c.z + d.z) * 0.5f;
avg.normalize();
Normal normal = new Normal(avg);
return normal;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment