Java-Gaming.org    
Featured games (91)
games approved by the League of Dukes
Games in Showcase (580)
games submitted by our members
Games in WIP (500)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
  JavaGaming.org - Pastebin



Author: ziddia (posted 2014-01-20 19:31:11, viewed 70 times)

1   2   3   4   5   6   7   8   9   10   11   12   13   14   15   16   17   18   19   20   21   22   23   24   25   26   27   28   29   30   31   32   33   34   35   36   37   38   39   40   41   42   43   44   45   46   47   48   49   50   51   52   53   54   55   56   57   58   59   60   61   62   63   64   65   66   67   68   69   70   71   72   73   74   75   76   77   78   79   80   81   82   83   84   85   86   87   88   89   90   91   92   93   94   95   96   97   98   99   100   101   102   103   104   105   106   107   108   109   110   111   112   113   114   115   116   117   118   119   120   121   122   123   124   125   126   127   128   129   130   131   132   133   134   135   136   137   138   139   140   141   142   143   144   145   146   147   148   149   150   151   152   153   154   155   156   157   158   159   160   161   162   163   164   165   166   167   168   169   170   171   172   173   174   175   176   177   178   179   180   181   182   183   184   185   186   187   188   189   190   191   192   193   194   195   196   197   198   199   200   201   202   203   204   205   206   207   208   209   210   211   212   213   214   215   216   217   218   219   220   221   222   223  
package com.ziddia.game;

import java.nio.FloatBuffer;

import org.lwjgl.BufferUtils;
import org.lwjgl.opengl.GL20;
import org.lwjgl.util.vector.Matrix4f;
import org.lwjgl.util.vector.Vector3f;

/**
 * A 
 * 
 * @author Ziddia
 *
 */
public class Camera 
{
   
   Matrix4f projection;
   Matrix4f view;
   Matrix4f model;
   
   Vector3f cameraPos, cameraAngle;
   
   /** The locations in memory. */
   public int projectionLoc, viewLoc, modelLoc;
   
   // shader we be using yo
   int shaderID;
   
   /** Float buffer for the matrices */
   FloatBuffer matrixBuffer;
   
   /** FOV and aspect ratio, used for zooming. */
   float fov, aspect;
   
   /**
    * Create a new camera and set up the matrices.
    */
   public Camera()
   {
      projection = makeProjection(60F, Game.width / Game.height, 0.1F, 100F);
      view = new Matrix4f();
      model = new Matrix4f();
      
      this.fov = 60F;
      this.aspect = Game.width / Game.height;
      
      cameraPos = new Vector3f(0, 0, -1);
      cameraAngle = new Vector3f(0, 0, 0);
   }
   
   /**
    * Creates a projection matrix.
    * 
    * @param fov
    * @param aspect
    * @param near
    * @param far
    * @return
    */
   public static Matrix4f makeProjection(float fov, float aspect, float near, float far)
   {
      
      // Setup projection matrix
      Matrix4f projectionMatrix = new Matrix4f();

      float y_scale = Game.coTangent(Game.degreesToRadians(fov / 2f));
      float x_scale = y_scale / aspect;
      float frustum_length = far - near;

      projectionMatrix.m00 = x_scale;
      projectionMatrix.m11 = y_scale;
      projectionMatrix.m22 = -((far + near) / frustum_length);
      projectionMatrix.m23 = -1;
      projectionMatrix.m32 = -((2 * near * far) / frustum_length);
      projectionMatrix.m33 = 0;
      
      return projectionMatrix;
   }
   
   /**
    * Retrieves the locations of the shaders.
    */
   public void init(int shaderID)
   {
      this.shaderID = shaderID;
      projectionLoc = GL20.glGetUniformLocation(shaderID, "projectionMatrix");
      viewLoc = GL20.glGetUniformLocation(shaderID, "viewMatrix");
      modelLoc = GL20.glGetUniformLocation(shaderID, "modelMatrix");
      
      matrixBuffer = BufferUtils.createFloatBuffer(16);
   }
   
   /**
    * Updates the FOV.
    * @param newFOV
    */
   public void changeFOV(float newFOV)
   {
      this.fov = newFOV;
      
      float y_scale = Game.coTangent(Game.degreesToRadians(fov / 2f));
      float x_scale = y_scale / aspect;
      
      projection.m00 = x_scale;
      projection.m11 = y_scale;
   }
   

    /**
     * Add (x, y, z) to the cameraPos
     */
    public void addPosition(float x, float y, float z)
    {
        cameraPos.x += x;
        cameraPos.y += y;
        cameraPos.z += z;
    }

    /**
     * Add a cameraPos vector to the camera cameraPos
     */
    public void addPosition(Vector3f cameraPos)
    {
       addPosition(cameraPos.x, cameraPos.y, cameraPos.z);
    }

