Real time shading

Java Cool Dude

New member
Since I'm still a n00b when it comes to coding 3D stuff, I thought I might stop by here and ask for your honest opinion about my real time shadows program.
Here's a first snap
shad1.JPG

And here comes the second
shad2.JPG

Sorry for the lousy JPG quality*swears at paint*, but you get the idea.

I make the shadows by computing the x/z coordinates of a vertix corresponding to a y coordinate = 0f, such that x belongs to the line of slope (light.y -vertex.y)/(light.x-vertex.x) and a constant B = (light.y - xYslope*light.x);
same applies for z;

ver.x = tempx + loca.x;
ver.y = tempy + loca.y;
ver.z = tempz + loca.z;

xYslope = (light.y - ver.y)/(light.x - ver.x);
b = (light.y - xYslope*light.x);

zYslope = (light.y - ver.y)/(light.z - ver.z);
c = (light.y - zYslope*light.z);

xIntersect = -(b)/xYslope ;
zIntersect = -(c)/zYslope;

objectGeom.setColor(i,objColor);
objectGeom.setCoordinate(i, new Point3f(ver.x,ver.y,ver.z));


shadowGeom.setCoordinate(i, new Point3f(xIntersect, .005f ,zIntersect));
shadowGeom.setColor(i,new Color3f(.0f,.0f,.0f));
 
Looks nice. Another way to get it working a little more "automatically" is to use a matrix to project the vertices down into the plane polygon.

From my Matrix.cpp file:

void Matrix::loadShadowMatrix(const Plane &plane, const Vertex &lightPos){
float dist = plane.distance(lightPos);

m[ 0] = dist - lightPos.x * plane.normal.x;
m[ 4] = - lightPos.x * plane.normal.y;
m[ 8] = - lightPos.x * plane.normal.z;
m[12] = - lightPos.x * plane.offset;

m[ 1] = - lightPos.y * plane.normal.x;
m[ 5] = dist - lightPos.y * plane.normal.y;
m[ 9] = - lightPos.y * plane.normal.z;
m[13] = - lightPos.y * plane.offset;

m[ 2] = - lightPos.z * plane.normal.x;
m[ 6] = - lightPos.z * plane.normal.y;
m[10] = dist - lightPos.z * plane.normal.z;
m[14] = - lightPos.z * plane.offset;

m[ 3] = - plane.normal.x;
m[ 7] = - plane.normal.y;
m[11] = - plane.normal.z;
m[15] = dist - plane.offset;
}

I used this technique in my shadowed particles demo, where I projected the smoke down to the plane.
 
Thx for the comment Humus, but before giving your method a try, I think I'm gonna go this way:
See a polygon vertex is a point3f(x,y,z) "I use Java3d" and the light source itself is a point3f(). My idea is to set a new Vector3f() having the light source for origin, and the the polygon vertex for direction. Now if I extend that Vector3f() by a simple scalar multiplication, and do some necessary if conditions " example if vetor3f.y = plane.y || vector3f.x = plane.x" then set the shadow polygon vertex such as shadow.setCoordinate(i, new Point3f(extendedVector.x,extendedVector.y,extendedVector.z);

Let me hear your input
 
Euuuuuuuuuh could you please throw me a bone concerning the term offSet in your matrix?
I don't have any Plane class in java3D, so I'm assuming a plane is just a 2 vector3f.
Offset.......hmmm:confused:
 
A plane is defined by Ax + Bx + Cx + D = 0, where (A,B,C) is the normal and D is the signed distance to the origin, so offset = D.
 
Got my own method to work "currently with no inclined planes" here's what I do:
Code:
      ratio =(light.y)/(light.y - ver[i].y);//Shadows casted on plane y = 0;
         ratio =((Math.abs(light.x-ver[i].x)/(light.x-ver[i].x))*8f + light.x)/(light.x - ver[i].x);//Plane x = 8f || x = -8f "parrallel to y axis", same thing for z = n || z = -n

      xPro = (ver[i].x - light.x)*ratio + light.x;
     yPro = (ver[i].y - light.y)*ratio + light.y;
      zPro = (ver[i].z - light.z)*ratio + light.z;

 (ver[i].x - light.x) sends the vertex to the plane where the source of light is the origin

*ratio  extends my vector to the desired length

+ light.x; sends the vertex back into the (0,0,0) origin plane


      shadowPoints[i] = new Point3f(xPro, yPro, zPro);
 
A small suggestion ... :)

Something that will help your productivity is to write your own Vector/Vertex class and overload the + - * / operators. For a small piece of code like this it may not matter, but you'll gain a whole lot of it in the end. With my Vertex class I'd instead of

xPro = (ver.x - light.x)*ratio + light.x;
yPro = (ver.y - light.y)*ratio + light.y;
zPro = (ver.z - light.z)*ratio + light.z;

write something like

pro = (ver - light) * ratio + light;

and get the same behaviour. By overloading the (float *) cast I can directly pass it to glVertex3fv() too. Like glVertex3fv(pro);
 
Humus said:
A small suggestion ... :)

Something that will help your productivity is to write your own Vector/Vertex class and overload the + - * / operators. For a small piece of code like this it may not matter, but you'll gain a whole lot of it in the end. With my Vertex class I'd instead of

xPro = (ver.x - light.x)*ratio + light.x;
yPro = (ver.y - light.y)*ratio + light.y;
zPro = (ver.z - light.z)*ratio + light.z;

write something like

pro = (ver - light) * ratio + light;

and get the same behaviour. By overloading the (float *) cast I can directly pass it to glVertex3fv() too. Like glVertex3fv(pro);

Knocks head on desk for missing such an obvious tweaking.
Thx man I'm looking forward to talking to you about the whole 3D coding thing when I make my transition from java/java3d to c/c++
Thx again pal, I must say you and nitrogl inspired me to get into 3D coding rather than 2D :D
 
Back
Top