Recent

Author Topic: Simple OpenGL Lighting  (Read 5995 times)

ranny

  • Jr. Member
  • **
  • Posts: 57
Simple OpenGL Lighting
« on: August 26, 2021, 11:19:52 am »
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

  • Full Member
  • ***
  • Posts: 240
Re: Simple OpenGL Lighting
« Reply #1 on: August 26, 2021, 04:01:18 pm »
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

  • Sr. Member
  • ****
  • Posts: 412
    • New ZenGL.
Re: Simple OpenGL Lighting
« Reply #2 on: August 26, 2021, 08:25:35 pm »
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.
Rus: Стремлюсь к созданию минимальных и достаточно быстрых приложений.
Работаю над ZenGL.
Eng: I strive to create applications that are minimal and reasonably fast.
Working on ZenGL. :)

ranny

  • Jr. Member
  • **
  • Posts: 57
Re: Simple OpenGL Lighting
« Reply #3 on: September 02, 2021, 09:03:26 am »
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

  • Jr. Member
  • **
  • Posts: 57
Re: [SOLVED] Simple OpenGL Lighting
« Reply #4 on: September 03, 2021, 11:51:51 am »
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

ChrisR

  • Full Member
  • ***
  • Posts: 240
Re: Simple OpenGL Lighting
« Reply #5 on: September 04, 2021, 07:13:11 pm »
It sounds like you need to address the Winding Order for your triangles. OpenGL allows you to specify glFrontFace as GL_CW or GL_CCW (clockwise or counter clockwise). GL_CCW is the default for OpenGL and most triangular mesh formats. I would suggest you ensure all faces of models use a consistent winding order. This will allow you to differentiate the Front and Back face.

The image below is from the free Surfice software, which you can compile with Lazarus or download as compiled for macOS, Linux or Windows. If you select the "Phong" shader, you will see a slider named "LightBackfaces". Notice in the attached cutaway image, the default behavior is to treat backfires differently, to give a visual cue that this is inside the object: the backfires are de-saturated, slightly darker and less glossy than the front faces.

ranny

  • Jr. Member
  • **
  • Posts: 57
Re: Simple OpenGL Lighting
« Reply #6 on: September 08, 2021, 03:30:55 pm »
Hi,

Pretty sure I have all the polgons in the right winding order although I think it may be CW rather than CCW ehich I will look at and see what difference it makes if I change it.

I tried downloading the Surfice software but havent managed to install it, I will need to see what I am doing wrong (error says Unable to load OpenGL Core 3.3).

Attached images show progress so far and its better than it was a coupled of weeks ago!

Cheers and thanks.

 

TinyPortal © 2005-2018