Author Topic: Shader - Colours And Lighting.  (Read 4417 times)

0 Members and 1 Guest are viewing this topic.

Offline Clyde

  • A Little Fuzzy Wuzzy
  • DBF Aficionado
  • ******
  • Posts: 7271
  • Karma: 71
    • View Profile
Shader - Colours And Lighting.
« on: May 06, 2012 »
Hi!

I've setup the lighting outside of the shader in the usual ways. And am finding my feet with simple stuffs. But am having problems with getting the final object to display with it's overall colour with the shader - including the light, entity and texture colours. I had a go with the following:

vert_shader:
Code: [Select]

// vert shader
varying vec4 tex_coord;



void main()
{
   gl_Position      = gl_ModelViewProjectionMatrix * gl_Vertex;
   
   tex_coord=gl_MultiTexCoord0;
   
   gl_TexCoord[0]   = tex_coord;//gl_MultiTexCoord0;


}


fragment shader:
Code: [Select]

// test_one_frag_shader


float light_colour;
float texture_colour;
float final_colour;

varying vec4 tex_coord;

uniform sampler2D texture;


void main()
{
   texture_colour   =texture2D( texture, tex_coord );
   light_colour   =gl_LightSource[0].ambient;


   final_colour   =texture_colour + light_colour;


   gl_FragColor   =final_colour;
}


If I just use: gl_FragColor=texture2D( texture, gl_TexCoord[0] ); then I get the object with it's texture, but no lighting. It's probably not as simple as what I thought; perhaps normals. Anyhelp, would be great!! :D

[edit] Or to put it another way, how do I get the lighting colour from lighting that I've already setup using GL_LIGHT0. And then incorporate that into the shader?


Cheers!
Clyde.
« Last Edit: May 07, 2012 by Clyde »
Still Putting The IT Into Gravy
If Only I Knew Then What I Know Now.

Challenge Trophies Won:

Offline Jim

  • Founder Member
  • DBF Aficionado
  • ********
  • Posts: 5301
  • Karma: 402
    • View Profile
Re: Shader - Colours And Lighting.
« Reply #1 on: May 07, 2012 »
Hi Clyde, Can you post the calls you are making to glLight()?  Your shader is only using one part of the light colours (ambient).
Also, is texture_colour + light_colour likely to saturate (i.e. all go to white? - when added r,g,b all go to >= 1).
Do you want (texture_colour * light_colour), which would give you a scale on the texture colour?
All depends on how you've set up your lights.

Jim
Challenge Trophies Won:

Offline Clyde

  • A Little Fuzzy Wuzzy
  • DBF Aficionado
  • ******
  • Posts: 7271
  • Karma: 71
    • View Profile
Re: Shader - Colours And Lighting.
« Reply #2 on: May 07, 2012 »
I thought that ambient would give me the lights colour, I was giving it ago but came unstuck. so the multiplying are me experimenting. I'm only after displaying a model with it's texture and lighting.

Haven't used the specular or any of the other bits like materials, at the moment but will be used later. Same goes for the second or more lights. but this is early stages.

Code: [Select]
unsigned int light_id_one=0x4000;
unsigned int light_id_two=0x4001;

float light_ambient  [4]={ 1.00f, 1.00f,   1.00f, 1.00f };
float light_specular [4]={ 0.00f, 0.00f,   0.00f, 0.00f };

float light_pos_one[4]={ -20.00f, 0.00f, -90.00f, 1.00f };
float light_pos_two[4]={  20.00f, 0.00f, -90.00f, 1.00f };

// setup light numero uno.
glLightfv( GL_LIGHT0, GL_AMBIENT , &light_ambient [0] );
glLightfv( GL_LIGHT0, GL_POSITION, &light_pos_one [0] );

// setup light part deux.
glLightfv( GL_LIGHT1, GL_AMBIENT , &light_ambient [0] );
glLightfv( GL_LIGHT1, GL_POSITION, &light_pos_two [0] );

and then in drawing.
Code: [Select]
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
//uncomment for second light.
//glEnable(GL_LIGHT1);
glMatrixMode( GL_MODELVIEW );
glLoadIdentity();
Still Putting The IT Into Gravy
If Only I Knew Then What I Know Now.

Challenge Trophies Won:

Offline Raizor

  • Founder Member
  • Pentium
  • ********
  • Posts: 1154
  • Karma: 175
    • View Profile
Re: Shader - Colours And Lighting.
« Reply #3 on: May 07, 2012 »
I found this and this useful references for OpenGL lighting a while back.
raizor

Challenge Trophies Won:

Offline hellfire

  • Sponsor
  • Pentium
  • *******
  • Posts: 1294
  • Karma: 466
    • View Profile
    • my stuff
Re: Shader - Colours And Lighting.
« Reply #4 on: May 07, 2012 »
Code: [Select]
float light_colour;
light_colour= gl_LightSource[0].ambient;
This doesn't compile because gl_LightSource::ambient is a vec4.
Always check the shader compilation status and error text as described here.
Use GLSL Validate to make sure your shader is correct - nvidia often accepts shader code which is syntactically wrong and thus fails on other cards.
« Last Edit: May 07, 2012 by hellfire »
Challenge Trophies Won:

Offline Clyde

  • A Little Fuzzy Wuzzy
  • DBF Aficionado
  • ******
  • Posts: 7271
  • Karma: 71
    • View Profile
Re: Shader - Colours And Lighting.
« Reply #5 on: May 08, 2012 »
Ok, I'm using similar but with shaderinfologiv, but if it's not the same results will try that.
Thanks for the validating link, didn't know about the nvidia able to send wonky code. I guess you get that with experience.

I gather that my lighting is setup wrong. perhaps I should set the light in the shader and not globally in the gl rendering. Not sure, thanks all the same for the links and info.
Still Putting The IT Into Gravy
If Only I Knew Then What I Know Now.

Challenge Trophies Won: