irrCg v0.8 (Initial) - CgFX (Texture States, MultiPass etc.)

Announce new projects or updates of Irrlicht Engine related tools, games, and applications.
Also check the Wiki
Nadro
Posts: 1648
Joined: Sun Feb 19, 2006 9:08 am
Location: Warsaw, Poland

Post by Nadro »

@userdima
Can You send to me Your example? I will check it (You shouldn't use sementics for matrices in shader file, because You probably not define them?). I will try repair Your example or irrCg, because maybe this is library bug.

@rootroot1
Thanks :) Which version of Irrlicht are You use? irrCg v0.8 require Irrlicht v1.6.x and up, but optionally I can will add compatible mode also for Irrlicht v1.5.x, because ISceneNodeAnimatorList class is part of Irrlicht core.
Library helping with network requests, tasks management, logger etc in desktop and mobile apps: https://github.com/GrupaPracuj/hermes
rootroot1
Posts: 44
Joined: Wed Nov 25, 2009 6:42 pm
Location: Odessa, Ukraine

Post by rootroot1 »

Thanks for reply !!!! You was right!
I am using Irrlicht 1.6, but not from the latest svn trunk,
I've downloaded it and now IrrCg compiles well :D :D

I'm happy, but I have more questions.... Sorry for my interest..

I want to compile your shader with Fx Composer,
but technique D3D9 compiles wish error :cry: :cry: :cry:
whats wrong ??

Could you make a simple example for using cgfx shaders fom Nvidia shaders library ????

Thanks in advance.
Best regards, Andrew.
userdima
Posts: 22
Joined: Wed Jun 04, 2008 8:30 pm

Post by userdima »

Thanks for answer Nadro, your project is excellent!
Here is shader code http://pastebin.com/m422f80f6
And example http://pastebin.com/m40905e35
Sorry for my english, I'm from belarus!
Nadro
Posts: 1648
Joined: Sun Feb 19, 2006 9:08 am
Location: Warsaw, Poland

Post by Nadro »

@userdima
Thanks for it, but I will check it after New Year because today I can't and tomorrow I will go to family and I will back after New Year.

@rootroot1
I will synchronize them with help in userdima issue, so I will show some example after New Year.
Library helping with network requests, tasks management, logger etc in desktop and mobile apps: https://github.com/GrupaPracuj/hermes
tinhtoitrangtay
Posts: 70
Joined: Tue Oct 28, 2008 12:59 pm

Post by tinhtoitrangtay »

Hi Nadro
Thank you so much help me. Now i can run ok. but i have a problem i hope you can help me. I try change code IrrCg merge link to Irrlicht but i can't i hope you can change code merge link IrrCg to Irrlicht. Thanks
Nadro
Posts: 1648
Joined: Sun Feb 19, 2006 9:08 am
Location: Warsaw, Poland

Post by Nadro »

Today I was back to home, so I will solve problem with cgfx and fx composer soon.

@tinhtoitrangtay
Can You give me more information about Your problem?
Library helping with network requests, tasks management, logger etc in desktop and mobile apps: https://github.com/GrupaPracuj/hermes
rootroot1
Posts: 44
Joined: Wed Nov 25, 2009 6:42 pm
Location: Odessa, Ukraine

Post by rootroot1 »

Hi, Nadro !!!!!!!!!!!!!!!!!!!! Happy New Year and Merry Christmas !!!!!!!

I have a few questions.
I want to implement shaders from Cg reference manual in my game..

But I am not good at it.

There is a shader (aniso)
Code:

Code: Select all

float4x4 WorldViewProj;
float3x3 WorldIT;
float3x4 World;
float3 LightVec;
float3 EyePos;

struct appdata { 
float3 Position : POSITION; 
float3 Normal : NORMAL; 
}; 
struct vpconn { 
float4 Hposition : POSITION; 
float4 TexCoord0 : TEXCOORD0; 
}; 
vpconn main(appdata IN)
{ 
vpconn OUT; 
float3 worldNormal = normalize(mul(WorldIT, IN.Normal)); 
//build float4 
float4 tempPos; 
tempPos.xyz = IN.Position.xyz; 
tempPos.w = 1.0; 
//compute world space position 
float3 worldSpacePos = mul(World, tempPos); 
//vector from vertex to eye, normalized 
float3 vertToEye = normalize(EyePos - worldSpacePos); 
//h = normalize(l + e) 
float3 halfAngle = normalize(vertToEye + LightVec); 
OUT.TexCoord0.x = max(dot(LightVec,worldNormal),0.0); 
OUT.TexCoord0.y = max(dot(halfAngle,worldNormal),0.0); 
// transform into homogeneous-clip space 
OUT.Hposition = mul(WorldViewProj, tempPos); 
return OUT; 
} 

