User Name
Password

Go Back   Planetarion Forums > Non Planetarion Discussions > Programming and Discussion
Register FAQ Members List Calendar Arcade Today's Posts

Reply
Thread Tools Display Modes
Unread 27 Dec 2003, 18:56   #1
Structural Integrity
Rawr rawr
 
Structural Integrity's Avatar
 
Join Date: Dec 2000
Location: Upside down
Posts: 5,300
Structural Integrity needs a job and a girlfriendStructural Integrity needs a job and a girlfriendStructural Integrity needs a job and a girlfriendStructural Integrity needs a job and a girlfriendStructural Integrity needs a job and a girlfriendStructural Integrity needs a job and a girlfriendStructural Integrity needs a job and a girlfriendStructural Integrity needs a job and a girlfriendStructural Integrity needs a job and a girlfriendStructural Integrity needs a job and a girlfriendStructural Integrity needs a job and a girlfriend
Question OpenGL - shadow casting

I posted this same question on the gamedev forums, but since that forum is slow with giving answers on specific questions I decided to try here too because I know there are a good few people here who do OpenGL.

At school we're currently working on a connect4 game, and I and another guy are supposed to make the GUI. We've come a long way, and I'm adding neat features right now, one of which is a shadow cast on the floor.

I've been working on this shadow stuff all day now, and so far I've managed to do some raycasting onto the plane in which the floor lies. However, the structure in which the objects (the board and the stones) are stored requires me to do a lot of calculations for every vertice every frame, and kills the framerate. Thereby, I can't figure out how to do a rotation of the models so my raycasting works properly, so the shadows look like poo.

I read somewhere that one can use the stencil buffer for shadow casting, and I've seen a NEHE tutorial about it, but I cannot figure out how to use it.
So, how do I cast a shadow in OpenGL on a plane, without killing framerates?

Thanks!
Structural Integrity is offline   Reply With Quote
Unread 31 Dec 2003, 11:25   #2
Structural Integrity
Rawr rawr
 
Structural Integrity's Avatar
 
Join Date: Dec 2000
Location: Upside down
Posts: 5,300
Structural Integrity needs a job and a girlfriendStructural Integrity needs a job and a girlfriendStructural Integrity needs a job and a girlfriendStructural Integrity needs a job and a girlfriendStructural Integrity needs a job and a girlfriendStructural Integrity needs a job and a girlfriendStructural Integrity needs a job and a girlfriendStructural Integrity needs a job and a girlfriendStructural Integrity needs a job and a girlfriendStructural Integrity needs a job and a girlfriendStructural Integrity needs a job and a girlfriend
Re: OpenGL - shadow casting

I found a solution somewhere. Apparently you can do a matrix multiplication so it appears to be projected onto a plane.

Code:
void CRender::drawShadows()
{
	
	//following vector and scalar are the plane in which the shadow lies
	Vector3 N;	//normal of the floor
	float D;	//distance to origin

	Vector3 Q;	//position of the light source


	N.x = 0;
	N.y = 1;
	N.z = 0;
	D = 3.3f;

	Q.x = 0;
	Q.y = 5;
	Q.z = 15;



	float dot = N.x * Q.x + N.y * Q.y + N.z * Q.z + D * 1; 
	float matrix[4][4];

	

	glPushMatrix();

	glEnable(GL_STENCIL_TEST);
	if (shadowcaster)
	{
		shadowcaster = 0;

		glClear(GL_STENCIL_BUFFER_BIT);	//maak de buffer leeg

		//bereken de matrix
		matrix[0][0] = dot - Q.x * N.x;
		matrix[1][0] = 0.f - Q.x * N.y;
		matrix[2][0] = 0.f - Q.x * N.z;
		matrix[3][0] = 0.f - Q.x * D;

		matrix[0][1] = 0.f - Q.y * N.x;
		matrix[1][1] = dot - Q.y * N.y;
		matrix[2][1] = 0.f - Q.y * N.z;
		matrix[3][1] = 0.f - Q.y * D;

		matrix[0][2] = 0.f - Q.z * N.x;
		matrix[1][2] = 0.f - Q.z * N.y;
		matrix[2][2] = dot - Q.z * N.z;
		matrix[3][2] = 0.f - Q.z * D;

		matrix[0][3] = 0.f - 1 * N.x;
		matrix[1][3] = 0.f - 1 * N.y;
		matrix[2][3] = 0.f - 1 * N.z;
		matrix[3][3] = dot - 1 * D;
		
		glMultMatrixf((float *)matrix);	//vemenigvuldig de nieuwe matrix met de ouwe matrix

		glStencilFunc(GL_ALWAYS, 1, 0xFFFFFFFF);
		glStencilOp(GL_REPLACE, GL_REPLACE, GL_REPLACE);

		
		
		glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE);
		glDepthMask(GL_FALSE);
			this->drawObjects(1);
		glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
		glDepthMask(GL_TRUE);
	}
		glStencilFunc(GL_EQUAL, 1, 0xFFFFFFFF);
		glStencilOp(GL_KEEP, GL_KEEP, GL_KEEP);
			glColor4f(0,0,0,0.6f);
			this->drawFloor(DRAW_OPTION_NO_COLOR);

		glDisable(GL_STENCIL_TEST);

	glPopMatrix();

	return;

}
I optimised it a bit so that the shadow is recalculated when the camera has moved.
I looked into making a projection, but it's far too slow. The models used have far too many polygons and the shadow shouldn't be simplified as the user is able to view it from very close. Also, the shadow changes from time to time.
The stencil buffer works best IMO.

