TextureCoordinate MeshUtility::SphericalUVMapping(const FVector3& pointOnSphere) { //see http://en.wikipedia.org/wiki/UV_mapping FVector3 d = -pointOnSphere; Normalize(d); return TextureCoordinate( 0.5f - ( std::atan2(d.z, d.x) / (2 * PI) ), 0.5f - 2 * ( std::asin(d.y) / (2 * PI) ) ); }
std::unique_ptr<Mesh> MeshUtility::MakeCube(float lengthOfOneSide) { //float_t halfLength = lengthOfOneSide / 2; std::unique_ptr<Mesh> cube = std::make_unique<Mesh>(); cube->AddPositions( ModelUtility::CubePositions(lengthOfOneSide) ); cube->AddTextureCoordinate(TextureCoordinate(0, 0)); cube->AddTextureCoordinate(TextureCoordinate(1, 0)); cube->AddTextureCoordinate(TextureCoordinate(1, 1)); cube->AddTextureCoordinate(TextureCoordinate(0, 1)); //front AddFaceToMesh(*cube, 0, 1, 2, 0, 1, 2); AddFaceToMesh(*cube, 0, 2, 3, 0, 2, 3); //back AddFaceToMesh(*cube, 5, 4, 7, 0, 1, 2); AddFaceToMesh(*cube, 5, 7, 6, 0, 2, 3); //right AddFaceToMesh(*cube, 1, 5, 6, 0, 1, 2); AddFaceToMesh(*cube, 1, 6, 2, 0, 2, 3); //left AddFaceToMesh(*cube, 4, 0, 3, 0, 1, 2); AddFaceToMesh(*cube, 4, 3, 7, 0, 2, 3); //top AddFaceToMesh(*cube, 3, 2, 6, 0, 1, 2); AddFaceToMesh(*cube, 3, 6, 7, 0, 2, 3); //bottom AddFaceToMesh(*cube, 4, 5, 1, 0, 1, 2); AddFaceToMesh(*cube, 4, 1, 0, 0, 2, 3); cube->UpdateEdgeAdjacency(); return cube; }
void GeometryBuilder::CreateHorizontal(const CVector& start, const CVector& end, float width, bool flip, const STexInfo& texture_info) { if(m_building_obj) { if(flip == GEO_H_DOWN) { //printf("GeometryBuilder: Adding a floor\n"); if(texture_info.rotation90) { StartSurface(); Texture(texture_info.tex_id); TextureCoordinate(0 * texture_info.tex_scale_u + texture_info.tex_offset_u, 0 * texture_info.tex_scale_v + texture_info.tex_offset_v); //bottom left Vertex(start.x, start.y, start.z); TextureCoordinate(0 * texture_info.tex_scale_u + texture_info.tex_offset_u, 1 * texture_info.tex_scale_v + texture_info.tex_offset_v); //bottom right Vertex(start.x, start.y, start.z + width); TextureCoordinate(-1 * texture_info.tex_scale_u + texture_info.tex_offset_u, 1 * texture_info.tex_scale_v + texture_info.tex_offset_v); //top right Vertex(end.x, end.y, end.z + width); TextureCoordinate(-1 * texture_info.tex_scale_u + texture_info.tex_offset_u, 0 * texture_info.tex_scale_v + texture_info.tex_offset_v); //top left Vertex(end.x, end.y, end.z); EndSurface(); } else { StartSurface(); Texture(texture_info.tex_id); TextureCoordinate(0 * texture_info.tex_scale_u + texture_info.tex_offset_u, 0 * texture_info.tex_scale_v + texture_info.tex_offset_v); //bottom left Vertex(start.x, start.y, start.z); TextureCoordinate(1 * texture_info.tex_scale_u + texture_info.tex_offset_u, 0 * texture_info.tex_scale_v + texture_info.tex_offset_v); //bottom right Vertex(start.x, start.y, start.z + width); TextureCoordinate(1 * texture_info.tex_scale_u + texture_info.tex_offset_u, 1 * texture_info.tex_scale_v + texture_info.tex_offset_v); //top right Vertex(end.x, end.y, end.z + width); TextureCoordinate(0 * texture_info.tex_scale_u + texture_info.tex_offset_u, 1 * texture_info.tex_scale_v + texture_info.tex_offset_v); //top left Vertex(end.x, end.y, end.z); EndSurface(); } } else //flip == GEO_H_UP { if(texture_info.rotation90) { //printf("GeometryBuilder: Adding a roof\n"); StartSurface(); Texture(texture_info.tex_id); TextureCoordinate(-1 * texture_info.tex_scale_u + texture_info.tex_offset_u, 0 * texture_info.tex_scale_v + texture_info.tex_offset_v); //top left Vertex(start.x, start.y, start.z); TextureCoordinate(0 * texture_info.tex_scale_u + texture_info.tex_offset_u, 0 * texture_info.tex_scale_v + texture_info.tex_offset_v); //bottom left Vertex(end.x, end.y, end.z); TextureCoordinate(0 * texture_info.tex_scale_u + texture_info.tex_offset_u, 1 * texture_info.tex_scale_v + texture_info.tex_offset_v); //bottom right Vertex(end.x, end.y, end.z + width); TextureCoordinate(-1 * texture_info.tex_scale_u + texture_info.tex_offset_u, 1 * texture_info.tex_scale_v + texture_info.tex_offset_v); //top right Vertex(start.x, start.y, start.z + width); EndSurface(); } else { StartSurface(); Texture(texture_info.tex_id); TextureCoordinate(0 * texture_info.tex_scale_u + texture_info.tex_offset_u, 1 * texture_info.tex_scale_v + texture_info.tex_offset_v); //top left Vertex(start.x, start.y, start.z); TextureCoordinate(0 * texture_info.tex_scale_u + texture_info.tex_offset_u, 0 * texture_info.tex_scale_v + texture_info.tex_offset_v); //bottom left Vertex(end.x, end.y, end.z); TextureCoordinate(1 * texture_info.tex_scale_u + texture_info.tex_offset_u, 0 * texture_info.tex_scale_v + texture_info.tex_offset_v); //bottom right Vertex(end.x, end.y, end.z + width); TextureCoordinate(1 * texture_info.tex_scale_u + texture_info.tex_offset_u, 1 * texture_info.tex_scale_v + texture_info.tex_offset_v); //top right Vertex(start.x, start.y, start.z + width); EndSurface(); } } } }
// Adam's stair building code void GeometryBuilder::CreateStairs(const CVector& start, const CVector& end, int StepCount, const STexInfo& stair_top, const STexInfo& stair_side) { if(m_building_obj) { CVector total_run, total_rise, total_width; total_run.x = end.x - start.x; // x,0,0 total_rise.y = end.y - start.y; // 0,y,0 total_width.z = end.z - start.z; // 0,0,z CVector run = total_run/StepCount; CVector rise = total_rise/(StepCount + 1); //printf("GeometryBuilder: Building stairs with %d stairs\n", StepCount); CVector v1,v2,v3,v4; v1 = start; v4 = v1 + total_width; for( int i = 0; i < StepCount; i++ ) { v2 = v1 + rise; v3 = v4 + rise; //draw ^ v1 -> v2 -> v3 -> v4 if(stair_side.rotation90) { StartSurface(); Texture(stair_side.tex_id); TextureCoordinate(0 * stair_side.tex_scale_u + stair_side.tex_offset_u, 1 * stair_side.tex_scale_v + stair_side.tex_offset_v); Vertex(v1); TextureCoordinate(-1 * stair_side.tex_scale_u + stair_side.tex_offset_u, 1 * stair_side.tex_scale_v + stair_side.tex_offset_v); Vertex(v2); TextureCoordinate(-1 * stair_side.tex_scale_u + stair_side.tex_offset_u, 0 * stair_side.tex_scale_v + stair_side.tex_offset_v); Vertex(v3); TextureCoordinate(0 * stair_side.tex_scale_u + stair_side.tex_offset_u, 0 * stair_side.tex_scale_v + stair_side.tex_offset_v); Vertex(v4); EndSurface(); } else { StartSurface(); Texture(stair_side.tex_id); TextureCoordinate(1 * stair_side.tex_scale_u + stair_side.tex_offset_u, 0 * stair_side.tex_scale_v + stair_side.tex_offset_v); Vertex(v1); TextureCoordinate(1 * stair_side.tex_scale_u + stair_side.tex_offset_u, 1 * stair_side.tex_scale_v + stair_side.tex_offset_v); Vertex(v2); TextureCoordinate(0 * stair_side.tex_scale_u + stair_side.tex_offset_u, 1 * stair_side.tex_scale_v + stair_side.tex_offset_v); Vertex(v3); TextureCoordinate(0 * stair_side.tex_scale_u + stair_side.tex_offset_u, 0 * stair_side.tex_scale_v + stair_side.tex_offset_v); Vertex(v4); EndSurface(); } v1 = v2 + run; v4 = v3 + run; //draw ^ v2 -> v1 -> v4 -> v3 if(stair_top.rotation90) { StartSurface(); Texture(stair_top.tex_id); TextureCoordinate(0 * stair_top.tex_scale_u + stair_top.tex_offset_u, 1 * stair_top.tex_scale_v + stair_top.tex_offset_v); Vertex(v2); TextureCoordinate(-1 * stair_top.tex_scale_u + stair_top.tex_offset_u, 1 * stair_top.tex_scale_v + stair_top.tex_offset_v); Vertex(v1); TextureCoordinate(-1 * stair_top.tex_scale_u + stair_top.tex_offset_u, 0 * stair_top.tex_scale_v + stair_top.tex_offset_v); Vertex(v4); TextureCoordinate(0 * stair_top.tex_scale_u + stair_top.tex_offset_u, 0 * stair_top.tex_scale_v + stair_top.tex_offset_v); Vertex(v3); EndSurface(); } else { StartSurface(); Texture(stair_top.tex_id); TextureCoordinate(1 * stair_top.tex_scale_u + stair_top.tex_offset_u, 0 * stair_top.tex_scale_v + stair_top.tex_offset_v); Vertex(v2); TextureCoordinate(1 * stair_top.tex_scale_u + stair_top.tex_offset_u, 1 * stair_top.tex_scale_v + stair_top.tex_offset_v); Vertex(v1); TextureCoordinate(0 * stair_top.tex_scale_u + stair_top.tex_offset_u, 1 * stair_top.tex_scale_v + stair_top.tex_offset_v); Vertex(v4); TextureCoordinate(0 * stair_top.tex_scale_u + stair_top.tex_offset_u, 0 * stair_top.tex_scale_v + stair_top.tex_offset_v); Vertex(v3); EndSurface(); } } } }
/** Creates a vertical wall, starting at start and going to end. Start should be the lower */ void GeometryBuilder::CreateVertical(const CVector& start, const CVector& end, float height, bool flip, const STexInfo& texture_info) { if(m_building_obj) { if(flip == GEO_V_RIGHT) { //printf("GeometryBuilder: Adding a right wall\n"); //Right hand wall, normal pointing to the left if(texture_info.rotation90) //TODO change this system ENTIRELY. { StartSurface(); Texture(texture_info.tex_id); TextureCoordinate(0 * texture_info.tex_scale_u + texture_info.tex_offset_u, 1 * texture_info.tex_scale_v + texture_info.tex_offset_v); //bottom right Vertex(start.x, start.y, start.z); TextureCoordinate(-1 * texture_info.tex_scale_u + texture_info.tex_offset_u, 1 * texture_info.tex_scale_v + texture_info.tex_offset_v); //top right Vertex(start.x, start.y + height, start.z); TextureCoordinate(-1 * texture_info.tex_scale_u + texture_info.tex_offset_u, 0 * texture_info.tex_scale_v + texture_info.tex_offset_v); //top left Vertex(end.x, end.y + height, end.z); TextureCoordinate(0 * texture_info.tex_scale_u + texture_info.tex_offset_u, 0 * texture_info.tex_scale_v + texture_info.tex_offset_v); //bottom left Vertex(end.x, end.y, end.z); EndSurface(); } else { StartSurface(); Texture(texture_info.tex_id); TextureCoordinate(1 * texture_info.tex_scale_u + texture_info.tex_offset_u, 0 * texture_info.tex_scale_v + texture_info.tex_offset_v); //bottom right Vertex(start.x, start.y, start.z); TextureCoordinate(1 * texture_info.tex_scale_u + texture_info.tex_offset_u, 1 * texture_info.tex_scale_v + texture_info.tex_offset_v); //top right Vertex(start.x, start.y + height, start.z); TextureCoordinate(0 * texture_info.tex_scale_u + texture_info.tex_offset_u, 1 * texture_info.tex_scale_v + texture_info.tex_offset_v); //top left Vertex(end.x, end.y + height, end.z); TextureCoordinate(0 * texture_info.tex_scale_u + texture_info.tex_offset_u, 0 * texture_info.tex_scale_v + texture_info.tex_offset_v); //bottom left Vertex(end.x, end.y, end.z); EndSurface(); } } else //flip == GEO_V_LEFT { //printf("GeometryBuilder: Adding a left wall\n"); //left hand wall, normal pointing to the right if(texture_info.rotation90) { StartSurface(); Texture(texture_info.tex_id); TextureCoordinate(0 * texture_info.tex_scale_u + texture_info.tex_offset_u, 0 * texture_info.tex_scale_v + texture_info.tex_offset_v); //bottom left Vertex(start.x, start.y, start.z); TextureCoordinate(0 * texture_info.tex_scale_u + texture_info.tex_offset_u, 1 * texture_info.tex_scale_v + texture_info.tex_offset_v); //bottom right Vertex(end.x, end.y, end.z); TextureCoordinate(-1 * texture_info.tex_scale_u + texture_info.tex_offset_u, 1 * texture_info.tex_scale_v + texture_info.tex_offset_v); //top right Vertex(end.x, end.y + height, end.z); TextureCoordinate(-1 * texture_info.tex_scale_u + texture_info.tex_offset_u, 0 * texture_info.tex_scale_v + texture_info.tex_offset_v); //top left Vertex(start.x, start.y + height, start.z); EndSurface(); } else { StartSurface(); Texture(texture_info.tex_id); TextureCoordinate(0 * texture_info.tex_scale_u + texture_info.tex_offset_u, 0 * texture_info.tex_scale_v + texture_info.tex_offset_v); //bottom left Vertex(start.x, start.y, start.z); TextureCoordinate(1 * texture_info.tex_scale_u + texture_info.tex_offset_u, 0 * texture_info.tex_scale_v + texture_info.tex_offset_v); //bottom right Vertex(end.x, end.y, end.z); TextureCoordinate(1 * texture_info.tex_scale_u + texture_info.tex_offset_u, 1 * texture_info.tex_scale_v + texture_info.tex_offset_v); //top right Vertex(end.x, end.y + height, end.z); TextureCoordinate(0 * texture_info.tex_scale_u + texture_info.tex_offset_u, 1 * texture_info.tex_scale_v + texture_info.tex_offset_v); //top left Vertex(start.x, start.y + height, start.z); EndSurface(); } } } }