2024-10-01 08:25:12 +01:00
|
|
|
structure GlDraw =
|
|
|
|
|
struct
|
2025-09-11 01:43:23 +01:00
|
|
|
open DrawMsg
|
|
|
|
|
|
2024-10-01 08:25:12 +01:00
|
|
|
type t =
|
|
|
|
|
{ textVertexBuffer: Word32.word
|
|
|
|
|
, textProgram: Word32.word
|
|
|
|
|
, textDrawLength: int
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fun createShader (shaderType, shaderString) =
|
|
|
|
|
let
|
2024-10-02 22:09:22 +01:00
|
|
|
val shader = Gles3.createShader shaderType
|
2024-10-01 08:25:12 +01:00
|
|
|
val _ = Gles3.shaderSource (shader, shaderString)
|
|
|
|
|
val _ = Gles3.compileShader shader
|
|
|
|
|
in
|
|
|
|
|
shader
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
fun createProgram (vertexShader, fragmentShader) =
|
|
|
|
|
let
|
|
|
|
|
val program = Gles3.createProgram ()
|
|
|
|
|
val _ = Gles3.attachShader (program, vertexShader)
|
|
|
|
|
val _ = Gles3.attachShader (program, fragmentShader)
|
|
|
|
|
val _ = Gles3.linkProgram program
|
|
|
|
|
in
|
|
|
|
|
program
|
|
|
|
|
end
|
|
|
|
|
|
2026-01-22 00:04:22 +00:00
|
|
|
fun create () =
|
2024-10-01 08:25:12 +01:00
|
|
|
let
|
2024-10-02 22:09:22 +01:00
|
|
|
(* create vertex buffer, program, etc. for text. *)
|
|
|
|
|
val textVertexBuffer = Gles3.createBuffer ()
|
2025-09-11 01:31:19 +01:00
|
|
|
val xyzRgbVertexShader = createShader
|
|
|
|
|
(Gles3.VERTEX_SHADER, GlShaders.xyzRgbVertexShaderString)
|
2024-10-02 22:09:22 +01:00
|
|
|
|
|
|
|
|
val rgbFragmentShader = createShader
|
|
|
|
|
(Gles3.FRAGMENT_SHADER, GlShaders.rgbFragmentShaderString)
|
|
|
|
|
|
2025-09-11 01:31:19 +01:00
|
|
|
val textProgram = createProgram (xyzRgbVertexShader, rgbFragmentShader)
|
2024-11-15 10:26:22 +00:00
|
|
|
|
2024-10-02 22:09:22 +01:00
|
|
|
(* clean up shaders which are no longer needed once progran is linked. *)
|
2025-09-11 01:31:19 +01:00
|
|
|
val _ = Gles3.deleteShader xyzRgbVertexShader
|
2024-10-02 22:09:22 +01:00
|
|
|
val _ = Gles3.deleteShader rgbFragmentShader
|
2024-10-06 10:23:07 +01:00
|
|
|
|
2025-09-11 01:31:19 +01:00
|
|
|
(* because we only have a single vertex buffer,
|
|
|
|
|
* we only need to bind and set attributes once. *)
|
2024-10-02 22:09:22 +01:00
|
|
|
val _ = Gles3.bindBuffer textVertexBuffer
|
|
|
|
|
|
2025-09-11 01:31:19 +01:00
|
|
|
(* enable xyz component from uploaded array *)
|
|
|
|
|
val _ = Gles3.vertexAttribPointer (0, 3, 6, 0)
|
|
|
|
|
val _ = Gles3.enableVertexAttribArray 0
|
|
|
|
|
(* enable rgb component from uploaded array *)
|
|
|
|
|
val _ = Gles3.vertexAttribPointer (1, 3, 6, 12)
|
|
|
|
|
val _ = Gles3.enableVertexAttribArray 1
|
2024-10-09 11:22:39 +01:00
|
|
|
|
2025-09-11 01:31:19 +01:00
|
|
|
val _ = Gles3.useProgram textProgram
|
2024-10-09 11:22:39 +01:00
|
|
|
in
|
|
|
|
|
{ textVertexBuffer = textVertexBuffer
|
|
|
|
|
, textProgram = textProgram
|
2025-09-11 01:31:19 +01:00
|
|
|
, textDrawLength = 0
|
2024-11-15 10:26:22 +00:00
|
|
|
}
|
|
|
|
|
end
|
|
|
|
|
|
2026-01-22 00:04:22 +00:00
|
|
|
fun uploadText (drawState: t, vec) =
|
2024-11-15 10:26:22 +00:00
|
|
|
let
|
2026-01-22 00:04:22 +00:00
|
|
|
val {textVertexBuffer, textProgram, textDrawLength = _} = drawState
|
2024-11-15 10:26:22 +00:00
|
|
|
|
|
|
|
|
val _ = Gles3.bufferData (vec, Vector.length vec, Gles3.STATIC_DRAW)
|
2025-09-11 01:43:23 +01:00
|
|
|
val newTextDrawLength = Vector.length vec div 6
|
2024-11-15 10:26:22 +00:00
|
|
|
in
|
|
|
|
|
{ textVertexBuffer = textVertexBuffer
|
|
|
|
|
, textProgram = textProgram
|
2025-09-11 01:31:19 +01:00
|
|
|
, textDrawLength = newTextDrawLength
|
2024-10-09 11:22:39 +01:00
|
|
|
}
|
|
|
|
|
end
|
|
|
|
|
|
2024-10-02 22:09:22 +01:00
|
|
|
fun draw (drawObject: t) =
|
2026-01-22 00:04:22 +00:00
|
|
|
let val {textVertexBuffer, textDrawLength, textProgram} = drawObject
|
|
|
|
|
in Gles3.drawArrays (Gles3.TRIANGLES, 0, textDrawLength)
|
2024-10-01 08:25:12 +01:00
|
|
|
end
|
|
|
|
|
end
|