Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion common/src/main/kotlin/com/lambda/graphics/RenderMain.kt
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ import com.lambda.graphics.gl.Matrices.resetMatrices
import com.lambda.graphics.renderer.esp.global.DynamicESP
import com.lambda.graphics.renderer.esp.global.StaticESP
import com.lambda.module.modules.client.GuiSettings
import com.lambda.util.Communication.info
import com.lambda.util.math.Vec2d
import com.mojang.blaze3d.systems.RenderSystem.getProjectionMatrix
import org.joml.Matrix4f
Expand Down
35 changes: 27 additions & 8 deletions common/src/main/kotlin/com/lambda/graphics/buffer/Buffer.kt
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ abstract class Buffer(
* Edge case to handle vertex arrays
*/
val isVertexArray: Boolean = false,
val validate: Boolean = true
) {
/**
* Specifies how the buffers are used
Expand Down Expand Up @@ -105,27 +106,43 @@ abstract class Buffer(
/**
* Index of the current buffer.
*/
var index: Int = 0; private set
private var index: Int = 0; private set(value) {
if (field == value) return
field = value
id = bufferIds[value]
}

/**
* ID of the current buffer.
*/
var id: Int = 0; get() {
if (field == 0) field = bufferIds[0]
return field
} private set

/**
* List of all the buffers.
*/
private val bufferIds = IntArray(buffers)

/**
* Binds the buffer id to the [target].
* Execute the [block] in a bound context
*/
open fun bind(id: Int) = glBindBuffer(target, id)
fun bind(block: Buffer.() -> Unit) {
bind()
block(this)
bind(0)
}

/**
* Binds current the buffer [index] to the [target].
* Binds the buffer id to the [target].
*/
fun bind() = bind(bufferAt(index))
open fun bind(id: Int) = glBindBuffer(target, id)

/**
* Returns the id of the buffer based on the index.
* Binds current the buffer [index] to the [target].
*/
fun bufferAt(index: Int) = bufferIds[index]
fun bind() = bind(id)

/**
* Swaps the buffer [index] if [buffers] is greater than 1.
Expand Down Expand Up @@ -317,6 +334,8 @@ abstract class Buffer(
abstract fun upload(data: ByteBuffer, offset: Long)

private fun validate() {
if (!validate) return

check(usage in GL_STREAM_DRAW..GL_DYNAMIC_COPY)
{ "Usage is invalid, refer to the documentation table." }

Expand Down Expand Up @@ -359,7 +378,7 @@ abstract class Buffer(
var lastIbo = 0
var prevIbo = 0

fun createPipelineBuffer(bufferTarget: Int) = object : Buffer(buffers = 1) {
fun createPipelineBuffer(bufferTarget: Int) = object : Buffer(buffers = 1, validate = false) {
override val target: Int = bufferTarget

override val usage: Int = GL_STATIC_DRAW
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,14 @@ import org.lwjgl.opengl.GL30C.*
import java.nio.IntBuffer

open class FrameBuffer(
protected var width: Int = 1,
protected var height: Int = 1,
var width: Int = 1,
var height: Int = 1,
private val depth: Boolean = false
) {
private val fbo = glGenFramebuffers()
val fbo = glGenFramebuffers()

private val colorAttachment = glGenTextures()
private val depthAttachment by lazy(::glGenTextures)
val colorAttachment = glGenTextures()
val depthAttachment by lazy(::glGenTextures)

private val clearMask = if (!depth) GL_COLOR_BUFFER_BIT
else GL_COLOR_BUFFER_BIT or GL_DEPTH_BUFFER_BIT
Expand All @@ -57,8 +57,18 @@ open class FrameBuffer(
return this
}

private fun update() {
if (width == lastWidth && height == lastWidth) {
fun bind() {
glBindFramebuffer(GL_FRAMEBUFFER, fbo)
}

fun updateScreenSized() {
width = mc.framebuffer.viewportWidth
height = mc.framebuffer.viewportHeight
update()
}

fun update() {
if (width == lastWidth && height == lastHeight) {
glClear(clearMask)
return
}
Expand Down Expand Up @@ -114,5 +124,9 @@ open class FrameBuffer(
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE)
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE)
}

fun unbind() {
glBindFramebuffer(GL_FRAMEBUFFER, mc.framebuffer.fbo)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,7 @@
package com.lambda.graphics.buffer.pixel

import com.lambda.graphics.buffer.Buffer
import com.lambda.graphics.gl.putTo
import com.lambda.graphics.texture.Texture
import com.lambda.threading.runSafeGameScheduled
import com.lambda.util.math.MathUtils.toInt
import org.lwjgl.opengl.GL45C.*
import java.nio.ByteBuffer
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,33 +20,55 @@ package com.lambda.graphics.buffer.vertex
import com.lambda.graphics.buffer.Buffer
import com.lambda.graphics.buffer.vertex.attributes.VertexAttrib
import com.lambda.graphics.buffer.vertex.attributes.VertexMode
import com.lambda.graphics.pipeline.PersistentBuffer
import net.minecraft.client.render.BufferRenderer
import org.lwjgl.opengl.GL30C.*
import org.lwjgl.opengl.GL32C.glDrawElementsBaseVertex
import java.nio.ByteBuffer

class VertexArray(
private val vertexMode: VertexMode,
private val attributes: VertexAttrib.Group
val vertexMode: VertexMode,
val attributes: VertexAttrib.Group
) : Buffer(isVertexArray = true) {
override val usage: Int = -1
override val target: Int = -1
override val access: Int = -1

fun render(
private var linkedVBO: PersistentBuffer? = null

fun renderIndices(
ibo: PersistentBuffer
) = linkedVBO?.let { vbo ->
renderInternal(
indicesSize = ibo.byteBuffer.bytesPut - ibo.uploadOffset,
indicesPointer = ibo.byteBuffer.pointer + ibo.uploadOffset,
verticesOffset = vbo.uploadOffset
)
} ?: throw IllegalStateException("Unable to use vertex array without having a VBO linked to it.")

private fun renderInternal(
indicesSize: Long,
indicesPointer: Long,
verticesOffset: Int
) {
bind()
glDrawElementsBaseVertex(
vertexMode.mode,
indicesSize.toInt() / UInt.SIZE_BYTES,
GL_UNSIGNED_INT,
indicesPointer,
verticesOffset / attributes.stride
)
bind(0)
verticesOffset: Long
) = bind {
glDrawElementsBaseVertex(
vertexMode.mode,
indicesSize.toInt() / Int.SIZE_BYTES,
GL_UNSIGNED_INT,
indicesPointer,
verticesOffset.toInt() / attributes.stride,
)
}

fun linkVbo(vbo: PersistentBuffer, block: VertexArray.() -> Unit = { }) {
linkedVBO = vbo

bind {
vbo.use {
attributes.link()
block(this@VertexArray)
}
}
}

override fun map(size: Long, offset: Long, block: (ByteBuffer) -> Unit) = throw UnsupportedOperationException()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,36 +21,34 @@ import org.lwjgl.opengl.GL11C.GL_FLOAT
import org.lwjgl.opengl.GL11C.GL_UNSIGNED_BYTE
import org.lwjgl.opengl.GL20C.glEnableVertexAttribArray
import org.lwjgl.opengl.GL20C.glVertexAttribPointer
import org.lwjgl.opengl.GL33.glVertexAttribDivisor

sealed class VertexAttrib(
private val componentCount: Int,
componentSize: Int,
private val normalized: Boolean,
private val single: Boolean,
private val type: Int
) {
open class Float(
normalized: Boolean = false, single: Boolean = false
) : VertexAttrib(1, 4, normalized, single, GL_FLOAT) {
normalized: Boolean = false
) : VertexAttrib(1, 4, normalized, GL_FLOAT) {
companion object : Float()
}

open class Vec2(
normalized: Boolean = false, single: Boolean = false
) : VertexAttrib(2, 4, normalized, single, GL_FLOAT) {
normalized: Boolean = false
) : VertexAttrib(2, 4, normalized, GL_FLOAT) {
companion object : Vec2()
}

open class Vec3(
normalized: Boolean = false, single: Boolean = false
) : VertexAttrib(3, 4, normalized, single, GL_FLOAT) {
normalized: Boolean = false
) : VertexAttrib(3, 4, normalized, GL_FLOAT) {
companion object : Vec3()
}

open class Color(
normalized: Boolean = true, single: Boolean = false
) : VertexAttrib(4, 1, normalized, single, GL_UNSIGNED_BYTE) {
normalized: Boolean = true
) : VertexAttrib(4, 1, normalized, GL_UNSIGNED_BYTE) {
companion object : Color()
}

Expand All @@ -59,7 +57,6 @@ sealed class VertexAttrib(
fun link(index: Int, pointer: Long, stride: Int) {
glEnableVertexAttribArray(index)
glVertexAttribPointer(index, componentCount, type, normalized, stride, pointer)
if (single) glVertexAttribDivisor(index, 1)
}

@Suppress("ClassName")
Expand All @@ -70,15 +67,7 @@ sealed class VertexAttrib(

// GUI
object FONT : Group(
Vec3, Vec2, Color
)

object RECT : Group(
Vec3, Vec2, Color
)

object RECT_OUTLINE : Group(
Vec3, Vec2, Float, Color
Vec2, Vec2, Color
)

// WORLD
Expand All @@ -94,6 +83,14 @@ sealed class VertexAttrib(
Vec3, Vec2, Color
)

object LINE : Group(
Vec2, Float, Color
)

object MULTILINE : Group(
Vec2, Float, Float, Float, Color
)

val stride = attributes.sumOf { attribute ->
attribute.size
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ object GlStateUtils {
field.set(flag)
}

private fun blend(flag: Boolean) {
fun blend(flag: Boolean) {
if (flag) {
glEnable(GL_BLEND)
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA)
Expand Down
Loading