technique D3D9
{
    pass P0
    {
        VertexProgram = compile vs_3_0 main();
    }
}


.....

Code: Select all

I set parameters, but nothing works 
class ICGE_CGFX_CB : public ICgShaderConstantSetCallBack
{
public:
    ICGE_CGFX_CB() 
    {
    }

virtual void OnSetConstants(ICgServices* Services,const CGeffect& Effect,const CGpass& Pass,const CGprogram& Vertex,const CGprogram& Fragment,const SMaterial& Material)
    {
  
        // Vertex.

        matrix4 Matrix;
        pWVP = cgGetNamedEffectParameter(Effect, "WorldViewProj");
        Matrix = Services->getDevice()->getVideoDriver()->getTransform(ETS_PROJECTION);
        Matrix *= Services->getDevice()->getVideoDriver()->getTransform(ETS_VIEW);
        Matrix *= Services->getDevice()->getVideoDriver()->getTransform(ETS_WORLD);
        cgSetMatrixParameterfr(pWVP, Matrix.pointer());

		 matrix4 mWorld;
		 World = cgGetNamedEffectParameter(Effect, "World");
       mWorld = Services->getDevice()->getVideoDriver()->getTransform(ETS_WORLD);
       cgSetMatrixParameterfr(World, mWorld.pointer());

		matrix4 mWorldInverseTranspose;
		WorldIT = cgGetNamedEffectParameter(Effect, "WorldIT");
		mWorld.getInverse(mWorldInverseTranspose);
		mWorldInverseTranspose.getTransposed(mWorldInverseTranspose);
		cgSetMatrixParameterfr(WorldIT, mWorldInverseTranspose.pointer());

		if(Services->getDevice()->getSceneManager()->getActiveCamera() != NULL)
		{
			vector3df CameraPosition = Services->getDevice()->getSceneManager()->getActiveCamera()->getAbsolutePosition();
			EyePosition = cgGetNamedParameter(Vertex, "EyePos");
			Services->setParameter3f(EyePosition, CameraPosition.X, CameraPosition.Y, CameraPosition.Z);
		}
		
		LightVec = cgGetNamedParameter(Vertex, "LightVec");
		Services->setParameter3f(LightVec,1.0,0,1.0);

    }

private:
        CGparameter pWVP;
	CGparameter World;
	CGparameter WorldIT;

	CGparameter EyePosition;
	CGparameter LightVec;

}

And another question:
How to set cubemap in Shader Callback ?? (from basic refraction example)

Code:

Code: Select all

float4 main(in float3 refractVec : TEXCOORD0, 
in float3 reflectVec : TEXCOORD1, 
in float3 fresnelTerm : COLOR0, 
uniform samplerCUBE environmentMaps[2], 
uniform float enableRefraction, 
uniform float enableFresnel) : COLOR 
{ 
float3 refractColor = texCUBE(environmentMaps[0], 
refractVec).rgb; 
float3 reflectColor = texCUBE(environmentMaps[1], 
reflectVec).rgb; 
float3 reflectRefract = lerp(refractColor, reflectColor, 
fresnelTerm); 
float3 finalColor = enableRefraction ? 
(enableFresnel ? reflectRefract : refractColor) : 
(enableFresnel ? reflectColor : fresnelTerm); 
return float4(finalColor, 1.0); 
} 
Thanks in advance!!!!!
I think, it would be usefull for ALL
rootroot1
Posts: 44
Joined: Wed Nov 25, 2009 6:42 pm
Location: Odessa, Ukraine

Post by rootroot1 »

Another try ... :cry: :cry: :cry:

Running this code

Code: Select all

//vertex params
float4x4 WorldViewProj;
float4 LightVector; //in object space
float4 EyePosition; //in object space

