Page 17 of 22

Posted: Sun Jul 30, 2006 7:49 pm
by Emil_halim
hi all

here is an other effect, it is Glow effects.

Screenshots , the left faerie has glow effect ,the right one has not.
Image

Code: Select all

/****************************************************
             Glow effect with CG

 based on (Real-Time "TRON 2.0" Glow in Half-Life)
 http://collective.valve-erc.com/index.php?go=tron1

            adapted By Emil Halim
****************************************************/

#include <MagicIncludes.h>

// global declration of Irrlicht interfaces
IrrlichtDevice* device;
IVideoDriver*   driver;
ISceneManager*  smgr;


bool ProgramRun;

int main()
{
    device = createDevice(EDT_OPENGL, dimension2d<s32>(640, 480), 32);
    bool rslt = InitMagic(device);
    if(rslt == false)
          printf("Magic Library will only work with OpenGL driver");

	driver = device->getVideoDriver();
	smgr = device->getSceneManager();

	TCGShader* cgProg = new TCGShader;

    cgProg->AddToVertexProgram("void main( float4 position          : POSITION,             ");
    cgProg->AddToVertexProgram("           float4 color             : COLOR,                ");
    cgProg->AddToVertexProgram("           float2 texCoord          : TEXCOORD,             ");
    cgProg->AddToVertexProgram("           out float4 oPosition     : POSITION,             ");
    cgProg->AddToVertexProgram("           out float4 oColor        : COLOR,                ");
    cgProg->AddToVertexProgram("           out float2 oTexCoord0    : TEXCOORD0,            ");
    cgProg->AddToVertexProgram("           out float2 oTexCoord1    : TEXCOORD1,            ");
    cgProg->AddToVertexProgram("           out float2 oTexCoord2    : TEXCOORD2,            ");
    cgProg->AddToVertexProgram("           out float2 oTexCoord3    : TEXCOORD3,            ");
    cgProg->AddToVertexProgram("           out float2 oTexCoord4    : TEXCOORD4,            ");
    cgProg->AddToVertexProgram("           uniform float XOffset,                           ");
    cgProg->AddToVertexProgram("           uniform float YOffset,                           ");
    cgProg->AddToVertexProgram("           uniform float4x4 ModelViewProj)                  ");
    cgProg->AddToVertexProgram("{                                                           ");
    cgProg->AddToVertexProgram("       oPosition = mul(ModelViewProj,position);             ");
    cgProg->AddToVertexProgram("       oColor    = color;                                   ");
    cgProg->AddToVertexProgram("       oTexCoord0 = texCoord + float2(XOffset,YOffset);     ");
    cgProg->AddToVertexProgram("       oTexCoord1 = texCoord + float2(XOffset*2,YOffset*2); ");
    cgProg->AddToVertexProgram("       oTexCoord2 = texCoord - float2(XOffset,YOffset);     ");
    cgProg->AddToVertexProgram("       oTexCoord3 = texCoord - float2(XOffset*2,YOffset*2); ");
    cgProg->AddToVertexProgram("       oTexCoord4 = texCoord;                               ");
    cgProg->AddToVertexProgram("}                                                           ");

    cgProg->setVertexProfile(CG_PROFILE_ARBVP1);
    cgProg->CompileVertexProgram("main");
    printf("%s \n", cgProg->GetCompiledVertexProgram());

    cgProg->AddToPixelProgram("void main(  float2 TexCoord0  : TEXCOORD0,            ");
    cgProg->AddToPixelProgram("            float2 TexCoord1  : TEXCOORD1,            ");
    cgProg->AddToPixelProgram("            float2 TexCoord2  : TEXCOORD2,            ");
    cgProg->AddToPixelProgram("            float2 TexCoord3  : TEXCOORD3,            ");
    cgProg->AddToPixelProgram("            float2 TexCoord4  : TEXCOORD4,            ");
    cgProg->AddToPixelProgram("            out float4 color     : COLOR,             ");
    cgProg->AddToPixelProgram("            uniform sampler2D txtr0 : texunit0,       ");
    cgProg->AddToPixelProgram("            uniform sampler2D txtr1 : texunit1)       ");
    cgProg->AddToPixelProgram("{                                                     ");
    cgProg->AddToPixelProgram("       float4 col1 = tex2D(txtr0, TexCoord0) / 2.0f;  ");
    cgProg->AddToPixelProgram("       float4 col2 = tex2D(txtr0, TexCoord1) / 2.0f;  ");
    cgProg->AddToPixelProgram("       float4 col3 = tex2D(txtr0, TexCoord2) / 2.0f;  ");
    cgProg->AddToPixelProgram("       float4 col4 = tex2D(txtr0, TexCoord3) / 2.0f;  ");
    cgProg->AddToPixelProgram("       float4 avg1 = (col1 + col2) / 2.0f;            ");
    cgProg->AddToPixelProgram("       float4 avg2 = (col3 + col4) / 2.0f;            ");
    cgProg->AddToPixelProgram("       float4 blur = avg1 + avg2;                     ");
    cgProg->AddToPixelProgram("       float4 newCol  = tex2D(txtr0, TexCoord4);      ");
    cgProg->AddToPixelProgram("       float4 dark    = newCol * newCol ;             ");
    cgProg->AddToPixelProgram("       color  = dark+blur;                            ");
    cgProg->AddToPixelProgram("}                                                     ");

    cgProg->setPixelProfile(CG_PROFILE_ARBFP1);
    cgProg->CompilePixelProgram("main");
    printf("%s \n", cgProg->GetCompiledPixelProgram());

    CGparameter XOffsetParam = cgProg->GetVertexVariable("XOffset");
    CGparameter YOffsetParam = cgProg->GetVertexVariable("YOffset");
    CGparameter ModelViewProjParam = cgProg->GetVertexVariable("ModelViewProj");

    TCGAnimatedMesh* node = new TCGAnimatedMesh("../../media/faerie.md2",smgr->getRootSceneNode(), smgr, -1);
	node->setMaterialFlag(EMF_LIGHTING, false);
	node->setAnimationSpeed(0.05 );
    node->setShaderProgram(cgProg);
    node->setRotation(core::vector3df(0,90,0));
    node->setPosition(core::vector3df(-15,10,0));
    node->setMaterialTexture( 0, driver->getTexture("../../media/Faerie5.bmp") );

    TCGAnimatedMesh* node1 = new TCGAnimatedMesh("../../media/faerie.md2",smgr->getRootSceneNode(), smgr, -1);
	node1->setMaterialFlag(EMF_LIGHTING, false);
	node1->setAnimationSpeed(0.05 );
    node1->setRotation(core::vector3df(0,90,0));
    node1->setPosition(core::vector3df(15,10,0));
    node1->setMaterialTexture( 0, driver->getTexture("../../media/Faerie5.bmp") );

	scene::ICameraSceneNode* cam = smgr->addCameraSceneNodeFPS(0, 10.0f, 10.0f);
    cam->setPosition(core::vector3df(0,30,-70));
    cam->setTarget(core::vector3df(0,0,0));

    HideMouse();

    ProgramRun = true;
    int lastFPS = -1;

	while(device->run()&& ProgramRun)
	{

		driver->beginScene(true, true, SColor(0,200,200,200));

                 if(KeyDown(KEY_ESCAPE))ProgramRun=false;

                  cgGLSetStateMatrixParameter(ModelViewProjParam,CG_GL_MODELVIEW_PROJECTION_MATRIX,
                                            CG_GL_MATRIX_IDENTITY);

                  cgGLSetParameter1f(XOffsetParam,0.002f);
                  cgGLSetParameter1f(YOffsetParam,0.002f);

                 smgr->drawAll();

		driver->endScene();
		int fps = driver->getFPS();
        if (lastFPS != fps)
         {
            wchar_t tmp[1024];
            swprintf(tmp, 1024, L"Glow effect with CG (%s)(fps:%d)",driver->getName(), fps);
            device->setWindowCaption(tmp);
            lastFPS = fps;
         }
	}

	device->drop();

	return 0;
}
enjoy it.

