Forum > OpenGL

Simple OpenGL Lighting

(1/2) > >>

ranny:
Hi,

It has been a while since I was working on some OpenGL stuff and I got to a point where I could produce some 3D plots of functions, however, if they were a solid colour then they did not look very good.  I was wanting to add a simple addition of lighting so that as the surfaces turn away form the light source they would be darker.  Obvious stuff!  However, I have not been able to find a simple bit of code that can help me with it.  I have looked at a few tutorials but not found anything that I can make sense of.

Any help wit this would be greatly appreciated.

Thanks

ChrisR:
OpenGL has changed a lot over the years. If you are interested in legacy OpenGL where you have access to the fixed function pipeline, you may want to look at the GLScene demos. In legacy OpenGL, the fixed function pipeline imbues materials with properties like ambient, diffuse, and specular lighting.

If you want to use modern OpenGL, there is no fixed function pipeline, and everything is done using the programmable GLSL shaders. Here is an example
 https://github.com/neurolabusc/plyview
where the GLSL fragment shader is here:
 https://github.com/neurolabusc/plyview/blob/ea98c5d9dc0eebfaef426c32e111d037c9a4ee19/unit1.pas#L138

So the GLSL shader is:

```
#version 330
in vec4 vClr;
in vec3 vN, vL, vV;
out vec4 color;
uniform float Ambient = 0.4;
uniform float Diffuse = 0.7;
uniform float Specular = 0.6;
uniform float Roughness = 0.1;
void main() {
 vec3 n = normalize(vN);
 vec3 v = normalize(vV);
 vec3 h = normalize(vL+v);
 float diffuse = dot(vL,n);
 vec3 AmbientColour = vClr.rgb;
 vec3 DiffuseColour = vClr.rgb;
 vec3 SpecularColour = vec3(1.0, 1.0, 1.0);
 float specular =  pow(max(0.0,dot(n,h)),1.0/(Roughness * Roughness));
 color = vec4(AmbientColour*Ambient + DiffuseColour*diffuse*Diffuse +SpecularColour*specular* Specular, 1.0);
}
```

The final color ("color") is the sum of the ambient (constant lighting), the diffuse Lambertian reflection (which depends on the relation of the surface normal and light position), and the specular glint reflection (which depends on surface normal, light position and eye position).

 
A much more complex Lazarus project that illustrates different shaders is Surfice

https://www.nitrc.org/plugins/mwiki/index.php/surfice:MainPage
https://github.com/neurolabusc/surf-ice

You may find this useful as it provides a lot of GLSL shaders as text files in its "Resources" folder, and the graphical interface allows you to drag sliders to change properties of the shaders. Many of these shaders illustrate more sophisticated lighting models light MatCaps, Hemispheric lighting, etc.

Seenkao:
GLScene не лучший вариант для использования OpenGL. Но подходящий, если нет желания сильно углубляться в сам OpenGL.

Я бы посоветовал для начального уровня использовать "устаревший" OpenGL. А хорошо усвоившись и поняв основы, переходить на язык шейдеров.

Google translate:
GLScene is not the best choice for using OpenGL. But suitable if you do not want to go deeply into OpenGL itself.

I would recommend using the "legacy" OpenGL for the entry level. And having mastered well and having understood the basics, switch to the language of shaders.

Tutorial En
Tutorial Deutsch - more detailed lessons. You can just retype it and you can already understand something.

Game Engine - maybe you are interested in something. Some of them use OpenGL specifically.

ranny:
Hi,

Thanks for the replies and sorry for the delay in responding.  I totally forgot that I asked this question some time ago, actually in July 2019 and got some useful help.   I also see a related issue in March 2020.  So collectively I have a few leads.

What I am experiencing is, similar to the March 2020 post, is an object that is rotated gets darker when it should be getting lit when it comes into view.  I understand the normals but I think my issue is that I am looking at a surfface, not a solid object.  I want it to be lit on top and underneath when it is rotated.  I thought I would be able to "fix" the issue by taking the absolute value of the normal however whilst I got that to work in a homegrown program of my own (something I created that did all the rotation and 3D viewing from scratch but was painfully slow), it doesn't work in Open GL.

I am going to create a new program with a solid and see how that performs with lighting to help me understand the program requirements better.

So, thanks for all the comments and links and I will give it another review.

ranny:
Hello again.

Think I have got it working well now.  I included the following statement in my OpenGL initiation procedure:-

glLightModeli(GL_LIGHT_MODEL_TWO_SIDE,GL_TRUE);

It appears this allows the polygons to be lilt from both sides and the results are pretty good.

Thanks

Navigation

[0] Message Index

[#] Next page

Go to full version