From b7ac22ec34d1f5a9a2bc387e64f0a7953fb992a5 Mon Sep 17 00:00:00 2001 From: UnknownShadow200 Date: Sat, 8 Feb 2025 12:23:43 +1100 Subject: [PATCH] Fix lighting using some values calculated for last vertex, instead of the current vertex --- GL/draw.c | 41 +++++------------------------------------ GL/lighting.c | 39 ++++++++++++++++++--------------------- GL/private.h | 8 +------- 3 files changed, 24 insertions(+), 64 deletions(-) diff --git a/GL/draw.c b/GL/draw.c index 6e0df5c..f39e00e 100644 --- a/GL/draw.c +++ b/GL/draw.c @@ -90,17 +90,6 @@ GL_FORCE_INLINE IndexParseFunc _calcParseIndexFunc(GLenum type) { x = __x; y = __y; z = __z; \ } - -GL_FORCE_INLINE void transformToEyeSpace(GLfloat* point) { - _glMatrixLoadModelView(); - mat_trans_single3_nodiv(point[0], point[1], point[2]); -} - -GL_FORCE_INLINE void transformNormalToEyeSpace(GLfloat* normal) { - _glMatrixLoadNormal(); - mat_trans_normal3(normal[0], normal[1], normal[2]); -} - GL_FORCE_INLINE PolyHeader *_glSubmissionTargetHeader(SubmissionTarget* target) { gl_assert(target->header_offset < aligned_vector_size(&target->output->vector)); return aligned_vector_at(&target->output->vector, target->header_offset); @@ -622,42 +611,22 @@ static void transform(SubmissionTarget* target) { } } -static void mat_transform_normal3(const float* xyz, const float* xyzOut, const uint32_t count, const uint32_t inStride, const uint32_t outStride) { - const uint8_t* dataIn = (const uint8_t*) xyz; - uint8_t* dataOut = (uint8_t*) xyzOut; - +static void mat_transform_normal3(VertexExtra* extra, const uint32_t count) { ITERATE(count) { - const float* in = (const float*) dataIn; - float* out = (float*) dataOut; - - TransformNormalNoMod(in, out); - - dataIn += inStride; - dataOut += outStride; + TransformNormalNoMod(extra->nxyz, extra->nxyz); + extra++; } } static void light(SubmissionTarget* target) { - - static AlignedVector* eye_space_data = NULL; - - if(!eye_space_data) { - eye_space_data = (AlignedVector*) malloc(sizeof(AlignedVector)); - aligned_vector_init(eye_space_data, sizeof(EyeSpaceData)); - } - - aligned_vector_resize(eye_space_data, target->count); - /* Perform lighting calculations and manipulate the colour */ Vertex* vertex = _glSubmissionTargetStart(target); VertexExtra* extra = aligned_vector_at(target->extras, 0); - EyeSpaceData* eye_space = (EyeSpaceData*) eye_space_data->data; _glMatrixLoadNormal(); - mat_transform_normal3(extra->nxyz, eye_space->n, target->count, sizeof(VertexExtra), sizeof(EyeSpaceData)); + mat_transform_normal3(extra, target->count); - EyeSpaceData* ES = aligned_vector_at(eye_space_data, 0); - _glPerformLighting(vertex, ES, target->count); + _glPerformLighting(vertex, extra, target->count); } GL_FORCE_INLINE int _calc_pvr_face_culling() { diff --git a/GL/lighting.c b/GL/lighting.c index fc61a1c..806ed61 100644 --- a/GL/lighting.c +++ b/GL/lighting.c @@ -499,14 +499,14 @@ GL_FORCE_INLINE void _glLightVertexPoint( #undef _PROCESS_COMPONENT } -void _glPerformLighting(Vertex* vertices, EyeSpaceData* es, const uint32_t count) { +void _glPerformLighting(Vertex* vertices, VertexExtra* extra, const uint32_t count) { GLubyte i; GLuint j; Material* material = _glActiveMaterial(); Vertex* vertex = vertices; - EyeSpaceData* data = es; + VertexExtra* data = extra; /* Calculate the colour material function once */ void (*updateColourMaterial)(const GLubyte*) = NULL; @@ -529,32 +529,29 @@ void _glPerformLighting(Vertex* vertices, EyeSpaceData* es, const uint32_t count } } - /* Calculate the ambient lighting and set up colour material */ + if(!_glEnabledLightCount()) { + return; + } + for(j = 0; j < count; ++j, ++vertex, ++data) { + /* Calculate the ambient lighting and set up colour material */ if(updateColourMaterial) { updateColourMaterial(vertex->bgra); } /* Copy the base colour across */ - vec4cpy(data->finalColour, material->baseColour); - } + float finalColour[4]; + vec4cpy(finalColour, material->baseColour); - if(!_glEnabledLightCount()) { - return; - } - - vertex = vertices; - data = es; - for(j = 0; j < count; ++j, ++vertex, ++data) { /* Direction to vertex in eye space */ float Vx = -vertex->xyz[0]; float Vy = -vertex->xyz[1]; float Vz = -vertex->xyz[2]; VEC3_NORMALIZE(Vx, Vy, Vz); - const float Nx = data->n[0]; - const float Ny = data->n[1]; - const float Nz = data->n[2]; + const float Nx = data->nxyz[0]; + const float Ny = data->nxyz[1]; + const float Nz = data->nxyz[2]; for(i = 0; i < MAX_GLDC_LIGHTS; ++i) { LightSource* light = _glLightAt(i); @@ -588,7 +585,7 @@ void _glPerformLighting(Vertex* vertices, EyeSpaceData* es, const uint32_t count if(NdotH < 0.0f) NdotH = 0.0f; _glLightVertexDirectional( - data->finalColour, + finalColour, i, LdotN, NdotH ); } else { @@ -627,17 +624,17 @@ void _glPerformLighting(Vertex* vertices, EyeSpaceData* es, const uint32_t count if(NdotH < 0.0f) NdotH = 0.0f; _glLightVertexPoint( - data->finalColour, + finalColour, i, LdotN, NdotH, att ); } } } - vertex->bgra[R8IDX] = clamp(data->finalColour[0] * 255.0f, 0, 255); - vertex->bgra[G8IDX] = clamp(data->finalColour[1] * 255.0f, 0, 255); - vertex->bgra[B8IDX] = clamp(data->finalColour[2] * 255.0f, 0, 255); - vertex->bgra[A8IDX] = clamp(data->finalColour[3] * 255.0f, 0, 255); + vertex->bgra[R8IDX] = clamp(finalColour[0] * 255.0f, 0, 255); + vertex->bgra[G8IDX] = clamp(finalColour[1] * 255.0f, 0, 255); + vertex->bgra[B8IDX] = clamp(finalColour[2] * 255.0f, 0, 255); + vertex->bgra[A8IDX] = clamp(finalColour[3] * 255.0f, 0, 255); } } diff --git a/GL/private.h b/GL/private.h index 422690d..cc42dae 100644 --- a/GL/private.h +++ b/GL/private.h @@ -445,13 +445,7 @@ GL_FORCE_INLINE GLboolean _glCheckImmediateModeInactive(const char* func) { return GL_FALSE; } -typedef struct { - float n[3]; // 12 bytes - float finalColour[4]; //28 bytes - uint32_t padding; // 32 bytes -} EyeSpaceData; - -extern void _glPerformLighting(Vertex* vertices, EyeSpaceData *es, const uint32_t count); +extern void _glPerformLighting(Vertex* vertices, VertexExtra* extra, const uint32_t count); unsigned char _glIsClippingEnabled(); void _glEnableClipping(unsigned char v);