//fragment params
sampler2D DiffuseMap;
sampler2D NormalMap;
sampler2D IlluminationMap;
float	  Ambient;

struct a2v {
	float4 Position : POSITION; //in object space
	float3 Normal : NORMAL; //in object space
	float2 TexCoord : TEXCOORD0;
	float3 T : TEXCOORD1; //in object space
	float3 B : TEXCOORD2; //in object space
	float3 N : TEXCOORD3; //in object space
};

struct v2f {
	float4 Position : POSITION; //in projection space
	float4 Normal : COLOR0; //in tangent space
	float4 LightVectorUnsigned : COLOR1; //in tangent space
	float3 TexCoord0 : TEXCOORD0;
	float3 TexCoord1 : TEXCOORD1;
	float4 LightVector : TEXCOORD2; //in tangent space
	float4 HalfAngleVector : TEXCOORD3; //in tangent space
};

v2f main_v(a2v IN)
{
	v2f OUT;
	// pass texture coordinates for
	// fetching the diffuse map
	OUT.TexCoord0.xy = IN.TexCoord.xy;
	// pass texture coordinates for
	// fetching the normal map
	OUT.TexCoord1.xy = IN.TexCoord.xy;
	// compute the 3x3 transform from
	// tangent space to object space
	float3x3 objToTangentSpace;
	objToTangentSpace[0] = IN.T;
	objToTangentSpace[1] = IN.B;
	objToTangentSpace[2] = IN.N;
	// transform normal from
	// object space to tangent space
	OUT.Normal.xyz = 0.5 * mul(objToTangentSpace, IN.Normal) + 0.5;
	// transform light vector from
	// object space to tangent space
	float3 lightVectorInTangentSpace =
	mul(objToTangentSpace, LightVector.xyz);
	OUT.LightVector.xyz = lightVectorInTangentSpace;
	OUT.LightVectorUnsigned.xyz = 0.5 *
	lightVectorInTangentSpace + 0.5;
	// compute view vector
	float3 viewVector =
	normalize(EyePosition.xyz - IN.Position.xyz);
	// compute half angle vector
	float3 halfAngleVector =
	normalize(LightVector.xyz + viewVector);
	// transform half-angle vector from
	// object space to tangent space
	OUT.HalfAngleVector.xyz =
	mul(objToTangentSpace, halfAngleVector);
	// transform position to projection space
	OUT.Position = mul(WorldViewProj, IN.Position);
	return OUT;
}

struct v2f {
	float4 Position : POSITION; //in projection space
	float4 Normal : COLOR0; //in tangent space
	float4 LightVectorUnsigned : COLOR1; //in tangent space
	float3 TexCoord0 : TEXCOORD0;
	float3 TexCoord1 : TEXCOORD1;
	float4 LightVector : TEXCOORD2; //in tangent space
	float4 HalfAngleVector : TEXCOORD3; //in tangent space
};

float4 main_f(v2f IN) : COLOR
{
	// fetch base color
	float4 color = tex2D(DiffuseMap, IN.TexCoord0.xy);
	// fetch bump normal and expand it to [-1,1]
	float4 bumpNormal = 2 *
	(tex2D(NormalMap, IN.TexCoord1.xy) - 0.5);
	// compute the dot product between
	// the bump normal and the light vector,
	// compute the dot product between
	// the bump normal and the half angle vector,
	// fetch the illumination map using
	// the result of the two previous dot products
	// as texture coordinates
	// returns the diffuse color in the
	// color components and the specular color in the
	// alpha component
	float2 illumCoord =
	float2(dot(IN.LightVector.xyz, bumpNormal.xyz),
	dot(IN.HalfAngleVector.xyz, bumpNormal.xyz));
	float4 illumination = tex2D(IlluminationMap, illumCoord);
	// expand iterated normal to [-1,1]
	float4 normal = 2 * (IN.Normal - 0.5);
	// compute self-shadowing term
	float shadow = saturate(4 * dot(normal.xyz,
	IN.LightVectorUnsigned.xyz));
	// compute final color
	return (Ambient * color + shadow)
	* (illumination * color + illumination.wwww);
}

technique D3D9
{
    pass P0
    {
        VertexProgram = compile vs_3_0 main_v();
        FragmentProgram = compile ps_3_0 main_f();
    }
}

Code: Select all

