PDA

View Full Version : planar shadows /vertex programs



ScottManDeath
02-25-2002, 03:24 AM
Hi

I have some problems with calcing shadows using vertex programs
I just want to project an objects planar shadow on a plane given by three points.This
should all be done using vertex programs.

Heres the way I try it:
I have 2 vertex state programs and one vertex program

Vertex State program 0
-gets 3 points in c[48],c[49],c[50], object space
- transform to eye space using modelview matrix
-calculates the plane normal and the distance to origin (should be in eye space ?)

Vertex state program 1
-gets the light position in v[0] in object space
-transforms it to eye space using modelview matrix

Vertex Program
-transform vertex to eye space
-calc line from light in c[65] to vertex
-intersects line with plane in c[64]
-transform projected vertex into clip space

The problems:
-shadows aren't really flat
-shadows are not in my plane
-when I rotate the object the shadow rotates instead of staying fixed but changing its shape
-there is some projection, but not the correct one

here is the render loop and the programs

the tracked matrices(VCP parsed by nvparse)
!!VCP1.0
c[0]=TRACK(GL_MODELVIEW_PROJECTION_NV,GL_IDENTITY_NV);
c[4]=TRACK(GL_MODELVIEW,GL_IDENTITY_NV);
c[8]=TRACK(GL_PROJECTION,GL_IDENTITY_NV);
c[12]=TRACK(GL_MODELVIEW,GL_INVERSE_TRANSPOSE_NV);
c[16]=TRACK(GL_MODELVIEW,GL_INVERSE_NV);
c[20]=TRACK(GL_MODELVIEW,GL_TRANSPOSE_NV);
#some numbers
c[95]=(0.0,0.5,1.0,2.0);

++++++++++++++++++++++++
void OnRender()
{
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glPushMatrix();
glLoadIdentity();

// position scene
glTranslatef(stranslation[0],stranslation[1],stranslation[2]);
glMultMatrixf(srotation);

glPushMatrix();
glTranslatef(light_pos[0],light_pos[1],light_pos[2]);
glColor3f(1.0f,1.0f,1.0f);
glutSolidSphere(0.2f,16,16);
glPopMatrix();

// render plane
glColor3f(0.0f,0.7f,0.0f);
glBegin(GL_TRIANGLES);
glVertex4fv(planepoints[0]);
glVertex4fv(planepoints[1]);
glVertex4fv(planepoints[2]);
glEnd();

// update plane points
glProgramParameter4fvNV(GL_VERTEX_PROGRAM_NV,48,pl anepoints[0]);
glProgramParameter4fvNV(GL_VERTEX_PROGRAM_NV,49,pl anepoints[1]);
glProgramParameter4fvNV(GL_VERTEX_PROGRAM_NV,50,pl anepoints[2]);

// calc the plane
float tmp[4]={1.0f,1.0f,1.0f,1.0f};
glExecuteProgramNV(GL_VERTEX_STATE_PROGRAM_NV,vsp_ id[0],tmp);
glLightfv(GL_LIGHT0,GL_POSITION,light_pos);

// translate the light in eye space
glExecuteProgramNV(GL_VERTEX_STATE_PROGRAM_NV,vsp_ id[1],light_pos);
glPushMatrix();
//translate/rotate the object
glTranslatef(otranslation[0],otranslation[1],otranslation[2]);
glMultMatrixf(orotation);

//the object
glEnable(GL_LIGHTING);
RenderShape(shape);
glDisable(GL_LIGHTING);

// the shadow
glEnable(GL_VERTEX_PROGRAM_NV);
glBindProgramNV(GL_VERTEX_PROGRAM_NV,vp_id[0]);
RenderShape(shape);
glDisable(GL_VERTEX_PROGRAM_NV);
glPopMatrix();
glPopMatrix();
glutSwapBuffers();
}


++++++++++++++++++++++++++++++++++++++++++++
!!VSP1.0
# IN
# plane points p1 ,p2, p3 in c[48]-c[50]
# OUT
# plane normal and distance to origin in eye space

#point 1 in eye space
MOV R0,c[48];
DP4 R1.x,R0,c[4];
DP4 R1.y,R0,c[5];
DP4 R1.z,R0,c[6];
DP4 R1.w,R0,c[7];

#point 2 in eye space
MOV R0,c[49];
DP4 R2.x,R0,c[4];
DP4 R2.y,R0,c[5];
DP4 R2.z,R0,c[6];
DP4 R2.w,R0,c[7];

#point 3 in eye space
MOV R0,c[50];
DP4 R3.x,R0,c[4];
DP4 R3.y,R0,c[5];
DP4 R3.z,R0,c[6];
DP4 R3.w,R0,c[7];

#difference vector a
ADD R4,R2,-R1;

#difference vector b
ADD R5,R3,-R1;

#plane normal vector a cross b
MUL R6.xyz,R4.zxyw,R5.yzxw;
MAD R6.xyz,R4.yzxw,R5.zxyw,-R6;

# normalize normal
DP3 R6.w,R6,R6;
RSQ R6.w,R6.w;
MUL R6.xyz,R6,R6.w;

#normal in c[64]
MOV c[64].xyz,R6;

#distance to origin in c[64].w
DP3 c[64].w,R6,-R2;

END
++++++++++++++++++++++++++++++++++++++
!!VSP1.0
#light in eyespace
DP4 R0.x,v[0],c[4];
DP4 R0.y,v[0],c[5];
DP4 R0.z,v[0],c[6];
DP4 R0.w,v[0],c[7];
MOV c[65],R0;
END


++++++++++++++++++++++++++++++++
!!VP1.0
#vertex in eye
DP4 R0.x,v[OPOS],c[4];
DP4 R0.y,v[OPOS],c[5];
DP4 R0.z,v[OPOS],c[6];
DP4 R0.w,v[OPOS],c[7];

#vector vertex light
ADD R1,R0,-c[65];

#plane normal dot light +distance
MOV R2,c[65];
DP3 R3.w,c[64],R2;
ADD R3.w,c[64].w,R3.w;

#plane normal dot vector vertex light
DP3 R4,c[64],R1;

#division
RSQ R4.w,-R4.w;
MUL R5,R3,R4.w;
# projecte point into plane in eye space
MAD R6,R5.w,R1,R2;
MOV R6.w,c[95].z;

# point --> clipspace
DP4 o[HPOS].x,c[8],R6;
DP4 o[HPOS].y,c[9],R6;
DP4 o[HPOS].z,c[10],R6;
DP4 o[HPOS].w,c[11],R6;

END
I hope i didn't make a stupid mistake, perhaps one of you sees what's obviously.

Bye
ScottManDeath