Question about openGL shaders

  • (2 Pages)
  • +
  • 1
  • 2

17 Replies - 2171 Views - Last Post: 10 February 2011 - 05:52 AM Rate Topic: -----

#1 Kain6622  Icon User is offline

  • D.I.C Regular

Reputation: 17
  • View blog
  • Posts: 255
  • Joined: 18-March 10

Question about openGL shaders

Posted 08 February 2011 - 03:58 AM

Hi, I'm currently using glsl in my applications and am trying to make a library for each individual thing such as lighting, shadowing ect... my question is about performance and the best way I should go about this, as it stands with concerns to lighting is the best way I should go about writing the vertice shader. should I have a vertices shader for each type of light i.e Directional, point and spot lighing or should I contain all of these within the one vertex shader file?
Thanks in advances for any advice.
Dave

Is This A Good Question/Topic? 0
  • +

Replies To: Question about openGL shaders

#2 anonymous26  Icon User is offline

  • D.I.C Lover

Reputation: 1
  • View blog
  • Posts: 3,638
  • Joined: 26-November 10

Re: Question about openGL shaders

Posted 08 February 2011 - 06:18 AM

Keep things separate. It makes it easier to add features and debug. Efficiency-wise, I can't really see how you would benefit either way. :)
Was This Post Helpful? 1
  • +
  • -

#3 Kain6622  Icon User is offline

  • D.I.C Regular

Reputation: 17
  • View blog
  • Posts: 255
  • Joined: 18-March 10

Re: Question about openGL shaders

Posted 08 February 2011 - 01:29 PM

Thanks ButchDean, though a quick question... won't that affect the out come of the different lights to have then in seperate shader files? so instead of then all sharing the final colour in case they over lap would having them seperate nean that the last light updates will be the color of the vertex? also is there a limit to how many shader programs that you can run at the same time or is that just dependant on the graphics card you have?
Was This Post Helpful? 0
  • +
  • -

#4 anonymous26  Icon User is offline

  • D.I.C Lover

Reputation: 1
  • View blog
  • Posts: 3,638
  • Joined: 26-November 10

Re: Question about openGL shaders

Posted 08 February 2011 - 04:36 PM

Is there a reason for wanting to split things up this way?
Was This Post Helpful? 1
  • +
  • -

#5 Kain6622  Icon User is offline

  • D.I.C Regular

Reputation: 17
  • View blog
  • Posts: 255
  • Joined: 18-March 10

Re: Question about openGL shaders

Posted 09 February 2011 - 05:41 AM

Not entirely the reason I ask is more down to which choice is more efficient but gives the same effect, as I'm kind of new to shaders I'm not entirely sure what method is used more often by the industry.
Was This Post Helpful? 0
  • +
  • -

#6 stayscrisp  Icon User is online

  • フカユ
  • member icon

Reputation: 1009
  • View blog
  • Posts: 4,196
  • Joined: 14-February 08

Re: Question about openGL shaders

Posted 09 February 2011 - 05:52 AM

I guess it all depends on what you eventually hope to use your shaders for. When I was using HLSL I had the lighting in one file and split them into separate techniques, these techniques could be selected whenever I wanted to change between them. I don't think GLSL has this does it?
Was This Post Helpful? 1
  • +
  • -

#7 Kain6622  Icon User is offline

  • D.I.C Regular

Reputation: 17
  • View blog
  • Posts: 255
  • Joined: 18-March 10

Re: Question about openGL shaders

Posted 09 February 2011 - 06:29 AM

I can't say for sure at the moment is you can have seperate techniques for the lighting, what I have at present is one vertex shader that holds a struct array for each type of light the user could have and it checks if a certain type of light is present if that is true then to call the update function for that type of light within the shader? though this is still got a few bugs in it, in theory this should work as the lights all share the common properties of modelview/projection matrix and the final color to be sent to the vertices. i'm not sure if I can have more than one main() function within the one shader files if that's how you meant by splitting the techniques?
Was This Post Helpful? 0
  • +
  • -

