From 1ebd4a748caa377375b2eaba30c8276347f8ea77 Mon Sep 17 00:00:00 2001 From: Dany LE Date: Tue, 15 Feb 2022 22:01:26 +0100 Subject: [PATCH] correct mouse position inside shader coodinate system --- Diya/DiyaBoot.class.st | 4 ++-- Diya/GLSimpleShader.class.st | 6 ++++-- Diya/OpenGLSL.class.st | 2 +- Diya/SimpleDiyaRenderer.class.st | 15 ++++++++++----- 4 files changed, 17 insertions(+), 10 deletions(-) diff --git a/Diya/DiyaBoot.class.st b/Diya/DiyaBoot.class.st index 2973141..ffd85d2 100644 --- a/Diya/DiyaBoot.class.st +++ b/Diya/DiyaBoot.class.st @@ -80,11 +80,11 @@ DiyaBoot >> initialize [ DiyaBoot >> processEvent: event [ |mappedEvt| mappedEvt := event mapped. - mappedEvt type = SDL_KEYDOWN ifTrue: [ Transcript show: 'keydown...'. ^running := false. ]. + mappedEvt type = SDL_KEYDOWN ifTrue: [ Transcript show: 'keydown...';cr. ^running := false. ]. mappedEvt type = SDL_QUIT ifTrue:[ ^running:= false ]. mappedEvt type = SDL_FINGERDOWN ifTrue:[^self setCursorPosition: mappedEvt ]. mappedEvt type = SDL_FINGERMOTION ifTrue:[^self setCursorPosition: mappedEvt ]. - mappedEvt type = SDL_MOUSEMOTION ifTrue:[DiyaRendererContext uniqueInstance mouse: mappedEvt]. + mappedEvt type = SDL_MOUSEMOTION ifTrue:[DiyaRendererContext uniqueInstance mouse: (mappedEvt x) @ (mappedEvt y)]. ] { #category : #events } diff --git a/Diya/GLSimpleShader.class.st b/Diya/GLSimpleShader.class.st index 3d4e2e0..c712ed5 100644 --- a/Diya/GLSimpleShader.class.st +++ b/Diya/GLSimpleShader.class.st @@ -15,8 +15,10 @@ uniform vec2 u_mouse; uniform float u_time; void main() { - vec2 mouse = vec2(u_mouse)/vec2(u_resolution); - gl_FragColor = vec4(mouse.x, mouse.y, abs(sin(u_time)), 1.0); + vec2 mouse = u_mouse / u_resolution; + vec2 px = (gl_FragCoord.xy/u_resolution); + + gl_FragColor = vec4(px/mouse, abs(sin(u_time)), 1.0); } ' ] diff --git a/Diya/OpenGLSL.class.st b/Diya/OpenGLSL.class.st index 83bb4f5..b9fdf84 100644 --- a/Diya/OpenGLSL.class.st +++ b/Diya/OpenGLSL.class.st @@ -214,7 +214,7 @@ OpenGLSL >> locateUniforms [ OpenGLSL >> setUniform: uname value: values [ |uniform| uniform := uniforms at: uname asSymbol ifAbsent:[ - ^self error: 'Uniform', uname, 'is not defined in this program']. + ^self error: 'Uniform ', uname, ' is not defined in this program']. uniform value: values ] diff --git a/Diya/SimpleDiyaRenderer.class.st b/Diya/SimpleDiyaRenderer.class.st index 89bf219..e6dbebf 100644 --- a/Diya/SimpleDiyaRenderer.class.st +++ b/Diya/SimpleDiyaRenderer.class.st @@ -22,7 +22,7 @@ SimpleDiyaRenderer >> initialize [ super initialize. vertexBuffer := OpenGLVertexBuffer new. arrayBuffer := OpenGLVertexArray new. - bufferData := FFIExternalArray externalNewType: GLfloat size: 9. + bufferData := FFIExternalArray externalNewType: GLfloat size: 12. bufferData autoRelease. shader := GLSimpleShader uniqueInstance. @@ -36,13 +36,15 @@ SimpleDiyaRenderer >> render [ shader setUniform: #u_time value: DiyaClock uniqueInstance elapsedTime asFloat. shader setUniform: #u_resolution value: { context resolution x. context resolution y }. context mouse ifNotNil: [ - shader setUniform: #u_mouse value: { context mouse x. context mouse y }. + "in shader, window origin is bottom left conor of the window + the mouse position should be transformed to this coodinate" + shader setUniform: #u_mouse value: { context mouse x. context resolution y - context mouse y }. ]. arrayBuffer enableAttribute: 0. vertexBuffer bind: GL_ARRAY_BUFFER. OpenGLVertexArray vertexAttributePointerIndex: 0 size:3 type: GL_FLOAT normalized: GL_FALSE stride: 0 pointer: nil . - OpenGL drawArrays: GL_TRIANGLES first:0 count: 3. + OpenGL drawArrays: GL_QUADS first:0 count: 4. arrayBuffer disableAttribute: 0. ] @@ -55,9 +57,12 @@ bufferData at: 4 put: 1.0; at: 5 put: -1.0; at: 6 put: 0; - at: 7 put: 0; + at: 7 put: 1.0; at: 8 put: 1.0; - at: 9 put: 0. + at: 9 put: 0; + at: 10 put: -1.0; + at: 11 put: 1.0; + at: 12 put: 0. arrayBuffer bind. vertexBuffer bind: GL_ARRAY_BUFFER. vertexBuffer data: GL_ARRAY_BUFFER data:bufferData usage: GL_STATIC_DRAW.