Posted: Sun Jul 30, 2006 10:06 pm
by omaremad
Hmm nice, i never thought of doing the glow per object, i ussually do the same shader as you did a posprocessor. each one of course has its advanatges :D

Posted: Mon Jul 31, 2006 4:56 am
by Emil_halim
the best advantage ,in my own opinion , is that you do not need to render to a texture for doing the effect as metion in the tutorial ,instead one CG program for doing all the effect.

BTW, you could easlly adjust the code for apply the glow effect for entire drawing.

Posted: Mon Jul 31, 2006 1:01 pm
by omaremad
yeah thats your advantgae my advantage is that:

-each node can have its own shader other than glow
- the glow can exceed the polygons of the node and illuminate other things
-the sahder is used once per scene rather than per object

Posted: Tue Aug 01, 2006 2:04 pm
by dawasw
Emil I liked your glow. I think its a good way to select which nodes will have glow because I can set up a perfomance option for example:

- High Shader Effects = All nodes with glow

- Low Shader Effects = just World glow without NPC characters

Actually Im gonna try how this glow work right now. =]

Posted: Tue Aug 01, 2006 2:37 pm
by dawasw
#include <MagicIncludes.h>

you use such main header file for project and in the latest library the is no such header. I'm just asking if I just missed something, because whatever I do (even include Magic3D.h) it all screwes up and I get 300 errors from dev-cpp compiler.

