0
\$\begingroup\$

Problem: The spotlight I've set up with OpenGL won't light up anything behind it, no matter what values I set for GL_SPOT_DIRECTION

    float[] LightDir = new float[]{0,0,-1,0};         
    float[] LightPos = new float[]{0,0,15f,1};

Initialization code for lighting

        glEnable(GL_LIGHTING);
        glEnable(GL_LIGHT0);
        glLightModel(GL_LIGHT_MODEL_AMBIENT,asFlippedFloatBuffer(new float[]{0.1f, 0.1f, 0.1f, 1f}));
        glLight(GL_LIGHT0,GL_DIFFUSE,asFlippedFloatBuffer(new float[]{0.5f, 0.5f, 0.5f, 1f}));
        glLight(GL_LIGHT0, GL_POSITION, asFlippedFloatBuffer(new float[]{0, 0, 0, 1}));
        glLightf(GL_LIGHT0,GL_SPOT_CUTOFF,60.0f);
        glLightf(GL_LIGHT0,GL_SPOT_EXPONENT,2.0f);

        glEnable(GL_COLOR_MATERIAL);
        glColorMaterial(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE);
        glShadeModel(GL_SMOOTH);

Lighting code ran every loop

         glLight(GL_LIGHT0,GL_SPOT_DIRECTION,asFlippedFloatBuffer(LightDir));
         glLight(GL_LIGHT0,GL_POSITION,asFlippedFloatBuffer(LightPos));

The Following shows: enter image description here

To try and change the direction I hit a key that does

LightDir[1]++;

The angle of the light goes up as expected, but seems to stop turning once it's pointed straight up. Which looks like enter image description here

Which is the problem, it wont turn around. Iv'e tried every possible value for LightDir in all it's values, but I cannot get it to illuminate the back pf the tube. I've even tried to transform to light around the scene with glRotate and glTranslate. The light is supposed to function as a flashlight. Is this something that would be potentially solved with Shaders? Thanks in advance!

EDIT:

Anytime LightDir[2] goes above 0 the light goes dark.

\$\endgroup\$
  • \$\begingroup\$ Seems to be caused by per-vertex lighting instead of per-fragment (per-pixel) \$\endgroup\$ – Kromster Jun 3 '15 at 5:59
2
\$\begingroup\$

You're not actually rotating the light - at least not in the direction you think you are.

You said you're using LightDir[1]++ to change the direction of the light. The direction starts as (0, 0, -1) - the negative Z direction (which appears to be towards the camera, in your program).

After you press the "rotate light" key a bunch of times, the light direction will be something like (0, 50, -1) - which is almost straight up (and not normalized).

In other words, it's not "no matter what values I set for GL_SPOT_DIRECTION" - your program doesn't tell OpenGL to make the light point backwards, only to make it point forwards and up. The direction (0, 0, 1) would be backwards.

| improve this answer | |
\$\endgroup\$
  • \$\begingroup\$ Understanding that GL_SPOT_DIRECTION is a vector ( right?, just put that toghether after reading your answer. ), I've tried changing all values of the Array, not just LightDir[1], Changing LightDir[2] and LightDir[0] seemed to still cause the same problem \$\endgroup\$ – White Development Studios Jun 3 '15 at 10:17
  • \$\begingroup\$ @WhiteDevelopmentStudios Yes, it's a vector. What did you try changing it to? \$\endgroup\$ – user253751 Jun 3 '15 at 19:31
  • \$\begingroup\$ I tried several values for every index. Anytime LightDir[2] goes above one though, it doesn't produce any light. {0,0,-1,0} and {0,0,1,0} don't produce opposite results like they should. I've confirmed this on 2 different computers. \$\endgroup\$ – White Development Studios Jun 3 '15 at 19:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.