add buttonDrawObject and calls to successfully draw button

This commit is contained in:
2024-07-31 08:41:19 +01:00
parent b1b929e055
commit 5525725ef3
8 changed files with 98 additions and 11 deletions

Binary file not shown.

View File

@@ -23,8 +23,13 @@ ann
"allowVectorExps true" "allowVectorExps true"
in in
imperative-shell/constants.sml imperative-shell/constants.sml
imperative-shell/app-draw.sml
end end
imperative-shell/app-draw.sml
imperative-shell/input-callbacks.sml imperative-shell/input-callbacks.sml
imperative-shell/shell.sml
ann
"allowVectorExps true"
in
imperative-shell/shell.sml
end

View File

@@ -58,8 +58,8 @@ void deleteShader(unsigned int shader) {
glDeleteShader(shader); glDeleteShader(shader);
} }
void vertexAttribPointer(int location, int numVecComponents) { void vertexAttribPointer(int location, int numVecComponents, int stride, int offset) {
glVertexAttribPointer(location, numVecComponents, GL_FLOAT, GL_FALSE, numVecComponents * sizeof(float), (void*) 0); glVertexAttribPointer(location, numVecComponents, GL_FLOAT, GL_FALSE, stride * sizeof(float), (void*)offset);
} }
void enableVertexAttribArray(int location) { void enableVertexAttribArray(int location) {

View File

@@ -36,7 +36,7 @@ struct
val shaderSource = _import "shaderSource" public : shader * string -> unit; val shaderSource = _import "shaderSource" public : shader * string -> unit;
val vertexAttribPointer = val vertexAttribPointer =
_import "vertexAttribPointer" public : int * int -> unit; _import "vertexAttribPointer" public : int * int * int * int -> unit;
val enableVertexAttribArray = val enableVertexAttribArray =
_import "enableVertexAttribArray" public : int -> unit; _import "enableVertexAttribArray" public : int -> unit;

View File

@@ -46,7 +46,7 @@ struct
, Vector.length Constants.graphLines , Vector.length Constants.graphLines
, Gles3.STATIC_DRAW () , Gles3.STATIC_DRAW ()
) )
val _ = Gles3.vertexAttribPointer (0, 2) val _ = Gles3.vertexAttribPointer (0, 2, 2, 0)
val _ = Gles3.enableVertexAttribArray 0 val _ = Gles3.enableVertexAttribArray 0
in in
graphDrawObject graphDrawObject
@@ -54,11 +54,70 @@ struct
fun drawGraphLines (graphDrawObject: draw_object) = fun drawGraphLines (graphDrawObject: draw_object) =
let let
val {program, ...} = graphDrawObject val {vertexBuffer, program, ...} = graphDrawObject
val _ = Gles3.useProgram (program) val _ = Gles3.bindBuffer vertexBuffer
val _ = Gles3.vertexAttribPointer (0, 2, 2, 0)
val _ = Gles3.enableVertexAttribArray 0
val _ = Gles3.useProgram program
val _ = Gles3.drawArrays val _ = Gles3.drawArrays
(Gles3.TRIANGLES (), 0, Vector.length Constants.graphLines div 2) (Gles3.TRIANGLES (), 0, Vector.length Constants.graphLines div 2)
in in
() ()
end end
val buttonVec =
#[ 0.5, 0.5, 1.0, 0.0, 0.0
, 0.5, ~0.5, 1.0, 0.0, 0.0
, ~0.5, 0.5, 1.0, 0.0, 0.0
, 0.5, ~0.5, 0.0, 0.0, 1.0
, ~0.5, ~0.5, 0.0, 0.0, 1.0
, ~0.5, 0.5, 0.0, 0.0, 1.0
]
fun initButton () =
let
val buttonDrawObject = initDrawObject
( Constants.colouredVertexShaderString
, Constants.colouredFragmentShaderString
)
val {vertexBuffer, program, ...} = buttonDrawObject
val _ = Gles3.bindBuffer vertexBuffer
val _ =
Gles3.bufferData
(buttonVec, Vector.length buttonVec, Gles3.STATIC_DRAW ())
val _ = Gles3.vertexAttribPointer (0, 2, 5, 0)
val _ = Gles3.enableVertexAttribArray 0
val _ = Gles3.vertexAttribPointer (1, 3, 5, 8)
val _ = Gles3.enableVertexAttribArray 1
in
buttonDrawObject
end
fun uploadButtonVector (buttonDrawObject: draw_object, vec) =
let
val {vertexBuffer, ...} = buttonDrawObject
val _ = Gles3.bindBuffer vertexBuffer
val _ = Gles3.bufferData (vec, Vector.length vec, Gles3.STATIC_DRAW ())
in
()
end
fun drawButton (buttonDrawObject: draw_object, vec) =
let
val {vertexBuffer, program, ...} = buttonDrawObject
val _ = Gles3.bindBuffer vertexBuffer
val _ = Gles3.vertexAttribPointer (0, 2, 5, 0)
val _ = Gles3.enableVertexAttribArray 0
val _ = Gles3.vertexAttribPointer (1, 3, 5, 8)
val _ = Gles3.enableVertexAttribArray 1
val _ = Gles3.useProgram program
val _ = Gles3.drawArrays
(Gles3.TRIANGLES (), 0, Vector.length buttonVec div 5)
in
()
end
end end

