Skip to content

Commit

Permalink
feat: use gradle application plugin and refactor keyboard event handler
Browse files Browse the repository at this point in the history
  • Loading branch information
JunioJsv committed Oct 24, 2022
1 parent 4b4c1b8 commit e944dbe
Show file tree
Hide file tree
Showing 17 changed files with 262 additions and 174 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
build
.idea
.gradle
*.log
*.log
.fleet
15 changes: 6 additions & 9 deletions build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
plugins {
application
distribution
id("org.jetbrains.kotlin.jvm") version ("1.3.61")
}

Expand All @@ -9,6 +11,10 @@ repositories {
mavenCentral()
}

application {
mainClassName = "juniojsv.engine.MainKt"
}

dependencies {
implementation("de.javagl:obj:0.3.0")
implementation("org.joml:joml:1.9.20")
Expand All @@ -22,15 +28,6 @@ dependencies {
implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk8")
}

tasks.jar {
manifest {
attributes["Main-Class"] = "juniojsv.engine.MainKt"
}
from(configurations.runtimeClasspath.get().map {
if (it.isDirectory) it else zipTree(it)
})
}

tasks.compileKotlin {
kotlinOptions.jvmTarget = "1.8"
}
Expand Down
166 changes: 87 additions & 79 deletions src/main/kotlin/juniojsv/engine/Engine.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package juniojsv.engine

import juniojsv.engine.features.entity.*
import juniojsv.engine.features.mesh.ObjMeshProvider
import juniojsv.engine.features.utils.KeyboardHandler
import juniojsv.engine.features.utils.Mesh
import juniojsv.engine.features.utils.Shaders
import juniojsv.engine.features.utils.Textures
Expand All @@ -11,97 +12,68 @@ import juniojsv.engine.features.window.Window
import org.joml.Vector3f
import org.lwjgl.glfw.GLFW
import org.lwjgl.opengl.GL11
import kotlin.math.cos
import kotlin.math.sin
import kotlin.random.Random

class Engine(resolution: Resolution) : Window(resolution) {
private lateinit var light: Light
private lateinit var drawables: MutableList<IRender>
private lateinit var camera: Camera
private lateinit var sum: IRender

override val title: String = "juniojsv.engine"

override fun onCreate() {

camera = Camera(Vector3f(0f), this)
drawables = mutableListOf()
light = Light(Vector3f(0f, 1000f, 0f))

val scale = 2f
private val keyboard = KeyboardHandler()
private var sky: IRender? = null
private var terrain: IRender? = null
private var light: Being? = null

override fun onCreate(context: IRenderContext) {
onSetupKeyBoard(context)
GL11.glEnable(GL11.GL_DEPTH_TEST)
GL11.glEnable(GL11.GL_CULL_FACE)
GL11.glCullFace(GL11.GL_BACK)
GL11.glClearColor(0f, 0f, 0f, 1f)
Mesh.CUBEMAP_SHELL.decode { mesh ->
drawables.add(SkyBox(mesh, Textures.SKYBOX_DEFAULT_CUBEMAP, Shaders.SKYBOX_EFFECT, 500f * scale))
sky = SkyBox(mesh, Textures.SKYBOX_DEFAULT_CUBEMAP, Shaders.SKYBOX_PROGRAM, 500000f)
}

ObjMeshProvider("mesh/terrain.obj").decode { mesh ->
drawables.add(
Being(
mesh,
null,
Shaders.DEFAULT_EFFECT,
Vector3f(0f, -150f * scale, 0f),
scale = scale
)
terrain = Being(
mesh,
Textures.TERRAIN_TEXTURE,
Shaders.TERRAIN_PROGRAM,
Vector3f(0f, -150000f, 0f),
scale = 1000f
)
}

context.setCurrentAmbientLight(
Light(
Vector3f(0f, 25850f, 540000f),
Vector3f(.85f, .35f, .1f)
)
)
ObjMeshProvider("mesh/sphere.obj").decode { mesh ->
sum = Being(
light = Being(
mesh,
null,
Shaders.DEPTH_EFFECT,
light.position,
scale = 100f
Shaders.LIGHT_SHADER,
context.getAmbientLight()!!.position,
scale = 85000f
)
repeat(18) {
val position = Vector3f(
Random.nextFloat() * 500f - 250f,
Random.nextFloat() * 256f,
Random.nextFloat() * 500f - 250f
)
drawables.add(
Being(
mesh,
null,
Shaders.DEFAULT_EFFECT,
position,
scale = Random.nextFloat() * 5f
)
)
}
}

GL11.glEnable(GL11.GL_DEPTH_TEST)
GL11.glEnable(GL11.GL_CULL_FACE)
GL11.glCullFace(GL11.GL_BACK)
GL11.glClearColor(1f, 1f, 1f, 1f)
}

override fun draw(context: IRenderContext) {
GL11.glClear(GL11.GL_COLOR_BUFFER_BIT)
GL11.glClear(GL11.GL_DEPTH_BUFFER_BIT)

val time = (GLFW.glfwGetTime() / 8)
sky?.render(context)
light?.render(context)
terrain?.render(context)

light.position.set(
Vector3f(
cos(time).toFloat() * 1000f,
-sin(time).toFloat() * 1000f,
0f
)
)

for (entity in drawables) {
entity.render(context, camera, light)
}
sum.render(context, camera, light)
keyboard.pump(context)
}

override fun onCursorOffsetEvent(context: IRenderContext, offsetX: Double, offsetY: Double) {
val delta = context.getDelta()
camera.rotate((offsetX * 10f) * delta, (offsetY * 10f) * delta)
override fun onCursorOffsetEvent(context: IRenderContext, x: Double, y: Double) {
val delta = context.getDelta().toFloat()
context.getCamera().rotation.add(
(x.toFloat() * 10f) * delta,
(y.toFloat() * 10f) * delta,
0f
)
GLFW.glfwSetCursorPos(
getWindowContext().id,
(getResolution().width / 2).toDouble(),
Expand All @@ -111,18 +83,54 @@ class Engine(resolution: Resolution) : Window(resolution) {

override fun onMouseButtonEvent(button: Int, action: Int, mods: Int) {}

override fun onKeyBoardEvent(context: IRenderContext, key: Int, code: Int, action: Int, mods: Int) {
val speed = 500f
val delta = context.getDelta().toFloat()
when (key) {
GLFW.GLFW_KEY_ESCAPE ->
GLFW.glfwSetWindowShouldClose(getWindowContext().id, true)
GLFW.GLFW_KEY_W -> camera.move(Camera.CameraMovement.FORWARD, speed * delta)
GLFW.GLFW_KEY_A -> camera.move(Camera.CameraMovement.LEFT, speed * delta)
GLFW.GLFW_KEY_S -> camera.move(Camera.CameraMovement.BACKWARD, speed * delta)
GLFW.GLFW_KEY_D -> camera.move(Camera.CameraMovement.RIGHT, speed * delta)
GLFW.GLFW_KEY_Q -> camera.move(Camera.CameraMovement.UP, speed * delta)
GLFW.GLFW_KEY_E -> camera.move(Camera.CameraMovement.DOWN, speed * delta)
override fun onKeyBoardEvent(context: IRenderContext, key: Int, code: Int, action: Int, mods: Int) =
keyboard.handle(key, action)

private fun onSetupKeyBoard(context: IRenderContext) = context.getCamera().also { camera ->
val speed = 10000f
with(keyboard) {
setKeyAction(GLFW.GLFW_KEY_ESCAPE) {
GLFW.glfwSetWindowShouldClose(
getWindowContext().id,
true
)
}
setKeyAction(GLFW.GLFW_KEY_W) { delta ->
camera.move(
Camera.CameraMovement.FORWARD,
speed * delta.toFloat()
)
}
setKeyAction(GLFW.GLFW_KEY_A) { delta ->
camera.move(
Camera.CameraMovement.LEFT,
speed * delta.toFloat()
)
}
setKeyAction(GLFW.GLFW_KEY_S) { delta ->
camera.move(
Camera.CameraMovement.BACKWARD,
speed * delta.toFloat()
)
}
setKeyAction(GLFW.GLFW_KEY_D) { delta ->
camera.move(
Camera.CameraMovement.RIGHT,
speed * delta.toFloat()
)
}
setKeyAction(GLFW.GLFW_KEY_Q) { delta ->
camera.move(
Camera.CameraMovement.UP,
speed * delta.toFloat()
)
}
setKeyAction(GLFW.GLFW_KEY_E) { delta ->
camera.move(
Camera.CameraMovement.DOWN,
speed * delta.toFloat()
)
}
}
}
}
29 changes: 9 additions & 20 deletions src/main/kotlin/juniojsv/engine/features/entity/Being.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,33 +6,20 @@ import juniojsv.engine.features.texture.TwoDimensionTexture
import juniojsv.engine.features.window.IRenderContext
import org.joml.Matrix4f
import org.joml.Vector3f
import org.lwjgl.glfw.GLFW
import org.lwjgl.opengl.GL11

class Being(
private val mesh: Mesh,
private val texture: TwoDimensionTexture?,
private val shader: ShadersProgram?,
private val position: Vector3f = Vector3f(0f),
private val rotation: Vector3f = Vector3f(0f),
@Suppress("UNUSED_PARAMETER")
val position: Vector3f = Vector3f(0f),
@Suppress("UNUSED_PARAMETER")
val rotation: Vector3f = Vector3f(0f),
var scale: Float = 1f
) : IRender {

fun move(offsetX: Float, offsetY: Float, offsetZ: Float, increment: Boolean = false) {
with(position) {
x = if (increment) x + offsetX else offsetX
y = if (increment) y + offsetY else offsetY
z = if (increment) z + offsetZ else offsetZ
}
}

fun rotate(offsetX: Float, offsetY: Float, offsetZ: Float, increment: Boolean = false) {
with(rotation) {
x += if (increment) x + offsetX else offsetX
y += if (increment) y + offsetY else offsetY
z += if (increment) z + offsetZ else offsetZ
}
}

private fun transformation(): Matrix4f = Matrix4f()
.apply {
translate(position)
Expand All @@ -42,8 +29,10 @@ class Being(
scale(scale)
}

override fun render(context: IRenderContext, camera: Camera, light: Light) {
override fun render(context: IRenderContext) {
val transformation = transformation()
val camera = context.getCamera()
val light = context.getAmbientLight()!!

if (shader != null) {
with(shader) {
Expand All @@ -55,7 +44,7 @@ class Being(
putUniform("transformation", transformation)
putUniform("light_position", light.position)
putUniform("light_color", light.color)
putUniform("fog_color", light.color)
putUniform("time", GLFW.glfwGetTime().toFloat())

putUniform("has_texture", if (texture != null) 1 else 0)
context.setCurrentTexture(texture)
Expand Down
21 changes: 9 additions & 12 deletions src/main/kotlin/juniojsv/engine/features/entity/Camera.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,17 @@ import kotlin.math.cos
import kotlin.math.sin
import kotlin.math.tan

class Camera(private val position: Vector3f, private val window: Window) {
var fov = 90f
class Camera(
@Suppress("UNUSED_PARAMETER")
val position: Vector3f,
private val window: Window
) {
var fov = 120f
var near = .1f
var far = 10000f
var far = 10000000f

private val rotation: Vector3f = Vector3f(0f)
@Suppress("UNUSED_PARAMETER")
val rotation: Vector3f = Vector3f(0f)

enum class CameraMovement {
FORWARD,
Expand All @@ -23,8 +28,6 @@ class Camera(private val position: Vector3f, private val window: Window) {
DOWN
}

fun getPosition() = position

fun move(direction: CameraMovement, speed: Float) {
with(position) {
val sinRotX = sin(rotation.x) * speed
Expand Down Expand Up @@ -55,12 +58,6 @@ class Camera(private val position: Vector3f, private val window: Window) {
}
}


fun rotate(offsetX: Double, offsetY: Double) {
rotation.x += offsetX.toFloat()
rotation.y += offsetY.toFloat()
}

fun projection(): Matrix4f = Matrix4f().apply {
window.getResolution().also { resolution ->
val ratio = resolution.width.toFloat() / resolution.height.toFloat()
Expand Down
2 changes: 1 addition & 1 deletion src/main/kotlin/juniojsv/engine/features/entity/IRender.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@ package juniojsv.engine.features.entity
import juniojsv.engine.features.window.IRenderContext

interface IRender {
fun render(context: IRenderContext, camera: Camera, light: Light)
fun render(context: IRenderContext)
}
Loading

0 comments on commit e944dbe

Please sign in to comment.