7 #include "../ports/system.h"
8 #include "../common/common.h"
9 #include "../shared/images.h"
26 #define IMAGE_BUFFER_SET_BLACK(buffer, offset) do {int i; for (i = 0; i < DEPTH; i++) {buffer[offset + i] = 0xFF; } } while(0);
28 static void SL_Bresenham (
int x0,
int y0,
int x1,
int y1,
int width,
int height,
bool rotated,
unsigned char* outputBuffer)
34 const int maxOffset = width * height *
DEPTH;
52 offset = x0 * DEPTH * height + y0;
54 offset = y0 * width * DEPTH + x0 *
DEPTH;
56 if (offset >= 0 && offset < maxOffset) {
62 int fraction = 2 * dy - (dx >> 1);
72 offset = x0 * DEPTH * height + y0;
74 offset = y0 * width * DEPTH + x0 *
DEPTH;
76 if (offset >= 0 && offset < maxOffset) {
82 int fraction = dx - (dy >> 1);
92 offset = x0 * DEPTH * height + y0;
94 offset = y0 * width * DEPTH + x0 *
DEPTH;
96 if (offset >= 0 && offset < maxOffset) {
109 const float d = -(
DotProduct(planeNormal, planeOrigin));
110 const float numerator =
DotProduct(planeNormal, origin) + d;
111 const float denominator =
DotProduct(planeNormal, vector);
113 if (fabs(denominator) < 0.00001)
117 return -(numerator / denominator);
131 VectorAdd(origin, v_temp, intersectPoint);
141 vec3_t zDistance = { 0.0f, 0.0f, 0.0f };
142 const vec3_t zNormal = { 0.0f, 0.0f, 1.0f };
144 vec3_t vTemp, intersectPoint;
145 float lineX1, lineY1, lineX2, lineY2;
148 const float minX = mins[0];
149 const float maxX = maxs[0];
150 const float minY = mins[1];
151 const float maxY = maxs[1];
152 const float minZ = mins[2];
153 const float maxZ = maxs[2];
155 const int numberOfSlices = (
int) ((maxZ - minZ) / stepsize);
158 bool rotated =
false;
159 unsigned char* pictureBuffer;
161 lineX1 = lineY1 = lineX2 = lineY2 = 0.0f;
163 width = (
int) (maxX - minX);
164 height = (
int) (maxY - minY);
167 if (height > width) {
169 width = (
int) (maxY - minY);
170 height = (
int) (maxX - minX);
178 for (
int sliceIndex = 0; sliceIndex < numberOfSlices; sliceIndex++) {
179 const float zHeight = minZ + (sliceIndex * stepsize);
180 zDistance[2] = zHeight;
183 if (!singleFile && !multipleContour)
184 memset(pictureBuffer, 0, width * height * DEPTH);
187 for (
int j = 0; j < tile->nummodels; j++) {
189 for (
int faceIndex = 0; faceIndex < model->
numfaces; faceIndex++) {
200 for (
int edgeIndex = 0; edgeIndex < face->
numedges; edgeIndex++) {
202 int edge = tile->surfedges[face->
firstedge + edgeIndex];
207 v[0][0] = tile->vertexes[e->
v[1]].point[0];
208 v[0][1] = tile->vertexes[e->
v[1]].point[1];
209 v[0][2] = tile->vertexes[e->
v[1]].point[2];
212 v[0][0] = tile->vertexes[e->
v[0]].point[0];
213 v[0][1] = tile->vertexes[e->
v[0]].point[1];
214 v[0][2] = tile->vertexes[e->
v[0]].point[2];
219 + ((edgeIndex + 1) % face->
numedges)];
224 v[1][0] = tile->vertexes[e->
v[1]].point[0];
225 v[1][1] = tile->vertexes[e->
v[1]].point[1];
226 v[1][2] = tile->vertexes[e->
v[1]].point[2];
229 v[1][0] = tile->vertexes[e->
v[0]].point[0];
230 v[1][1] = tile->vertexes[e->
v[0]].point[1];
231 v[1][2] = tile->vertexes[e->
v[0]].point[2];
235 if (((v[0][2] < zHeight) && (v[1][2] > zHeight))
236 || ((v[1][2] < zHeight) && (v[0][2] > zHeight))) {
243 zNormal, intersectPoint);
248 lineX1 = intersectPoint[0];
249 lineY1 = intersectPoint[1];
252 lineX2 = intersectPoint[0];
253 lineY2 = intersectPoint[1];
270 lineX1 = lineX1 /
scale;
271 lineY1 = lineY1 /
scale;
272 lineX2 = lineX2 /
scale;
273 lineY2 = lineY2 /
scale;
276 if (lineX1 >= 0 && lineX1 < width && lineY1 >= 0 && lineY1 < height
277 && lineX2 >= 0 && lineX2 < width && lineY2 >= 0 && lineY2 < height) {
278 const int x1 = (
int) lineX1;
279 const int y1 = (
int) lineY1;
280 const int x2 = (
int) lineX2;
281 const int y2 = (
int) lineY2;
284 SL_Bresenham(x1, height - y1, x2, height - y2, width, height, rotated, pictureBuffer);
286 SL_Bresenham(x1, y1, x2, y2, width, height, rotated, pictureBuffer);
293 Com_sprintf(filename,
sizeof(filename),
"out%d.png", sliceIndex + 1);
300 Q_strncpyz(filename,
"out.png",
sizeof(filename));
315 void SL_BSPSlice (
const TR_TILE_TYPE* model,
float thickness,
int scale,
bool singleFile,
bool multipleContour)
321 if (model ==
nullptr)
324 for (
int i = 0;
i < model->nummodels;
i++) {
334 mins[2] = (float) floor(mins[2]) + 0.01f;
335 maxs[2] = (float) floor(maxs[2]) + 0.01f;
337 SL_SliceTheWorld(model, mins, maxs, thickness, scale, singleFile, multipleContour);
#define VectorCopy(src, dest)
#define Mem_AllocTypeN(type, n)
static bool SL_CreatePNGFile(const char *filename, unsigned char *buffer, int width, int height)
int FS_OpenFile(const char *filename, qFILE *file, filemode_t mode)
Finds and opens the file in the search path.
static void SL_SliceTheWorld(const TR_TILE_TYPE *tile, const vec3_t mins, const vec3_t maxs, float stepsize, int scale, bool singleFile, bool multipleContour)
slice the world in Z planes going from min_z to max_z by stepsize...
static const vec3_t scale
bool Com_sprintf(char *dest, size_t size, const char *fmt,...)
copies formatted string with buffer-size checking
typedef int(ZCALLBACK *close_file_func) OF((voidpf opaque
void Com_Printf(const char *const fmt,...)
#define VectorScale(in, scale, out)
void SL_BSPSlice(const TR_TILE_TYPE *model, float thickness, int scale, bool singleFile, bool multipleContour)
void Q_strncpyz(char *dest, const char *src, size_t destsize)
Safe strncpy that ensures a trailing zero.
#define DotProduct(x, y)
Returns the distance between two 3-dimensional vectors.
void AddPointToBounds(const vec3_t v, vec3_t mins, vec3_t maxs)
If the point is outside the box defined by mins and maxs, expand the box to accommodate it...
#define IMAGE_BUFFER_SET_BLACK(buffer, offset)
#define PATHFINDING_HEIGHT
15 max, adjusting above 8 will require a rewrite to the DV code
#define VectorAdd(a, b, dest)
vec_t VectorNormalize(vec3_t v)
Calculate unit vector for a given vec3_t.
static void SL_Bresenham(int x0, int y0, int x1, int y1, int width, int height, bool rotated, unsigned char *outputBuffer)
void R_WritePNG(qFILE *f, byte *buffer, int width, int height)
#define MAX_WORLD_WIDTH
-MAX_WORLD_WIDTH up tp +MAX_WORLD_WIDTH
static bool SL_VectorIntersectPlane(const vec3_t origin, const vec3_t vector, const vec3_t planeOrigin, const vec3_t planeNormal, vec3_t intersectPoint)
QGL_EXTERN int GLboolean GLfloat * v
static float SL_DistanceToIntersection(const vec3_t origin, const vec3_t vector, const vec3_t planeOrigin, const vec3_t planeNormal)
#define VectorSubtract(a, b, dest)