I know I'm such a n00b but hmm whats wrong this time lol ?

Posted: Tue Aug 01, 2006 3:01 pm
by Emil_halim
Hi dawasw

actually MagicLibrary is not released yet, i was thinking to finshing it then post it , my be i am wrong so that i will post it today and i will continue
it after that.

Posted: Tue Aug 01, 2006 4:25 pm
by dawasw
Well actually you don't have to hurry ;)

If it won't be a problem it would be great to test it on more nodes and stuff (Im curious about a 'bigger scale' effect I would say - I mean perfomance and stuff on my card etc.)

I might use it in my little project too of course. (I tried Omaremad's blur&glow but its good rather for 'receive damage effect' and I just wanted a simple glow :P)

Do as you like I don't wana cause pain to other people :shock:

Posted: Tue Aug 01, 2006 7:25 pm
by Emil_halim
dawasw wrote: Do as you like I don't wana cause pain to other people :shock:
hay man , of course you donot cause any pain for me of for any one here,
you are welcome any time.

here is the new library MagicLibrary
http://bcxdx.spoilerspace.com/BCXGL/MagicLibrary.zip

here is a folderTree showing you what you will find in MagicLibrary
Image

ToDo List
=======
make CGOctree, CGTerrain SceneNodes and more........

Posted: Tue Aug 01, 2006 8:49 pm
by dawasw
Cool Ill test it now!

Posted: Tue Aug 01, 2006 9:22 pm
by Emil_halim
for OmerEmad:

as i said to you appling the Glow for entire Scene is so easy and very fast
by my trick , which is that , you render all your stuff normally but to a texture , then apply my Glow Shader for reder Quad texture. that is all.

here is a screen shots of my idea.

without Glow
Image

with Glow
Image

if you need the code please let me know.

Enjoy it.

Posted: Thu Aug 03, 2006 6:08 pm
by Emil_halim
Hi All

here is an other effect with CG shader, Night Vision

Screenshot
Image

Code: Select all

/************************************
         NightVision Demo

     converted from NVIDIA SDK
           By Emil Halim
*************************************/

#include <MagicIncludes.h>

// global declration of Irrlicht interfaces
IrrlichtDevice* device;
IVideoDriver*   driver;
ISceneManager*  smgr;

bool ProgramRun;

float _nBrightness;

class MyEventReceiver : public IEventReceiver
{
public:
	virtual bool OnEvent(SEvent event)
	{
		if (event.EventType == EET_GUI_EVENT)
		{
			s32 id = event.GUIEvent.Caller->getID();
			IGUIEnvironment* env = device->getGUIEnvironment();

			switch(event.GUIEvent.EventType)
			{

			case EGET_SCROLL_BAR_CHANGED:
				if (id == 104)
				{
					s32 pos = ((IGUIScrollBar*)event.GUIEvent.Caller)->getPos();

                    _nBrightness = pos / 255.0;

				}
				break;

			}
		}

		return false;
	}
};


int main()
{
    device = createDevice(EDT_OPENGL, dimension2d<s32>(640, 480), 32);
    bool rslt = InitMagic(device);
    if(rslt == false)
          printf("Magic Library will only work with OpenGL driver");

	driver = device->getVideoDriver();
	smgr = device->getSceneManager();
	IGUIEnvironment* env = device->getGUIEnvironment();

	MyEventReceiver receiver;
	device->setEventReceiver(&receiver);

	IGUIScrollBar* scrollbar = env->addScrollBar(true, rect<s32>(150, 45, 350, 60), 0, 104);
	scrollbar->setMax(255);
	scrollbar->setPos(5);

	TCGShader* cgProg = new TCGShader;
    cgProg->AddToPixelProgram(" void main( float2 TexCoord0        : TEXCOORD0,    ");
    cgProg->AddToPixelProgram("            out float4 vOutputColor : COLOR,        ");
    cgProg->AddToPixelProgram("            uniform sampler2D txtr0 : texunit0,     ");
    cgProg->AddToPixelProgram("            uniform float4x4  mColorTransform,      ");
    cgProg->AddToPixelProgram("            uniform float3    vBlue,                ");
    cgProg->AddToPixelProgram("            uniform float     nIntensity)           ");
    cgProg->AddToPixelProgram(" {                                                  ");
    cgProg->AddToPixelProgram("    float4 vColor;                                  ");
    cgProg->AddToPixelProgram("    float  nLuminance;                              ");
    cgProg->AddToPixelProgram("    vColor = tex2D(txtr0, TexCoord0);               ");
    cgProg->AddToPixelProgram("    vColor = mul(mColorTransform, vColor);          ");
    cgProg->AddToPixelProgram("    vColor = max(vColor, float4(0.01, 0, 0, 1));    ");
    cgProg->AddToPixelProgram("    nLuminance = vColor.y * (1.33f * (1 +           ");
    cgProg->AddToPixelProgram("         (vColor.y + vColor.z)/vColor.x) - 1.68f);  ");
    cgProg->AddToPixelProgram("    vOutputColor.xyz = vBlue*(nLuminance* nIntensity); ");
    cgProg->AddToPixelProgram("    vOutputColor.w = vColor.w;                       ");
    cgProg->AddToPixelProgram(" }                                                   ");

    cgProg->setPixelProfile(CG_PROFILE_ARBFP1);
    cgProg->CompilePixelProgram("main");
    printf("%s \n", cgProg->GetCompiledPixelProgram());

    static CGparameter mColorTransformParam = cgProg->GetPixelVariable("mColorTransform");
    static CGparameter vBlueParam           = cgProg->GetPixelVariable("vBlue");
    static CGparameter nIntensityParam      = cgProg->GetPixelVariable("nIntensity");

    float _mColorTransform[4][4];
    float _vBlue[3];


    _mColorTransform[0][0] = 0.5149f;    _mColorTransform[0][1] = 0.3244f;    _mColorTransform[0][2] = 0.1607f;     _mColorTransform[0][3] = 0.0f;
    _mColorTransform[1][0] = 0.2654f;    _mColorTransform[1][1] = 0.6704f;    _mColorTransform[1][2] = 0.0642f;     _mColorTransform[1][3] = 0.0f;
    _mColorTransform[2][0] = 0.0248f;    _mColorTransform[2][1] = 0.1248f;    _mColorTransform[2][2] = 0.8504f;     _mColorTransform[2][3] = 0.0f;
    _mColorTransform[3][0] = 0.0000f;    _mColorTransform[3][1] = 0.0000f;    _mColorTransform[3][2] = 0.0000f;     _mColorTransform[3][3] = 0.0f;

    _vBlue[0] = .62f;   _vBlue[1] = 0.6f;   _vBlue[2] = 1.0f;

    _nBrightness = 0.13f;

    device->getFileSystem()->addZipFileArchive("../../media/map-20kdm2.pk3");
    scene::IAnimatedMesh* mesh = smgr->getMesh("20kdm2.bsp");
	scene::ISceneNode* node = smgr->addOctTreeSceneNode(mesh->getMesh(0));
    node->setPosition(core::vector3df(-1300,-144,-1249));

    scene::ICameraSceneNode* cam1 = smgr->addCameraSceneNode();

    ProgramRun = true;
    int lastFPS = -1;

	while(device->run()&& ProgramRun)
	{

		driver->beginScene(true, true, SColor(0,200,200,200));

                 if(KeyDown(KEY_ESCAPE))ProgramRun=false;

                cgGLSetMatrixParameterfr(mColorTransformParam, &(_mColorTransform[0][0]));
                cgGLSetParameter3fv(vBlueParam, &(_vBlue[0]));
                cgGLSetParameter1f(nIntensityParam, _nBrightness);

                 cgProg->StartShaderProgram();

                 smgr->drawAll();

                 cgProg->StopShaderProgram();

                 env->drawAll();

		driver->endScene();
		int fps = driver->getFPS();
         if (lastFPS != fps)
         {
            wchar_t tmp[1024];
            swprintf(tmp, 1024, L"Night Vision Demo (%s)(fps:%d)",driver->getName(), fps);
            device->setWindowCaption(tmp);
            lastFPS = fps;
         }
	}

	device->drop();

	return 0;
}

Enjoy it.

Posted: Wed Aug 09, 2006 11:02 pm
by bicunisa
Hey Emil ;)


Can this already work with Irrlicht 1.1? because I see you are still working with the 1.0 version.

Why don't you submit your work to integrate it with the core Irrlicht source?

Posted: Thu Aug 10, 2006 5:40 am
by Emil_halim
Hi bicunisa

i was working in making Magic Library for Irrlicht 1.1 since it released , but because there was many changes that applied for OpenGl Driver,so that it may takes some times to finish it.


the core code of Magic Library relies on OpenGL and Windows Operating system and as you know Irrlicht has DirextX and OpenGl and it is portable for other operating system so that we can not integrated it with Irrlicht.

Posted: Thu Aug 10, 2006 7:35 pm
by bicunisa
Oh well, maybe later MagicLibrary could be multi operating system, and use openGL and DirectX as Irrlicht does, so you can integrate it ;)

By the way, how much time do you think it's going to take to update to Irrlicht 1.1? I'm not putting pressure on you, just simple curiosity on my part :D