1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Development Java Hidden Surface Removal

Discussion in 'Tech Support' started by Cabe6403, 19 Dec 2009.

  1. Cabe6403

    Cabe6403 Supreme Commander

    Joined:
    3 Apr 2007
    Posts:
    1,205
    Likes Received:
    44
    Ok, so I'm working on a piece of code in java which displays a 3D rotating cube made out of surfaces - which contain three lines - which contain 2 points.

    So Point3D > Line3D > Surface3D > Shape3D

    I'm having trouble getting hidden surface removal to work. I'm not bothering with Z-buffering or anything fancy like that just now rather just determining visibility using

    L.N = |L|.|N|.cos(theta)

    Where N is the normal vector of the surface and L is the viewpoint vector

    visible if 0<= cos(theta) <= 1

    However it seems to be removing random surfaces and I've been working on this for ages now with no improvement.

    Anyone have any idea what's wrong / how to fix it?

    The following code is located in the Surface3D class. L[] is an array of the 3 Line3D objects used to make the surface.

    Code:
    public void draw(Graphics g){
    		
    		float ax = L[1].getSrc().x() - L[0].getSrc().x();
    		float ay = L[1].getSrc().y() - L[0].getSrc().y();
    		float az = L[1].getSrc().z() - L[0].getSrc().z();
    		
    		float bx = L[2].getSrc().x() - L[0].getSrc().x();
    		float by = L[2].getSrc().y() - L[0].getSrc().y();
    		float bz = L[2].getSrc().z() - L[0].getSrc().z();
    		
    		float nx = (ay*bz)-(az*by);
    		float ny = (az*bx)-(ax*bz);
    		float nz = (ax*by)-(ay*bx);
    		
    		float nx2 = nx/(float)(Math.sqrt((double)(nx*nx + ny*ny + nz*nz)));
    		float ny2 = ny/(float)(Math.sqrt((double)(nx*nx + ny*ny + nz*nz)));
    		float nz2 = nz/(float)(Math.sqrt((double)(nx*nx + ny*ny + nz*nz)));
    		
    		float px = L[1].getSrc().x();
    		float py = L[1].getSrc().y();
    		float pz = 2000 - L[1].getSrc().z();
    		
    		float px2 = px/(float)(Math.sqrt((double)(px*px + py*py + pz*pz)));
    		float py2 = py/(float)(Math.sqrt((double)(px*px + py*py + pz*pz)));
    		float pz2 = pz/(float)(Math.sqrt((double)(px*px + py*py + pz*pz)));
    						
    		float CosTheta = (px2*nx2)+(py2*ny2)+(pz2*nz2);
    
    		if ((CosTheta >= 0)||(CosTheta <= 1)){
    			System.out.println("here");
    			L[0].draw(g);
    			L[1].draw(g);
    			L[2].draw(g);
    		}
    	}
     
  2. Cabe6403

    Cabe6403 Supreme Commander

    Joined:
    3 Apr 2007
    Posts:
    1,205
    Likes Received:
    44
    38 views and no one has any suggestions? I don't mind if they're wrong :p
     

Share This Page