1
0
mirror of https://github.com/lxsang/Diya-API.git synced 2024-12-29 04:48:22 +01:00
Diya-API/Diya/DiyaText.class.st

163 lines
4.8 KiB
Smalltalk
Raw Normal View History

2022-03-01 22:58:58 +01:00
Class {
#name : #DiyaText,
#superclass : #Diya2DNode,
#instVars : [
'fontStyle',
'fontSize',
2022-03-03 19:19:40 +01:00
'fontName',
2022-03-04 20:28:38 +01:00
'data',
'style'
2022-03-01 22:58:58 +01:00
],
2022-03-03 19:19:40 +01:00
#pools : [
'FT2Types'
],
2022-03-01 22:58:58 +01:00
#category : #'Diya-Graphics'
}
2022-03-03 19:19:40 +01:00
{ #category : #'as yet unclassified' }
DiyaText class >> data: string [
^ (self new) data: string; yourself
]
{ #category : #'as yet unclassified' }
DiyaText class >> data: string shader: s [
^ (self with:s) data: string; yourself
]
2022-03-01 22:58:58 +01:00
{ #category : #accessing }
DiyaText >> data [
^ data
]
{ #category : #accessing }
DiyaText >> data: anObject [
data := anObject
]
2022-03-03 19:19:40 +01:00
{ #category : #accessing }
DiyaText >> draw [
| offset tex2D|
2022-03-03 19:19:40 +01:00
data ifNil: [ ^self ].
2022-03-04 20:28:38 +01:00
offset := 0.0@0.0.
2022-03-03 19:19:40 +01:00
OpenGL enable: GL_CULL_FACE.
OpenGL enable: GL_BLEND.
OpenGL blendFnWithSfactor: GL_SRC_ALPHA dfactor: GL_ONE_MINUS_SRC_ALPHA.
self shader use.
2022-03-05 11:55:20 +01:00
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}.
2022-03-03 23:23:43 +01:00
self shader setUniform: #u_texture value: 0.
self shader setUniform: #u_resolution value: { context resolution x. context resolution y }.
2022-03-04 20:28:38 +01:00
style := DiyaFontManager uniqueInstance style: self fontStyle from: self fontName.
2022-03-03 19:19:40 +01:00
OpenGL pixelstorei: GL_UNPACK_ALIGNMENT param: 1.
"configure vao vbo for texture QUAD"
2022-03-04 20:28:38 +01:00
context texture0 active.
tex2D := style textureOf: self fontSize.
context texture0 setImage2D: tex2D.
OpenGLTexture parameteri: GL_TEXTURE_2D pname: GL_TEXTURE_WRAP_S param: GL_CLAMP_TO_EDGE.
OpenGLTexture parameteri: GL_TEXTURE_2D pname: GL_TEXTURE_WRAP_T param: GL_CLAMP_TO_EDGE.
OpenGLTexture parameteri: GL_TEXTURE_2D pname: GL_TEXTURE_MIN_FILTER param: GL_LINEAR.
OpenGLTexture parameteri: GL_TEXTURE_2D pname: GL_TEXTURE_MAG_FILTER param: GL_LINEAR.
2022-03-03 19:19:40 +01:00
context vao enableAttribute: 0.
2022-03-04 20:28:38 +01:00
OpenGLVertexArray vertexAttributePointerIndex: 0 size:4 type: GL_FLOAT normalized: GL_FALSE stride: 16 pointer: nil .
data do:[:c | self drawCharacter: c asciiValue at: offset withTexture:tex2D. ].
2022-03-03 19:19:40 +01:00
context vao disableAttribute: 0.
"reset value"
OpenGL pixelstorei: GL_UNPACK_ALIGNMENT param: 4.
OpenGL disable: GL_CULL_FACE.
OpenGL disable: GL_BLEND.
]
{ #category : #accessing }
DiyaText >> drawCharacter: c at: offset withTexture: tex2D [
|x y w h charsize texcoord|
c = (Character space asciiValue) ifTrue:[
^offset setX: (offset x + ((tex2D spacing )* (self scale x)) ) setY: offset y.
].
charsize := tex2D charSize: c.
((offset x > extent x) and: (charsize x > 0)) ifTrue:[
2022-03-04 20:28:38 +01:00
offset setX: 0.0 setY: (offset y )- (tex2D linespace * (self scale y)).].
charsize := tex2D charSize: c.
texcoord := tex2D texCoordOf: c.
x := offset x "+ ((tex bearing x )*(self scale x))".
y := offset y - ((tex2D cellh) * (self scale y)) "(((tex height) - (tex bearing y))*(self scale y))".
w := (charsize x)*(self scale x).
h := (charsize y)*(self scale y).
{x. y + h. texcoord origin x. texcoord origin y.
x. y. texcoord origin x. texcoord corner y.
x + w. y. texcoord corner x. texcoord corner y.
x. y + h. texcoord origin x. texcoord origin y.
x + w. y. texcoord corner x. texcoord corner y.
x + w. y + h. texcoord corner x. texcoord origin y. } withIndexDo: [ :e :i| context buffer at:i put: e ].
2022-03-04 20:28:38 +01:00
context vbo subData: GL_ARRAY_BUFFER offset: 0 data: context buffer.
2022-03-03 19:19:40 +01:00
OpenGL drawArrays: GL_TRIANGLES first:0 count:6.
offset setX: (offset x + ((charsize x )* (self scale x)) ) setY: offset y.
2022-03-03 19:19:40 +01:00
]
{ #category : #accessing }
2022-03-04 20:28:38 +01:00
DiyaText >> fillVerticesBuffer: buffer at: offset tex: tex [
2022-03-05 11:55:20 +01:00
|x y w h|
"x := offset x + ((tex bearing x )*(self scale x)).
2022-03-04 20:28:38 +01:00
y := offset y - (((tex height) - (tex bearing y))*(self scale y)).
w := (tex width)*(self scale x).
h := (tex height)*(self scale y)."
x := 0.
y := 0.
w := tex width.
h := tex height.
2022-03-05 11:55:20 +01:00
{x. y + h. 0.0. 0.0.
2022-03-03 19:19:40 +01:00
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.
2022-03-05 11:55:20 +01:00
x + w. y + h. 1.0. 0.0. } withIndexDo: [ :e :i| buffer at:i put: e ]
"{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. }"
2022-03-03 19:19:40 +01:00
]
{ #category : #accessing }
DiyaText >> fontName [
^ fontName
]
{ #category : #accessing }
DiyaText >> fontName: anObject [
2022-03-04 20:28:38 +01:00
fontName := anObject.
2022-03-03 19:19:40 +01:00
]
2022-03-01 22:58:58 +01:00
{ #category : #accessing }
DiyaText >> fontSize [
^ fontSize
]
{ #category : #accessing }
DiyaText >> fontSize: anObject [
2022-03-04 20:28:38 +01:00
fontSize := anObject.
2022-03-01 22:58:58 +01:00
]
{ #category : #accessing }
DiyaText >> fontStyle [
^ fontStyle
]
{ #category : #accessing }
DiyaText >> fontStyle: anObject [
2022-03-04 20:28:38 +01:00
fontStyle := anObject.
2022-03-01 22:58:58 +01:00
]
{ #category : #initialization }
DiyaText >> initialize [
super initialize.
2022-03-04 20:28:38 +01:00
self fontName: 'Ubuntu'.
self fontStyle: 'Regular'.
self fontSize: 14.
2022-03-01 22:58:58 +01:00
data := nil.
]