Skip to content

Commit 2abd06a

Browse files
committed
make the pbo use the texture correctly
1 parent 071d238 commit 2abd06a

File tree

2 files changed

+11
-24
lines changed

2 files changed

+11
-24
lines changed

common/src/main/kotlin/com/lambda/graphics/buffer/pixel/PixelBuffer.kt

Lines changed: 9 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import com.lambda.graphics.gl.putTo
2222
import com.lambda.graphics.texture.Texture
2323
import com.lambda.util.math.MathUtils.toInt
2424
import org.lwjgl.opengl.GL45C.*
25+
import java.lang.IllegalStateException
2526
import java.nio.ByteBuffer
2627

2728
/**
@@ -31,19 +32,13 @@ import java.nio.ByteBuffer
3132
* Functions that perform an upload operation, a pixel unpack, will use the buffer object bound to the target GL_PIXEL_UNPACK_BUFFER.
3233
* If a buffer is bound, then the pointer value that those functions take is not a pointer, but an offset from the beginning of that buffer.
3334
*
34-
* @property width The width of the texture
35-
* @property height The height of the texture
36-
* @property format The image format that will be uploaded
3735
* @property texture The [Texture] instance to use
3836
* @property asynchronous Whether to use 2 buffers or not
3937
* @property bufferMapping Whether to map a block in memory to upload or not
4038
*
4139
* @see <a href="https://www.khronos.org/opengl/wiki/Pixel_Buffer_Object">Reference</a>
4240
*/
4341
class PixelBuffer(
44-
private val width: Int,
45-
private val height: Int,
46-
private val format: Int,
4742
private val texture: Texture,
4843
private val asynchronous: Boolean = false,
4944
private val bufferMapping: Boolean = false,
@@ -52,9 +47,8 @@ class PixelBuffer(
5247
override val target: Int = GL_PIXEL_UNPACK_BUFFER
5348
override val access: Int = GL_MAP_WRITE_BIT
5449

55-
private val channels = channelMapping[format] ?: throw IllegalArgumentException("Invalid image format, expected OpenGL format, got $format instead")
56-
private val internalFormat = reverseChannelMapping[channels] ?: throw IllegalArgumentException("Invalid internal image format, expected channels count, got $channels instead")
57-
private val size = width * height * channels * 1L
50+
private val channels = channelMapping[texture.format] ?: throw IllegalArgumentException("Invalid image format, expected OpenGL format, got ${texture.format} instead")
51+
private val size = texture.width * texture.height * channels * 1L
5852

5953
override fun upload(
6054
data: ByteBuffer,
@@ -69,8 +63,9 @@ class PixelBuffer(
6963
GL_TEXTURE_2D, // Target
7064
0, // Mipmap level
7165
0, 0, // x and y offset
72-
width, height, // width and height of the texture (set to your size)
73-
format, // Format (depends on your data)
66+
texture.width, // Width of the texture
67+
texture.height, // Height of the texture
68+
texture.format, // Format of your texture (depends on your data)
7469
GL_UNSIGNED_BYTE, // Type (depends on your data)
7570
0, // PBO offset (for asynchronous transfer)
7671
)
@@ -88,10 +83,12 @@ class PixelBuffer(
8883
}
8984

9085
init {
86+
if (!texture.initialized) throw IllegalStateException("Cannot use uninitialized textures for pixel buffers")
87+
9188
glBindTexture(GL_TEXTURE_2D, texture.id)
9289

9390
// Allocate texture storage
94-
glTexImage2D(GL_TEXTURE_2D, 0, internalFormat, width, height, 0, format, GL_UNSIGNED_BYTE, 0)
91+
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, texture.width, texture.height, 0, texture.format, GL_UNSIGNED_BYTE, 0)
9592
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR)
9693
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR)
9794

@@ -113,15 +110,5 @@ class PixelBuffer(
113110
GL_RGBA to 4,
114111
GL_BGRA to 4,
115112
)
116-
117-
/**
118-
* Returns an internal format based on how many channels there are
119-
*/
120-
private val reverseChannelMapping = mapOf(
121-
1 to GL_RED,
122-
2 to GL_RG,
123-
3 to GL_RGB,
124-
4 to GL_RGBA,
125-
)
126113
}
127114
}

common/src/main/kotlin/com/lambda/graphics/texture/AnimatedTexture.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ import com.lambda.util.Communication.logError
2222
import com.lambda.util.LambdaResource
2323
import com.lambda.util.stream
2424
import org.lwjgl.BufferUtils
25-
import org.lwjgl.opengl.GL11.GL_RGBA
2625
import org.lwjgl.stb.STBImage
2726
import java.nio.ByteBuffer
2827

@@ -82,6 +81,7 @@ class AnimatedTexture(path: LambdaResource) : Texture(image = null, forceConsist
8281
gif = STBImage.stbi_load_gif_from_memory(buffer, pDelays, pWidth, pHeight, pLayers, pChannels, 4)
8382
?: throw IllegalStateException("There was an unknown error while loading the gif file")
8483

84+
initialized = true
8585
width = pWidth.get()
8686
height = pHeight.get()
8787
frames = pLayers.get()
@@ -90,6 +90,6 @@ class AnimatedTexture(path: LambdaResource) : Texture(image = null, forceConsist
9090

9191
pDelays.getIntBuffer(frames).get(frameDurations)
9292

93-
pbo = PixelBuffer(width, height, format = GL_RGBA, this@AnimatedTexture)
93+
pbo = PixelBuffer(this@AnimatedTexture)
9494
}
9595
}

0 commit comments

Comments
 (0)