Lights in shader

Hi all,
As gl_LightSource is deprecated I suppose we have to pass all data about light source to the shader.

Is someone has an example of the good way to access light in modern shader from openframeworks
thanks

1 Like

the latest OF version in master already has lights implemented through shaders when using the programmable renderer. you can also access the light data through the ofLightsData() function in order to pass it to custom shaders

2 Likes

ok thanks a lot

Hi @arturo,

Excited to see lights moving to a shader approach.
I have been searching through the ofGLProgrammableRenderer code but can’t seem to find where the lights are getting set in the shaders. I noticed that the enableLighting() function in ofBaseRenderer is not implemented and there is no use of ofLightsData() in ofGLProgrammableRenderer.

When I try ofSetCurrentRenderer( ofGLProgrammableRenderer::TYPE ); at the top of setup() in examples/3d/3DPrimitives example, I get a bunch of errors like:

[ error ] ofShader: begin(): couldn't begin, shader not loaded
[ error ] ofShader: setupShaderFromSource(): GL_VERTEX_SHADER shader failed to compile
[ error ] ofShader: GL_VERTEX_SHADER shader reports:
ERROR: 0:8: Invalid qualifiers 'out' in global variable context
ERROR: 0:8: Invalid qualifiers 'out' in global variable context
ERROR: 0:8: Invalid qualifiers 'out' in global variable context
ERROR: 0:8: Invalid qualifiers 'out' in global variable context
ERROR: 0:8: Invalid qualifiers 'in' in global variable context
ERROR: 0:8: Invalid qualifiers 'in' in global variable context
ERROR: 0:8: Invalid qualifiers 'in' in global variable context
ERROR: 0:8: Invalid qualifiers 'in' in global variable context
ERROR: 0:8: Use of undeclared identifier 'position'
ERROR: 0:8: Use of undeclared identifier 'normal'
ERROR: 0:8: Use of undeclared identifier 'transformedNormal'
ERROR: 0:8: Use of undeclared identifier 'tempNormal'
ERROR: 0:8: Use of undeclared identifier 'eyePosition3'
ERROR: 0:8: Use of undeclared identifier 'eyePosition'
ERROR: 0:8: Use of undeclared identifier 'eyePosition'
ERROR: 0:8: Use of undeclared identifier 'outtexcoord'
ERROR: 0:8: Use of undeclared identifier 'texcoord'
ERROR: 0:8: Use of undeclared identifier 'texcoord'
ERROR: 0:8: Use of undeclared identifier 'position'

[ error ] ofShader: setupShaderFromSource(): GL_FRAGMENT_SHADER shader failed to compile
[ error ] ofShader: GL_FRAGMENT_SHADER shader reports:
ERROR: 0:7: Invalid qualifiers 'out' in global variable context
ERROR: 0:10: Invalid qualifiers 'in' in global variable context
ERROR: 0:10: Invalid qualifiers 'in' in global variable context
ERROR: 0:10: Invalid qualifiers 'in' in global variable context
ERROR: 0:10: Invalid qualifiers 'in' in global variable context
ERROR: 0:10: Invalid qualifiers 'in' in global variable context
ERROR: 0:10: Use of undeclared identifier 'transformedNormal'
ERROR: 0:10: Use of undeclared identifier 'eyePosition3'
ERROR: 0:10: Use of undeclared identifier 'transformedNormal'
ERROR: 0:10: Use of undeclared identifier 'transformedNormal'
ERROR: 0:10: Use of undeclared identifier 'eyePosition3'
ERROR: 0:10: Use of undeclared identifier 'transformedNormal'
ERROR: 0:10: Use of undeclared identifier 'eyePosition3'
ERROR: 0:16: Use of undeclared identifier 'fragColor'

Am I doing something wrong?

the lights are used from the materials since you might want to have different types of lighting, which will be defined by the material shader. lights are only a position + some parameters like the type…

not sure why are you getting that error, the correct way to set the programmable renderer no is:

ofSetOpenGLVersion(3,2);

but setting it the way you are doing it works too so not sure what’s going on

I created an empty project with only ofSetOpenGLVersion(3,2); called in setup().
I get the error

[ error ] ofShader: begin(): couldn't begin, shader not loaded

It looks like setup(const string & glslVersion) is not called on the programmable renderer. In the beginDefaultShader method it reaches the line

nextShader = &defaultNoTexNoColor();

But since setup was never called, the shader is never loaded. I am working from the latest master version from git

oh, yes, ofSetGLVersion has to be called in main before calling ofSetupOpenGL so it uses the correct renderer when setting up openGL

ofSetOpenGLVersion(3,2); is called by default, no need to call it in the setup or main or whatever it really shouldn’t make any difference,

what happens if you print cout<<glGetString(GL_VERSION); do you see a 3.2? my guess is you are maybe on 3.1 or 2.1

are you on windows or mac, if so can you paste the specs and also the advanced log from the openGlinfo example?

1 Like

I am on OSX 10.8.5 with the NVIDIA GeForce GT 650M 1024 MB. The openGlInfo example says that my openGL version is 2.1. But according to this document, I should have support for 3.2.

Attached is the full openGL profile
openglReport.cpp (24.8 KB)

After calling ofSetOpenGLVersion(3,2); in main, the openGL version prints out correctly at 3.2. I guess 3.2 is not set by default?

not unless you set it explicitly or use the programmable renderer, the default with the typical main file is 2.1

so, does the new lighting work for you setting the GL version?

hm… I thought that if you
call
ofSetCurrentRenderer(ofGLProgrammableRenderer::TYPE);
in the main…
ofSetOpenGLVersion(3,2); was also called by default…

otherwise shaders will not work anyway…

Ah yes. The lighting works in the of3DPrimitives example when setting the gl version in the main. But I do get these warnings on startup :

[warning] ofShader: GL_VERTEX_SHADER shader reports:
WARNING: 0:2: extension 'GL_ARB_texture_rectangle' is not supported

[warning] ofShader: GL_FRAGMENT_SHADER shader reports:
WARNING: 0:2: extension 'GL_ARB_texture_rectangle' is not supported

When I hit ‘escape’ to exit the program I get a crash. Looks to be in the deconstructor of ofLight::Data trying to access the renderer.

@igiso calling ofSetCurrentRenderer(ofGLProgrammableRenderer::TYPE); did set the openGL version to 3.2 when called in main.h

lol ok now I got what Arturo was saying… :slight_smile:

by the way it seems like the boost::shared_ptr already autonomously released glRenderer right when you clicked exit.

probably commenting out everything inside the ~Data() will “solve” this problem once and for all without a single trace of memory leak (…??)
visit:

:octocat:

perfect, the problems you are getting should be solved with: https://github.com/openframeworks/openFrameworks/commit/b42ed6414824f0de2b499e40305373bd0d310ae5 and https://github.com/openframeworks/openFrameworks/commit/9423e2ea82fb110f8183137fffe7103176c72615

I just pulled down from the master branch. I do not get the shader warnings on startup. But I still get the crash when hitting escape.