@@ -22,6 +22,7 @@ import com.lambda.graphics.gl.putTo
2222import com.lambda.graphics.texture.Texture
2323import com.lambda.util.math.MathUtils.toInt
2424import org.lwjgl.opengl.GL45C.*
25+ import java.lang.IllegalStateException
2526import 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 */
4341class 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}
0 commit comments