strange shader attempt

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
vectorcorpse
Posts: 86
Joined: Thu Feb 14, 2008 7:30 pm
Location: Portugal

strange shader attempt

Post by vectorcorpse »

i am new to shader programing, and this is my 1st attempt in glsl.
i am using an application similar to render monkey to experiment with glsl and now i am trying to port it to irrlicht but i am having some trouble porting the script
so here is the full thing in hope someone can help me or if find this usefull for anything or anyone:

vertex shader:

Code: Select all

uniform float Pointsize;

void main(void)
{
 gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;
vec4 mod = gl_ModelViewMatrix * gl_Vertex;

gl_PointSize = 2 *Pointsize/ -mod.z ;
}
fragment shader

Code: Select all

uniform sampler2D Colormap;
void main(void){
 
vec4 C = texture2D(Colormap,gl_PointCoord);

 gl_FragColor = C * 0.7 ;
}
my atempt to use it on irrlicht

Code: Select all

#include <irrlicht.h>
using namespace irr;
float Pointsize=480;
int main()
{
   IrrlichtDevice* device = createDevice(
      video::EDT_OPENGL,
      core::dimension2d<s32>(640, 480), 32, false, false, false, 0);
   
   video::IVideoDriver* driver = device->getVideoDriver();
   scene::ISceneManager* smgr = device->getSceneManager();

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

   s32 customMaterial = services->addHighLevelShaderMaterialFromFiles(
      "shaders/shader_v.glsl", "main", video::EVST_VS_1_1,
      "shaders/shader_f.glsl", "main", video::EPST_PS_1_1,
      0,
      video::EMT_TRANSPARENT_ADD_COLOR);

   	video::SMaterial Colormap;
	Colormap.setTexture(0, 
driver->getTexture("../../media/portal1.bmp"));
	Colormap.Lighting = false;

	scene::ISceneNode * node = smgr->addSphereSceneNode();
	if (node)
	{
		node->setPosition(core::vector3df(0,10,-10));
		node->getMaterial(0) = Colormap;
		node->setMaterialFlag(video::EMF_LIGHTING, false);
		node->setMaterialType(video::EMT_TRANSPARENT_ADD_COLOR);
	}

   
   scene::ICameraSceneNode* camera = smgr->addCameraSceneNodeFPS(0, 50, 10);
   camera->setPosition(core::vector3df(0,10,-10));
   camera->setTarget(core::vector3df(6,0,0));
   
   while(device->run())
   {
      driver->beginScene(true, true, video::SColor(255,100,101,140));
     
      smgr->drawAll();

      driver->endScene();
   }
   device->drop();

   return 0;
}
original script used to create the irr main.cpp

Code: Select all

shader = gl.Shader(Vertexshader,Fragmentshader);
shader.Uniformi("Colormap",0);
shader.Uniform("Pointsize",World.getCamHeight());

tan = shader.Loc("Tangent");


function render(){
	Colormap.Bind(0);

	gl.Rotate(60 * World.getTime(), 0,1,0);
	gl.Rotate(60 * World.getTime(), 1,0,0);

	shader.Bind();

	Sphere.Vertex.Bind();

	gl.Disable(gl.DEPTH_TEST);
	gl.Enable(gl.BLEND);
	gl.BlendFunc(gl.SRC_ALPHA,gl.ONE);

	Sphere.Draw(gl.POINT_SPRITE);

	gl.Disable(gl.BLEND);
	gl.Enable(gl.DEPTH_TEST);

	Sphere.Vertex.Unbind();

	shader.Unbind();
	
	}

function resizeEvent(w,h){

	shader.Uniform("Pointsize",World.getCamHeight());
	}
i think i have to pass the tangent information from the mesh to the shader but i cant figure out how
BlindSide
Admin
Posts: 2821
Joined: Thu Dec 08, 2005 9:09 am
Location: NZ!

Post by BlindSide »

Look at the Irrlicht shader example to learn about shader constant callbacks, and use one to set the PointSize uniform variable.

Cheers
ShadowMapping for Irrlicht!: Get it here
Need help? Come on the IRC!: #irrlicht on irc://irc.freenode.net
vectorcorpse
Posts: 86
Joined: Thu Feb 14, 2008 7:30 pm
Location: Portugal

Post by vectorcorpse »

