1
0
mirror of https://github.com/lxsang/Diya-API.git synced 2024-12-27 03:48:21 +01:00

WIP: font rendering...

This commit is contained in:
Dany LE 2022-03-03 23:23:43 +01:00
parent d80f31bd42
commit ce08ac49e2
3 changed files with 165 additions and 31 deletions

View File

@ -61,7 +61,7 @@ DiyaBoot >> createWindow [
DiyaBoot >> init [
| status |
status := SDL2 init: SDL_INIT_EVERYTHING.
status = 0
status = 0
ifFalse: [ ^ self error: SDL2 getErrorMessage ].
display := SDL_DisplayMode externalNew autoRelease.
SDL2 SDLGetCurrentDisplayMode: display from:0.
@ -104,13 +104,14 @@ DiyaBoot >> render [
event := SDL_Event new.
root := DiyaRootNode new.
DiyaRenderer uniqueInstance root: root.
rec := root addNode: (DiyaRectangle size: 120@160 shader: GLSimpleShader uniqueInstance) at: 120 @ 160.
rec rotation: (Float pi / -8.0).
rec scale: 1.5@1.5.
text := root addNode: (DiyaText data: 'Hello,world' shader: GLTexShader uniqueInstance) at: 0@0.
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 rotation: (Float pi / -8.0).
rec scale: 1.5@1.5."
text := root addNode: (DiyaText data: 'Hello,world' shader: TotoShader uniqueInstance) at: 0@0.
OpenGL viewportX: 0 Y:0 W: display w H: display h.
"TODO: maybe give node to customize this"
[ running ] whileTrue: [
[ running ] whileTrue: [
(SDL2 pollEvent: event) > 0 ifTrue: [
self processEvent: event
].

View File

@ -37,14 +37,20 @@ DiyaText >> data: anObject [
DiyaText >> draw [
|face offset|
data ifNil: [ ^self ].
offset := 50.0@50.0.
offset := 100.0@100.0.
OpenGL enable: GL_CULL_FACE.
OpenGL enable: GL_BLEND.
OpenGL blendFnWithSfactor: GL_SRC_ALPHA dfactor: GL_ONE_MINUS_SRC_ALPHA.
self shader use.
shader setUniform: #u_time value: DiyaClock uniqueInstance elapsedTime asFloat.
shader setUniform: #u_projection value: context projection buffer.
shader setUniform: #u_resolution value: { context resolution x. context resolution y }.
self shader setUniform: #u_time value: DiyaClock uniqueInstance elapsedTime asFloat.
self shader setUniform: #u_projection value: context projection buffer.
self shader setUniform: #u_texture value: 0.
self shader setUniform: #u_resolution value: { context resolution x. context resolution y }.
context mouse ifNotNil: [
"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 }.
].
face := DiyaFontManager uniqueInstance face: self fontStyle from: self fontName.
"set fontsize"
face setPixelWidth:0 height: self fontSize.
@ -52,11 +58,10 @@ DiyaText >> draw [
"configure vao vbo for texture QUAD"
context vao enableAttribute: 0.
OpenGLVertexArray vertexAttributePointerIndex: 0 size:4 type: GL_FLOAT normalized: GL_FALSE stride: 16 pointer: nil .
data do:[:c | self drawCharacter: c at: offset with: face].
data do:[:c | self drawCharacter: c at: offset with: face.].
context vao disableAttribute: 0.
"reset value"
OpenGL pixelstorei: GL_UNPACK_ALIGNMENT param: 4.
OpenGLTexture bind: GL_TEXTURE_2D texture: 0.
OpenGL disable: GL_CULL_FACE.
OpenGL disable: GL_BLEND.
@ -140,6 +145,6 @@ DiyaText >> initialize [
super initialize.
fontStyle := 'Regular'.
fontName := 'Ubuntu'.
fontSize := 20.
fontSize := 40.
data := nil.
]

View File

@ -7,30 +7,158 @@ Class {
{ #category : #'as yet unclassified' }
TotoShader class >> fragmentShader [
^'
#version 330 core
in vec2 TexCoords;
#ifdef GL_ES
precision highp float;
#endif
uniform sampler2D text;
varying vec2 texcoord;
uniform sampler2D u_texture;
uniform vec2 u_resolution;
uniform vec2 u_mouse;
uniform float u_time;
void main(void) {
gl_FragColor = texture2D(u_texture, texcoord); //vec4(1, 1, 1, texture2D(u_texture, texcoord).r) * vec4(1,1,1,1);
}'!
void main()
{
vec4 sampled = vec4(1.0, 1.0, 1.0, texture(text, TexCoords).r);
gl_FragColor = vec4(1.0,1.0,1.0, 1.0) * sampled;
} '
|pkgn dest pkg fileReference fileName writer core hePackage ignoresClasses|
FFIMethodRegistry resetAll.
dest := Smalltalk arguments first.
pkgn := Smalltalk arguments at:2.
core := HEPackage new.
core classes: OrderedCollection new.
core traits: OrderedCollection new.
core extensionMethods: OrderedCollection new.
core packageName: pkgn.
packages := #(
'Kernel-Chronology-Extras'
'Jobs'
'Collections-Arithmetic'
'Ring-Deprecated-Core-Kernel'
'Math-Operations-Extensions'
'Collections-Atomic'
'Collections-DoubleLinkedList'
'DeprecatedFileStream'
'Zinc-Resource-Meta-Core'
'Zinc-Character-Encoding-Core'
'FileSystem-Core'
'FileSystem-Disk'
'FileSystem-Memory'
'Compression'
'FileSystem-Zip'
'Multilingual-Encodings'
'Multilingual-Languages'
'Multilingual-TextConversion'
'Text-Core'
'AST-Core'
'Debugging-Core'
'OpalCompiler-Core'
'Kernel-Traits'
'AST-Core-Traits'
'Collections-Abstract-Traits'
'Transcript-Core-Traits'
'TraitsV2-Compatibility'
'Alien-Core'
'System-Model'
'PragmaCollector'
'UnifiedFFI'
'UnifiedFFI-Legacy'
'Slot-Core'
'System-Localization'
'STON-Core'
'Diya'
"
'Graphics-Primitives'
'Graphics-Transformations'
'Graphics-Canvas'
'FreeType'
'Graphics-Display Objects'
'EmbeddedFreeType'
'Text-Scanning'
'Multilingual-OtherLanguages'
'Multilingual-TextConverterOtherLanguages'
'Graphics-Fonts'
'System-FileRegistry'
'Graphics-Files'
'Graphics-Shapes'"
).
ignoresClasses := {
RGCommentDefinition.
RGMethodDefinition.
ChangeRecord.
PluggableListMorph.
TextLine.
TransformMorph.
StringMorph.
AthensCairoSDLSurface.
SourceFileArray.
GlyphForm
}.
packages do:[:name | |pkg trait classes extensionMethods|
Transcript show: 'Processing package ',name;cr.
FFIMethodRegistry resetAll.
pkg := RPackageOrganizer default packageNamed: name asSymbol.
traits := (pkg definedClasses select: #isTrait) do: [ :e | core traits add:(HETrait for: e) ].
(pkg definedClasses reject: #isTrait) do: [ :e |
(ignoresClasses includes:e superclass) ifFalse:[
core addClass: (HEClass for: e) ].
].
pkg extensionMethods do: [ :e |
(ignoresClasses includes:e classBinding value) ifFalse:[
core extensionMethods add:(HEMethod for: e)
]
].
].
"SDL binding class"
pkg := RPackageOrganizer default packageNamed: 'OSWindow-SDL2' asSymbol.
(pkg classNamesForClassTag: 'Bindings') do:[:e|
(ignoresClasses includes:e asClass) ifFalse:[
core addClass:(HEClass for:e asClass) ]].
"FreeType binding class"
pkg := RPackageOrganizer default packageNamed: 'FreeType' asSymbol.
(pkg classNamesForClassTag: 'Base') do:[:e|
(ignoresClasses includes:e asClass) ifFalse:[
core addClass:(HEClass for:e asClass) ]].
(pkg classNamesForClassTag: 'Bindings') do:[:e|
(ignoresClasses includes:e asClass) ifFalse:[
core addClass:(HEClass for:e asClass) ]].
"fixing some missing classes"
core addClass: (HEClass for:OSWindowPlatformSpecificHandle);
addClass: (HEClass for:ChunkWriteStream);
addClass: (HEClass for:ChunkReadStream);
addClass: (HEClass for:STCommandLineHandler);
addClass: (HEClass for:FreeTypeFace)
.
fileReference := (dest,'/',(pkgn replaceAll: Character space with: $-),'.hermes') asFileReference.
fileName := fileReference fullName.
writer := HEBinaryReaderWriter new
stream: (File openForWriteFileNamed:fileName);
yourself.
core writeInto: writer.
writer stream flush.
Transcript show: 'File exported to ', dest, '/', pkgn, '.hermes'; cr.
]
{ #category : #'as yet unclassified' }
TotoShader class >> vertexShader [
^'
#version 330 core
layout (location = 0) in vec4 vertex; // <vec2 pos, vec2 tex>
out vec2 TexCoords;
varying vec2 texcoord;
uniform mat4 u_projection;
void main()
{
gl_Position = u_projection * vec4(vertex.xy, 0.0, 1.0);
TexCoords = vertex.zw;
} '
void main(void) {
gl_Position = u_projection * vec4(gl_Vertex.xy, 0, 1);
texcoord = gl_Vertex.zw;
}'
]
{ #category : #initialization }
TotoShader >> setUpUniforms [
self addUniform: #u_texture of: Uniform1i.
]