Simple Shadows

Started by
25 comments, last by XorMultiPleXus 17 years, 10 months ago
Is the background still one quad? If yes, then use projected shadows. I you don't have time to play with it i can put on my homepage some simple code (http://xormultiplexus.googlepages.com/home), but i need a few time to put it in one piece. If you want it, then you must decide where to render the shadow. On that mortal kombat picture i would say you want the shadow to be on the ground. So make the ground one quad and the rest is not important. The problem is that, if the ground is not one polygon it wont be possible to do the shadow. One other solution could be to create a quad that would represent the ground. But you will not render that quad, but insted you will use it to make projection on it and to achieve more realistic look for you shadow. This way you don't need to do any changes to the background polygon.

decide ;-D

XorMultiPleXus

btw. that game of yours look good ;-D
Advertisement
Hi. The background in the game is just a texture, so it would be quite easy to create a polygon to represent the ground. A code example would be great!
- http://vlab.dk
I tried to adjust the shadows matrix to the light source with some code I found on the Nvidia website.



It only works if I tweak the lightsource to a specific point (not the original position of my light) which is kind of strange. The code requires me to specify the normal of the ground, but I don't think I'm doing that right although I have tried a lot of different settings.
An image of it.

4.jpg

I would still like the other code because I think it would work better
- http://vlab.dk
Ok

download the geom.h and geom.cpp from my page (http://xormultiplexus.googlepages.com/downloads).

It is easy high school math which will help us.

Now the code (the explanation is after the code listing):

/* CODE BEGIN */

//part 1
double * a = new double[3];
double * b = new double[3];
double * c = new double[3];
double * n;

a[0] = -300;
a[1] = -100;
a[2] = 0;

b[0] = 300;
b[1] = -100;
b[2] = 0;

c[0] = 0;
c[1] = 300;
c[2] = 0;

n = normal(a, b, c);

//part 2
glEnable(GL_STENCIL_TEST);

glDepthMask(GL_FALSE);
glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE);

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

//part 3
glBegin(GL_TRIANGLES);

//part 4
double * r = new double[3];
double * u = new double[3];

Plane * rov = createPlane(a, b, c, n);

delete [] a;
delete [] b;
delete [] c;
delete [] n;

//part 5
Light * l = new Light;
l->position = new double[3];
l->position[0] = -20;
l->position[1] = -50;
l->position[2] = 40;

Line * p;

//part 6
for (unsigned int i = 0; i < ceruzka->fc; i++)
{

getVertex(i, 0, ceruzka, r);
p = createLine(l->position, r);
double * x = pointOfIntersection(rov, p);
glVertex3dv(x);
delete [] x;
delete [] p->A;
delete [] p->s;
delete p;

getVertex(i, 1, ceruzka, r);
p = createLine(l->position, r);
x = pointOfIntersection(rov, p);
glVertex3dv(x);
delete [] x;
delete [] p->A;
delete [] p->s;
delete p;

getVertex(i, 2, ceruzka, r);
p = createLine(l->position, r);
x = pointOfIntersection(rov, p);
glVertex3dv(x);
delete [] x;
delete [] p->A;
delete [] p->s;
delete p;

}

//part 7
glEnd();

delete [] r;
delete [] u;

glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
glDepthMask(GL_TRUE);

/* CODE END */

Explanation:

part 1: i have declared and defined point A, B, C and a normal verctor n which is computed via the normal(A, B, C) function

part 2: we will start stencil test

part 3: we will start rendering our shadow mesh

part 4: the rov variable is the plane which in your case represents the ground and we will compute it with the createPlane(A, B, C, n) function. Don't forget to delete the variables a, b, c and n because you don't need them anymore.

part 5: we will create our light, only it's position

part 6: the variable ceruzka->fc is how much triangles we will render. The getVertex(...) gets the vertex from the ceruzka structure (IT IS NOT a part of the files geom.h and geom.cpp; it is my own structure which i use for keeping the geometry). The createLine is used to create a line from the light to the vertex and with the pointOfIntersection(...) we will aquire where the vertex lies on the plane (your ground). So in one iteration of the for cycle we will project one triangle of some mesh onto the plane and render it into the stencil buffer. So the whole for-cycle will render the shadow where it should be.

part 7: end the rendering, clean the memory and turn off stencil test ;-D

Hope it helped, this technique is old and lame but it is enougth for your project. It is called planar shadows (http://www.opengl.org/resources/features/StencilTalk/sld020.htm).

XorMultiPleXus
Hi. It looks great. I'll will be a couple of days before I implement it because i have a really difficult exam Wednesday so I have to prepare for a couple of days ;-)
Good luck

This topic is closed to new replies.

Advertisement