class IBumpCG : public ICgShaderConstantSetCallBack
{
public:
    IBumpCG() 
    {
    }

    virtual void OnSetConstants(ICgServices* Services,const CGeffect& Effect,const CGpass& Pass,const CGprogram& Vertex,const CGprogram& Fragment,const SMaterial& Material)
    {
    
        // Vertex.
        matrix4 Matrix;
        pWVP = cgGetNamedEffectParameter(Effect, "WorldViewProj");
        Matrix = Services->getDevice()->getVideoDriver()->getTransform(ETS_PROJECTION);
        Matrix *= Services->getDevice()->getVideoDriver()->getTransform(ETS_VIEW);
        Matrix *= Services->getDevice()->getVideoDriver()->getTransform(ETS_WORLD);
        cgSetMatrixParameterfr(pWVP, Matrix.pointer());

		/*matrix4 mWorld;
		World = cgGetNamedEffectParameter(Effect, "World");
        mWorld = Services->getDevice()->getVideoDriver()->getTransform(ETS_WORLD);
        cgSetMatrixParameterfr(World, mWorld.pointer());

		matrix4 mWorldInverseTranspose;
		WorldIT = cgGetNamedEffectParameter(Effect, "WorldIT");
		mWorld.getInverse(mWorldInverseTranspose);
		mWorldInverseTranspose.getTransposed(mWorldInverseTranspose);
		cgSetMatrixParameterfr(WorldIT, mWorldInverseTranspose.pointer());*/

		if(Services->getDevice()->getSceneManager()->getActiveCamera() != NULL)
		{
			vector3df CameraPosition = Services->getDevice()->getSceneManager()->getActiveCamera()->getAbsolutePosition();
			EyePosition = cgGetNamedParameter(Vertex, "EyePosition");
			Services->setParameter4f(EyePosition, CameraPosition.X, CameraPosition.Y, CameraPosition.Z,0);
		}
		
		LightVec = cgGetNamedParameter(Vertex, "LightVector");
		Services->setParameter4f(LightVec,1.0,0,1.0,0);

		// Fragment.

        DiffuseMap = cgGetNamedEffectParameter(Effect, "DiffuseMap");
        Services->setTexture( DiffuseMap,Material.TextureLayer[0],0 ); // We can't use optimized EnableTexture function instead of setTexture in OpenGL mode, because next cgSetSamplerState need bind, active texture.
        cgSetSamplerState(DiffuseMap); // Important for apply sampler states from CgFX.

        NormalMap = cgGetNamedEffectParameter(Effect, "NormalMap");
        Services->setTexture( NormalMap,Material.TextureLayer[1],1 );
        cgSetSamplerState(NormalMap);

		IlluminationMap = cgGetNamedEffectParameter(Effect, "IlluminationMap");
        Services->setTexture( IlluminationMap, Material.TextureLayer[2], 2);
        cgSetSamplerState(IlluminationMap);
    
		Ambient = cgGetNamedEffectParameter(Effect, "Ambient");
		Services->setParameter1f(Ambient, 10.0);
	}

private:
        CGparameter pWVP;
	CGparameter World;
	CGparameter WorldIT;

	CGparameter EyePosition;
	CGparameter LightVec;

        CGparameter	DiffuseMap;
        CGparameter	NormalMap;
	CGparameter	IlluminationMap;
	CGparameter	Ambient;
};
I got this
Image
Virion
Competition winner
Posts: 2148
Joined: Mon Dec 18, 2006 5:04 am

Post by Virion »

nice rainbow :lol:
neoncore
Posts: 14
Joined: Wed Jun 10, 2009 9:09 am

i am getting strange linker error

Post by neoncore »

i am getting strange linker error

i am using latest IDE And Compiler [Codeblocks + Mingw]
,Irrlicht 1.6 [From the main page not SVN]
and IrrCg Version 0.7.1 (latest stable)

Code: Select all

