The Hello World of 3D Rendering.

Co-authored-by: sigonasr2 <sigonasr2@gmail.com>
main
sigonasr2, Sig, Sigo 3 years ago
parent e0f6a3f225
commit 9014eb92e6
  1. 20
      src/sig/Panel.java
  2. 11
      src/sig/SigRenderer.java
  3. 46
      src/sig/Triangle.java

@ -1,9 +1,27 @@
package sig; package sig;
import javax.swing.JPanel; import javax.swing.JPanel;
import javax.vecmath.Vector3d;
import java.awt.Graphics; import java.awt.Graphics;
import java.awt.Color;
public class Panel extends JPanel{ public class Panel extends JPanel{
public void paintComponent(Graphics g) { public void paintComponent(Graphics g) {
Vector3d origin = new Vector3d(0,0,10);
for (int x=0;x<SigRenderer.SCREEN_WIDTH;x++) {
for (int y=0;y<SigRenderer.SCREEN_HEIGHT;y++) {
Vector3d dir = new Vector3d((-SigRenderer.SCREEN_WIDTH/2d+x),(-SigRenderer.SCREEN_HEIGHT/2d+y),SigRenderer.SCREEN_WIDTH);
if (SigRenderer.tri.rayTriangleIntersect(origin, dir)) {
g.setColor(Color.BLACK);
g.fillRect(SigRenderer.SCREEN_WIDTH-x,SigRenderer.SCREEN_HEIGHT-y,1,1);
//System.out.println("Intersects at "+origin+"/"+dir);
}
if (SigRenderer.tri2.rayTriangleIntersect(origin, dir)) {
g.setColor(Color.BLUE);
g.fillRect(SigRenderer.SCREEN_WIDTH-x,SigRenderer.SCREEN_HEIGHT-y,1,1);
//System.out.println("Intersects at "+origin+"/"+dir);
}
}
}
} }
} }

@ -6,20 +6,23 @@ import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener; import java.awt.event.MouseMotionListener;
public class SigRenderer implements MouseListener,MouseMotionListener{ public class SigRenderer implements MouseListener,MouseMotionListener{
Triangle tri; public static Triangle tri;
public static Triangle tri2;
public final static int SCREEN_WIDTH=1280; public final static int SCREEN_WIDTH=1280;
public final static int SCREEN_HEIGHT=720; public final static int SCREEN_HEIGHT=720;
public final static long TIMEPERTICK = 16666667l; public final static long TIMEPERTICK = 16666667l;
public static double DRAWTIME=0; public static double DRAWTIME=0;
Vector3d origin = new Vector3d(0,0,-10);
Vector3d dir = new Vector3d(0,0,1);
public void runGameLoop() { public void runGameLoop() {
} }
SigRenderer(JFrame f) { SigRenderer(JFrame f) {
tri = new Triangle(new Vector3d(-1,-1,0),new Vector3d(1,0,0),new Vector3d(0,2,0)); tri = new Triangle(new Vector3d(-1,-1,0),new Vector3d(1,-1,0),new Vector3d(0,2,0));
System.out.println(tri); tri2 = new Triangle(new Vector3d(-1,-1,120),new Vector3d(1,-1,120),new Vector3d(0,2,120));
System.out.println(tri.getNormal());
Panel p = new Panel(); Panel p = new Panel();

@ -20,7 +20,51 @@ public class Triangle {
BC.sub(C); BC.sub(C);
Vector3d crossP = new Vector3d(); Vector3d crossP = new Vector3d();
crossP.cross(AB,BC); crossP.cross(AB,BC);
crossP.normalize(); //crossP.normalize();
return crossP; return crossP;
} }
public double distanceFromOrigin() {
return getNormal().dot(A);
}
public boolean rayTriangleIntersect(Vector3d origin,Vector3d dir) {
Vector3d N = getNormal();
double NrayDir = N.dot(dir);
if (NrayDir<=0.001) { //Very small, so it's parallel.
return false;
}
double d = distanceFromOrigin();
double T=(getNormal().dot(origin)+d)/(NrayDir);
if (T<0) {return false;} //Triangle is behind the ray.
//System.out.println("Not behind.");
Vector3d scaleMult = (Vector3d)dir.clone();
scaleMult.scale(T);
Vector3d P = (Vector3d)origin.clone();
P.add(scaleMult);
Vector3d C;
Vector3d edge0 = (Vector3d)B.clone(); edge0.sub(A);
Vector3d vp0 = (Vector3d)P.clone(); vp0.sub(A);
C = new Vector3d(); C.cross(edge0,vp0);
if (N.dot(C)<0) {return false;}
Vector3d edge1 = (Vector3d)this.C.clone(); edge1.sub(B);
Vector3d vp1 = (Vector3d)P.clone(); vp1.sub(B);
C = new Vector3d(); C.cross(edge1,vp1);
if (N.dot(C)<0) {return false;}
Vector3d edge2 = (Vector3d)A.clone(); edge2.sub(this.C);
Vector3d vp2 = (Vector3d)P.clone(); vp2.sub(this.C);
C = new Vector3d(); C.cross(edge2,vp2);
if (N.dot(C)<0) {return false;}
return true;
}
} }

Loading…
Cancel
Save