Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit c0dea964 authored by Martin Storsjo's avatar Martin Storsjo Committed by Mathias Agopian
Browse files

Calculate specular lighting correctly

Since the lighting calculations are done in object space, the vector
from the object to the viewer also needs to be transformed to object
space.
parent 4f31af93
Loading
Loading
Loading
Loading
+1 −0
Original line number Original line Diff line number Diff line
@@ -287,6 +287,7 @@ struct light_t {
    vec4_t      normalizedObjPosition;
    vec4_t      normalizedObjPosition;
    vec4_t      spotDir;
    vec4_t      spotDir;
    vec4_t      normalizedSpotDir;
    vec4_t      normalizedSpotDir;
    vec4_t      objViewer;
    GLfixed     spotExp;
    GLfixed     spotExp;
    GLfixed     spotCutoff;
    GLfixed     spotCutoff;
    GLfixed     spotCutoffCosine;
    GLfixed     spotCutoffCosine;
+8 −3
Original line number Original line Diff line number Diff line
@@ -216,6 +216,8 @@ static inline void light_picker(ogles_context_t* c)
static inline void validate_light_mvi(ogles_context_t* c)
static inline void validate_light_mvi(ogles_context_t* c)
{
{
    uint32_t en = c->lighting.enabledLights;
    uint32_t en = c->lighting.enabledLights;
    // Vector from object to viewer, in eye coordinates
    const vec4_t eyeViewer = { 0, 0, 0x1000, 0 };
    while (en) {
    while (en) {
        const int i = 31 - gglClz(en);
        const int i = 31 - gglClz(en);
        en &= ~(1<<i);
        en &= ~(1<<i);
@@ -223,6 +225,9 @@ static inline void validate_light_mvi(ogles_context_t* c)
        c->transforms.mvui.point4(&c->transforms.mvui,
        c->transforms.mvui.point4(&c->transforms.mvui,
                &l.objPosition, &l.position);
                &l.objPosition, &l.position);
        vnorm3(l.normalizedObjPosition.v, l.objPosition.v);
        vnorm3(l.normalizedObjPosition.v, l.objPosition.v);
        c->transforms.mvui.point4(&c->transforms.mvui,
                &l.objViewer, &eyeViewer);
        vnorm3(l.objViewer.v, l.objViewer.v);
    }
    }
}
}


@@ -379,9 +384,9 @@ void lightVertex(ogles_context_t* c, vertex_t* v)
            // specular
            // specular
            if (ggl_unlikely(s && l.implicitSpecular.v[3])) {
            if (ggl_unlikely(s && l.implicitSpecular.v[3])) {
                vec4_t h;
                vec4_t h;
                h.x = d.x;
                h.x = d.x + l.objViewer.x;
                h.y = d.y;
                h.y = d.y + l.objViewer.y;
                h.z = d.z + 0x10000;
                h.z = d.z + l.objViewer.z;
                vnorm3(h.v, h.v);
                vnorm3(h.v, h.v);
                s = dot3(n.v, h.v);
                s = dot3(n.v, h.v);
                s = (s<0) ? (twoSide?(-s):0) : s;
                s = (s<0) ? (twoSide?(-s):0) : s;