Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add additional tests and comments #223

Merged
merged 12 commits into from
Jun 9, 2024
3 changes: 3 additions & 0 deletions src/main/kotlin/solve/rendering/canvas/OpenGLCanvas.kt
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ import solve.rendering.engine.utils.plus
import solve.rendering.engine.utils.toFloatVector
import com.huskerdev.openglfx.canvas.GLCanvas as OpenGLFXCanvas

/**
* Encapsulates initialization logic of the OpenGLFX library component.
*/
abstract class OpenGLCanvas {
val canvas: OpenGLFXCanvas = OpenGLFXCanvas(LWJGLExecutor.LWJGL_MODULE, flipY = true)

Expand Down
17 changes: 14 additions & 3 deletions src/main/kotlin/solve/rendering/canvas/SceneCanvas.kt
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,17 @@ import solve.utils.structures.DoublePoint
import tornadofx.*
import solve.rendering.engine.scene.Scene as EngineScene

/**
* Aggregates logic related to scene control.
* Provides interface for managing frames and changing visualization settings.
*/
class SceneCanvas : OpenGLCanvas() {
val displayScale: Float
get() = canvas.scene?.window?.renderScaleX?.toFloat() ?: 1f

val scaledIdentityFramesSizeScale: Float
get() = IdentityFramesSizeScale * displayScale

private var sceneController: SceneController? = null
private var engineScene: EngineScene? = null

Expand Down Expand Up @@ -78,6 +88,7 @@ class SceneCanvas : OpenGLCanvas() {

init {
initializeCanvasEvents()
ServiceLocator.registerService(this)
}

fun setNewScene(scene: Scene) {
Expand Down Expand Up @@ -158,8 +169,8 @@ class SceneCanvas : OpenGLCanvas() {
val canvasScreenPosition = canvas.getScreenPosition()
contextMenu.show(
canvas.scene.window,
canvasScreenPosition.x + screenPoint.x.toDouble(),
canvasScreenPosition.y + screenPoint.y.toDouble()
canvasScreenPosition.x + screenPoint.x.toDouble() / displayScale,
canvasScreenPosition.y + screenPoint.y.toDouble() / displayScale
)
}
}
Expand Down Expand Up @@ -446,7 +457,7 @@ class SceneCanvas : OpenGLCanvas() {
)
}
val getScreenScale = {
window.camera.zoom.toDouble() / IdentityFramesSizeScale
window.camera.zoom.toDouble() / scaledIdentityFramesSizeScale
}
val associationAdorner = AssociationAdorner(
framesSize.x.toDouble(),
Expand Down
4 changes: 4 additions & 0 deletions src/main/kotlin/solve/rendering/engine/camera/Camera.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ import solve.rendering.engine.utils.plus
import solve.rendering.engine.utils.times
import solve.rendering.engine.utils.toFloatVector

/**
* Used to move around the scene and scale it.
* Provides methods for calculating projection matrices that are used in shaders.
*/
class Camera(var position: Vector2f = Vector2f(), zoom: Float = 1f) {
var zoom: Float = zoom
private set(value) {
Expand Down
32 changes: 0 additions & 32 deletions src/main/kotlin/solve/rendering/engine/components/Sprite.kt

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ import org.lwjgl.opengl.GL11.GL_LINES
import org.lwjgl.opengl.GL11.GL_POINTS
import org.lwjgl.opengl.GL11.GL_TRIANGLES

/**
* Used to draw with EBO in OpenGL.
*/
enum class PrimitiveType(
val verticesNumber: Int,
val openGLPrimitive: Int,
Expand Down
15 changes: 14 additions & 1 deletion src/main/kotlin/solve/rendering/engine/core/batch/RenderBatch.kt
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,13 @@ import solve.rendering.engine.core.texture.Texture
import solve.rendering.engine.shader.ShaderAttributeType
import solve.rendering.engine.utils.toList

/**
* Used to draw similar objects in a single draw call.
* @param maxBatchSize maximum allowed number of the objects in the batch.
* @param zIndex depth index of the batch.
* @param primitiveType used to determine the type of the drawing primitives in EBO.
* @param attributes used to specify the attributes of each vertex.
*/
open class RenderBatch(
private val maxBatchSize: Int,
val zIndex: Int,
Expand Down Expand Up @@ -58,25 +65,30 @@ open class RenderBatch(
textures.clear()
}

// Compares batches according to their depth.
override fun compareTo(other: RenderBatch): Int = batchComparator.compare(this, other)

// Binds vertices data, attributes data and textures before the rendering.
fun bind() {
glBindVertexArray(vaoID)
attributes.forEachIndexed { index, _ -> glEnableVertexAttribArray(index) }
textures.forEachIndexed { index, texture -> texture.bindToSlot(index + 1) }
}

// Unbinds vertices data, attributes data and textures after the rendering.
fun unbind() {
attributes.forEachIndexed { index, _ -> glDisableVertexAttribArray(index) }
textures.forEach { texture -> texture.unbind() }
glBindVertexArray(0)
}

// Cleans the vertices data buffer.
fun rebuffer() {
glBindBuffer(GL_ARRAY_BUFFER, vboID)
glBufferSubData(GL_ARRAY_BUFFER, 0, verticesDataBuffer)
}

// Deletes the batch buffers.
fun deleteBuffers() {
glDeleteBuffers(vboID)
glDeleteBuffers(eboID)
Expand Down Expand Up @@ -150,6 +162,7 @@ open class RenderBatch(
}
}

// Used to generate vertex indices to specify an order of their drawing.
private fun generateElementsIndices(): IntArray {
val elementsBuffer = IntArray(maxBatchSize * primitiveType.drawingOrderElementsNumber)
for (i in 0 until maxBatchSize) {
Expand Down Expand Up @@ -190,7 +203,7 @@ open class RenderBatch(
}

companion object {
private const val MaxTexturesNumber = 7
const val MaxTexturesNumber = 7

private val batchComparator = Comparator.comparingInt<RenderBatch> { it.zIndex }
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
package solve.rendering.engine.core.input

import javafx.scene.robot.Robot
import org.joml.Vector2i
import solve.rendering.engine.utils.minus
import solve.rendering.engine.utils.pointToSegmentDistance
import solve.rendering.engine.utils.toFloatVector
import solve.rendering.engine.utils.toVector2i
import solve.scene.model.Landmark
import java.awt.Color
import java.awt.MouseInfo

/**
* Used to handle mouse click on landmarks.
*/
object MouseInputHandler {
private const val LineHandledDistanceMultiplier = 4f
private const val PointHandledDistanceMultiplier = 1.5f
Expand Down Expand Up @@ -62,17 +62,4 @@ object MouseInputHandler {

return minKeypointDistanceIndex
}

fun getClickedPixelColor(): Color {
val pointerLocation = MouseInfo.getPointerInfo().location
val robot = Robot()

val javaFXColor = robot.getPixelColor(pointerLocation.x.toDouble(), pointerLocation.y.toDouble())

return Color(
(javaFXColor.red * ColorSegmentMaxValue).toInt(),
(javaFXColor.green * ColorSegmentMaxValue).toInt(),
(javaFXColor.blue * ColorSegmentMaxValue).toInt()
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ import kotlin.math.abs
import kotlin.math.max
import kotlin.math.min

/**
* Used to draw the frames.
* Encapsulates logic to provide virtualization at the data layer.*
*/
class FramesRenderer(
window: Window
) : Renderer(window) {
Expand Down Expand Up @@ -165,6 +169,7 @@ class FramesRenderer(
}
}

// Updates the buffers with actual frames textures.
private fun updateBuffersTextures() {
val cameraGridCellPosition = getScreenTopLeftGridCellPosition()
if (cameraGridCellPosition != cameraLastGridCellPosition) {
Expand All @@ -173,6 +178,7 @@ class FramesRenderer(
cameraLastGridCellPosition = cameraGridCellPosition
}

// Defines textures that became visible in the last draw call and uploads them to the buffers.
private fun loadNewTexturesToBuffers(cameraGridCellPosition: Vector2i) {
val cameraPosition = Vector2i(cameraGridCellPosition)
cameraPosition.x.coerceIn(0 until gridWidth)
Expand Down Expand Up @@ -226,6 +232,7 @@ class FramesRenderer(
return framesRect
}

// Uploads the rect of frames (that was taken from frames selection) to the buffers.
private fun loadRectFramesToBuffers(framesRect: IntRect) {
val rectFrames = getFramesAtRect(framesRect)

Expand All @@ -250,6 +257,7 @@ class FramesRenderer(
}
}

// Returns the integer position of the frame cell located in the top-left corner of the screen.
private fun getScreenTopLeftGridCellPosition(): Vector2i {
val cameraGridCellPosition = getCameraCellPosition()
return (cameraGridCellPosition - Vector2f(buffersSize) / 2f).toIntVector()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ import solve.scene.model.Landmark
import solve.scene.model.Layer
import solve.scene.model.Scene

/**
* The base class for all landmarks renderers.
* Encapsulates logic to provide virtualization at the data layer.
*/
abstract class LandmarkLayerRenderer(
window: Window,
protected val getScene: () -> Scene?
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ import solve.rendering.engine.utils.toVector2i
import solve.scene.view.association.AssociationManager
import kotlin.math.min

/**
* Used to draw points associations.
*/
class PointAssociationsRenderer(
window: Window,
private val associationManager: AssociationManager
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ import solve.scene.model.VisualizationFrame
import solve.utils.ceilToInt
import kotlin.math.min

/**
* The base class for all renderers.
* Provides methods for managing shaders and controlling the rendering process
*/
abstract class Renderer(protected val window: Window) : Comparable<Renderer> {
protected abstract val maxBatchSize: Int

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ import org.lwjgl.opengl.GL30.GL_TEXTURE_2D_ARRAY
import org.lwjgl.opengl.GL30.glGenerateMipmap
import java.nio.ByteBuffer

/**
* Used to store a large number of textures of the same size.
*/
class ArrayTexture(
width: Int,
height: Int,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ enum class TextureFilterType(val openGLParamValue: Int) {
Smoothed(GL_LINEAR)
}

/**
* The base class for all textures.
* Provides an interface for creating, binding and deleting.
*/
abstract class Texture(private val filterType: TextureFilterType = TextureFilterType.Smoothed) {
protected abstract val textureOpenGLType: Int

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ import org.lwjgl.stb.STBImage.stbi_image_free
import org.lwjgl.stb.STBImage.stbi_load
import org.lwjgl.stb.STBImage.stbi_set_flip_vertically_on_load

/**
* Used to store a default 2D texture.
*/
class Texture2D(
private val filePath: String,
filterType: TextureFilterType = TextureFilterType.Smoothed
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ package solve.rendering.engine.core.texture

import java.nio.ByteBuffer

/**
* Used to store a texture data before it can be loaded to the GPU memory.
*/
class Texture2DData(
val data: ByteBuffer,
val width: Int,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ package solve.rendering.engine.core.texture
import org.lwjgl.opengl.GL11.GL_RGB
import org.lwjgl.opengl.GL11.GL_RGBA

/**
* Used to store a type of color encoding of the texture.
*/
enum class TextureChannelsType(val channelsNumber: Int, val openGLType: Int) {
RGB(3, GL_RGB),
RGBA(4, GL_RGBA);
Expand Down
3 changes: 3 additions & 0 deletions src/main/kotlin/solve/rendering/engine/scene/Scene.kt
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ import solve.scene.model.Scene
import solve.scene.model.VisualizationFrame
import java.util.concurrent.CopyOnWriteArrayList

/**
* Aggregates all renderers of the application scene.
*/
class Scene(
val framesRenderer: FramesRenderer,
val pointAssociationsRenderer: PointAssociationsRenderer,
Expand Down
10 changes: 0 additions & 10 deletions src/main/kotlin/solve/rendering/engine/scene/Transform.kt

This file was deleted.

Loading
Loading