    /**
     * Add cameraAngle (rx, ry, rz) to the camera
     */
    public void addRotation(float rx, float ry, float rz)
    {
        cameraAngle.x += rx;
        cameraAngle.y += ry;
        cameraAngle.z += rz;
    }

    /**
     * Add a cameraAngle vector to the camera cameraAngle
     */
    public void addRotation(Vector3f cameraAngle)
    {
        addRotation(cameraAngle.x, cameraAngle.y, cameraAngle.z);
    }

    /**
     * Move the camera with an amount in a direction.
     */
    public void move(float amount, float direction)
    {
        cameraPos.z += amount * Math.sin(Math.toRadians(cameraAngle.y + 90 * direction));
        cameraPos.x += amount * Math.cos(Math.toRadians(cameraAngle.y + 90 * direction));
    }

    /**
     * Set the cameraPos of this Camera to (x, y, z)
     */
    public void setPosition(float x, float y, float z)
    {
        cameraPos.x = x;
        cameraPos.y = y;
        cameraPos.z = z;
    }

    /**
     * Set the cameraAngle of this camera to (rx, ry, rz)
     */
    public void setRotation(float rx, float ry, float rz)
    {
        cameraAngle.x = rx;
        cameraAngle.y = ry;
        cameraAngle.z = rz;
    }
   
   /**
    * Updates the camera angles.
    */
   public void update(Vector3f modelPos, Vector3f modelAngle, Vector3f modelScale)
   {
      // refresh matrices
      model.setIdentity();
      view.setIdentity();
      
      // rotate the camera
      Matrix4f.rotate((float) Math.toRadians(cameraAngle.x), new Vector3f(1, 0, 0), view, view);
      Matrix4f.rotate((float) Math.toRadians(cameraAngle.y), new Vector3f(0, 1, 0), view, view);
      Matrix4f.rotate((float) Math.toRadians(cameraAngle.z), new Vector3f(0, 0, 1), view, view);
      
      // update the view matrix
      Matrix4f.translate(cameraPos, view, view);
      
      // rotate
      Matrix4f.rotate(Game.degreesToRadians(modelAngle.z), new Vector3f(0, 0, 1), model, model);
      Matrix4f.rotate(Game.degreesToRadians(modelAngle.y), new Vector3f(0, 1, 0), model, model);
      Matrix4f.rotate(Game.degreesToRadians(modelAngle.x), new Vector3f(1, 0, 0), model, model);
      
      // translate
      Matrix4f.translate(modelPos, model, model);
      
      // scaling
      Matrix4f.scale(modelScale, model, model);
      
      // use shader
      GL20.glUseProgram(shaderID);
      
      // input data
      projection.store(matrixBuffer);
      matrixBuffer.flip();
      GL20.glUniformMatrix4(projectionLoc, false, matrixBuffer);
      
      view.store(matrixBuffer);
      matrixBuffer.flip();
      GL20.glUniformMatrix4(viewLoc, false, matrixBuffer);
      
      model.store(matrixBuffer);
      matrixBuffer.flip();
      GL20.glUniformMatrix4(modelLoc, false, matrixBuffer);
      
      GL20.glUseProgram(0);
   }

}





Dump your java code here :



Special syntax:
  • To highlight a line (yellow background), prefix it with '@@'
  • To indicate that a line should be removed (red background), prefix it with '-'
  • To indicate that a line should be added (green background), prefix it with '+'
  • To post multiple snippets, seperate them by '~~~~'
  EOF
 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

xsi3rr4x (50 views)
2014-04-15 18:08:23

BurntPizza (46 views)
2014-04-15 03:46:01

UprightPath (62 views)
2014-04-14 17:39:50

UprightPath (44 views)
2014-04-14 17:35:47

Porlus (60 views)
2014-04-14 15:48:38

tom_mai78101 (84 views)
2014-04-10 04:04:31

BurntPizza (142 views)
2014-04-08 23:06:04

tom_mai78101 (242 views)
2014-04-05 13:34:39

trollwarrior1 (201 views)
2014-04-04 12:06:45

CJLetsGame (208 views)
2014-04-01 02:16:10
List of Learning Resources
by SHC
2014-04-18 03:17:39

List of Learning Resources
by Longarmx
2014-04-08 03:14:44

Good Examples
by matheus23
2014-04-05 13:51:37

Good Examples
by Grunnt
2014-04-03 15:48:46

Good Examples
by Grunnt
2014-04-03 15:48:37

Good Examples
by matheus23
2014-04-01 18:40:51

Good Examples
by matheus23
2014-04-01 18:40:34

Anonymous/Local/Inner class gotchas
by Roquen
2014-03-11 15:22:30
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!