Ich habe letztens versucht, selber eine 3D-Rendering Klasse zu programmieren (Habe mich nicht unglaublich tief mit beschäftigt).
Doch jetzt ist die Pyramide oft so komisch gedreht, wenn ich die Focal Length nicht unglaublich hoch habe. Wie könnte man das ändern?
Doch jetzt ist die Pyramide oft so komisch gedreht, wenn ich die Focal Length nicht unglaublich hoch habe. Wie könnte man das ändern?
Java:
public class Matrix {
//[row][column]
public double[][] numbers;
public Matrix(double[][] numbers) {
this.numbers = numbers;
}
public Matrix multiply(Matrix other) {
if(other.numbers.length != this.numbers[0].length) return null;
Matrix newMatrix = new Matrix(new double[this.numbers[0].length][other.numbers.length]);
for(int row = 0; row < this.numbers.length; row++) {
for(int column = 0; column < this.numbers[row].length; column++) {
for(int otherColumn = 0; otherColumn < other.numbers[0].length; otherColumn++) {
newMatrix.numbers[row][otherColumn]
+= this.numbers[row][column] * other.numbers[column][otherColumn];
}
}
}
return newMatrix;
}
}
Code:
import Tools.Math.Matrix;
public class Vertex {
public Matrix absolutePosition = new Matrix(new double[1][3]);
public Matrix position = new Matrix(new double[1][3]);
public Matrix middle = new Matrix(new double[1][3]);
private Matrix rotation = new Matrix(new double[1][3]);
public Vertex(double x, double y, double z, double mx, double my, double mz) {
position.numbers[0][0] = x;
position.numbers[0][1] = y;
position.numbers[0][2] = z;
middle.numbers[0][0] = mx;
middle.numbers[0][1] = my;
middle.numbers[0][2] = mz;
absolutePosition.numbers[0][0] = x;
absolutePosition.numbers[0][1] = y;
absolutePosition.numbers[0][2] = z;
}
public void rotate(double xDeg, double yDeg, double zDeg) {
rotation.numbers[0][0] += xDeg;
rotation.numbers[0][1] += yDeg;
rotation.numbers[0][2] += zDeg;
rotateOnXAxis(xDeg);
rotateOnYAxis(yDeg);
rotateOnZAxis(zDeg);
}
public void rotateOnXAxis(double deg) {
Matrix yAndZ = getRotationOnAxis(position.numbers[0][1], position.numbers[0][2]);
yAndZ = yAndZ.multiply(getRotationMatrix(deg));
position.numbers[0][1] = yAndZ.numbers[0][0];
position.numbers[0][2] = yAndZ.numbers[0][1];
}
public void rotateOnYAxis(double deg) {
Matrix xAndZ = getRotationOnAxis(position.numbers[0][0], position.numbers[0][2]);
xAndZ = xAndZ.multiply(getRotationMatrix(deg));
position.numbers[0][0] = xAndZ.numbers[0][0];
position.numbers[0][2] = xAndZ.numbers[0][1];
}
public void rotateOnZAxis(double deg) {
Matrix xAndY = getRotationOnAxis(position.numbers[0][0], position.numbers[0][1]);
xAndY = xAndY.multiply(getRotationMatrix(deg));
position.numbers[0][0] = xAndY.numbers[0][0];
position.numbers[0][1] = xAndY.numbers[0][1];
}
private Matrix getRotationOnAxis(double firstPos, double secondPos) {
Matrix rotationOnAxisMatrix = new Matrix(new double[1][2]);
rotationOnAxisMatrix.numbers[0][0] = firstPos;
rotationOnAxisMatrix.numbers[0][1] = secondPos;
return rotationOnAxisMatrix;
}
private Matrix getRotationMatrix(double theta) {
Matrix rotationMatrix = new Matrix(new double[2][2]);
rotationMatrix.numbers[0][0] = Math.cos(theta);
rotationMatrix.numbers[0][1] = -Math.sin(theta);
rotationMatrix.numbers[1][0] = Math.sin(theta);
rotationMatrix.numbers[1][1] = Math.cos(theta);
return rotationMatrix;
}
public double getProjectedX(double focalLength) {
double projectedX = translate(position.numbers[0][0] + middle.numbers[0][0], focalLength);
System.out.println("x:" + projectedX);
return projectedX;
}
public double getProjectedY(double focalLength) {
double projectedY = translate(position.numbers[0][1] + middle.numbers[0][1], focalLength);
return projectedY;
}
private double translate(double xOrY, double focalLength) {
return (focalLength*xOrY)/(focalLength+position.numbers[0][2] + middle.numbers[0][2]);
}
}
Code:
public class Edge {
Vertex one, two;
public Edge(Vertex one, Vertex two) {
this.one = one;
this.two = two;
}
}
Code:
import java.awt.Graphics2D;
import java.awt.geom.Line2D;
import java.util.ArrayList;
import java.util.List;
public class Wireframe {
List<Vertex> vertexes;
List<Edge> edges;
private Matrix pos;
public Wireframe() {
this(0, 0, 0);
}
public Wireframe(double x, double y, double z) {
vertexes = new ArrayList<>();
edges = new ArrayList<>();
pos = new Matrix(new double[][] {{x, y, z}});
}
public void setPos(double x, double y, double z) {
pos.numbers[0][0] = x;
pos.numbers[0][1] = y;
pos.numbers[0][2] = z;
for(Vertex g: vertexes) {
g.middle.numbers[0][0] = x;
g.middle.numbers[0][0] = y;
g.middle.numbers[0][0] = z;
}
}
public void move(double x, double y, double z) {
pos.numbers[0][0] += x;
pos.numbers[0][1] += y;
pos.numbers[0][2] += z;
for(Vertex g: vertexes) {
g.middle.numbers[0][0] += x;
g.middle.numbers[0][1] += y;
g.middle.numbers[0][2] += z;
}
}
public void add(Edge add) {
edges.add(add);
if(!vertexes.contains(add.one))
vertexes.add(add.one);
if(!vertexes.contains(add.two))
vertexes.add(add.two);
}
public void draw(Graphics2D g2d, double focalLength) {
for(Edge g: edges) {
Line2D line = new Line2D.Double(g.one.getProjectedX(focalLength), g.one.getProjectedY(focalLength),
g.two.getProjectedX(focalLength), g.two.getProjectedY(focalLength));
g2d.draw(line);
}
}
public void turn(double xDeg, double yDeg, double zDeg) {
for(Vertex g: vertexes) {
g.rotate(Math.toRadians(xDeg), Math.toRadians(yDeg), Math.toRadians(zDeg));
}
}
}
Code:
import java.awt.Color;
import java.awt.Graphics2D;
import Tools.Game.GameWindow;
public class Test extends GameWindow {
Vertex a, b, c, d, e;
Edge ab, ac, bd, cd;
Edge ae, be, ce, de;
Wireframe frame;
public Test(int width, int hight, String name) {
super(width, hight, name);
setResizable(true);
init();
}
public static void main(String[] args) {
Test test = new Test(1000, 700, "test");
Thread mainThread = new Thread(test);
mainThread.start();
}
@Override
public void update(double dt) {
frame.move(0, 0, dt*1000);
frame.turn(0, dt*100, 0);
draw((Graphics2D) this.getGraphics());
}
@Override
public void init() {
frame = new Wireframe();
System.out.println(getWidth());
//**
a = new Vertex(-100, -100, 0, 500, 500, 500);
b = new Vertex(100, -100, 0, 500, 500, 500);
c = new Vertex(-100, 100, 0, 500, 500, 500);
d = new Vertex(100, 100, 0, 500, 500, 500);
e = new Vertex(0, 0, -100, 500, 500, 500);
//**/
ab = new Edge(a, b);
ac = new Edge(a, c);
bd = new Edge(b, d);
cd = new Edge(c, d);
ae = new Edge(a, e);
be = new Edge(b, e);
ce = new Edge(c, e);
de = new Edge(d, e);
frame.add(ab);
frame.add(ac);
frame.add(bd);
frame.add(cd);
frame.add(ae);
frame.add(be);
frame.add(ce);
frame.add(de);
frame.turn(90, 0, 0);
}
@Override
public void changeScene(int scene) {
}
@Override
public void draw(Graphics2D g2d) {
if(dbi == null) {
dbi = createImage(getWidth(), getHeight());
dbg = dbi.getGraphics();
}
renderOffScreen((Graphics2D) dbg);
g2d.drawImage(dbi, 0, 0, getWidth(), getHeight(), null);
}
@Override
public void renderOffScreen(Graphics2D g2d) {
g2d.setColor(Color.WHITE);
g2d.fillRect(0, 0, getWidth(), getHeight());
g2d.setColor(Color.BLACK);
frame.draw(g2d, 100);
}
}