Код: Выделить всё
package def_pack;
import java.io.IOException;
import javax.swing.JFrame;
public class Main {
public static void main(String[] args) throws IOException {
JFrame f = new JFrame("3D Test");
f.setSize(1920, 1080);
f.setResizable(false);
f.setLocationRelativeTo(null);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
GamePanel gamePanel = new GamePanel();
f.add(gamePanel);
gamePanel.setSize(1920,1080);
gamePanel.startThread();
f.setVisible(true);
}
}
< /code>
код для геймпанели.package def_pack;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.io.IOException;
import javax.swing.JPanel;
public class GamePanel extends JPanel implements Runnable{
public Thread thr;
public scene s;
GamePanel() throws IOException{
s = new scene();
}
@Override
public void run() {
while (true) {
try {
Thread.sleep(1);
} catch (InterruptedException e) {e.printStackTrace();}
try {
update();
repaint();
} catch (IOException e) {e.printStackTrace();}
}
}
public void update() throws IOException {
s.update();
}
public void paintComponent(Graphics g) {
Graphics2D g2d= (Graphics2D)g;
g2d.clearRect(0, 0, 1920, 1080);
try {
s.draw(g2d);
} catch (InterruptedException e) {e.printStackTrace();}
}
public void startThread() {
thr = new Thread(this);
thr.start();
}
}
< /code>
код для класса Vertex3d: < /p>
package def_pack;
public class vertex3D {
public double x,y,z;
public double[] transformedCoords= {0,0,0};
vertex3D(double x, double y, double z){
this.x=x;
this.y=-y;
this.z=z;
}
public void update(double camX, double camY, double camZ) {
// get relative coordinates
this.transformedCoords[0]=this.x-camX;
this.transformedCoords[1]=this.y-camY;
this.transformedCoords[2]=this.z-camZ;
}
}
< /code>
Код для класса сцены: < /p>
package def_pack;
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.Polygon;
import java.awt.Rectangle;
import java.awt.geom.Area;
import java.io.IOException;
import java.util.ArrayList;
public class scene {
public double camX=0;
public double camY=0;
public double camZ=0;
public vertex3D[] testQuad = {new vertex3D(-3,-2,2),new vertex3D(-3,-2,4),new vertex3D(-3,3,4),new vertex3D(-3,3,2)};
public int meshesAmount;
public ArrayList meshes = new ArrayList();
public Rectangle screen = new Rectangle(0,0,1920,1080);
public int scrW=1920;
public int scrH=1080;
public int sizer=300;
scene() throws IOException {
meshes.add(testQuad);
meshesAmount=meshes.size();
}
public void update(){
camZ+=0.001;
for (int i=0;i
newX*=-1;
newY*=-1;
< /code>
или < /p>
meshes.get(i)[j].transformedCoords[2]=-meshes.get(i)[j].transformedCoords[2];
Изображение Quad от начала Quad, -> Ближай дистанцию 0 для ближайшего к Quad Quad Quad.>
Подробнее здесь: https://stackoverflow.com/questions/797 ... tex-vertex