Sorry for the Dutch comment on some lines. The project-group insisted on using Dutch comments because else the teachers might not be able to read it. Stupid teachers.


BTW, a little screenie of how it looks now with shadow:
http://www.structweb.com/img/connect4/school_c4_2.jpg
Structural Integrity is offline   Reply With Quote
Unread 31 Dec 2003, 19:11   #3
Structural Integrity
Rawr rawr
 
Structural Integrity's Avatar
 
Join Date: Dec 2000
Location: Upside down
Posts: 5,300
Structural Integrity needs a job and a girlfriendStructural Integrity needs a job and a girlfriendStructural Integrity needs a job and a girlfriendStructural Integrity needs a job and a girlfriendStructural Integrity needs a job and a girlfriendStructural Integrity needs a job and a girlfriendStructural Integrity needs a job and a girlfriendStructural Integrity needs a job and a girlfriendStructural Integrity needs a job and a girlfriendStructural Integrity needs a job and a girlfriendStructural Integrity needs a job and a girlfriend
Re: OpenGL - shadow casting



Anyway, I got a new problem:



When the shadow is recalculated (ie: stencil buffer, cleared, new shadow casted on it)
http://www.structweb.com/img/connect4/c4_err_1.jpg


When the stencil buffer is NOT cleared, and the shadow is only drawn
http://www.structweb.com/img/connect4/c4_err_2.jpg


I only redraw the shadow (clear buffer, do matrix multiplication, draw objects) when the camera moved. But when I redraw the shadow, it shows up white.
I only have this problem on my new video card (Radeon9600XT) but my old one didn't show it (Geforce2TI). I can't remember having made any changes in that part of the code.

Any ideas of what this could be? I've checked about for color settings, but nothing.
Structural Integrity is offline   Reply With Quote
Unread 1 Jan 2004, 13:42   #4
Cyp
∞+♪˛
 
Join Date: Nov 2000
Location: :uo!te]oŻ|
Posts: 428
Cyp is an unknown quantity at this point
Re: OpenGL - shadow casting

Unless there's a weird call to glBlendFunc, I don't know...
Cyp is offline   Reply With Quote
Unread 1 Jan 2004, 15:12   #5
Structural Integrity
Rawr rawr
 
Structural Integrity's Avatar
 
Join Date: Dec 2000
Location: Upside down
Posts: 5,300
Structural Integrity needs a job and a girlfriendStructural Integrity needs a job and a girlfriendStructural Integrity needs a job and a girlfriendStructural Integrity needs a job and a girlfriendStructural Integrity needs a job and a girlfriendStructural Integrity needs a job and a girlfriendStructural Integrity needs a job and a girlfriendStructural Integrity needs a job and a girlfriendStructural Integrity needs a job and a girlfriendStructural Integrity needs a job and a girlfriendStructural Integrity needs a job and a girlfriend
Re: OpenGL - shadow casting

Aha, I found it. Crazy me did the popMatrix() call in the drawShadow function too late, after it draws the floor.
Structural Integrity is offline   Reply With Quote
Reply



Forum Jump


All times are GMT +1. The time now is 01:36.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2002 - 2018