Skip to content

Commit

Permalink
Merge branch 'dev' of https://github.com/kpekala/yagl into dev
Browse files Browse the repository at this point in the history
  • Loading branch information
kpekala committed Jun 3, 2022
2 parents 917e33f + 3211e84 commit 8ee8911
Show file tree
Hide file tree
Showing 7 changed files with 60 additions and 63 deletions.
4 changes: 2 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ plugins {
}

group 'com.example'
version '1.0-SNAPSHOT'
version '0.1.1'


repositories {
Expand Down Expand Up @@ -56,4 +56,4 @@ jlink {

jlinkZip {
group = 'distribution'
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.example.paint.app;

import com.example.paint.utils.FPSCounter;
import com.example.paint.utils.Input;
import com.example.paint.utils.OBJLoader;
import com.example.paint.yagl.model.Samples;
Expand All @@ -15,34 +16,29 @@
import javafx.application.Platform;
import javafx.scene.canvas.Canvas;
import javafx.scene.input.KeyCode;
import javafx.scene.input.MouseEvent;
import javafx.scene.robot.Robot;
import javafx.scene.text.Text;

import java.io.IOException;
import java.util.Arrays;

import static com.example.paint.yagl.utils.ColorUtils.defaultColor;


public class AppController {
public class SceneController {

public Canvas canvas;
public Text fpsCounter;
private Drawer drawer;
private final Scene scene = new Scene();
private final Camera camera = scene.getCamera();

private final int[] fpss = new int[10];
private int fpsIndex = 0;

public void initialize() {
drawer = new Drawer(new JavaFXDrawable(canvas), new Vector2f((float) canvas.getWidth(),(float)canvas.getHeight()));
try {
Model objModel = loadModelFromFile("/panda.obj", "/rock/material.lib");
objModel.move(new Vector3f(0,0.3f,5));
scene.addToScene(objModel);
setUpScene();
//setUpScene();
} catch (IOException e) {
System.out.println("Error when loading .obj file");
System.out.println(e.getMessage());
Expand Down Expand Up @@ -90,49 +86,19 @@ private void mainLoop() {
}

private void onUpdate() {
// Robot robot = new Robot();
// System.out.println(robot.getMousePosition());
float moveSpeed = 0.1f;
float rotateSpeed = 0.01f;
float rotateSpeed = 0.05f;
long s = System.currentTimeMillis();

if (Input.isPressed(KeyCode.R)) {
camera.rotate(Vector3f.up(rotateSpeed));
}
if (Input.isPressed(KeyCode.T)){
camera.rotate(Vector3f.down(rotateSpeed));
}
if (Input.isPressed(KeyCode.Y)){
scene.rotateCubes(Vector3f.up(rotateSpeed));
}
if(Input.isPressed(KeyCode.W)){
camera.move(Vector3f.forward(moveSpeed));
}
if (Input.isPressed(KeyCode.S)){
camera.move(Vector3f.back(moveSpeed));
}
if(Input.isPressed(KeyCode.A)){
camera.move(Vector3f.left(moveSpeed));
}
if (Input.isPressed(KeyCode.D)){
camera.move(Vector3f.right(moveSpeed));
}
if(Input.isPressed(KeyCode.UP)){
camera.rotate(Vector3f.left(rotateSpeed));
}
if (Input.isPressed(KeyCode.DOWN)){
camera.rotate(Vector3f.right(rotateSpeed));
}
if(Input.isPressed(KeyCode.LEFT)){
camera.rotate(Vector3f.down(rotateSpeed));
}
if (Input.isPressed(KeyCode.RIGHT)){
camera.rotate(Vector3f.up(rotateSpeed));
}

camera.move(Vector3f.forward(moveSpeed * Input.getAxis(Input.AxisType.VERTICAL)));
camera.move(Vector3f.right(moveSpeed * Input.getAxis(Input.AxisType.HORIZONTAL)));
camera.move(Vector3f.up(moveSpeed * Input.judge(KeyCode.DIGIT1, KeyCode.DIGIT2)));
scene.rotateAll(Vector3f.up(rotateSpeed * Input.judge(KeyCode.Q,KeyCode.E)));
scene.rotateAll(Vector3f.right(rotateSpeed * Input.judge(KeyCode.R,KeyCode.T)));
Platform.runLater(() ->{
draw();
updateFPSCounter(System.currentTimeMillis() - s);
var fps = FPSCounter.updateAndGetFPS(System.currentTimeMillis() - s);
fpsCounter.setText(fps + " fps");
});
}
private void draw() {
Expand All @@ -145,14 +111,4 @@ private void draw() {
// }
}

private void updateFPSCounter(long frameDuration) {
double diffSec = frameDuration / 1000f;
int fps = (int) (1/diffSec);
fpss[fpsIndex++] = fps;
if (fpsIndex == fpss.length){
int avg = Arrays.stream(fpss).sum()/fpss.length;
fpsCounter.setText(avg + " fps");
fpsIndex = 0;
}
}
}
20 changes: 20 additions & 0 deletions src/main/java/com/example/paint/utils/FPSCounter.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.example.paint.utils;

import java.util.Arrays;

public class FPSCounter {
private static final int[] fpss = new int[10];
private static int fpsIndex = 0;
private static int avgFPS = 0;

public static int updateAndGetFPS(long frameDuration) {
double diffSec = frameDuration / 1000f;
int fps = (int) (1/diffSec);
fpss[fpsIndex++] = fps;
if (fpsIndex == fpss.length){
avgFPS = Arrays.stream(fpss).sum()/fpss.length;
fpsIndex = 0;
}
return avgFPS;
}
}
21 changes: 21 additions & 0 deletions src/main/java/com/example/paint/utils/Input.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@

public class Input {

public enum AxisType{
HORIZONTAL, VERTICAL
}

private static App app;

private static HashMap<KeyCode,Boolean> keys = new HashMap<>();
Expand All @@ -26,4 +30,21 @@ public static void onKeyPressed(KeyCode keyCode){
public static void onKeyReleased(KeyCode keyCode){
keys.put(keyCode,false);
}

public static int getAxis(AxisType type){
return switch (type) {
case HORIZONTAL -> judge(KeyCode.D, KeyCode.A);
case VERTICAL -> judge(KeyCode.W, KeyCode.S);
};
}

public static int judge(KeyCode frontKey, KeyCode backKey) {
var key1 = isPressed(frontKey);
var key2 = isPressed(backKey);
if (key1 && !key2)
return 1;
if(key2 && !key1)
return -1;
return 0;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,15 +39,15 @@ public Vector3f reverse(){
public static Vector3f forward(float distance){
return new Vector3f(0,0,1 * distance);
}
public static Vector3f back(float distance){
return new Vector3f(0,0,-1 * distance);
}
public static Vector3f up(float distance){
return new Vector3f(0,1 * distance,0);
}
public static Vector3f down(float distance){
return new Vector3f(0,-1 * distance,0);
}
public static Vector3f back(float distance){
return new Vector3f(0,0,-1 * distance);
}

public static Vector3f right(float distance){
return new Vector3f(1 * distance,0,0);
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/com/example/paint/yagl/scene/Scene.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,13 @@ public List<Model> getDrawableModels() {
return models;
}

public void moveCubes(Vector3f vector3f) {
public void moveAll(Vector3f vector3f) {
for(var model: models){
model.move(vector3f);
}
}

public void rotateCubes(Vector3f vector3f) {
public void rotateAll(Vector3f vector3f) {
for(var model: models){
model.rotate(vector3f);
}
Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/hello-view.fxml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<?import javafx.scene.layout.VBox?>
<?import javafx.scene.text.Text?>

<VBox alignment="CENTER" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" style="-fx-background-color: #444444;" xmlns="http://javafx.com/javafx/11.0.1" xmlns:fx="http://javafx.com/fxml/1" fx:controller="com.example.paint.app.AppController">
<VBox alignment="CENTER" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" style="-fx-background-color: #444444;" xmlns="http://javafx.com/javafx/11.0.1" xmlns:fx="http://javafx.com/fxml/1" fx:controller="com.example.paint.app.SceneController">
<AnchorPane>
<Canvas fx:id="canvas" height="600.0" width="600.0" />
<Text fx:id="fpsCounter" fill="#04ff00" layoutX="14.0" layoutY="27.0" strokeType="OUTSIDE" strokeWidth="0.0" wrappingWidth="66.13671875" />
Expand Down

0 comments on commit 8ee8911

Please sign in to comment.