Page 1 of 1

GLSL Shader question...

Posted: Mon Jun 25, 2007 6:35 pm
by dabeav
I found this great GLSL shader code here on the forums. However I can't figure out why it renders the way it does. I have looked at the #10 demo which uses shaders and it renders with the shader on top of the texture, creating a blended image. However this shader renders with seemingly no regard to the underlying texture?? I would love to use this shader if I could figure out how to blend the Cel shading effect onto the underlying texture. Any suggestions? http://irrlicht.sourceforge.net/phpBB2/ ... hp?t=18400

Posted: Tue Jun 26, 2007 2:30 am
by dabeav
Ok, I found a much easier version of this to read in C# over at the irrlicht.net CP forums.

http://irrlichtnetcp.sourceforge.net/ph ... hlight=cel

I have located why the textures do not blend, however I am not sure what to do to make them blend. In the line:

if (bCanDoGLSL_1_1) {
// Shader uses built-in OpenGL GLSL uniforms, hence needs no explicit constants.
// Therefore a callback function is not needed and is NULL.
mtlToonShader = (MaterialType)gpu.AddHighLevelShaderMaterial(
vertToonShader, "main", VertexShaderType._1_1,
fragToonShader, "main", PixelShaderType._1_1,
null, MaterialType.Solid,0);

mtlToonShader is a material type and it is listed as the shader which contains the material type of Solid. If I can change the material type to be a light map with the loaded texture being the diffuse map (1st texture) and the shader being the light map (2nd texture) that will work perfectly, however I am not sure how to do that...??

Or does the problem lie here:

if (node!=null)
{
node.SetMaterialFlag(MaterialFlag.Lighting, false);
node.SetMD2Animation (MD2Animation.Wave );
node.SetMaterialTexture( 0, driver.GetTexture("sydney.bmp") );
node.SetMaterialType(mtlToonShader); // Override material type
}

I see it set the "sydney.bmp" texture, but then it over rides the material type all together. I guess it all boils down to the question, can I use the shader that has been created as a texture that can be blended with the base texture or not??

Posted: Tue Jun 26, 2007 11:15 am
by omaremad
You are mixing up two diffrent types

Blending type 1: This is transperency where the fragments(screenpixels) of a modelmix with already esisting pixels from another model thats drawn previously. this is achieved by changing MaterialType.Solid to a transparent material type.

Other type is changing the models pixels the shader calculates the final colour by mixing the texture and other colours, the shader is not rendered after the texture; its all one step. you need to code this in the shader, either learn shaders or describe want you want an ill help you.

Posted: Wed Jun 27, 2007 12:43 pm
by dabeav
Thank you. Ok here is what I would like to do...

For the most part the shader in the link above does what I want it to but not quite. Basically I like the cartoon effect where the shading of the character comes in bands based on the vertex (or fragments) normal angle to the light source. However I would like one additional component. I want to be able to apply a base texture to the object, then have the "toon" shader multiply the shader value with the texture value and use the product as the actual fragment color. Or in other words, instead of having ONLY the bands of shading, I want the bands of shading to be a shadowing effect OVER the base texture....

If you could use the shader in the link above as a base for the new shader that would be great!

Posted: Sun Jul 01, 2007 1:29 am
by omaremad
sorry about the lateness.

Code: Select all

// OpenGL Vertex Program 1.1
const stringc vertToonShader =
"varying vec3 normal;"
"varying vec2 tcoords;"
"void main()"
"{"
"   normal = gl_NormalMatrix * gl_Normal;"
"   gl_Position = ftransform();"
"tcoords = gl_MultiTexCoord0.xy;"
"}";

// --------------------------------------------------------------------------

// OpenGL Fragment Program 1.1
const stringc fragToonShader =
"varying vec3 normal;"
"varying vec2 tcoords;"
"unifrom sampler2D texture;"
"void main()"
"{"
"   float intensity;"
"   vec4 color;"
"   vec3 n = normalize(normal);"
""
"   intensity = dot(vec3(gl_LightSource[0].position),n);"
""
"   if (intensity > 0.95)"
"      color = vec4(1.0,0.5,0.5,1.0);"
"   else if (intensity > 0.5)"
"      color = vec4(0.6,0.3,0.3,1.0);"
"   else if (intensity > 0.25)"
"      color = vec4(0.4,0.2,0.2,1.0);"
"   else"
"      color = vec4(0.2,0.1,0.1,1.0);"
""
"   gl_FragColor = color * texture2D(texture,tcoords);"
"}";

//

Posted: Mon Jul 02, 2007 11:38 am
by dabeav
Thank you! I think that will work nicely.

Posted: Fri Aug 01, 2008 5:15 pm
by grayman
how? show us screenshots, i want make games like Zelda Wind Waker, im good modeler and use Blender and .b3d format, teach me how to load it please.

Posted: Fri Aug 01, 2008 7:36 pm
by grayman
i used this code to load it, im using irrlicht 1.4.1 on ubuntu, netbeans, it compiles fine but Sydney appears completely black!

Code: Select all


// --------------------------------------------------------------------------

// Irrlicht Toon (Cel) Shader Demo

// sio2 'at' users.sourceforge.net

// --------------------------------------------------------------------------



#include <irrlicht.h>



// --------------------------------------------------------------------------



using namespace irr;

using namespace core;

using namespace scene;

using namespace video;

using namespace io;

using namespace gui;



// --------------------------------------------------------------------------



#pragma comment(lib, "Irrlicht.lib")







// OpenGL Vertex Program 1.1

const stringc vertToonShader =

"varying vec3 normal;"

"varying vec2 tcoords;"

"void main()"

"{"

"   normal = gl_NormalMatrix * gl_Normal;"

"   gl_Position = ftransform();"

"tcoords = gl_MultiTexCoord0.xy;"

"}";



// --------------------------------------------------------------------------



// OpenGL Fragment Program 1.1

const stringc fragToonShader =

"varying vec3 normal;"

"varying vec2 tcoords;"

"uniform sampler2D texture;"

"void main()"

"{"

"   float intensity;"

"   vec4 color;"

"   vec3 n = normalize(normal);"

""

"   intensity = dot(vec3(gl_LightSource[0].position),n);"

""

"   if (intensity > 0.95)"

"      color = vec4(1.0,0.5,0.5,1.0);"

"   else if (intensity > 0.5)"

"      color = vec4(0.6,0.3,0.3,1.0);"

"   else if (intensity > 0.25)"

"      color = vec4(0.4,0.2,0.2,1.0);"

"   else"

"      color = vec4(0.2,0.1,0.1,1.0);"

""

"   gl_FragColor = color * texture2D(texture,tcoords);"

"}";



// --------------------------------------------------------------------------



int main()

{

	// Create device

	video::E_DRIVER_TYPE driverType = video::EDT_OPENGL;

	IrrlichtDevice *device = createDevice(driverType, dimension2di(800, 600), 32, false, false, false, 0);

	if (!device) {

		printf("Error creating Irrlicht device\n");

		return 0;

	}



	// Obtain device internals

	IVideoDriver* driver = device->getVideoDriver();

	ISceneManager* smgr = device->getSceneManager();



	// Set a window caption

	device->setWindowCaption(L"ToonShader - Irrlicht Engine Demo");



	// Create GLSL shaders

	video::IGPUProgrammingServices* gpu = driver->getGPUProgrammingServices();

	s32 mtlToonShader = video::EMT_SOLID; // Fallback material type

	bool bCanDoGLSL_1_1 = false;

	if (gpu && (driverType == video::EDT_OPENGL)) {

		bCanDoGLSL_1_1 = true; // provisionally accept

		if (!driver->queryFeature(video::EVDF_ARB_FRAGMENT_PROGRAM_1)) {

			printf("queryFeature(video::EVDF_ARB_FRAGMENT_PROGRAM_1) failed\n");

			bCanDoGLSL_1_1 = false;

		}

		if (!driver->queryFeature(video::EVDF_ARB_VERTEX_PROGRAM_1)) {

			printf("queryFeature(video::EVDF_ARB_VERTEX_PROGRAM_1) failed\n");

			bCanDoGLSL_1_1 = false;

		}

	}

	if (bCanDoGLSL_1_1) {

		// Shader uses built-in OpenGL GLSL uniforms, hence needs no explicit constants.

		// Therefore a callback function is not needed and is NULL.

		mtlToonShader = gpu->addHighLevelShaderMaterial(

					vertToonShader.c_str(), "main", video::EVST_VS_1_1,

					fragToonShader.c_str(), "main", video::EPST_PS_1_1,

					NULL, video::EMT_SOLID);

	} else {

		// This demo is for OpenGL!

		printf("This demo requires OpenGL with GLSL High-Level shaders\n");

		mtlToonShader = video::EMT_SOLID;

	}



	// Add an animated mesh

	IAnimatedMesh* mesh = smgr->getMesh("./data/sydney.md2");

	IAnimatedMeshSceneNode* node = smgr->addAnimatedMeshSceneNode( mesh );

	if (node)

	{

		node->setMaterialFlag(EMF_LIGHTING, false);

		node->setMD2Animation ( scene::EMAT_STAND );

		node->setMaterialTexture( 0, driver->getTexture("./data/sydney.bmp") );

		node->setMaterialType((video::E_MATERIAL_TYPE)mtlToonShader); // Override material type

	}



	// Add a viewing camera

	smgr->addCameraSceneNode(0, vector3df(0,30,-40), vector3df(0,5,0));



	// Main rendering loop

	while(device->run())

	{

		driver->beginScene(true, true, SColor(255,100,101,140));

		smgr->drawAll();

		driver->endScene();

	}



	device->drop();



	// Done

	return 0;

}



// --------------------------------------------------------------------------

Posted: Sat Aug 02, 2008 1:39 am
by BlindSide
You don't have a light.