Skip to content

Commit

Permalink
Lighting: Fix model normal data inheritance
Browse files Browse the repository at this point in the history
  • Loading branch information
julianxhokaxhiu committed Mar 25, 2023
1 parent 9c012b6 commit 1b48e7c
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 15 deletions.
3 changes: 2 additions & 1 deletion Changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@

- Core: Fix `ERROR: COULD NOT CHANGE TO DIRECTORY` error when starting the Condor minigame
- Graphics: Fix missing snow footsteps ( https://github.com/julianxhokaxhiu/FFNx/issues/550 )
- Input: Allow Cloud to walk/run based on the left analogue stick position ( https://github.com/julianxhokaxhiu/FFNx/issues/523 )
- Input: Allow Cloud to walk/run based on the left analogue stick position ( https://github.com/julianxhokaxhiu/FFNx/issues/523 + https://github.com/julianxhokaxhiu/FFNx/issues/557 )
- Lighting: Fix model normal data inheritance

## FF8

Expand Down
32 changes: 18 additions & 14 deletions src/gl/gl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -170,27 +170,33 @@ void gl_draw_without_lighting(struct indexed_primitive* ip, uint32_t clip)
// draw a set of primitives with lighting
void gl_draw_with_lighting(struct indexed_primitive *ip, struct polygon_data *polydata, uint32_t clip)
{
static vector3<float> *normaldata;
bool has_model_data = false;
static std::vector<vector3<float>> normals;
static vector3<float> zero = { 0.0f, 0.0f, 0.0f };

normaldata = nullptr;
normals.resize(ip->vertexcount);
std::fill(normals.begin(), normals.end(), zero);

// If the user has no preference, we can try to optimize some of the flow
// User wants to attempt to load model data
if (!prefer_lighting_cpu_calculations)
{
// If models do provide normal data, use it
if (polydata->normaldata != NULL) normaldata = polydata->normaldata;
if (polydata->normaldata != NULL)
{
for (uint32_t idx = 0; idx < ip->vertexcount; idx++)
{
normals[idx] = polydata->has_normindextable ? polydata->normaldata[polydata->normindextabledata[idx]] : polydata->normaldata[idx];
}

has_model_data = true;
}
}

// If we still didn't get normalized data, we have to calculate them on the CPU
if (normaldata == nullptr)
// If the previous code was not able to fetch the model normal data, we have to calculate it on the CPU
if (!has_model_data)
{
static std::vector<vector3<float>> normals;
static vector3<float> zero = { 0.0f, 0.0f, 0.0f };
vector3<float> e12, e13, triNormal;

normals.resize(ip->vertexcount);
std::fill(normals.begin(), normals.end(), zero);

// Calculate vertex normals by averaging adjacent triangle normals
// Vertex normals are calculated here because battle models dont seem to include normals
for (uint32_t idx = 0; idx < ip->indexcount; idx+=3)
Expand Down Expand Up @@ -218,11 +224,9 @@ void gl_draw_with_lighting(struct indexed_primitive *ip, struct polygon_data *po
{
normalize_vector(&normals[idx]);
}

normaldata = normals.data();
}

gl_draw_indexed_primitive(ip->primitivetype, ip->vertextype, ip->vertices, normaldata, ip->vertexcount, ip->indices, ip->indexcount, 0, polydata->boundingboxdata, clip, true);
gl_draw_indexed_primitive(ip->primitivetype, ip->vertextype, ip->vertices, normals.data(), ip->vertexcount, ip->indices, ip->indexcount, 0, polydata->boundingboxdata, clip, true);
}

// main rendering routine, draws a set of primitives according to the current render state
Expand Down

0 comments on commit 1b48e7c

Please sign in to comment.