Tools for preprocessing data files from Quake to make them suitable for use on PS1 hardware
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

423 lines
13 KiB

#include "common.h"
#include "lighting.h"
bool sample_lightmap(const world_t* world, const face_t* face, const BoundBox& bounds, const Vec3& point, unsigned char* outSample)
{
if (face->lightmap < 0)
return false;
const unsigned char* lightmap = &world->lightmap[face->lightmap];
const plane_t* plane = &world->planes[face->plane_id];
Vec3 minBounds = (bounds.min / 16).floor() * 16;
Vec3 maxBounds = (bounds.max / 16).ceil() * 16;
int width, height;
int u, v;
switch (plane->type)
{
case 0:
case 3:
// Towards X
width = (int)(maxBounds.y - minBounds.y);
height = (int)(maxBounds.z - minBounds.z);
u = (int)(point.y - minBounds.y);
v = (int)(point.z - minBounds.z);
break;
case 1:
case 4:
// Towards Y
width = (int)(maxBounds.x - minBounds.x);
height = (int)(maxBounds.z - minBounds.z);
u = (int)(point.x - minBounds.x);
v = (int)(point.z - minBounds.z);
break;
case 2:
case 5:
// Towards Z
width = (int)(maxBounds.x - minBounds.x);
height = (int)(maxBounds.y - minBounds.y);
u = (int)(point.x - minBounds.x);
v = (int)(point.y - minBounds.y);
break;
default:
printf("Error: unknown plane type %d\n", plane->type);
return 0;
}
if (u < 0 || v < 0 || u > width || v > height)
return false;
*outSample = lightmap[(v >> 4) * (width >> 4) + (u >> 4)];
return true;
}
unsigned char sample_lightmap(const world_t* world, const face_t* face, const BoundBox& bounds, const Vec3& point)
{
unsigned char sample;
if (!sample_lightmap(world, face, bounds, point, &sample))
return 0;
return sample;
}
void export_lightmap(const world_t* world, const face_t* face, const BoundBox& bounds, int faceIdx)
{
if (face->lightmap < 0)
return;
const unsigned char* lightmap = &world->lightmap[face->lightmap];
const plane_t* plane = &world->planes[face->plane_id];
int width, height;
switch (plane->type)
{
case 0:
case 3:
// Towards X
width = (int)(ceil(bounds.max.y / 16) - floor(bounds.min.y / 16));
height = (int)(ceil(bounds.max.z / 16) - floor(bounds.min.z / 16));
break;
case 1:
case 4:
// Towards Y
width = (int)(ceil(bounds.max.x / 16) - floor(bounds.min.x / 16));
height = (int)(ceil(bounds.max.z / 16) - floor(bounds.min.z / 16));
break;
case 2:
case 5:
// Towards Z
width = (int)(ceil(bounds.max.x / 16) - floor(bounds.min.x / 16));
height = (int)(ceil(bounds.max.y / 16) - floor(bounds.min.y / 16));
break;
default:
printf("Error: unknown plane type %d\n", plane->type);
return;
}
width += 1;
char path[_MAX_PATH];
sprintf_s(path, _MAX_PATH, "lightmap_face%d_e%d_PT%d_%dx%d.raw", faceIdx, face->ledge_num, plane->type, width, height);
FILE* flm;
fopen_s(&flm, path, "wb");
if (!flm)
return;
for (int y = 0; y < height; ++y)
{
fwrite(&lightmap[y * width], sizeof(unsigned char), width, flm);
}
fclose(flm);
}
std::unordered_map<const edge_t*, EdgeData> analyze_edges(const world_t* world)
{
std::unordered_map<const edge_t*, EdgeData> edgeData;
for (int faceIdx = 0; faceIdx < world->numFaces; ++faceIdx)
{
const face_t* face = &world->faces[faceIdx];
const int* edgeList = &world->edgeList[face->ledge_id];
for (int i = 0; i < face->ledge_num; ++i, ++edgeList)
{
int edgeIdx = *edgeList;
if (edgeIdx < 0)
edgeIdx = -edgeIdx; // Reverse direction edge
const edge_t* edge = &world->edges[edgeIdx];
auto iter = edgeData.find(edge);
if (iter != edgeData.end())
{
iter->second.faces.push_back(face);
}
else
{
EdgeData newData = { 0 };
newData.edgeIndex = edgeIdx;
newData.faces.push_back(face);
edgeData[edge] = newData;
}
}
}
for (auto iter = edgeData.begin(); iter != edgeData.end(); ++iter)
{
size_t numFaces = iter->second.faces.size();
switch (numFaces)
{
case 1:
iter->second.isSharpEdge = true;
break;
case 2:
{
// TODO: take into account the face's side
auto faceA = iter->second.faces[0];
auto faceB = iter->second.faces[1];
const plane_t* planeA = &world->planes[faceA->plane_id];
const plane_t* planeB = &world->planes[faceB->plane_id];
vec3_t normalA = faceA->side ? -planeA->normal : planeA->normal;
vec3_t normalB = faceB->side ? -planeB->normal : planeB->normal;
double dot = normalA.dotProduct(normalB);
bool isSmooth = dot >= 0.5;//&& dot <= 1;
iter->second.isSharpEdge = !isSmooth;
break;
}
default:
printf("Edge at index %d has %d adjacent face(s), weird\n", iter->second.edgeIndex, numFaces);
break;
}
}
return edgeData;
}
unsigned char compute_faceVertex_light(const world_t* world, const face_t* face, unsigned short vertexIndex, const FaceBounds& faceBounds, const std::unordered_map<const edge_t*, EdgeData>& edgeData)
{
const vertex_t* vertex = &world->vertices[vertexIndex];
auto point = vertex->toVec();
// Sample this face's lighting contribution
unsigned int light = sample_lightmap(world, face, faceBounds.find(face)->second, point) + (0xFF - face->baselight);
int numSamples = 1;
// Collect edges connected to this vertex, filter out the smooth ones only
std::vector<const edge_t*> smoothEdges;
for (auto iter = edgeData.begin(); iter != edgeData.end(); ++iter)
{
auto edge = iter->first;
if (edge->vertex0 != vertexIndex && edge->vertex1 != vertexIndex)
continue;
if (iter->second.isSharpEdge)
continue;
// If the current face doesn't appear in this edge's adjacency list, we're not interested
for (auto faceIter = iter->second.faces.begin(); faceIter != iter->second.faces.end(); ++faceIter)
{
// TODO: actually I don't think this is the correct solution. We're allowed to sample light contributions from edges that aren't connected to this face.
// However we need to ensure we sample contributions from each face only once, and we need to check the angle between faces on a case-by-case basis.
// In fact I don't think we're interested in edges at all? Just in the faces that connect to a certain vertex.
if (*faceIter == face)
{
smoothEdges.push_back(edge);
break;
}
}
}
// Gather lighting contributions from neigbouring faces
for (auto edgeIter = smoothEdges.begin(); edgeIter != smoothEdges.end(); ++edgeIter)
{
auto faces = edgeData.find(*edgeIter)->second.faces;
for (auto faceIter = faces.begin(); faceIter != faces.end(); ++faceIter) // FIXME: "this" face doesn't always appear in faces list, when it absolutely should!
{
const face_t* otherFace = *faceIter;
if (otherFace == face) // Skip the current face, we only sample it once
continue;
light += sample_lightmap(world, otherFace, faceBounds.find(otherFace)->second, point) + (0xFF - otherFace->baselight);
++numSamples;
}
}
return (unsigned char)(light / numSamples);
}
unsigned char compute_faceVertex_light2(const world_t* world, const face_t* face, unsigned short vertexIndex, const FaceBounds& faceBounds, const VertexFaces& vertexFaces)
{
const vertex_t* vertex = &world->vertices[vertexIndex];
auto vertexFaceIter = vertexFaces.find(vertex);
if (vertexFaceIter == vertexFaces.end())
return 0;
auto point = vertex->toVec();
// Sample this face's lighting contribution
unsigned int light = sample_lightmap(world, face, faceBounds.find(face)->second, point) + (0xFF - face->baselight);
int numSamples = 1;
const plane_t* thisPlane = &world->planes[face->plane_id];
vec3_t thisNormal = face->side ? -thisPlane->normal : thisPlane->normal;
// Gather light samples from other faces adjacent to this vertex
for (auto faceIter = vertexFaceIter->second.begin(); faceIter != vertexFaceIter->second.end(); ++faceIter)
{
const face_t* otherFace = *faceIter;
if (otherFace == face)
continue;
const plane_t* otherPlane = &world->planes[otherFace->plane_id];
vec3_t otherNormal = otherFace->side ? -otherPlane->normal : otherPlane->normal;
double dot = thisNormal.dotProduct(otherNormal);
if (dot < 0.5)
continue; // Sharp edge, we don't want light contribution from this face
light += sample_lightmap(world, otherFace, faceBounds.find(otherFace)->second, point) + (0xFF - otherFace->baselight);
++numSamples;
}
return (unsigned char)(light / numSamples);
}
// Start with a hash set of all faces
// From the first face onward, group together faces into an interconnected surface based on angle between faces, using a flood-fill type of approach:
// - Add the first face to a surface face list
// - Take the next face from the surface face list (use index to go to next, don't remove it from the surface list)
// - For each face vertex, look up all other faces connected to that vertex
// - If the other face is not present in the original hash set, skip it (it was already added to a surface)
// - If the other face has an angle > 60 degrees with the current face, skip it (sharp edge)
// - Add the other face to the surface face list, remove it from the hash set
// - Repeat from step 'take the next face'
SurfaceList group_surfaces(const world_t* world, const VertexFaces& vertexFaces)
{
SurfaceList surfaces;
std::unordered_set<const face_t*> faceSet;
for (int i = 0; i < world->numFaces; ++i)
faceSet.insert(&world->faces[i]);
while (!faceSet.empty())
{
const face_t* firstFace = *faceSet.begin();
faceSet.erase(firstFace);
std::vector<const face_t*> surfaceFaces;
surfaceFaces.push_back(firstFace);
for (size_t faceIdx = 0; faceIdx < surfaceFaces.size(); ++faceIdx)
{
const face_t* thisFace = surfaceFaces[faceIdx];
const plane_t* thisPlane = &world->planes[thisFace->plane_id];
vec3_t thisNormal = thisFace->side ? -thisPlane->normal : thisPlane->normal;
for (int edgeListIdx = 0; edgeListIdx < thisFace->ledge_num; ++edgeListIdx)
{
int edgeIdx = world->edgeList[thisFace->ledge_id + edgeListIdx];
const edge_t* edge = &world->edges[abs(edgeIdx)];
for (int v = 0; v < 1; ++v)
{
unsigned short vertIndex = *(&edge->vertex0 + v);
const vertex_t* vertex = &world->vertices[vertIndex];
auto vertexFaceIter = vertexFaces.find(vertex);
if (vertexFaceIter == vertexFaces.end())
{
printf("Couldn't find list of faces for vertex %d, weird...\n", vertIndex);
continue;
}
for (auto faceIter = vertexFaceIter->second.begin(); faceIter != vertexFaceIter->second.end(); ++faceIter)
{
const face_t* otherFace = *faceIter;
if (faceSet.find(otherFace) == faceSet.end())
continue; // Face has already been added to a surface, skip it
const plane_t* otherPlane = &world->planes[otherFace->plane_id];
vec3_t otherNormal = otherFace->side ? -otherPlane->normal : otherPlane->normal;
double dot = thisNormal.dotProduct(otherNormal);
if (dot < 0.5)
continue; // Sharp edge, face belongs to a different surface
// Add face to this surface and make sure it won't be reconsidered for any other surfaces
surfaceFaces.push_back(otherFace);
faceSet.erase(otherFace);
}
}
}
}
Surface surface;
surface.faces.insert(surfaceFaces.begin(), surfaceFaces.end());
surfaces.push_back(surface);
}
return surfaces;
}
unsigned char compute_faceVertex_light4(const world_t* world, const face_t* refFace, const FaceBounds& faceBounds, Vec3 point)
{
if (refFace->lightmap < 0)
return 0;
auto faces = world->facesWithPoint(point);
if (faces.empty())
return 0;
const plane_t* refPlane = &world->planes[refFace->plane_id];
vec3_t refNormal = refFace->side ? -refPlane->normal : refPlane->normal;
unsigned int light = 0, numSamples = 0;
for (auto faceIter = faces.begin(); faceIter != faces.end(); ++faceIter)
{
const face_t* face = *faceIter;
const plane_t* plane = &world->planes[face->plane_id];
vec3_t normal = face->side ? -plane->normal : plane->normal;
// Check if the face is at a shallow angle with the reference face
double dot = normal.dotProduct(refNormal);
if (dot < 0.5)
continue;
unsigned char sample;
if (!sample_lightmap(world, face, faceBounds.find(face)->second, point, &sample))
continue;
light += sample + (0xFF - face->baselight);
numSamples++;
}
// We should always end up with at least one sample (that from refFace itself), so if we divide by zero here something is very much wrong
return (unsigned char)(light / numSamples);
}
unsigned char compute_faceVertex_light5(const world_t* world, const face_t* refFace, const FaceBounds& faceBounds, Vec3 point)
{
const int phaseCount = 18;
const float sampleRange = 16; // Increasing this value allows lightmap samples from further away
if (refFace->lightmap < 0)
return 0;
auto faces = world->facesWithPoint(point);
if (faces.empty())
return 0;
const Plane* refPlane = &world->planes[refFace->plane_id];
Vec3 refNormal = refFace->side ? -refPlane->normal : refPlane->normal;
Vec3 tangent, bitangent;
refPlane->getTangents(tangent, bitangent);
Vec3 jitter;
unsigned int light = 0, numSamples = 0;
for (int index = 0; index < phaseCount; ++index)
{
getJitterOffset(&jitter.x, &jitter.y, index, phaseCount);
Vec3 jitteredPoint = point + (tangent * jitter.x + bitangent * jitter.y) * sampleRange;
for (auto faceIter = faces.begin(); faceIter != faces.end(); ++faceIter)
{
const face_t* face = *faceIter;
const plane_t* plane = &world->planes[face->plane_id];
vec3_t normal = face->side ? -plane->normal : plane->normal;
// Check if the face is at a shallow angle with the reference face
double dot = normal.dotProduct(refNormal);
if (dot < 0.5)
continue;
unsigned char sample;
if (!sample_lightmap(world, face, faceBounds.find(face)->second, jitteredPoint, &sample))
continue;
light += sample + (0xFF - face->baselight);
numSamples++;
}
}
return (unsigned char)(light / numSamples);
}