From 7fa21d524b1c48bbbc9179e8b2a72c0e248abb71 Mon Sep 17 00:00:00 2001 From: Dany LE Date: Sat, 5 Mar 2022 11:55:20 +0100 Subject: [PATCH] let openGL do the final transformation --- Diya/DiyaBoot.class.st | 8 ++++---- Diya/DiyaRectangle.class.st | 25 +++++++++---------------- Diya/DiyaText.class.st | 21 ++++++--------------- Diya/OpenGLSL.class.st | 10 +++++++++- Diya/OpenGLSLUniform.class.st | 5 +++++ Diya/TotoShader.class.st | 14 ++++++++++---- Diya/UniformMatrix3fv.class.st | 18 ++++++++++++++++++ Diya/UniformMatrix4fv.class.st | 4 ++-- 8 files changed, 63 insertions(+), 42 deletions(-) create mode 100644 Diya/UniformMatrix3fv.class.st diff --git a/Diya/DiyaBoot.class.st b/Diya/DiyaBoot.class.st index 4f7c692..70e7c6d 100644 --- a/Diya/DiyaBoot.class.st +++ b/Diya/DiyaBoot.class.st @@ -103,12 +103,12 @@ DiyaBoot >> render [ event := SDL_Event new. root := DiyaRootNode new. DiyaRenderer uniqueInstance root: root. - rec := root addNode: (DiyaRectangle size: 120@160 shader: GLSimpleShader uniqueInstance) at: 200 @ 200. - rec := root addNode: (DiyaRectangle size: 50@50 shader: GLSimpleShader uniqueInstance) at: 200 @ 500. + rec := root addNode: (DiyaRectangle size: 120@160 shader: GLSimpleShader uniqueInstance) at: 250 @ 200. + rec := root addNode: (DiyaRectangle size: 50@50 shader: GLSimpleShader uniqueInstance) at: 250 @ 500. rec rotation: (Float pi / -8.0). rec scale: 1.5@1.5. - text := root addNode: (DiyaText data: 'the quick brown fox jumped over the lazy dog' shader: TotoShader uniqueInstance) at: 50@100. - text extent: 100@100. + text := root addNode: (DiyaText data: String loremIpsum shader: TotoShader uniqueInstance) at: 20@320. + text extent: 200@320. "text rotation:(Float pi / 4.0)." OpenGL viewportX: 0 Y:0 W: display w H: display h. "TODO: maybe give node to customize this" diff --git a/Diya/DiyaRectangle.class.st b/Diya/DiyaRectangle.class.st index 30c94f1..3b89f86 100644 --- a/Diya/DiyaRectangle.class.st +++ b/Diya/DiyaRectangle.class.st @@ -6,13 +6,17 @@ Class { { #category : #accessing } DiyaRectangle >> draw [ - self transformDo:[:i :e| - context buffer at:i put:e - ]. + { + 0.0. 0.0. 1.0. + 0.0. extent y. 1.0. + extent x. extent y. 1.0. + extent x. 0.0. 1.0. + } doWithIndex: [:e :i| context buffer at: i put: e]. shader use. shader setUniform: #u_time value: DiyaClock uniqueInstance elapsedTime asFloat. - shader setUniform: #u_projection value: context projection buffer. + shader setUniform: #u_projection value: {GL_FALSE. context projection buffer}. shader setUniform: #u_resolution value: { context resolution x. context resolution y }. + shader setUniform: #u_transform value: {GL_TRUE. self tf asGLBuffer}. context mouse ifNotNil: [ "in shader, window origin is bottom left conor of the window the mouse position should be transformed to this coodinate" @@ -20,7 +24,7 @@ DiyaRectangle >> draw [ ]. context vao enableAttribute: 0. context vbo bind: GL_ARRAY_BUFFER. - OpenGLVertexArray vertexAttributePointerIndex: 0 size:3 type: GL_FLOAT normalized: GL_FALSE stride: 12 pointer: nil. + OpenGLVertexArray vertexAttributePointerIndex: 0 size:3 type: GL_FLOAT normalized: GL_FALSE stride: 0 pointer: nil. context vbo subData: GL_ARRAY_BUFFER offset:0 data: context buffer. OpenGL drawArrays: GL_QUADS first:0 count: 4. context vao disableAttribute: 0. @@ -33,14 +37,3 @@ DiyaRectangle >> initialize [ translation := nil. children := nil ] - -{ #category : #accessing } -DiyaRectangle >> transformDo: block [ - |i| - i := 1. - self gltf: { 0.0@0.0. 0.0@ (extent y). extent. (extent x) @0. } do:[:x :y| - block value: i value:x. - block value: i + 1 value: y. - block value: i + 2 value: 0.0. - i := i+3]. -] diff --git a/Diya/DiyaText.class.st b/Diya/DiyaText.class.st index 1a109c4..296a636 100644 --- a/Diya/DiyaText.class.st +++ b/Diya/DiyaText.class.st @@ -44,8 +44,9 @@ DiyaText >> draw [ OpenGL enable: GL_TEXTURE_2D. OpenGL blendFnWithSfactor: GL_SRC_ALPHA dfactor: GL_ONE_MINUS_SRC_ALPHA. self shader use. - self shader setUniform: #u_time value: DiyaClock uniqueInstance elapsedTime asFloat. - self shader setUniform: #u_projection value: context projection buffer. + shader setUniform: #u_time value: DiyaClock uniqueInstance elapsedTime asFloat. + shader setUniform: #u_projection value: {GL_FALSE. context projection buffer}. + shader setUniform: #u_transform value: {GL_TRUE. self tf asGLBuffer}. self shader setUniform: #u_texture value: 0. self shader setUniform: #u_resolution value: { context resolution x. context resolution y }. style := DiyaFontManager uniqueInstance style: self fontStyle from: self fontName. @@ -83,27 +84,17 @@ DiyaText >> drawCharacter: c at: offset [ { #category : #accessing } DiyaText >> fillVerticesBuffer: buffer at: offset tex: tex [ - |x y w h i uvs| + |x y w h| x := offset x + ((tex bearing x )*(self scale x)). y := offset y - (((tex height) - (tex bearing y))*(self scale y)). w := (tex width)*(self scale x). h := (tex height)*(self scale y). - i := 1. - uvs := { 0@0. 0@1. 1@1. 0@0. 1@1. 1@0 }. - self gltf:{x@(y+h). x@y. (x+w)@y. x@(y+h). (x+w)@y. (x+w)@(y+h). } do:[:cx :cy| |uv| - uv := uvs at: ((i -1)/4) + 1. - buffer at:i put: cx. - buffer at: i +1 put: cy. - buffer at: i + 2 put: uv x. - buffer at: i + 3 put: uv y. - i := i+4. - ]. -"{x. y + h. 0.0. 0.0. + {x. y + h. 0.0. 0.0. x. y. 0.0. 1.0. x + w. y. 1.0. 1.0. x. y + h. 0.0. 0.0. x + w. y. 1.0. 1.0. - x + w. y + h. 1.0. 0.0. }" + x + w. y + h. 1.0. 0.0. } withIndexDo: [ :e :i| buffer at:i put: e ] ] { #category : #accessing } diff --git a/Diya/OpenGLSL.class.st b/Diya/OpenGLSL.class.st index 63bec86..552094d 100644 --- a/Diya/OpenGLSL.class.st +++ b/Diya/OpenGLSL.class.st @@ -87,6 +87,11 @@ OpenGLSL class >> linkProgram:program [ ^self ffiCall: #(void glLinkProgram(GLuint program)) ] +{ #category : #'as yet unclassified' } +OpenGLSL class >> resetShaders [ + self allSubclassesDo: [ :c| c reset ]. +] + { #category : #'as yet unclassified' } OpenGLSL class >> setShaderSourceFor: shader count: n string: s length: l [ ^ self ffiCall: #(void glShaderSource( GLuint shader,GLsizei n,const void* s,const GLint *l)) @@ -109,9 +114,11 @@ OpenGLSL class >> vertexShader [ precision mediump float; #endif uniform mat4 u_projection; +uniform mat3 u_transform; void main() { - gl_Position = u_projection * vec4(gl_Vertex.xy, 0, 1); + vec3 coord_global = u_transform * vec3(gl_Vertex.xy, 1.0); + gl_Position = u_projection * vec4(coord_global.xy, 0, 1.0); } ' ] @@ -207,6 +214,7 @@ OpenGLSL >> initialize [ self addUniform: #u_resolution of: Uniform2F. self addUniform: #u_mouse of: Uniform2F. self addUniform: #u_projection of: UniformMatrix4fv. + self addUniform: #u_transform of: UniformMatrix3fv. self setUpUniforms. self compile ] diff --git a/Diya/OpenGLSLUniform.class.st b/Diya/OpenGLSLUniform.class.st index 93bd29f..819693c 100644 --- a/Diya/OpenGLSLUniform.class.st +++ b/Diya/OpenGLSLUniform.class.st @@ -68,6 +68,11 @@ OpenGLSLUniform class >> uniform4i: location value: v0 value: v1 value: v2 value ^self ffiCall: #(void glUniform4i(GLint location,GLint v0,GLint v1,GLint v2, GLint v3)) ] +{ #category : #'as yet unclassified' } +OpenGLSLUniform class >> uniformMatrix3fv: location count: count transpose: transpose value: value [ + ^self ffiCall: #(void glUniformMatrix3fv(GLint location,GLsizei count,GLboolean transpose,const GLfloat *value)) +] + { #category : #'as yet unclassified' } OpenGLSLUniform class >> uniformMatrix4fv: location count: count transpose: transpose value: value [ ^self ffiCall: #(void glUniformMatrix4fv(GLint location,GLsizei count,GLboolean transpose,const GLfloat *value)) diff --git a/Diya/TotoShader.class.st b/Diya/TotoShader.class.st index 6ec9f18..8c8aa49 100644 --- a/Diya/TotoShader.class.st +++ b/Diya/TotoShader.class.st @@ -24,11 +24,17 @@ void main(void) { { #category : #'as yet unclassified' } TotoShader class >> vertexShader [ ^' -varying vec2 texcoord; + #ifdef GL_ES +precision mediump float; +#endif uniform mat4 u_projection; -void main(void) { - gl_Position = u_projection * vec4(gl_Vertex.xy, 0, 1); - texcoord = gl_Vertex.zw; +uniform mat3 u_transform; +varying vec2 texcoord; +void main() +{ + vec3 coord_global = u_transform * vec3(gl_Vertex.xy, 1.0); + gl_Position = u_projection * vec4(coord_global.xy, 0, 1.0); + texcoord = gl_Vertex.zw; }' ] diff --git a/Diya/UniformMatrix3fv.class.st b/Diya/UniformMatrix3fv.class.st new file mode 100644 index 0000000..1657aef --- /dev/null +++ b/Diya/UniformMatrix3fv.class.st @@ -0,0 +1,18 @@ +Class { + #name : #UniformMatrix3fv, + #superclass : #OpenGLSLUniform, + #pools : [ + 'OpenGLConstants', + 'OpenGLTypes' + ], + #category : #'Diya-OpenGL' +} + +{ #category : #accessing } +UniformMatrix3fv >> setUniformValue [ + OpenGLSLUniform + uniformMatrix3fv: location + count: 1 + transpose: value first + value: value last getHandle +] diff --git a/Diya/UniformMatrix4fv.class.st b/Diya/UniformMatrix4fv.class.st index 819ea39..8659220 100644 --- a/Diya/UniformMatrix4fv.class.st +++ b/Diya/UniformMatrix4fv.class.st @@ -13,6 +13,6 @@ UniformMatrix4fv >> setUniformValue [ OpenGLSLUniform uniformMatrix4fv: location count: 1 - transpose: GL_FALSE - value: value getHandle + transpose: value first + value: value last getHandle ]