2013-03-04 5 views
0

투사 된 그림자가 작동하는 방식을 테스트하는 약간의 프로그램을 작성했습니다.그림자 매트릭스가있는 투사 된 그림자, 간단한 테스트가 실패했습니다.

투영 지점 (삼각형의 꼭지점 일 수 있음)이 광원과 평면 사이에 위치하지 않지만 빛 자체 뒤에 위치하는 경우, 즉 빛이 포인트와 비행기.

문제는 내 작은 프로그램이 포인트가 빛과 비행기 사이에있는 경우에도 작동하지 않는다는 것입니다. 내가 수십 배의 계산을 확인, 그래서 오류가 논리해야한다 생각,하지만 난 그것을 찾을 수 없습니다 .. 여기

코드

public class test { 

    int x = 0; 
    int y = 1; 
    int z = 2; 
    int w = 3; 
    float floor[][] = { 
     {-100.0f, -100.0f, 0.0f}, 
     {100.0f, -100.0f, 0.0f}, 
     {100.0f, 100.0f, 0.0f}, 
     {-100.0f, 100.0f, 0.0f}}; 
    private float shadow_floor[] = new float[16]; 
    float light_position[] = {0.0f, 0.0f, 10.0f, 1.0f}; 

    public test() { 
     //Find floorplane based on thre known points 
     float plane_floor[] = calculatePlane(floor[1], floor[2], floor[3]); 

     //store shadowMatrix for floor 
     shadow_floor = shadowMatrix(plane_floor, light_position); 

     float[] point = new float[]{1.0f, 0.0f, 5.0f, 1.0f}; 

     float[] projectedPoint = pointFmatrixF(point, shadow_floor); 

     System.out.println("point: (" + point[x] + ", " + point[y] + ", " + point[z] + ", " 
       + point[w] + ")"); 
     System.out.println("projectedPoint: (" + projectedPoint[x] + ", " + projectedPoint[y] 
       + ", " + projectedPoint[z] + ", " + projectedPoint[w] + ")"); 
    } 

    public static void main(String args[]) { 
     test test = new test(); 
    } 

    // make shadow matrix 
    public float[] shadowMatrix(float plane[], float light_pos[]) { 
     float shadow_mat[] = new float[16]; 
     float dot; 


     dot = plane[x] * light_pos[x] + plane[y] * light_pos[y] 
       + plane[z] * light_pos[z] + plane[w] * light_pos[w]; 

     shadow_mat[0] = dot - light_pos[x] * plane[x]; 
     shadow_mat[4] = -light_pos[x] * plane[y]; 
     shadow_mat[8] = -light_pos[x] * plane[z]; 
     shadow_mat[12] = -light_pos[x] * plane[3]; 

     shadow_mat[1] = -light_pos[y] * plane[x]; 
     shadow_mat[5] = dot - light_pos[y] * plane[y]; 
     shadow_mat[9] = -light_pos[y] * plane[z]; 
     shadow_mat[13] = -light_pos[y] * plane[w]; 

     shadow_mat[2] = -light_pos[z] * plane[x]; 
     shadow_mat[6] = -light_pos[z] * plane[y]; 
     shadow_mat[10] = dot - light_pos[z] * plane[z]; 
     shadow_mat[14] = -light_pos[z] * plane[w]; 

     shadow_mat[3] = -light_pos[w] * plane[x]; 
     shadow_mat[7] = -light_pos[w] * plane[y]; 
     shadow_mat[11] = -light_pos[w] * plane[z]; 
     shadow_mat[15] = dot - light_pos[w] * plane[w]; 

     return shadow_mat; 
    } 

    public float[] calculatePlane(float p1[], float p2[], float p3[]) { 
     //Array for planlikningen 
     float plane[] = new float[4]; 

     //Gitt to vektorer (tre punkter) i planet kan normalen regnes ut 
     //Vi vil ha aboluttverdier 
     plane[x] = Math.abs(((p2[y] - p1[y]) * (p3[z] - p1[z])) - ((p2[z] - p1[z]) 
       * (p3[y] - p1[y]))); 
     plane[y] = Math.abs(((p2[z] - p1[z]) * (p3[x] - p1[x])) - ((p2[x] - p1[x]) 
       * (p3[z] - p1[z]))); 
     plane[z] = Math.abs(((p2[x] - p1[x]) * (p3[y] - p1[y])) - ((p2[y] - p1[y]) 
       * (p3[x] - p1[x]))); 
     plane[w] = -(plane[x] * p1[x] + plane[y] * p1[y] + plane[z] * p1[z]); 


     return plane; 
    } 

    public float[] pointFmatrixF(float[] point, float[] matrix) { 
     int x = 0; 
     int y = 1; 
     int z = 2; 
     float[] transformedPoint = new float[4]; 

     transformedPoint[x] = 
       matrix[0] * point[x] 
       + matrix[4] * point[y] 
       + matrix[8] * point[z] 
       + matrix[12]; 
     transformedPoint[y] = 
       matrix[1] * point[x] 
       + matrix[5] * point[y] 
       + matrix[9] * point[z] 
       + matrix[13]; 
     transformedPoint[z] = 
       matrix[2] * point[x] 
       + matrix[6] * point[y] 
       + matrix[10] * point[z] 
       + matrix[14]; 
     transformedPoint[w] = 1; 

     return transformedPoint; 
    } 
} 

비행기가 xy 평면 인 경우, 표시등이 켜져 (0, 0, 10)이고 점 (1, 0, 5)의 점은 평면에서 투영 된 점은 (2, 0, 0)이되어야하지만 프로그램이 반환됩니다 (400000.0, 0.0, 0.0, 1.0)

+0

나는 같은이, 당신은 수학이 작동하도록하기 위해해야 ​​할 일을했을 무엇에 대해 프로그램을 확인 걸립니다 프로그램 단계를 사용하여 종이에 예를 통해 일 것입니다. ... 프로그램을 종이에 신중하게 복사하는 데 도움이 될 수도 있습니다. 인쇄물보다 필기체 일 때 지우고 변경하는 것이 더 쉽습니다. –

+0

시도했지만 작동하지 않았다, 나는 항상 같은 오류가 발생했다, 오류는 논리 – elect

답변