Skip to content

Commit

Permalink
Add files via upload
Browse files Browse the repository at this point in the history
start
  • Loading branch information
justinchang1124 authored Sep 12, 2020
1 parent 72e5281 commit b6e6d61
Show file tree
Hide file tree
Showing 3 changed files with 546 additions and 0 deletions.
202 changes: 202 additions & 0 deletions Central.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,202 @@
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.KeyEvent;
import java.util.ArrayList;

/**
* The goal of this project is to explore 3D rendering in Java.
*
* @author Justin C
*/
public class Central extends Controller
{
/**
* Stores whether shadows is enabled for all shadow-bound Models.
*/
public static boolean shadows;

/**
* Used to count the number of triangles being rendered.
*/
private int trisCount, updateTris, pipelineSize;

/**
* Stores GUI info.
*/
private boolean paused, showHUD;

/**
* Resets constants.
*/
public void reset()
{
shadows = false;
paused = false;
showHUD = true;
Writer.reset();
Movement.reset();
Rotation.reset();
}

public void initialize()
{
System.out.println(Arguments.INSTRUCTIONS);
Arguments.initialize();
reset();
}

public void tick(long frameCount)
{
// Triangle counter.
updateTris %= 60;
updateTris++;
if (updateTris == 60)
trisCount = pipelineSize;

if (paused)
return;

// Camera controls.
if (mouseOver(0, 0, 200, Writer.HEIGHT))
Rotation.slowL();
if (mouseOver(Writer.WIDTH - 200, 0, 200, Writer.HEIGHT))
Rotation.slowR();
if (mouseOver(0, 0, Writer.WIDTH, 200))
Rotation.slowU();
if (mouseOver(0, Writer.HEIGHT - 200, Writer.WIDTH, 200))
Rotation.slowD();

if (mouseOver(0, 0, 100, Writer.HEIGHT))
Rotation.fastL();
if (mouseOver(Writer.WIDTH - 100, 0, 100, Writer.HEIGHT))
Rotation.fastR();
if (mouseOver(0, 0, Writer.WIDTH, 100))
Rotation.fastU();
if (mouseOver(0, Writer.HEIGHT - 100, Writer.WIDTH, 100))
Rotation.fastD();

for (Model p : Model.pipeline)
p.tick();

for (AnimatedModel p : AnimatedModel.pipeline)
p.tick();

// All other tick methods.
Rotation.tick();
Movement.tick(Rotation.heading);
}

public void render(Graphics g, long frameCount)
{
Graphics2D g2 = (Graphics2D) g;

// Draws the background.
g2.setColor(Color.black);
g2.fillRect(0, 0, Writer.WIDTH, Writer.HEIGHT);

// The list of all triangles to be rendered.
ArrayList<Triangle> pipeline = new ArrayList<Triangle>();

for (Model p : Model.pipeline)
p.addTo(pipeline, shadows);

for (AnimatedModel p : AnimatedModel.pipeline)
p.addTo(pipeline, shadows);

// Gets the number of triangle to be rendered.
pipelineSize = pipeline.size();

// Draws all entities.
g2.drawImage(
Triangle.getImage(pipeline,
new Basis(Movement.pos, Rotation.pitch, Rotation.heading),
Writer.WIDTH, Writer.HEIGHT,
Rotation.near, Rotation.far, Rotation.zoom), 0, 0, null);

if (showHUD)
{
// A text array that stores HUD info.
String[] textBox = {
"FPS: " + frameCount,
"Position: " + Movement.pos,
"Shadows: " + shadows,
"Heading / Pitch: " + Matrix.round(Rotation.heading, 3) +
" / " + Matrix.round(Rotation.pitch, 3),
"View Length / Zoom: " + Matrix.round(Rotation.near, 3) +
" / " + Matrix.round(Rotation.zoom, 3),
"Triangle Count: " + trisCount
};

// Draws the HUD.
for (int i = 0; i < textBox.length; i++)
Writer.showText(g2, textBox[i], 10, 20 * i + 20);

// Draws the center pointer.
g2.setColor(Color.white);
g2.fillRect(Writer.WIDTH / 2 - 3, Writer.HEIGHT / 2 - 3, 8, 8);
g2.setColor(Color.black);
g2.fillRect(Writer.WIDTH / 2 - 2, Writer.HEIGHT / 2 - 2, 6, 6);
}

if (paused)
{
// Pause GUI.
g2.setColor(Color.white);
g2.fillRect(Writer.WIDTH / 2 - 200, 400, 400, 128);
g2.setColor(Color.gray);
g2.fillRect(Writer.WIDTH / 2 - 190, 410, 380, 108);
Writer.setMainFont("times new roman", 0, 80);
Writer.showText(g2, "PAUSED", 490);
Writer.setMainFont("times new roman", 0, 14);
}
}

public void keyPressed()
{
Movement.pressKeys(keyCode);
}

public void keyTyped()
{

}

public void keyReleased()
{
Movement.releaseKeys(keyCode);
Rotation.releaseKeys(keyCode);

if (keyCode == KeyEvent.VK_E)
showHUD = !showHUD;
if (keyCode == KeyEvent.VK_CONTROL)
paused = !paused;
}

public void wheelInput()
{
Rotation.scroll(wheel);
}

public void mouseInput(int eventMouse)
{
if (clicks <= 0 || eventMouse != 1)
return;

if (!paused)
{
// Left clicking in renderer.
if (button == 1)
reset();

// Right clicking in renderer.
if (button == 3)
shadows = !shadows;
}
}

public static void main(String[] args)
{
new Loader(Writer.WIDTH, Writer.HEIGHT, "3D Rendering in Java", 60, new Central());
}
}
160 changes: 160 additions & 0 deletions Controller.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,160 @@
import java.awt.Graphics;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.awt.event.MouseWheelEvent;
import java.awt.event.MouseWheelListener;

