• Welcome to Dark Bit Factory & Gravity.
 

Shader - Colours And Lighting.

Started by Clyde, May 06, 2012

Previous topic - Next topic

0 Members and 0 Guests are viewing this topic.

Clyde

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:


// 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:


// 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.
Still Putting The IT Into Gravy
If Only I Knew Then What I Know Now.


Challenge Trophies Won:

Jim

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:

Clyde

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.


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.

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:

Raizor

I found this and this useful references for OpenGL lighting a while back.
raizor

Challenge Trophies Won:

hellfire

#4
Quote from: Clyde on May 06, 2012
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.
Challenge Trophies Won:

Clyde

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: