GLSL

You are an experienced programmer and have a problem with the engine, shaders, or advanced effects? Here you'll get answers.
No questions about C++ programming or topics which are answered in the tutorials!
Post Reply
Halan
Posts: 447
Joined: Tue Oct 04, 2005 8:17 pm
Location: Germany, Freak City
Contact:

GLSL

Post by Halan »

Hey,

I just made my first steps into GLSL. I wrote some code to calculate lighting myself just for fun.

Sadly it only works properly with the right camera angle (90 degrees), but lighting should be camera independet.

The people ##OpenGL told me the shader should be fine but it depens on my (irrlichts) implementation of OpenGL.

Should I use irrlichts getter-functions to get those matrices and set them via setShaderConstant or can I use OpenGls global variables directly in the shader?

Whatever here is the code:

Code: Select all

// Values for the Sunlight
uniform vec3 mLightDir;

uniform vec4 mLightColor;



void main()

{
	vec3 normal = gl_NormalMatrix * gl_Normal;
	vec4 diffuse = gl_FrontMaterial.diffuse * mLightColor;
	vec3 lightDir = normalize(mLightDir);
	
	// Get the dot product of normal and light
	float NdotL = max(dot(normal, lightDir), 0.0);

	// Calculate
	vec4 globalAmbient = gl_LightModel.ambient * gl_FrontMaterial.ambient;
	gl_FrontColor = NdotL * diffuse + globalAmbient;


	gl_Position = ftransform();
}
And the frag(but don't think youll need this):

Code: Select all

uniform sampler2D mTerrainData;

void main()
{
	gl_FragColor = gl_Color;
}
sudi
Posts: 1686
Joined: Fri Aug 26, 2005 8:38 pm

Post by sudi »

try this:

Code: Select all

vec3 lightDir = gl_ModelViewMatrixInverse * vec4(mLightDir);
Bc u have to transform the lightvektor into the same koordinate system as the Normal. I don't remeber if vec4(mLightDir) works. Maybe u have to use vec4(mLightDir, 1.0) or vec4(mLightDir, 0.0) don't remember one of the solution i just posted is totaly stupid bc it doesn't make sense when thinking about the way the matrix and vector is multiplied...find out which one... :D

EDIT: Btw when u calc the lightning in the VertexShader u will not have Perpixellightning.
We're programmers. Programmers are, in their hearts, architects, and the first thing they want to do when they get to a site is to bulldoze the place flat and build something grand. We're not excited by renovation:tinkering,improving,planting flower beds.
Halan
Posts: 447
Joined: Tue Oct 04, 2005 8:17 pm
Location: Germany, Freak City
Contact:

Post by Halan »

Sudi wrote:try this:

Code: Select all

vec3 lightDir = gl_ModelViewMatrixInverse * vec4(mLightDir);
Bc u have to transform the lightvektor into the same koordinate system as the Normal. I don't remeber if vec4(mLightDir) works. Maybe u have to use vec4(mLightDir, 1.0) or vec4(mLightDir, 0.0) don't remember one of the solution i just posted is totaly stupid bc it doesn't make sense when thinking about the way the matrix and vector is multiplied...find out which one... :D

EDIT: Btw when u calc the lightning in the VertexShader u will not have Perpixellightning.
Yeah i did something like that but using uniforms and it worked.

Well it is just a test I'll add per pixel lighting later.
Post Reply