thx :D ill have another look at the example, i managed to set the Pointsize using the example but i am having trouble setting the texture. The example has to many variants (for glsl and hlsl) and is getting me a bit confused. i still don't have a clear idea on how the constant callback function works :?
dlangdev
Posts: 1324
Joined: Tue Aug 07, 2007 7:28 pm
Location: Beaverton OR
Contact:

Post by dlangdev »

Off topic:

I wonder why most of the code I see here are mostly written in the vertex shader?

What's up with that?
Image
vectorcorpse
Posts: 86
Joined: Thu Feb 14, 2008 7:30 pm
Location: Portugal

Post by vectorcorpse »

ouct
i must be doing something really stupid, i'm completly lost trying to figure out how to pass glsl in and out irr

anyway and to get myself enbaraced publicly even more here is my failed attempt:

Code: Select all

#include <irrlicht.h>
#include <iostream>
using namespace irr;
#pragma comment(lib, "Irrlicht.lib")
float Pointsize=20;
video::SMaterial Colormap;
IrrlichtDevice* device = 0;

bool UseHighLevelShaders = true;
class MyShaderCallBack : public video::IShaderConstantSetCallBack

{

public:



	virtual void OnSetConstants(video::IMaterialRendererServices* services, s32 userData)

	{
		video::IVideoDriver* driver = services->getVideoDriver();

		core::matrix4 invWorld = driver->getTransform(video::ETS_WORLD);

		invWorld.makeInverse();


		if (UseHighLevelShaders)

			services->setVertexShaderConstant("Pointsize", (float*)(&Pointsize), 1);
			services->setPixelShaderConstant("Colormap", (float*)(&Colormap), 1);
	}

};

int main()
{
   IrrlichtDevice* device = createDevice(
      video::EDT_OPENGL,
      core::dimension2d<s32>(640, 480), 32, false, false, false, 0);
   
   video::IVideoDriver* driver = device->getVideoDriver();
   scene::ISceneManager* smgr = device->getSceneManager();

   video::IGPUProgrammingServices *services = driver->getGPUProgrammingServices();
   s32 customMaterial = 0;
	if (services)   
	{
	 MyShaderCallBack* mc = new MyShaderCallBack();
		if (UseHighLevelShaders)

		{

			// create material from high level shaders (hlsl or glsl)



			customMaterial = services->addHighLevelShaderMaterialFromFiles(

				"shaders/shader_v.glsl", "main", video::EVST_VS_1_1,

				"shaders/shader_f.glsl", "main", video::EPST_PS_1_1,

				mc, video::EMT_SOLID);
		 }
		mc->drop();
	}

	scene::ISceneNode * node = smgr->addSphereSceneNode();
		node->setPosition(core::vector3df(0,10,-10));
		node->setMaterialTexture(0, driver->getTexture("../../media/lightFalloff.png"));

		node->setMaterialFlag(video::EMF_LIGHTING, false);

		node->setMaterialType((video::E_MATERIAL_TYPE)customMaterial);



		//smgr->addTextSceneNode(gui->getBuiltInFont(), 

		//	L"PS & VS & EMT_SOLID", 

		//	video::SColor(255,255,255,255),	node);

   
   scene::ICameraSceneNode* camera = smgr->addCameraSceneNodeFPS(0, 50, 10);
   camera->setPosition(core::vector3df(0,10,-10));
   camera->setTarget(core::vector3df(6,0,0));
   
   while(device->run())
   {
      driver->beginScene(true, true, video::SColor(255,100,101,140));
     
      smgr->drawAll();

      driver->endScene();
   }
   device->drop();

   return 0;
}
i am getting confused and the hlsl calls on the example gets in the way and confuse me even more because i have to get them out of the way all the time :/
mtest
Posts: 10
Joined: Mon Feb 18, 2008 8:41 pm

Post by mtest »

node->setMaterialTexture(0,driver->getTexture
node->setMaterialTexture(1,driver->getTexture

and then the callback should pass irr:s32's to the shader, and interpret them with
vec4 texval1 = texture2D(myTexture0, gl_TexCoord[0])
whereas
gl_TexCoord[0] = gl_MultiTexCoord0;

If you were trying to pass your own SMaterial thingy for real, then I have no knowledge; possibly, simply run more than one shader on the object?

(I say this because I the direct call to a shader is returning a material.)

Try the tutorial at Clockwork coders.

http://www.clockworkcoders.com/oglsl/tutorials.html
Post Reply