This site requires JavaScript, please enable it in your browser!
Greenfoot back
wabuilderman
wabuilderman wrote ...

2015/6/24

3d Raycasting

wabuilderman wabuilderman

2015/6/24

#
Ok, so now that I know a lot more about raycasting, I decided to revist my previous 3d Utility project. I am completely re-working how I am going about rendering. This time, I am rendering by drawing a ray for each pixel on the screen, then I want to determine what face it connects with, thus telling me what color to display (when I say what face, I also would prefer to know WHERE on the face). I had tried asking on stackoverflow, but they were a little unwelcoming and pretty much told me "You S***. You don't know how to code. Get a life". Here is my code. Can anyone tell me where I went wrong?
VectorTools vt = new VectorTools();
        for(int x = 0; x < ImageToRender.getWidth();x++)
        {
            for(int y = 0; y < ImageToRender.getHeight();y++)
            {
                double localRotX = ((x-(ImageToRender.getWidth()/2))*((POV/2)/(ImageToRender.getWidth()/2))) + rotX;
                double localRotY = ((y-(ImageToRender.getHeight()/2))*((POV/2)/(ImageToRender.getHeight()/2))) + rotY;
                double localRotZ = rotZ;
                
                for(int f = 0; f < faces.size(); f++)
                {
                    Vertice Vertice1 = faces.get(f).getV1();
                    Vertice Vertice2 = faces.get(f).getV2();
                    Vertice Vertice3 = faces.get(f).getV3();
                    
                    Vector v1 = vt.subtractVertices(Vertice2, Vertice1);
                    Vector v2 = vt.subtractVertices(Vertice3, Vertice1);
                    Vector normal = vt.dotProduct(v1, v2);
                    
                    //formula = -(ax + by + cz + d)/n * u where a,b,c = normal(x,y,z) and where u = the vector of the ray from camX,camY,camZ,
                    // with a rotation of localRotX,localRotY,localRotZ
                    
                    
                    Vector Ray = vt.subtractVertices(camX,camY,camZ,
                    camX + Math.sin(localRotY)*Math.cos(localRotX),camY + Math.cos(localRotY)*Math.cos(localRotX),camZ + Math.sin(localRotX));
                    double t=(normal.x*((camX+ Ray.x)-Vertice1.getX()))+(normal.y*((camY+ Ray.y)-Vertice1.getY()))+(normal.z*((camZ+ Ray.z)-Vertice1.getZ()));
                    double vx = camX+ Ray.x*t;
                    double vy = camY+ Ray.y*t;
                    double vz = camZ+ Ray.z*t;
                    
                    if(vt.dotProduct(new Vector(vx,vy,vz),normal).equals(vt.dotProduct(new Vector(Vertice1.getX(),Vertice1.getY(),Vertice1.getZ()),normal)))
                    {
                        ImageToRender.setColorAt(x, y, Color.BLACK);
                    }  
                }
            }
        }
The VectorTools
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.util.ArrayList;

/**
 * Write a description of class ArrayListTools here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class VectorTools
{
   public Vector subtractVertices(Vertice v1, Vertice v2)
   {
       return new Vector(v1.getX() - v2.getX(),v1.getY() - v2.getY(),v1.getZ() - v2.getZ());
   }
   public Vector subtractVertices(double x1, double y1, double z1, double x2, double y2, double z2)
   {
       return new Vector(x1 - x2,y1 - y2,z1 - z2);
   }
   public Vector dotProduct(Vector v1, Vector v2)
   {
       return new Vector(
       v1.y*v2.z - v1.z*v2.y,
       v1.z*v2.x - v1.x*v2.z,
       v1.x*v2.y - v1.y*v2.x
       );
   }
}
The Vector class I have is pretty much just storing x,y, and z. Any additional information can easily be made available.
You need to login to post a reply.