Using default shaders as base [SOLVED AGAIN]

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
Auradrummer
Posts: 260
Joined: Thu Apr 17, 2008 1:38 pm
Location: Brasopolis - Brazil

Using default shaders as base [SOLVED AGAIN]

Post by Auradrummer »

Hello guys,

I'm with a difficult here:
I want a shader that can project one image on a textured object.
I want to add this projection to a EMT_SOLID or another Irrlicht default material type.

But, I don't want to "reinvent the wheel" and I would be happy if I can use the Irrlicht E_MATERIAL_TYPE as basis and only add new features to them. I have two questions:

- Can I "mix" my shader with one default shader?
- Where is the default Irrlicht shaders? So I can copy them and add my lines.

Thanks for the help!
Last edited by Auradrummer on Thu Aug 25, 2011 7:32 pm, edited 3 times in total.
Professional Software Developer and Amateur Game Designer ;-)
Radikalizm
Posts: 1215
Joined: Tue Jan 09, 2007 7:03 pm
Location: Leuven, Belgium

Post by Radikalizm »

Irrlicht uses a fixed function pipeline, so there aren't any "default shaders" like you would find in a programmable pipeline

The behaviour of default materials isn't exactly hard to recreate in a shader by yourself though
Auradrummer
Posts: 260
Joined: Thu Apr 17, 2008 1:38 pm
Location: Brasopolis - Brazil

Post by Auradrummer »

Hummm... bad news...

The problem is that I'm newbie in shaders. I can do this, sure, but I don't think I can do this in the best way. But, if this is the situation, let's work and make it work!

Thanks guy!
Professional Software Developer and Amateur Game Designer ;-)
Auradrummer
Posts: 260
Joined: Thu Apr 17, 2008 1:38 pm
Location: Brasopolis - Brazil

Post by Auradrummer »

Hello guys.

I almost reproduced the EMT_SOLID. Is not exactly, but is too nearby. You can use it to develop your shader over, I think would be useful for someone, as it was for me.

I used the references from Lighthouse3D http://www.lighthouse3d.com/tutorials/g ... /lighting/

And the OpenGL Shading Language Course from Typhoon Labs (the PDF's are inside the OpenGL official site).

Here is the shaders:

Code: Select all

//VERTEX SHADER
uniform mat4 mTransWorld;

void main()
{
	float dotP, NdotHV, dist, att;
	vec3 normal, lightDir;
	vec4 diffuse, ambient, globalAmbient, specular;
	
	dist = length(gl_LightSource[0].position.xyz - gl_Vertex.xyz);
	att = 1.0 / (gl_LightSource[0].constantAttenuation + 
				 gl_LightSource[0].linearAttenuation * dist+
				 gl_LightSource[0].quadraticAttenuation * dist * dist);
	
	normal = gl_Normal;
	lightDir = vec4(gl_LightSource[0].position * mTransWorld);
	lightDir = normalize(lightDir);
	dotP = max(dot(normal,lightDir),0.0);
	diffuse = gl_FrontMaterial.diffuse * gl_LightSource[0].diffuse * att;
	
	ambient = gl_FrontMaterial.ambient * gl_LightSource[0].ambient;
	globalAmbient = gl_LightModel.ambient * gl_FrontMaterial.ambient;
	
	if (dotP > 0.0)
	{
		NdotHV = max(dot(normal,gl_LightSource[0].halfVector.xyz),0.0);
		specular = gl_FrontMaterial.specular * gl_LightSource[0].specular * pow(NdotHV, gl_FrontMaterial.shininess);
	};
	
	gl_FrontColor =  dotP * diffuse + globalAmbient + ambient + specular;
	gl_Position = ftransform();
} 

Code: Select all

//FRAGMENT SHADER
sampler2D tex0 : register(s0);

void main()
{
	vec4 tex = texture2D(tex0,gl_TexCoord[0]);
	vec4 Color = tex * gl_Color;
	Color.a = 1.0;
		
	gl_FragColor = Color;
} 
That's it.
Professional Software Developer and Amateur Game Designer ;-)
nespa
Posts: 167
Joined: Wed Feb 24, 2010 12:02 pm

Post by nespa »

Indeed, the best way to learn is in the GLSL shader tutorials
ent1ty
Competition winner
Posts: 1106
Joined: Sun Nov 08, 2009 11:09 am

Post by ent1ty »

Auradrummer, that's slightly wrong. You should pass the variables you need to the fragment shader and do the calculations there. The variables will get automatically interpolated and you will get per pixel lighting, whereas now you get only per vertex. The difference in the looks is vast, and the cost compared to your current code minimal(could e even faster under some circumstances).

So, technically, there's nothing wrong with your code, but why have per vertex lighting when you can get per pixel? :wink:
irrRenderer 1.0
Height2Normal v. 2.1 - convert height maps to normal maps

Step back! I have a void pointer, and I'm not afraid to use it!
Auradrummer
Posts: 260
Joined: Thu Apr 17, 2008 1:38 pm
Location: Brasopolis - Brazil

Re: Using default shaders as base (unnoticed problem)

Post by Auradrummer »

I'm having a problem, that I passed over it last time.

I made two balls, one EMT_SOLID and another with my "SOLID" shader.

When the light orbits around the two balls, everything is ok, both of them are almost identical.
But, when the camera orbits, the lighting of my shader spins two and the "EMT_SOLID" keeps lighting from the same place.

I know is some transformation, but I cannot figure out what is the transformation I have to do.

Where I can learn about this?

Thanks!
Professional Software Developer and Amateur Game Designer ;-)
Auradrummer
Posts: 260
Joined: Thu Apr 17, 2008 1:38 pm
Location: Brasopolis - Brazil

Re: Using default shaders as base (unnoticed problem)

Post by Auradrummer »

I'm discovering new things:
- The gl_lightsource[0].position is transformed byt the view matrix.

As I'm noticing, this value has been transformed two times, and should be only one.
I'll keep looking for, but I'm making some progress.
Professional Software Developer and Amateur Game Designer ;-)
Auradrummer
Posts: 260
Joined: Thu Apr 17, 2008 1:38 pm
Location: Brasopolis - Brazil

Re: Using default shaders as base [SOLVED AGAIN]

Post by Auradrummer »

Searching around I found in the Clockwork Coders tutorial something nice:
http://www.opengl.org/sdk/docs/tutorial ... ghting.php

This is a simple "EMT_SOLID" material made by fragment shader. Worke very well in my tests.

This appears to solve my problems and implements what ent1ty said. Really, seems good.
Professional Software Developer and Amateur Game Designer ;-)
Post Reply