No shadows in OpenGL?

If you are a new Irrlicht Engine user, and have a newbie-question, this is the forum for you. You may also post general programming questions here.
Post Reply
Wolfgke

No shadows in OpenGL?

Post by Wolfgke »

I looked at the demo of the Irrlicht engine and saw that in the OpenGL renderer there are no realtime shadows. Why is that? And when will it become fixed? I would be very thankful for the answer, because I plan to use Irrlicht (with Dev-C++ where it is complicated to use DirectX) in a private project, but do like the realtime shadows of the demo.
William Finlayson
Posts: 61
Joined: Mon Oct 25, 2004 12:11 am

Post by William Finlayson »

Code: Select all

//! Draws a shadow volume into the stencil buffer. To draw a stencil shadow, do
//! this: Frist, draw all geometry. Then use this method, to draw the shadow
//! volume. Then, use IVideoDriver::drawStencilShadow() to visualize the shadow.
void CVideoOpenGL::drawStencilShadowVolume(const core::vector3df* triangles, s32 count, bool zfail)
{
	if (!StencilBuffer || !count)
		return;
	
	// unset last 3d material
	if (CurrentRenderMode == ERM_3D &&
		Material.MaterialType >= 0 && Material.MaterialType < (s32)MaterialRenderers.size())
	{
		MaterialRenderers[Material.MaterialType]->OnUnsetMaterial();
		ResetRenderStates = true;
	}
	
	// store current OpenGL	state
	glPushAttrib(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_ENABLE_BIT |
		GL_POLYGON_BIT	| GL_STENCIL_BUFFER_BIT	);

	glDisable(GL_LIGHTING);
	glDisable(GL_FOG);
	glDepthMask(GL_FALSE);
	glDepthFunc(GL_LEQUAL);
	glEnable(GL_STENCIL_TEST);
	glColorMask(GL_FALSE, GL_FALSE,	GL_FALSE, GL_FALSE ); // no color buffer drawing
	glStencilFunc(GL_ALWAYS, 1,	0xFFFFFFFFL	);
	glColorMask(0, 0, 0, 0);
	glEnable(GL_CULL_FACE);

	

	if (!zfail)
	{
		// ZPASS Method

		glStencilOp(GL_KEEP, GL_KEEP, GL_INCR);
		glCullFace(GL_BACK);
		glBegin(GL_TRIANGLES);

		s32 i;
		for(i =	0; i < count; ++i)
			glVertex3f(triangles[i].X, triangles[i].Y, triangles[i].Z);

		glEnd();

		glStencilOp(GL_KEEP, GL_KEEP, GL_DECR);
		glCullFace(GL_FRONT);
		
		glBegin(GL_TRIANGLES);
		for(i =	0; i < count; ++i)
			glVertex3f(triangles[i].X, triangles[i].Y, triangles[i].Z);
		
		glEnd();

	}
	else
	{
		// ZFAIL Method

		glStencilOp(GL_KEEP, GL_INCR, GL_KEEP);
		glCullFace(GL_FRONT);
		
		glBegin(GL_TRIANGLES);

		s32 i;
		for(i =	0; i < count; i++)
			glVertex3f(triangles[i].X, triangles[i].Y, triangles[i].Z);
		
		glEnd();

		glStencilOp(GL_KEEP, GL_DECR, GL_KEEP);
		glCullFace(GL_BACK);
		
		glBegin(GL_TRIANGLES);

		for(i =	0; i < count; i++)
			glVertex3f(triangles[i].X, triangles[i].Y, triangles[i].Z);
		
		glEnd();

	}

	glPopAttrib();
}