[100.0%] Linking executable: bin\2024.exe
obj\Engine\Graphics\Shaders\Cg\IrrCg.o:IrrCg.cpp:(.text+0x0): multiple definition of `irr::video::COpenGLTexture::getOpenGLTextureName() const'
obj\Engine\Graphics\COpenGLTexture.o:COpenGLTexture.cpp:(.text+0xa50): first defined here
obj\Engine\Graphics\Shaders\Cg\IrrCg.o:IrrCg.cpp:(.text+0x10): multiple definition of `irr::video::COpenGLDriver::setActiveTexture(unsigned int, irr::video::ITexture const*)'
obj\Engine\Graphics\COpenGLDriver.o:COpenGLDriver.cpp:(.text+0x1000): first defined here
obj\Engine\Graphics\Shaders\Cg\IrrCg.o:IrrCg.cpp:(.text+0x120): multiple definition of `irr::video::CD3D9Texture::getDX9Texture() const'
obj\Engine\Graphics\CD3D9Texture.o:CD3D9Texture.cpp:(.text+0xeb0): first defined here
collect2: ld returned 1 exit status
Process terminated with status 1 (0 minutes, 7 seconds)
0 errors, 0 warnings
Build log saved as: 
file://E:\Scartaris Engine\Client\Scartaris_build_log.html
 
mannigo
Posts: 2
Joined: Mon Jan 11, 2010 9:06 pm

I can not find the Cg/CG.h

Post by mannigo »

Hi, already downloaded version 0.71, and VC2008 I try to compile the examples, but the compiler tells me I can not find Cg/Cg.h. I looked into the folder of the library include IrrCG and is not. What can I do? (sorry for my english)
rootroot1
Posts: 44
Joined: Wed Nov 25, 2009 6:42 pm
Location: Odessa, Ukraine

Post by rootroot1 »

This is really noob question!!!!!!!!!!!!!!!!!!!!
Go to http://developer.nvidia.com/object/cg_download.html
Download Cg toolkit
Then setup paths(include and libs) in yours VC IDE
Nadro
Posts: 1648
Joined: Sun Feb 19, 2006 9:08 am
Location: Warsaw, Poland

Post by Nadro »

Hi,

@userdima / @rootroot1
Please be patient, I remember about Your issues, but now I have many other work, so currently I don't have much time for prepare better CgFX example. But I will do it as quick as it will be possible.

@mannigo
As rootroot1 said, You have to download Cg Toolkit.

@neoncore
For MingW You have to comments all lines in IrrCg.cpp (for 0.7.1 only) from 60 to 155.
Library helping with network requests, tasks management, logger etc in desktop and mobile apps: https://github.com/GrupaPracuj/hermes
mannigo
Posts: 2
Joined: Mon Jan 11, 2010 9:06 pm

Post by mannigo »

rootroot1 wrote:This is really noob question!!!!!!!!!!!!!!!!!!!!
Go to http://developer.nvidia.com/object/cg_download.html
Download Cg toolkit
Then setup paths(include and libs) in yours VC IDE
oh, sorry..... im new in all this, especially in shaders :oops: :lol:
Edwart
Posts: 46
Joined: Fri Dec 30, 2005 12:29 pm

Post by Edwart »

hi,

i wanted to recompile the first example with irrcg 0.7.1.
i downloaded the cg sdk and changed the pathes to it.
i changed the pathes to the irrlicht lib (1.7 or 1.4.2, - result is the same)

this is the error code - can you help me please?

Code: Select all

obj\main.o:main.cpp:(.text+0x384)||undefined reference to `IrrCg::ICgProgrammingServices::ICgProgrammingServices(irr::IrrlichtDevice*, bool, bool, CGenum)'|
obj\main.o:main.cpp:(.text+0x48d)||undefined reference to `IrrCg::ICgProgrammingServices::addCgShaderMaterialFromFiles(CGenum, char const*, char const*, char const*, char const*, char const*, char const*, char const*, char const*, IrrCg::ICgShaderConstantSetCallBack*, irr::video::E_MATERIAL_TYPE, char const**)'|
obj\main.o:main.cpp:(.text+0x549)||undefined reference to `IrrCg::ICgProgrammingServices::addCgShaderMaterialFromFiles(CGenum, char const*, char const*, char const*, char const*, char const*, char const*, char const*, char const*, IrrCg::ICgShaderConstantSetCallBack*, irr::video::E_MATERIAL_TYPE, char const**)'|
obj\main.o:main.cpp:(.text+0xecd)||undefined reference to `IrrCg::ICgProgrammingServices::~ICgProgrammingServices()'|
||=== Build finished: 4 errors, 0 warnings ===|
Post Reply