13 Vertex(
float x,
float y,
float z) :
x(
x),
y(
y),
z(
z),
u(
x / Config.AREA_WIDTH),
v(1.f -
z / Config.AREA_HEIGHT) {}
24 objFile <<
"v " << vertex.
x * Config.TILE_SIDE_SCALE <<
" " << vertex.
y * Config.HEIGHT_MAP_SCALE <<
" " << vertex.
z * Config.TILE_SIDE_SCALE << std::endl;
25 objFile <<
"vt " << vertex.
u <<
" " << vertex.
v << std::endl;
36 void writeFace(std::ofstream& objFile,
const int corner1,
const int corner2,
const int corner3)
40 objFile <<
"f " << corner1 <<
"/" << corner1 <<
" " << corner3 <<
"/" << corner3 <<
" " << corner2 <<
"/" << corner2 << std::endl;
43 objFile <<
"f " << corner1 <<
"/" << corner1 <<
" " << corner2 <<
"/" << corner2 <<
" " << corner3 <<
"/" << corner3 << std::endl;
51 void generateAreaObjs(
const std::vector<std::vector<int>>& heightMap,
const std::vector<std::vector<std::string>>& areaMap)
53 for (
int i = 0; i < areaMap.size(); i++)
55 for (
int j = 0; j < areaMap[i].size(); j++)
57 const std::string area = areaMap[i][j];
58 std::cout <<
"Generating area " << area << i << j << std::endl;
59 const std::string filename = area + std::to_string(i) + std::to_string(j) +
".obj";
61 std::ofstream objFile(Config.GENERATED_AREA_OBJ_PATH + filename);
63 if (!objFile.is_open())
65 throw std::runtime_error(
"Could not open " + area +
".obj for writing");
69 for (
int row = i * Config.AREA_HEIGHT; row < i * Config.AREA_HEIGHT + Config.AREA_HEIGHT; row++)
71 for (
int col = j * Config.AREA_WIDTH; col < j * Config.AREA_WIDTH + Config.AREA_WIDTH; col++)
76 float innerSquareRadius = Config.TILE_SIDE_UNIT / 4.0;
77 float squareRadius = Config.TILE_SIDE_UNIT / 2.0;
78 float xPlus = col + innerSquareRadius;
79 float xMinus = col - innerSquareRadius;
80 float zPlus = row + innerSquareRadius;
81 float zMinus = row - innerSquareRadius;
87 int innerTopRight = 1;
88 int innerBottomRight = 2;
89 int innerBottomLeft = 3;
94 TileHeight tileHeight = calculateTileHeights(heightMap, row, col);
95 float xPlusO = col + squareRadius;
96 float xMinusO = col - squareRadius;
97 float zPlusO = row + squareRadius;
98 float zMinusO = row - squareRadius;
106 int bottomCenterLeft = 7;
107 int bottomCenterRight = 8;
114 int leftCenterTop = 10;
115 int leftCenterBottom = 11;
122 int topCenterLeft = 13;
123 int topCenterRight = 14;
128 int rightCenterTop = 15;
129 int rightCenterBottom = 16;
133 writeFace(objFile, vertexCount + 1, vertexCount + 2, vertexCount + 3);
134 writeFace(objFile, vertexCount + 3, vertexCount + 4, vertexCount + 1);
137 writeFace(objFile, vertexCount + bottomRight, vertexCount + bottomCenterRight, vertexCount + innerBottomRight);
138 writeFace(objFile, vertexCount + bottomCenterRight, vertexCount + bottomCenterLeft, vertexCount + innerBottomLeft);
139 writeFace(objFile, vertexCount + innerBottomLeft, vertexCount + innerBottomRight, vertexCount + bottomCenterRight);
140 writeFace(objFile, vertexCount + bottomCenterLeft, vertexCount + bottomLeft, vertexCount + innerBottomLeft);
143 writeFace(objFile, vertexCount + leftTop, vertexCount + innerTopLeft, vertexCount + leftCenterTop);
144 writeFace(objFile, vertexCount + leftCenterTop, vertexCount + innerTopLeft, vertexCount + leftCenterBottom);
145 writeFace(objFile, vertexCount + leftCenterBottom, vertexCount + innerTopLeft, vertexCount + innerBottomLeft);
146 writeFace(objFile, vertexCount + innerBottomLeft, vertexCount + bottomLeft, vertexCount + leftCenterBottom);
149 writeFace(objFile, vertexCount + topRight, vertexCount + innerTopRight, vertexCount + topCenterRight);
150 writeFace(objFile, vertexCount + topCenterRight, vertexCount + innerTopRight, vertexCount + topCenterLeft);
151 writeFace(objFile, vertexCount + topCenterLeft, vertexCount + innerTopRight, vertexCount + innerTopLeft);
152 writeFace(objFile, vertexCount + innerTopLeft, vertexCount + leftTop, vertexCount + topCenterLeft);
155 writeFace(objFile, vertexCount + rightCenterTop, vertexCount + innerBottomRight, vertexCount + innerTopRight);
156 writeFace(objFile, vertexCount + innerTopRight, vertexCount + topRight, vertexCount + rightCenterTop);
157 writeFace(objFile, vertexCount + rightCenterBottom, vertexCount + innerBottomRight, vertexCount + rightCenterTop);
158 writeFace(objFile, vertexCount + innerBottomRight, vertexCount + rightCenterBottom, vertexCount + bottomRight);
169 std::cout <<
"Map object file generated" << std::endl;
void writeVertex(std::ofstream &objFile, const Vertex &vertex)
Writes a vertex to an OBJ file.
Definition: MapObjLoader.hpp:22
void generateAreaObjs(const std::vector< std::vector< int >> &heightMap, const std::vector< std::vector< std::string >> &areaMap)
Generates obj map for each area of the map.
Definition: MapObjLoader.hpp:51
void writeFace(std::ofstream &objFile, const int corner1, const int corner2, const int corner3)
Writes a face to an OBJ file.
Definition: MapObjLoader.hpp:36
WINDING_ORDER
Definition: MapObjLoader.hpp:3
@ CLOCKWISE
Definition: MapObjLoader.hpp:4
@ COUNTER_CLOCKWISE
Definition: MapObjLoader.hpp:5
Definition: MapObjLoader.hpp:10
Vertex(float x, float y, float z)
Definition: MapObjLoader.hpp:13
float y
Definition: MapObjLoader.hpp:11
float v
Definition: MapObjLoader.hpp:12
float x
Definition: MapObjLoader.hpp:11
float z
Definition: MapObjLoader.hpp:11
float u
Definition: MapObjLoader.hpp:12