Advertisement
Guest User

Untitled

a guest
Feb 23rd, 2013
108
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package com.paul.persp;
  2.  
  3. import com.paul.persp.geom.Point4f;
  4. import com.paul.persp.gfx.Screen;
  5. import com.paul.persp.maths.Matrix4f;
  6.  
  7. public class Camera3D {
  8.     private Matrix4f projMat;
  9.     private Matrix4f viewMat;
  10.    
  11.     public Camera3D() {
  12.         projMat = new Matrix4f();
  13.         projMat.setIdentity();
  14.         viewMat = new Matrix4f();
  15.         viewMat.setIdentity();
  16.     }
  17.    
  18.     public void setPerspectiveView(float fov, float aspectRatio, float near, float far) {
  19.         float coTan2 = (float) (1.0f / Math.tan(Math.toRadians(fov * 0.5f)));
  20.         float yScale = coTan2;
  21.         float xScale = yScale / aspectRatio;
  22.        
  23.         projMat.set(0, xScale);
  24.         projMat.set(1, 0.0f);
  25.         projMat.set(2, 0.0f);
  26.         projMat.set(3, 0.0f);
  27.        
  28.         projMat.set(4, 0.0f);
  29.         projMat.set(5, yScale);
  30.         projMat.set(6, 0.0f);
  31.         projMat.set(7, 0.0f);
  32.        
  33.         projMat.set(8, 0.0f);
  34.         projMat.set(9, 0.0f);
  35.         projMat.set(10, -far / (near - far));
  36.         projMat.set(11, -1.0f);
  37.        
  38.         projMat.set(12, 0.0f);
  39.         projMat.set(13, 0.0f);
  40.         projMat.set(14, -near * far / (near - far));
  41.         projMat.set(15, 0.0f);
  42.     }
  43.    
  44.     public void renderVertices(Point4f[] vertices, Screen screen) {
  45.         for(int i = 0; i < vertices.length; i++) {
  46.             Point4f transPos = Point4f.multiply(Point4f.multiply(vertices[i], viewMat), projMat);
  47.             if(transPos.getX() < -1 || transPos.getX() > 1 || transPos.getY() < -1 || transPos.getY() > 1) continue;
  48.             int x = (int) (Main.WIDTH * 0.5 + transPos.getX() * Main.WIDTH * 0.5 / transPos.getZ());
  49.             int y = (int) (Main.HEIGHT * 0.5 + transPos.getY() * Main.HEIGHT * 0.5 / transPos.getZ());
  50.             screen.drawPoint(x, y, 0xffffffff);
  51.         }
  52.     }
  53. }
Advertisement
Advertisement
Advertisement
RAW Paste Data Copied
Advertisement