void CVideoOpenGL::drawStencilShadow(bool clearStencilBuffer, video::SColor leftUpEdge,
			video::SColor rightUpEdge, video::SColor leftDownEdge, video::SColor rightDownEdge)
{
	if (!StencilBuffer)
		return;

		setTexture(0,0); 
		setTexture(1,0);
	glPushAttrib( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_ENABLE_BIT | GL_POLYGON_BIT | GL_STENCIL_BUFFER_BIT );


	glDisable( GL_LIGHTING );
	glDepthMask(GL_FALSE); 
	glDepthFunc( GL_LEQUAL );
	glEnable( GL_STENCIL_TEST );

	glFrontFace( GL_CCW );
	glColorMask( GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE );

	glEnable(GL_BLEND);
	glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

	glStencilFunc(GL_NOTEQUAL, 0, 0xFFFFFFFFL);
	glStencilOp(GL_KEEP, GL_KEEP, GL_KEEP);

	glDisable(GL_FOG);

	glPushMatrix();
	glLoadIdentity();

	glBegin(GL_TRIANGLE_STRIP);

	glColor4ub (leftUpEdge.getRed(), leftUpEdge.getGreen(), leftUpEdge.getBlue(), leftUpEdge.getAlpha() );
	glVertex3f(-10.1f, 10.1f,0.90f);

	glColor4ub (leftDownEdge.getRed(), leftDownEdge.getGreen(), leftDownEdge.getBlue(), leftDownEdge.getAlpha() );
	glVertex3f(-10.1f,-10.1f,0.90f);

	glColor4ub (rightUpEdge.getRed(), rightUpEdge.getGreen(), rightUpEdge.getBlue(), rightUpEdge.getAlpha() );
	glVertex3f( 10.1f, 10.1f,0.90f);

	glColor4ub (rightDownEdge.getRed(), rightDownEdge.getGreen(), rightDownEdge.getBlue(), rightDownEdge.getAlpha() );
	glVertex3f( 10.1f,-10.1f,0.90f);

	glEnd();

	glPopMatrix();
	glPopAttrib();

	if (clearStencilBuffer)
		glClear(GL_STENCIL_BUFFER_BIT);

	glDepthMask(GL_TRUE); 
	glEnable(GL_DEPTH_TEST); 

}
Replace those functions in CVideoOpenGL.cpp, recompile, and shadows work :)

Sorry about posting the whole functions, but I couldn't remember what was changed.
AmigaIrr
Posts: 94
Joined: Mon Jan 10, 2005 7:55 am
Location: France, Alsace

Post by AmigaIrr »

Good man !

But, have you the solution for the DX9 bug ??

I use only DX9c + Dev-C++... :roll:
L'eternité c'est long, surtout vers la fin...

Q6600 triton 79, 4 GO, 2* RAPTOR 150GO of ARECA 256 RAID 0, 3870 Zalmann, P5K. 24" Samsung. Antec nine hundred
William Finlayson
Posts: 61
Joined: Mon Oct 25, 2004 12:11 am

Post by William Finlayson »

I use only OpenGL + Dev-C++... :)
MikeR
Posts: 767
Joined: Sun Dec 26, 2004 4:03 pm
Location: Northern California USA
Contact:

Post by MikeR »

There is a whole long thread in the tools forum talking about directX and dev-cpp.
If it exists in the real world, it can be created in 3d

Sys specs:
AMD 3700+ 64 processor
1.0 gb ram
e-Geforce 6600 graphics 256 mb onboard ram
Murphy
Posts: 290
Joined: Mon Dec 13, 2004 12:06 am
Location: United States
Contact:

Post by Murphy »

AmigaIrr wrote:But, have you the solution for the DX9 bug ??
What bug?
Wolfgke

Post by Wolfgke »

@William Finlayson
Tha code did it. Thanks a lot!
William Finlayson
Posts: 61
Joined: Mon Oct 25, 2004 12:11 am

Post by William Finlayson »

np :)

You may also find problems with some of the materials in OpenGL. Take a look at the SpecialFX demo, and see if it matches the screenshots. If it isn't working right, I have a fix for that as well, modified from nx++. OpenGL can work just as well as DX in Irrlicht, it just needs a little prod here and there :D
firefly2442
Posts: 38
Joined: Mon May 31, 2004 7:55 am
Contact:

Post by firefly2442 »

Is this changed in irrlicht 0.8? It seems strange to me that shadows are enabled by default in DirectX and not OpenGL.
Murphy
Posts: 290
Joined: Mon Dec 13, 2004 12:06 am
Location: United States
Contact:

Post by Murphy »

firefly2442 wrote:Is this changed in irrlicht 0.8? It seems strange to me that shadows are enabled by default in DirectX and not OpenGL.
It's not a matter of them being enabled, it's a matter of Irrlicht's OpenGL code not being as tuned as its DirectX code -- I think Niko mostly uses DirectX. :) So it works on some systems, but not on others. Shadows worked fine in OpenGL on my old GeForce, but on my new Radeon, they don't.
Post Reply