• Create Account

Texture coordinate projection

Posted by polyfrag, 19 June 2013 · 322 views

I now have texture coordinates.

I use the Half-Life/Valve approach, using 2 planes for the texture coordinates of each side, explained in the comments http://www.gamedev.net/blog/1512/entry-2256503-collapsing-geometry/

When a brush side is first added, I check to see which of the 6 possible axial normals its normal closest to. Based on the match I index into an array of cross-axises. Basically, the axis that's supposed to be perpindicular to the brush side normal and texture u-coordinate plane normal.
```Vec3f PlaneCrossAxis(Vec3f normal)
{
float mag[6];
mag[0] = Magnitude(normal - Vec3f(0,1,0));
mag[1] = Magnitude(normal - Vec3f(0,-1,0));
mag[2] = Magnitude(normal - Vec3f(1,0,0));
mag[3] = Magnitude(normal - Vec3f(-1,0,0));
mag[4] = Magnitude(normal - Vec3f(0,0,1));
mag[5] = Magnitude(normal - Vec3f(0,0,-1));

int match = 0;

for(int i=0; i<6; i++)
{
if(mag[i] < mag[match])
match = i;
}

Vec3f crossaxis[6];
crossaxis[0] = Vec3f( 0, 0, -1 );
crossaxis[1] = Vec3f( 0, 0, 1 );
crossaxis[2] = Vec3f( 0, 1, 0 );
crossaxis[3] = Vec3f( 0, 1, 0 );
crossaxis[4] = Vec3f( 0, 1, 0 );
crossaxis[5] = Vec3f( 0, 1, 0 );

return crossaxis[match];
}
```
For left, right, front and back this is (0,1,0), for top (0,0,-1) and (0,0,1) for bottom. You might have to change the sign or the order of the cross product parameters I'm about to mention to get the texture to line up upright and sidewise.

I get the cross product of this cross-axis with the face normal to get the u-coordinate plane.
```	Vec3f uaxis = Normalize(Cross(PlaneCrossAxis(normal), normal)) / INI_CUBE_SIZE;
```
I scale the plane normal so that the texture tiles once on the face. And for the v element:
```	Vec3f vaxis = Normalize(Cross(uaxis, normal)) / INI_CUBE_SIZE;
```
When the vertices of the brush side are moved or the brush is rotated I'll have to regenerate the u/v planes. In rotation the texture mapping can be preserved by just rotating the plane normals and solving for d using a rotated point on the new plane. But I don't know how to change the plane normal if some vertex is moved. Any help is appreciated if anybody knows how it's done.

S M T W T F S
1234567
891011121314
151617 18 192021
22232425262728
293031

PARTNERS