View File

@@ -17,6 +17,27 @@ struct
\ FragColor = vec4(0.0f, 0.0f, 0.0f, 1.0f);\n\ \ FragColor = vec4(0.0f, 0.0f, 0.0f, 1.0f);\n\
\}" \}"
val colouredVertexShaderString =
"#version 300 es\n\
\layout (location = 0) in vec2 apos;\n\
\layout (location = 1) in vec3 col;\n\
\out vec3 frag_col;\n\
\void main()\n\
\{\n\
\ frag_col = col;\n\
\ gl_Position = vec4(apos.x, apos.y, 0.0f, 1.0f);\n\
\}"
val colouredFragmentShaderString =
"#version 300 es\n\
\precision mediump float;\n\
\in vec3 frag_col;\n\
\out vec4 FragColor;\n\
\void main()\n\
\{\n\
\ FragColor = vec4(frag_col.x, frag_col.y, frag_col.z, 1.0f);\n\
\}"
val graphLines: Real32.real vector = val graphLines: Real32.real vector =
#[ #[
(* x = ~0.95 *) (* x = ~0.95 *)

View File

@@ -16,18 +16,19 @@ struct
callbackListener mailbox callbackListener mailbox
end end
fun loop (window, graphDrawObject) = fun loop (window, graphDrawObject, buttonDrawObject) =
if not (Glfw.windowShouldClose window) then if not (Glfw.windowShouldClose window) then
let let
val _ = Gles3.clearColor (1.0, 1.0, 1.0, 1.0) val _ = Gles3.clearColor (1.0, 1.0, 1.0, 1.0)
val _ = Gles3.clear () val _ = Gles3.clear ()
val _ = AppDraw.drawGraphLines graphDrawObject val _ = AppDraw.drawGraphLines graphDrawObject
val _ = AppDraw.drawButton (buttonDrawObject, #[])
val _ = Glfw.pollEvents () val _ = Glfw.pollEvents ()
val _ = Glfw.swapBuffers window val _ = Glfw.swapBuffers window
in in
loop (window, graphDrawObject) loop (window, graphDrawObject, buttonDrawObject)
end end
else else
Glfw.terminate () Glfw.terminate ()
@@ -44,6 +45,7 @@ struct
val _ = Gles3.loadGlad () val _ = Gles3.loadGlad ()
val graphDrawObject = AppDraw.initGraphLines () val graphDrawObject = AppDraw.initGraphLines ()
val buttonDrawObject = AppDraw.initButton ()
val inputMailbox = Mailbox.mailbox () val inputMailbox = Mailbox.mailbox ()
(* Set callback sender *) (* Set callback sender *)
@@ -63,7 +65,7 @@ struct
(* Set callback listener *) (* Set callback listener *)
val _ = CML.spawn (fn () => callbackListener inputMailbox) val _ = CML.spawn (fn () => callbackListener inputMailbox)
in in
loop (window, graphDrawObject) loop (window, graphDrawObject, buttonDrawObject)
end end
end end

0
test.sml Normal file
View File