0

I am trying to draw a rotating cubic and add a spot light in a fixed position in front of this cubic. But because i set the wrong value in z-axis, the light don't show up. After i tried different position of light, the cubic finally displays as i wished. But i still don't know why this value works.

Here is the wrong code. But i think the value is reasonable.

    Matrix.setIdentityM(mLightModelMatrix, 0);
    Matrix.translateM(mLightModelMatrix, 0, 0.0f, 0.0f, 0.5f);
    Matrix.multiplyMV(mLightPosInWorldSpace, 0, mLightModelMatrix, 0, mLightPosInModelSpace, 0);
    Matrix.multiplyMV(mLightPosInEyeSpace, 0, mViewMatrix, 0, mLightPosInWorldSpace, 0);
    GLES20.glUniform3f(muLightPosHandler,
            mLightPosInEyeSpace[0],
            mLightPosInEyeSpace[1],
            mLightPosInEyeSpace[2]);

Here is the right code. But i don't know why it works.

    Matrix.setIdentityM(mLightModelMatrix, 0);
    Matrix.translateM(mLightModelMatrix, 0, 0.0f, 0.0f, 2.8f);
    Matrix.multiplyMV(mLightPosInWorldSpace, 0, mLightModelMatrix, 0, mLightPosInModelSpace, 0);
    Matrix.multiplyMV(mLightPosInEyeSpace, 0, mViewMatrix, 0, mLightPosInWorldSpace, 0);
    GLES20.glUniform3f(muLightPosHandler,
            mLightPosInEyeSpace[0],
            mLightPosInEyeSpace[1],
            mLightPosInEyeSpace[2]);

The difference between these two snippets is only the z-axis of light position. You can get all source code from here.

The reason i think 0.0f, 0.0f, 0.5f is a reasonable position is that the center point of the cubic front face is 0.0f, 0,0f, 0.5f before transformation. So it will give the cubic strongest light.

genpfault
  • 51,148
  • 11
  • 85
  • 139
Frank Cheng
  • 5,928
  • 9
  • 52
  • 80
  • 1
    I think that the problem is because the light is **on** the face of the cube rather than a little bit "in front" of it. If you move the light position to `0, 0, 0.6` (say) does the cube get lit? – ChrisF Dec 24 '12 at 09:27
  • @ChrisF Util when i increase to `0.0, 0.0, 0.7`, the cubic show up a bit. And from 0.7 to 2.8, the cubic become brighter and brighter. – Frank Cheng Dec 24 '12 at 09:34
  • 1
    Ah. I think I know the answer. It's because you have a spot light which is defined by a light cone. At any distance before 2.8 the cone isn't wide enough to light the entire cube. I'll draw a picture. – ChrisF Dec 24 '12 at 09:35

1 Answers1

3

It's because you have a spot light which is defined by a light cone. At any distance before 2.8 the cone isn't wide enough to light the entire cube, as demonstrated by this picture:

enter image description here

To get the whole face of the cube to be lit you either need to move the light further away (as you have found) or widen the light cone.

With only two triangles per face you won't get much lighting in the first case as none of the vertices are lit. It's the light on the vertices that determine the light on the face. If you break the model down into more faces you'll see the effect.

ChrisF
  • 134,786
  • 31
  • 255
  • 325
  • But i seems that the light in second picture is stronger then the first one. It seems opposite with real world. – Frank Cheng Dec 24 '12 at 09:48
  • 1
    @hongbosb - it's because the whole of the face is lit rather than a small section in the middle. With only two triangles per face you won't get much lighting in the first case as none of the vertices are lit. – ChrisF Dec 24 '12 at 09:50