OpenGL 3D Engine in Java

Work In Progress!

Wird bald weiter veröffentlicht

Klassen-Übersicht
Grundlegende Klassen:
Time
Input

Mathematische Klassen:
Vector2f
Vector3f
Matrix
Quaternions
Transformation

Render Klassen(OpenGL):
Window
Heart

GameEngine Klassen & Architekturen:
Scene
World
GameObject(Root-Object)
Game

Audio Klassen:

 

////////////////////////////////////////////////////////////////////////////////////////////////////////////////

Vektorenklassen sind obligatorisch in einer RenderingEngine

public class Vector2f
 {
 private float x;
 private float y;
public Vector2f(float x, float y)
 {
 this.x = x;
 this.y = y;
 }
public float length()
 {
 return (float)Math.sqrt(x * x + y * y);
 }
public float dot(Vector2f r)
 {
 return x * r.getX() + y * r.getY();
 }
public Vector2f normalized() //Kommentar
{
float length = length();

return new Vector2f(x / length, y / length);
}

 

public Vector2f lerp(Vector2f dest , float lerpFactor){    //Kommentar

return dest.sub(this).mul(lerpFactor).add(this);

}

 

public Vector2f rotate(float angle) //Kommentar
{
double rad = Math.toRadians(angle);
double cos = Math.cos(rad);
double sin = Math.sin(rad);

return new Vector2f((float)(x * cos - y * sin),(float)(x * sin + y * cos));
}

public Vector2f add(Vector2f r)
{
return new Vector2f(x + r.getX(), y + r.getY());
}

public Vector2f add(float r)
{
return new Vector2f(x + r, y + r);
}

public Vector2f sub(Vector2f r)
{
return new Vector2f(x - r.getX(), y - r.getY());
}

public Vector2f sub(float r)
{
return new Vector2f(x - r, y - r);
}

public Vector2f mul(Vector2f r)
{
return new Vector2f(x * r.getX(), y * r.getY());
}

public Vector2f mul(float r)
{
return new Vector2f(x * r, y * r);
}

public Vector2f div(Vector2f r)
{
return new Vector2f(x / r.getX(), y / r.getY());
}

public Vector2f div(float r)
{
return new Vector2f(x / r, y / r);
}

public Vector2f abs()
{
return new Vector2f(Math.abs(x), Math.abs(y));
}

public String toString()
{
return "(" + x + " " + y + ")";
}

public float getX()
{
return x;
}

public void setX(float x)
{
this.x = x;
}

public float getY()
{
return y;
}

public void setY(float y)
{
this.y = y;
}
}

////////////////////////////////////////////////////////////////////////////////////////////

import org.lwjgl.opengl.Display;    //OPENGL Fensterrender
import org.lwjgl.opengl.DisplayMode;

 

public class Window
{
public static void createWindow(int width, int height, String title)
{
Display.setTitle(title);
try
{
Display.setDisplayMode(new DisplayMode(width, height));
Display.create();

}
catch (LWJGLException e)
{
e.printStackTrace();
}
}

public static void render()
{
Display.update();
}

public static void dispose()
{
Display.destroy();

}

public static boolean isCloseRequested()
{
return Display.isCloseRequested();
}

public static int getWidth()
{
return Display.getDisplayMode().getWidth();
}

public static int getHeight()
{
return Display.getDisplayMode().getHeight();
}

public static String getTitle()
{
return Display.getTitle();
}

public Vector2f getCenter(){

return new Vector2f(getWidth()/2,getHeight()/2);
}
}

 

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////

public class Engine
{

private boolean isRunning;
private TestGame game;
//RENDERING (DIRECTX OR OPENGL)
//private D3Engine d3engine
//private OGEngine ogengine
private int width;
private int height;
private double frameTime;

public Engine(int width, int height, int framerate, TestGame game)
{

isRunning = false;
this.game =game;
this.width = width;
this.height = height;
this.frameTime = 1.0 / framerate;

}

public void createWindow(String title){

Window.createWindow(width, height, title);
this.renderingEngine = new RenderingEngine();
}

public void start()
{
if(isRunning)
return;

run();
}

public void stop()
{
if(!isRunning)
return;

isRunning = false;
}

private void run()
{
isRunning = true;

int frames = 0;
double frameCounter = 0;

//INIT

double lastTime = Time.getTime();
double unprocessedTime = 0;
Time.setDelta(frameTime);

while(isRunning)
{
boolean render = false;

double startTime = Time.getTime();
double passedTime = startTime - lastTime;
lastTime = startTime;

unprocessedTime += passedTime;
frameCounter += passedTime;

while(unprocessedTime > frameTime)
{

render = true;

unprocessedTime -= frameTime;

if(Window.isCloseRequested())
stop();

//INPUT

if(frameCounter >= 1.0)
{
System.out.println("Framerate : "+frames);

frames = 0;
frameCounter = 0;
}
}
if(render)
{

renderingEngine.render(game.getRootObject());
Window.render();
frames++;

}
else
{
try
{
Thread.sleep(1);
}
catch (InterruptedException e)
{
e.printStackTrace();
}
}
}

cleanUp();
}

private void cleanUp()
{
Window.dispose();
}

}