/**
* Organizes mouse & keyboard inputs and ensures that every subclass has
* initialize, tick, render, and input methods.
*
* @author Justin C
*/
public abstract class Controller
implements KeyListener, MouseMotionListener, MouseWheelListener, MouseListener
{
/**
* The Virtual Keyboard key that was most recently interacted with.
*/
public int keyCode;

/**
* (xM, yM) corresponds to the coordinates of the mouse.
*/
public int xM, yM;

/**
* The number of times that the mouse has clicked. The interval that must pass
* before this number resets to 0 depends on the system.
*/
public int clicks;

/**
* The mouse button that was most recently interacted with.
* (1 = left mouse button, 2 = scroll wheel button, 3 = right mouse button)
*/
public int button;

/**
* Is -1 or 1, depending on the direction of the most recent wheel rotation.
* The action that constitutes rotating the mouse wheel "up" or "down" depends
* on the system.
*/
public int wheel;

public abstract void initialize();

public abstract void tick(long frameCount);

public abstract void render(Graphics g, long frameCount);

public abstract void keyPressed();

public abstract void keyTyped();

public abstract void keyReleased();

public abstract void wheelInput();

/**
* Manages all mouse input, subdivided into 7 events by the eventDescription.
*
* @param eventDescription
* Is -3 when the mouse enters a window. Is 3 when the mouse exits a window.
* Is -2 when the mouse is moved. Is 2 when the mouse is dragged. Is -1 when
* the mouse is pressed. Is 1 when the mouse is released. Is 0 when the mouse
* is pressed and released.
*/
public abstract void mouseInput(int eventDescription);

/**
* Detects if the mouse is within the rectangle that has a top-left corner of
* (x, y) and dimensions (w, h).
*/
public boolean mouseOver(int x, int y, int w, int h)
{
return xM > x && xM < x + w && yM > y && yM < y + h;
}

/*
* All subsequent methods take a KeyEvent, MouseWheelEvent, or MouseEvent as a
* parameter. The event's traits are used to adjust the fields of this class
* accordingly and call the abstract method associated with the event.
*/
public void keyPressed(KeyEvent e)
{
keyCode = e.getKeyCode();
keyPressed();
}

public void keyTyped(KeyEvent e)
{
keyCode = e.getKeyCode();
keyTyped();
}

public void keyReleased(KeyEvent e)
{
keyCode = e.getKeyCode();
keyReleased();
}

public void mouseWheelMoved(MouseWheelEvent e)
{
wheel = e.getWheelRotation();
wheelInput();
}

public void setMouseValues(MouseEvent e)
{
xM = e.getX();
yM = e.getY();
button = e.getButton();
clicks = e.getClickCount();
}

public void mouseEntered(MouseEvent e)
{
setMouseValues(e);
mouseInput(-3);
}

public void mouseExited(MouseEvent e)
{
setMouseValues(e);
mouseInput(3);
}

public void mouseMoved(MouseEvent e)
{
setMouseValues(e);
mouseInput(-2);
}

public void mouseDragged(MouseEvent e)
{
setMouseValues(e);
mouseInput(2);
}

public void mousePressed(MouseEvent e)
{
setMouseValues(e);
mouseInput(-1);
}

public void mouseReleased(MouseEvent e)
{
setMouseValues(e);
mouseInput(1);
}

public void mouseClicked(MouseEvent e)
{
setMouseValues(e);
mouseInput(0);
}
}
Loading

0 comments on commit b6e6d61

Please sign in to comment.