#8 anonymous26  Icon User is offline

  • D.I.C Lover

Reputation: 1
  • View blog
  • Posts: 3,638
  • Joined: 26-November 10

Re: Question about openGL shaders

Posted 09 February 2011 - 09:58 AM

I wouldn't be too bothered about splitting things up, but I would be bothered about say, having to many light sources and causing a framerate drop for instance. When coding games it is easy to throw everything in there for maximum effect, but remember that it is better to get the best from being economical. :)
Was This Post Helpful? 1
  • +
  • -

#9 Kain6622  Icon User is offline

  • D.I.C Regular

Reputation: 17
  • View blog
  • Posts: 255
  • Joined: 18-March 10

Re: Question about openGL shaders

Posted 09 February 2011 - 12:08 PM

Thanks ButchDean, For the reason you stated in your last post about having to many lights can affect performance I had already limited each light type to 8 each and they lights are only present when they have been pushed onto a stack else the shader will ignore it completely :).

I've nearly got it shader to work but I'm still having some problems with multiple spot lights, for some reason I'm getting the occassional shader artifact when using more than one spot light ( certain areas go strainge/black then the spot light heads towards it ) I don't suppose you might know what could be causing this, I'm lead to believe that it have something to do with the final color calculation but I can't find anything on google that helps:

Here is my shader code for the spot lights:
#version 130

uniform mat4 projection_matrix;
uniform mat4 modelview_matrix;
uniform mat3 normal_matrix;

uniform vec4 material_ambient;
uniform vec4 material_diffuse;
uniform vec4 material_specular;
uniform vec4 material_emissive;
uniform float material_shininess;

uniform struct 
{
	vec4 position;
	vec4 diffuse;
	vec4 specular;
	vec4 ambient;
	
	float constant_attenuation;
	float linear_attenuation;
	float quadratic_attenuation;

	float spotCutOff;
	vec3 spotDirection;
	float spotExponent;
} light[2];

in vec3 a_Vertex;
in vec2 a_TexCoord0;
in vec3 a_Normal;

out vec4 color;
out vec2 texCoord0;

vec4 finalColor;
float attenuation[2];

void main(void) 
{
	vec3 N = normalize(normal_matrix * a_Normal);	
	vec4 pos = modelview_matrix * vec4(a_Vertex, 1.0);
	vec3 E = -(pos.xyz);
	
	for( int i = 0; i < 2; ++i )
	{
		vec3 lightPos = (modelview_matrix * light[i].position).xyz;
		vec3 lightDir = (lightPos - pos.xyz).xyz;

		float NdotL = max(dot(N, lightDir.xyz), 0.0);
		float dist = length(lightDir);

		finalColor = material_ambient * light[i].ambient;

		attenuation[i] = 1.0;
		
		  //If the surface is facing the light source
		if (NdotL > 0.0) 
		{
				//Find the angle between the light direction and spotlight direction
			float spotEffect = dot(normalize(light[i].spotDirection), normalize(-lightDir));

			//If it's greater than the cosine of the spotlight cutoff then it should be illuminated
			if (spotEffect > cos(light[i].spotCutOff)) 
			{	
				vec3 HV = normalize(lightPos + E);
				float NdotHV = max(dot(N, HV), 0.0);
				finalColor += material_specular * light[i].specular * pow(NdotHV, material_shininess);	
			
				spotEffect = pow(spotEffect, light[i].spotExponent);		
				attenuation[i] = spotEffect / (light[i].constant_attenuation + light[i].linear_attenuation * dist + light[i].quadratic_attenuation * dist * dist);

				finalColor += material_diffuse * light[i].diffuse * NdotL;		
			}
		}
	}
	
	color = material_emissive + (finalColor * attenuation[0]) + (finalColor * attenuation[1]);

	texCoord0 = a_TexCoord0;

	gl_Position = projection_matrix * pos;	
}



I've had a mess around with the color = section but still gives me artifacts either way but works fine if i'm only applying one attenuation reather than two?
Was This Post Helpful? 0
  • +
  • -

#10 anonymous26  Icon User is offline

  • D.I.C Lover

Reputation: 1
  • View blog
  • Posts: 3,638
  • Joined: 26-November 10

Re: Question about openGL shaders

Posted 09 February 2011 - 12:19 PM

Sounds to me like your calculations are only taking one light source into consideration. Does everything work fine from one light source being any one of the many you have? Do you have a screenshot? Are you working with a static camera?
Was This Post Helpful? 0
  • +
  • -

#11 Kain6622  Icon User is offline

  • D.I.C Regular

Reputation: 17
  • View blog
  • Posts: 255
  • Joined: 18-March 10

Re: Question about openGL shaders

Posted 09 February 2011 - 12:36 PM

The lighting of Directional and point lighting work fine together it's just the Spot light when I have multiple instances of it that causes some problems, the code from before was just for the spot light on it's own without any other type to make sure it was working before placing it with the other light sources. the spot light works fine is
color = finalColor * attenuation[0];
or
color = finalColor * attenuation[1];
it only happens when I try to get them both on the scene at the same time, it takes both light sources into account but still with artifacts when the second light reaches near the edge of the terrain. attatched is a screenshot.Attached Image
Was This Post Helpful? 0
  • +
  • -

#12 anonymous26  Icon User is offline

  • D.I.C Lover

Reputation: 1
  • View blog
  • Posts: 3,638
  • Joined: 26-November 10

Re: Question about openGL shaders

Posted 09 February 2011 - 12:46 PM

I remember having a problem like this. It turned out that my terrain wasn't well formed. Why is there a problem with only both lights? I couldn't tell you, but I'd certainly check the surface normals of your terrain as I'm pretty sure it's something along those lines. I wouldn't touch your lighting calculation yet. :)
Was This Post Helpful? 0
  • +
  • -

#13 Kain6622  Icon User is offline

  • D.I.C Regular

Reputation: 17
  • View blog
  • Posts: 255
  • Joined: 18-March 10

Re: Question about openGL shaders

Posted 09 February 2011 - 03:19 PM

I'll have a look into that but I doubt that it's the calculations for the normals as the code for that is straight from the book i'm learning from but i'll still check :). From looking into the code it seems that when both lights are on the first light domonates the second as the way the artifacts act are based on the position of light[0] always. it seems that the color produced from both lights is not being blended together at the end as when light[0] is at the far end of the terrain the black artifacts start coming from the other side until it starts to move back to the start position, the only thing i can think of off the top of my head is that the code works when using single lights but not when using two due to some kind of failing when trying to blend the colors... that both light[0] and light[1] are trying to blend the colors but somethings going wrong with one of the calculations to which i then get the black artifacts, I'll keep having a play with the code to see if I can find anything that might be the cause of this happening.

Edit:
My test is comfirmed, I stopped the light[0]'s moving pattern so that it was stationary and light[1] works fine then so I'll need to look into the calculations some more.

This post has been edited by Kain6622: 09 February 2011 - 03:33 PM

Was This Post Helpful? 0
  • +
  • -

#14 anonymous26  Icon User is offline

  • D.I.C Lover

Reputation: 1
  • View blog
  • Posts: 3,638
  • Joined: 26-November 10

Re: Question about openGL shaders

Posted 09 February 2011 - 04:12 PM

Has everything come from the book? If you're right, nice find! :)
Was This Post Helpful? 0
  • +
  • -

#15 Kain6622  Icon User is offline

  • D.I.C Regular

Reputation: 17
  • View blog
  • Posts: 255
  • Joined: 18-March 10

Re: Question about openGL shaders

Posted 09 February 2011 - 04:25 PM

The code i'm working with just now is from one of the books examples which i'm editting to allow multiple spot lights before adding the final working code to my light shader lib, so the base program is from the books, and i'm changing it as required to gain a better understanding of how the glsl works so i can use it with my own gl project... also using an example first is helpful when debugging as unless the very small chance of their code being wrong it cuts the possible reasons that could cause the found problem. :)
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2