1
0
mirror of https://github.com/lxsang/Diya-API.git synced 2025-02-22 10:52:48 +01:00

let openGL do the final transformation

This commit is contained in:
Dany LE 2022-03-05 11:55:20 +01:00
parent 8cb2cb543a
commit 7fa21d524b
8 changed files with 63 additions and 42 deletions

View File

@ -103,12 +103,12 @@ DiyaBoot >> render [
event := SDL_Event new. event := SDL_Event new.
root := DiyaRootNode new. root := DiyaRootNode new.
DiyaRenderer uniqueInstance root: root. DiyaRenderer uniqueInstance root: root.
rec := root addNode: (DiyaRectangle size: 120@160 shader: GLSimpleShader uniqueInstance) at: 200 @ 200. rec := root addNode: (DiyaRectangle size: 120@160 shader: GLSimpleShader uniqueInstance) at: 250 @ 200.
rec := root addNode: (DiyaRectangle size: 50@50 shader: GLSimpleShader uniqueInstance) at: 200 @ 500. rec := root addNode: (DiyaRectangle size: 50@50 shader: GLSimpleShader uniqueInstance) at: 250 @ 500.
rec rotation: (Float pi / -8.0). rec rotation: (Float pi / -8.0).
rec scale: 1.5@1.5. 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 := root addNode: (DiyaText data: String loremIpsum shader: TotoShader uniqueInstance) at: 20@320.
text extent: 100@100. text extent: 200@320.
"text rotation:(Float pi / 4.0)." "text rotation:(Float pi / 4.0)."
OpenGL viewportX: 0 Y:0 W: display w H: display h. OpenGL viewportX: 0 Y:0 W: display w H: display h.
"TODO: maybe give node to customize this" "TODO: maybe give node to customize this"

View File

@ -6,13 +6,17 @@ Class {
{ #category : #accessing } { #category : #accessing }
DiyaRectangle >> draw [ 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 use.
shader setUniform: #u_time value: DiyaClock uniqueInstance elapsedTime asFloat. 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_resolution value: { context resolution x. context resolution y }.
shader setUniform: #u_transform value: {GL_TRUE. self tf asGLBuffer}.
context mouse ifNotNil: [ context mouse ifNotNil: [
"in shader, window origin is bottom left conor of the window "in shader, window origin is bottom left conor of the window
the mouse position should be transformed to this coodinate" the mouse position should be transformed to this coodinate"
@ -20,7 +24,7 @@ DiyaRectangle >> draw [
]. ].
context vao enableAttribute: 0. context vao enableAttribute: 0.
context vbo bind: GL_ARRAY_BUFFER. 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. context vbo subData: GL_ARRAY_BUFFER offset:0 data: context buffer.
OpenGL drawArrays: GL_QUADS first:0 count: 4. OpenGL drawArrays: GL_QUADS first:0 count: 4.
context vao disableAttribute: 0. context vao disableAttribute: 0.
@ -33,14 +37,3 @@ DiyaRectangle >> initialize [
translation := nil. translation := nil.
children := 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].
]

View File

@ -44,8 +44,9 @@ DiyaText >> draw [
OpenGL enable: GL_TEXTURE_2D. OpenGL enable: GL_TEXTURE_2D.
OpenGL blendFnWithSfactor: GL_SRC_ALPHA dfactor: GL_ONE_MINUS_SRC_ALPHA. OpenGL blendFnWithSfactor: GL_SRC_ALPHA dfactor: GL_ONE_MINUS_SRC_ALPHA.
self shader use. self shader use.
self shader setUniform: #u_time value: DiyaClock uniqueInstance elapsedTime asFloat. shader setUniform: #u_time value: DiyaClock uniqueInstance elapsedTime asFloat.
self shader setUniform: #u_projection value: context projection buffer. 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_texture value: 0.
self shader setUniform: #u_resolution value: { context resolution x. context resolution y }. self shader setUniform: #u_resolution value: { context resolution x. context resolution y }.
style := DiyaFontManager uniqueInstance style: self fontStyle from: self fontName. style := DiyaFontManager uniqueInstance style: self fontStyle from: self fontName.
@ -83,27 +84,17 @@ DiyaText >> drawCharacter: c at: offset [
{ #category : #accessing } { #category : #accessing }
DiyaText >> fillVerticesBuffer: buffer at: offset tex: tex [ 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)). x := offset x + ((tex bearing x )*(self scale x)).
y := offset y - (((tex height) - (tex bearing y))*(self scale y)). y := offset y - (((tex height) - (tex bearing y))*(self scale y)).
w := (tex width)*(self scale x). w := (tex width)*(self scale x).
h := (tex height)*(self scale y). h := (tex height)*(self scale y).
i := 1. {x. y + h. 0.0. 0.0.
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. 0.0. 1.0. x. y. 0.0. 1.0.
x + w. y. 1.0. 1.0. x + w. y. 1.0. 1.0.
x. y + h. 0.0. 0.0. x. y + h. 0.0. 0.0.
x + w. y. 1.0. 1.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 } { #category : #accessing }

View File

@ -87,6 +87,11 @@ OpenGLSL class >> linkProgram:program [
^self ffiCall: #(void glLinkProgram(GLuint program)) ^self ffiCall: #(void glLinkProgram(GLuint program))
] ]
{ #category : #'as yet unclassified' }
OpenGLSL class >> resetShaders [
self allSubclassesDo: [ :c| c reset ].
]
{ #category : #'as yet unclassified' } { #category : #'as yet unclassified' }
OpenGLSL class >> setShaderSourceFor: shader count: n string: s length: l [ OpenGLSL class >> setShaderSourceFor: shader count: n string: s length: l [
^ self ffiCall: #(void glShaderSource( GLuint shader,GLsizei n,const void* s,const GLint *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; precision mediump float;
#endif #endif
uniform mat4 u_projection; uniform mat4 u_projection;
uniform mat3 u_transform;
void main() 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_resolution of: Uniform2F.
self addUniform: #u_mouse of: Uniform2F. self addUniform: #u_mouse of: Uniform2F.
self addUniform: #u_projection of: UniformMatrix4fv. self addUniform: #u_projection of: UniformMatrix4fv.
self addUniform: #u_transform of: UniformMatrix3fv.
self setUpUniforms. self setUpUniforms.
self compile self compile
] ]

View File

@ -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)) ^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' } { #category : #'as yet unclassified' }
OpenGLSLUniform class >> uniformMatrix4fv: location count: count transpose: transpose value: value [ OpenGLSLUniform class >> uniformMatrix4fv: location count: count transpose: transpose value: value [
^self ffiCall: #(void glUniformMatrix4fv(GLint location,GLsizei count,GLboolean transpose,const GLfloat *value)) ^self ffiCall: #(void glUniformMatrix4fv(GLint location,GLsizei count,GLboolean transpose,const GLfloat *value))

View File

@ -24,11 +24,17 @@ void main(void) {
{ #category : #'as yet unclassified' } { #category : #'as yet unclassified' }
TotoShader class >> vertexShader [ TotoShader class >> vertexShader [
^' ^'
varying vec2 texcoord; #ifdef GL_ES
precision mediump float;
#endif
uniform mat4 u_projection; uniform mat4 u_projection;
void main(void) { uniform mat3 u_transform;
gl_Position = u_projection * vec4(gl_Vertex.xy, 0, 1); varying vec2 texcoord;
texcoord = gl_Vertex.zw; 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;
}' }'
] ]

View File

@ -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
]

View File

@ -13,6 +13,6 @@ UniformMatrix4fv >> setUniformValue [
OpenGLSLUniform OpenGLSLUniform
uniformMatrix4fv: location uniformMatrix4fv: location
count: 1 count: 1
transpose: GL_FALSE transpose: value first
value: value getHandle value: value last getHandle
] ]