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

WIP: display Freetype font in openGL

This commit is contained in:
Dany LE 2022-03-03 19:19:40 +01:00
parent 6808a3aa0b
commit d80f31bd42
22 changed files with 765 additions and 115 deletions

14
Diya/Array2D.extension.st Normal file
View File

@ -0,0 +1,14 @@
Extension { #name : #Array2D }
{ #category : #'*Diya' }
Array2D >> asGLBuffer [
|buffer i|
i := 1.
buffer := FFIExternalArray externalNewType: #float size: self size.
self asArray do:[:e|
buffer at:i put:e.
i := i+1
].
buffer autoRelease.
^buffer
]

View File

@ -4,6 +4,15 @@ Class {
#category : #'Diya-Graphics' #category : #'Diya-Graphics'
} }
{ #category : #'as yet unclassified' }
Diya2DNode >> gltf: points [
^ points collect: [ :point | |coord|
coord := self tf +* { point x. point y. 1.0 }.
coord at:3 put: 0.0.
coord
]
]
{ #category : #initialization } { #category : #initialization }
Diya2DNode >> initialize [ Diya2DNode >> initialize [
super initialize. super initialize.

View File

@ -66,6 +66,7 @@ DiyaBoot >> init [
display := SDL_DisplayMode externalNew autoRelease. display := SDL_DisplayMode externalNew autoRelease.
SDL2 SDLGetCurrentDisplayMode: display from:0. SDL2 SDLGetCurrentDisplayMode: display from:0.
DiyaRendererContext reset. DiyaRendererContext reset.
DiyaFontManager uniqueInstance loadFonts.
] ]
{ #category : #events } { #category : #events }
@ -99,14 +100,16 @@ DiyaBoot >> randomColorChannel [
{ #category : #events } { #category : #events }
DiyaBoot >> render [ DiyaBoot >> render [
|event root rec| |event root rec text|
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: 120 @ 160. rec := root addNode: (DiyaRectangle size: 120@160 shader: GLSimpleShader uniqueInstance) at: 120 @ 160.
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: 'Hello,world' shader: GLTexShader uniqueInstance) at: 0@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"
[ running ] whileTrue: [ [ running ] whileTrue: [
(SDL2 pollEvent: event) > 0 ifTrue: [ (SDL2 pollEvent: event) > 0 ifTrue: [
self processEvent: event self processEvent: event
@ -197,7 +200,7 @@ DiyaBoot >> startx [
self createGLContext. self createGLContext.
self createRenderer. self createRenderer.
self showSystemInfo. self showSystemInfo.
DiyaRendererContext uniqueInstance display: display. DiyaRendererContext uniqueInstance display: display; useProjection: OrthoProjectionMatrix.
self render. self render.
context delete. context delete.
renderer destroy. renderer destroy.

View File

@ -1,37 +0,0 @@
Class {
#name : #DiyaComposableNode,
#superclass : #DiyaNode,
#instVars : [
'children'
],
#category : #'Diya-Graphics'
}
{ #category : #accessing }
DiyaComposableNode >> addNode: node [
^self addNode: node at: 0@0
]
{ #category : #accessing }
DiyaComposableNode >> addNode: node at: pos [
node parent: self.
node position: pos.
children add: node.
^ node
]
{ #category : #accessing }
DiyaComposableNode >> children [
^ children
]
{ #category : #initialization }
DiyaComposableNode >> initialize [
super initialize.
children := OrderedCollection new.
]
{ #category : #accessing }
DiyaComposableNode >> render [
children do: [:c | c render ].
]

View File

@ -16,13 +16,3 @@ void main()
} }
' '
] ]
{ #category : #accessing }
DiyaDefaultShader class >> vertextShader [
^ '
void main()
{
gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;
}
'
]

View File

@ -4,6 +4,7 @@ Class {
#instVars : [ #instVars : [
'translation', 'translation',
'parent', 'parent',
'children',
'scale', 'scale',
'rotation', 'rotation',
'tf', 'tf',
@ -22,12 +23,47 @@ DiyaNode class >> with: shader [
^self new shader: shader; yourself ^self new shader: shader; yourself
] ]
{ #category : #accessing }
DiyaNode >> addNode: node [
^self addNode: node at: 0@0
]
{ #category : #accessing }
DiyaNode >> addNode: node at: pos [
children ifNil: [ ^self ].
node parent: self.
node position: pos.
children add: node.
^ node
]
{ #category : #accessing }
DiyaNode >> boundingBox [
^ self subclassResponsibility
]
{ #category : #accessing }
DiyaNode >> children [
^children
]
{ #category : #accessing }
DiyaNode >> draw [
self subclassResponsibility
]
{ #category : #'as yet unclassified' }
DiyaNode >> gltf: points [
^self subclassResponsibility
]
{ #category : #initialization } { #category : #initialization }
DiyaNode >> initialize [ DiyaNode >> initialize [
super initialize. super initialize.
parent := nil. parent := nil.
shader := DiyaDefaultShader uniqueInstance. shader := nil.
context := DiyaRendererContext uniqueInstance. context := DiyaRendererContext uniqueInstance.
children := OrderedCollection new.
] ]
{ #category : #testing } { #category : #testing }
@ -58,7 +94,9 @@ DiyaNode >> position: anObject [
{ #category : #accessing } { #category : #accessing }
DiyaNode >> render [ DiyaNode >> render [
^self subclassResponsibility self draw.
children ifNil: [ ^self ].
children do: [:c | c render ].
] ]
{ #category : #accessing } { #category : #accessing }
@ -85,6 +123,9 @@ DiyaNode >> scale: anObject [
{ #category : #accessing } { #category : #accessing }
DiyaNode >> shader [ DiyaNode >> shader [
shader ifNil: [
parent ifNil: [ ^nil ].
^parent shader ].
^ shader ^ shader
] ]

View File

@ -18,22 +18,13 @@ DiyaRectangle class >> size: size shader:s [
] ]
{ #category : #accessing } { #category : #accessing }
DiyaRectangle >> initialize [ DiyaRectangle >> draw [
super initialize.
self size:10@10.
]
{ #category : #accessing }
DiyaRectangle >> render [
|buffer|
buffer := FFIExternalArray externalNewType: GLfloat size: 12.
self transformDo:[:i :e| self transformDo:[:i :e|
buffer at:i put:e context buffer at:i put:e
]. ].
context vbo bind: GL_ARRAY_BUFFER.
context vbo data: GL_ARRAY_BUFFER data:buffer usage: GL_STATIC_DRAW.
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_resolution value: { context resolution x. context resolution y }. shader setUniform: #u_resolution value: { context resolution x. context resolution y }.
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
@ -42,10 +33,18 @@ DiyaRectangle >> render [
]. ].
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: 0 pointer: nil . OpenGLVertexArray vertexAttributePointerIndex: 0 size:3 type: GL_FLOAT normalized: GL_FALSE stride: 12 pointer: nil.
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.
buffer free. ]
{ #category : #accessing }
DiyaRectangle >> initialize [
super initialize.
self size:10@10.
translation := nil.
children := nil
] ]
{ #category : #accessing } { #category : #accessing }
@ -58,25 +57,11 @@ DiyaRectangle >> size: anObject [
size := anObject. size := anObject.
] ]
{ #category : #accessing }
DiyaRectangle >> transform [
|origin topleft topright bottomright|
origin := (self tf +* #(0.0 0.0 1.0)) asPoint asGLCoord.
topleft := (self tf +* { 0.0. size y.1.0 }) asPoint asGLCoord.
topright := (self tf +* { size x. size y.1.0 }) asPoint asGLCoord.
bottomright := (self tf +* { size x. 0.0. 1.0 }) asPoint asGLCoord.
^
{
origin x. origin y. 0.0.
topleft x. topleft y. 0.0.
topright x. topright y. 0.0.
bottomright x. bottomright y. 0.0.
}
]
{ #category : #accessing } { #category : #accessing }
DiyaRectangle >> transformDo: block [ DiyaRectangle >> transformDo: block [
|i| |i|
i := 1. i := 1.
self transform do:[:e| block value:i value:e. i := i+1]. (self gltf: { 0.0@0.0. 0.0@ (size y). size. (size x) @0. }) do:[:p|
p do: [ :e|
block value:i value:e. i := i+1]].
] ]

View File

@ -5,7 +5,10 @@ Class {
'mouse', 'mouse',
'display', 'display',
'vbo', 'vbo',
'vao' 'vao',
'texture0',
'projection',
'buffer'
], ],
#pools : [ #pools : [
'OpenGLConstants', 'OpenGLConstants',
@ -19,10 +22,16 @@ DiyaRendererContext class >> cleanUpInstance: singleton [
singleton destroy singleton destroy
] ]
{ #category : #accessing }
DiyaRendererContext >> buffer [
^ buffer
]
{ #category : #accessing } { #category : #accessing }
DiyaRendererContext >> destroy [ DiyaRendererContext >> destroy [
vao delete. vao delete.
vbo delete. vbo delete.
texture0 delete.
] ]
{ #category : #accessing } { #category : #accessing }
@ -40,8 +49,14 @@ DiyaRendererContext >> initialize [
super initialize. super initialize.
vbo := OpenGLVertexBuffer new. vbo := OpenGLVertexBuffer new.
vao := OpenGLVertexArray new. vao := OpenGLVertexArray new.
texture0 := OpenGLTexture fromUnit: 0.
vao bind. vao bind.
vbo bind: GL_ARRAY_BUFFER. vbo bind: GL_ARRAY_BUFFER.
projection := Array2D identity: 4.
buffer := FFIExternalArray externalNewType: GLfloat size: 128.
buffer autoRelease.
vbo data: GL_ARRAY_BUFFER data: nil size:512 usage: GL_DYNAMIC_DRAW.
] ]
{ #category : #accessing } { #category : #accessing }
@ -54,11 +69,26 @@ DiyaRendererContext >> mouse: anObject [
mouse := anObject mouse := anObject
] ]
{ #category : #accessing }
DiyaRendererContext >> projection [
^ projection
]
{ #category : #'as yet unclassified' } { #category : #'as yet unclassified' }
DiyaRendererContext >> resolution [ DiyaRendererContext >> resolution [
^ (display w) @ (display h) ^ (display w) @ (display h)
] ]
{ #category : #accessing }
DiyaRendererContext >> texture0 [
^ texture0
]
{ #category : #'as yet unclassified' }
DiyaRendererContext >> useProjection: aClass [
projection := aClass fromDisplay: self display
]
{ #category : #accessing } { #category : #accessing }
DiyaRendererContext >> vao [ DiyaRendererContext >> vao [
^ vao ^ vao

View File

@ -1,13 +1,20 @@
Class { Class {
#name : #DiyaRootNode, #name : #DiyaRootNode,
#superclass : #DiyaComposableNode, #superclass : #DiyaNode,
#category : #'Diya-Graphics' #category : #'Diya-Graphics'
} }
{ #category : #accessing }
DiyaRootNode >> draw [
OpenGL clearColorR: 1.0 G: 0.0 B: 1.0 A:0.
OpenGL clear: GL_COLOR_BUFFER_BIT.
]
{ #category : #initialization } { #category : #initialization }
DiyaRootNode >> initialize [ DiyaRootNode >> initialize [
super initialize. super initialize.
parent := self. parent := self.
shader := DiyaDefaultShader uniqueInstance.
] ]
{ #category : #testing } { #category : #testing }
@ -15,15 +22,6 @@ DiyaRootNode >> isRoot [
^ true ^ true
] ]
{ #category : #accessing }
DiyaRootNode >> render [
OpenGL clearColorR: 1.0 G: 0 B: 1.0 A:0.
OpenGL clear: GL_COLOR_BUFFER_BIT.
"render all child node"
children do: [:c | c render ].
]
{ #category : #accessing } { #category : #accessing }
DiyaRootNode >> updateTF [ DiyaRootNode >> updateTF [
"donothing" "donothing"

View File

@ -4,11 +4,25 @@ Class {
#instVars : [ #instVars : [
'fontStyle', 'fontStyle',
'fontSize', 'fontSize',
'fontName',
'data' 'data'
], ],
#pools : [
'FT2Types'
],
#category : #'Diya-Graphics' #category : #'Diya-Graphics'
} }
{ #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
]
{ #category : #accessing } { #category : #accessing }
DiyaText >> data [ DiyaText >> data [
^ data ^ data
@ -19,6 +33,88 @@ DiyaText >> data: anObject [
data := anObject data := anObject
] ]
{ #category : #accessing }
DiyaText >> draw [
|face offset|
data ifNil: [ ^self ].
offset := 50.0@50.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 }.
face := DiyaFontManager uniqueInstance face: self fontStyle from: self fontName.
"set fontsize"
face setPixelWidth:0 height: self fontSize.
OpenGL pixelstorei: GL_UNPACK_ALIGNMENT param: 1.
"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].
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.
]
{ #category : #accessing }
DiyaText >> drawCharacter: c at: offset with: face [
|tex2D|
face loadCharacter: c asciiValue flags: (1 << 2) "FT_LOAD_RENDER".
tex2D := OpenGLTexImage2D new target: GL_TEXTURE_2D;
level: 0;
internalFormat: GL_RED;
width: face glyph width;
height: face glyph height;
border: 0;
format: GL_RED;
type: GL_UNSIGNED_BYTE;
data: (FTFaceRec fromHandle: face getHandle) glyph bitmap buffer;
yourself.
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.
"fill the buffer"
self fillVerticesBuffer: context buffer at: offset with: face.
context vbo subData: GL_ARRAY_BUFFER offset:0 data: context buffer.
OpenGL drawArrays: GL_TRIANGLES first:0 count:6.
offset setX: (offset x + ((face glyph advanceX )* (self scale x)) ) setY: offset y.
]
{ #category : #accessing }
DiyaText >> fillVerticesBuffer: buffer at: offset with: face [
|x y w h i|
x := offset x + ((face glyph hBearingX )*(self scale x)).
y := offset y - (((face glyph height) - (face glyph hBearingY))*(self scale y)).
w := (face glyph width)*(self scale x).
h := (face glyph height)*(self scale y).
i := 1.
{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. }
do: [ :e| buffer at:i put:e. i := i+1 ]
]
{ #category : #accessing }
DiyaText >> fontName [
^ fontName
]
{ #category : #accessing }
DiyaText >> fontName: anObject [
fontName := anObject
]
{ #category : #accessing } { #category : #accessing }
DiyaText >> fontSize [ DiyaText >> fontSize [
^ fontSize ^ fontSize
@ -42,7 +138,8 @@ DiyaText >> fontStyle: anObject [
{ #category : #initialization } { #category : #initialization }
DiyaText >> initialize [ DiyaText >> initialize [
super initialize. super initialize.
fontStyle := DiyaFontManager uniqueInstance face: 'Regular' from:'Ubuntu'. fontStyle := 'Regular'.
fontSize := 12. fontName := 'Ubuntu'.
fontSize := 20.
data := nil. data := nil.
] ]

View File

@ -22,16 +22,3 @@ void main()
} }
' '
] ]
{ #category : #accessing }
GLSimpleShader class >> vertextShader [
^ '
#ifdef GL_ES
precision mediump float;
#endif
void main()
{
gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;
}
'
]

23
Diya/GLTexShader.class.st Normal file
View File

@ -0,0 +1,23 @@
Class {
#name : #GLTexShader,
#superclass : #OpenGLSL,
#category : #'Diya-Shaders'
}
{ #category : #accessing }
GLTexShader class >> fragmentShader [
^ '
#ifdef GL_ES
precision mediump float;
#endif
uniform sampler2D u_texture;
uniform vec2 u_resolution;
void main(){
//vec2 uv = gl_FragCoord.xy / u_resolution;
vec4 sampled = vec4(1.0, 1.0, 1.0, texture2D(u_texture,gl_FragCoord.zw).r);
gl_FragColor = vec4(1.0,1.0,1.0,1.0)* sampled; //texture2D(u_texture,uv.xy);
}
'
]

View File

@ -13,6 +13,16 @@ OpenGL class >> begin: mode [
^ self ffiCall: #(void glBegin(GLenum mode)) ^ self ffiCall: #(void glBegin(GLenum mode))
] ]
{ #category : #'as yet unclassified' }
OpenGL class >> blendFnOn:buf sfactor: sfactor dfactor: dfactor [
^ self ffiCall: #(void glBlendFunci( GLuint buf,GLenum sfactor,GLenum dfactor))
]
{ #category : #'as yet unclassified' }
OpenGL class >> blendFnWithSfactor: sfactor dfactor: dfactor [
^ self ffiCall: #(void glBlendFunc( GLenum sfactor,GLenum dfactor))
]
{ #category : #accessing } { #category : #accessing }
OpenGL class >> checkSymbol [ OpenGL class >> checkSymbol [
^#glDrawArrays ^#glDrawArrays
@ -33,11 +43,21 @@ OpenGL class >> color3fR: red G: green B: blue [
^self ffiCall: #(void glColor3f(GLfloat red,GLfloat green,GLfloat blue)) ^self ffiCall: #(void glColor3f(GLfloat red,GLfloat green,GLfloat blue))
] ]
{ #category : #'as yet unclassified' }
OpenGL class >> disable: cap [
^ self ffiCall: #(void glDisable( GLenum cap))
]
{ #category : #'as yet unclassified' } { #category : #'as yet unclassified' }
OpenGL class >> drawArrays: mode first: idx count:n [ OpenGL class >> drawArrays: mode first: idx count:n [
^self ffiCall: #(void glDrawArrays(GLenum mode,GLint idx,GLsizei n)) ^self ffiCall: #(void glDrawArrays(GLenum mode,GLint idx,GLsizei n))
] ]
{ #category : #'as yet unclassified' }
OpenGL class >> enable: cap [
^ self ffiCall: #(void glEnable( GLenum cap))
]
{ #category : #accessing } { #category : #accessing }
OpenGL class >> end [ OpenGL class >> end [
^ self ffiCall: #(void glEnd( void )) ^ self ffiCall: #(void glEnd( void ))
@ -53,6 +73,11 @@ OpenGL class >> libNames [
^#('libGL.so.1') ^#('libGL.so.1')
] ]
{ #category : #accessing }
OpenGL class >> pixelstorei: pname param: param [
^self ffiCall: #(void glPixelStorei( GLenum pname,GLint param))
]
{ #category : #accessing } { #category : #accessing }
OpenGL class >> vertex3fX:x Y:y Z:z [ OpenGL class >> vertex3fX:x Y:y Z:z [
^self ffiCall: #(void glVertex3f( GLfloat x,GLfloat y,GLfloat z)) ^self ffiCall: #(void glVertex3f( GLfloat x,GLfloat y,GLfloat z))

View File

@ -7,13 +7,18 @@ Class {
'GL_ACTIVE_ATTRIBUTE_MAX_LENGTH', 'GL_ACTIVE_ATTRIBUTE_MAX_LENGTH',
'GL_ACTIVE_UNIFORMS', 'GL_ACTIVE_UNIFORMS',
'GL_ACTIVE_UNIFORM_MAX_LENGTH', 'GL_ACTIVE_UNIFORM_MAX_LENGTH',
'GL_ALPHA',
'GL_ARRAY_BUFFER', 'GL_ARRAY_BUFFER',
'GL_ARRAY_BUFFER_BINDING', 'GL_ARRAY_BUFFER_BINDING',
'GL_ATTACHED_SHADERS', 'GL_ATTACHED_SHADERS',
'GL_BLEND',
'GL_BLUE',
'GL_BYTE', 'GL_BYTE',
'GL_CLAMP_TO_EDGE',
'GL_COLOR_BUFFER_BIT', 'GL_COLOR_BUFFER_BIT',
'GL_COMPILE_STATUS', 'GL_COMPILE_STATUS',
'GL_COMPUTE_SHADER', 'GL_COMPUTE_SHADER',
'GL_CULL_FACE',
'GL_DELETE_STATUS', 'GL_DELETE_STATUS',
'GL_DEPTH_BUFFER_BIT', 'GL_DEPTH_BUFFER_BIT',
'GL_DOUBLE', 'GL_DOUBLE',
@ -23,31 +28,44 @@ Class {
'GL_FLOAT', 'GL_FLOAT',
'GL_FRAGMENT_SHADER', 'GL_FRAGMENT_SHADER',
'GL_GEOMETRY_SHADER', 'GL_GEOMETRY_SHADER',
'GL_GREEN',
'GL_HALF_FLOAT', 'GL_HALF_FLOAT',
'GL_INFO_LOG_LENGTH', 'GL_INFO_LOG_LENGTH',
'GL_INT', 'GL_INT',
'GL_INT_2_10_10_10_REV', 'GL_INT_2_10_10_10_REV',
'GL_LINEAR',
'GL_LINES', 'GL_LINES',
'GL_LINE_LOOP', 'GL_LINE_LOOP',
'GL_LINE_STRIP', 'GL_LINE_STRIP',
'GL_LINK_STATUS', 'GL_LINK_STATUS',
'GL_ONE_MINUS_SRC_ALPHA',
'GL_PACK_ALIGNMENT',
'GL_POINTS', 'GL_POINTS',
'GL_POLYGON', 'GL_POLYGON',
'GL_QUADS', 'GL_QUADS',
'GL_QUAD_STRIP', 'GL_QUAD_STRIP',
'GL_RED',
'GL_SHADER_SOURCE_LENGTH', 'GL_SHADER_SOURCE_LENGTH',
'GL_SHADER_TYPE', 'GL_SHADER_TYPE',
'GL_SHORT', 'GL_SHORT',
'GL_SRC_ALPHA',
'GL_STATIC_DRAW', 'GL_STATIC_DRAW',
'GL_STENCIL_BUFFER_BIT', 'GL_STENCIL_BUFFER_BIT',
'GL_TESS_CONTROL_SHADER', 'GL_TESS_CONTROL_SHADER',
'GL_TESS_EVALUATION_SHADER', 'GL_TESS_EVALUATION_SHADER',
'GL_TEXTURE_2D',
'GL_TEXTURE_BUFFER', 'GL_TEXTURE_BUFFER',
'GL_TEXTURE_MAG_FILTER',
'GL_TEXTURE_MIN_FILTER',
'GL_TEXTURE_UNIT_BASE',
'GL_TEXTURE_WRAP_S',
'GL_TEXTURE_WRAP_T',
'GL_TRIANGLES', 'GL_TRIANGLES',
'GL_TRIANGLE_FAN', 'GL_TRIANGLE_FAN',
'GL_TRIANGLE_STRIP', 'GL_TRIANGLE_STRIP',
'GL_TRUE', 'GL_TRUE',
'GL_UNIFORM_BUFFER', 'GL_UNIFORM_BUFFER',
'GL_UNPACK_ALIGNMENT',
'GL_UNSIGNED_BYTE', 'GL_UNSIGNED_BYTE',
'GL_UNSIGNED_INT', 'GL_UNSIGNED_INT',
'GL_UNSIGNED_INT_10F_11F_11F_REV', 'GL_UNSIGNED_INT_10F_11F_11F_REV',
@ -81,7 +99,11 @@ OpenGLConstants class >> initCommonConstants [
GL_DYNAMIC_DRAW := 16r88E8. GL_DYNAMIC_DRAW := 16r88E8.
GL_FALSE := 0. GL_FALSE := 0.
GL_TRUE := 1. GL_TRUE := 1.
GL_ARRAY_BUFFER_BINDING := 16r8894 GL_ARRAY_BUFFER_BINDING := 16r8894.
GL_RED := 16r1903.
GL_BLUE := 16r1905.
GL_GREEN := 16r1904.
GL_ALPHA := 16r1903.
] ]
{ #category : #'class initialization' } { #category : #'class initialization' }
@ -104,6 +126,12 @@ OpenGLConstants class >> initCommonMode [
GL_QUADS := 16r0007. GL_QUADS := 16r0007.
GL_QUAD_STRIP := 16r0008. GL_QUAD_STRIP := 16r0008.
GL_POLYGON := 16r0009. GL_POLYGON := 16r0009.
GL_BLEND := 16r0BE2.
GL_SRC_ALPHA := 16r0302.
GL_ONE_MINUS_SRC_ALPHA := 16r0303.
GL_CULL_FACE := 16r0B44.
GL_PACK_ALIGNMENT := 16r0D05.
GL_UNPACK_ALIGNMENT := 16r0CF5
] ]
{ #category : #'class initialization' } { #category : #'class initialization' }
@ -128,10 +156,24 @@ OpenGLConstants class >> initCommonShader [
GL_ACTIVE_UNIFORM_MAX_LENGTH := 16r8B87. GL_ACTIVE_UNIFORM_MAX_LENGTH := 16r8B87.
] ]
{ #category : #'class initialization' }
OpenGLConstants class >> initTextureConstants [
GL_TEXTURE_WRAP_S := 16r2802.
GL_TEXTURE_2D := 16r0DE1.
GL_CLAMP_TO_EDGE := 16r812F.
GL_TEXTURE_WRAP_T := 16r2803.
GL_TEXTURE_MIN_FILTER := 16r2801.
GL_TEXTURE_MAG_FILTER := 16r2800.
GL_LINEAR := 16r2601.
GL_TEXTURE_UNIT_BASE := 16r84C0.
]
{ #category : #'class initialization' } { #category : #'class initialization' }
OpenGLConstants class >> initialize [ OpenGLConstants class >> initialize [
self initCommonMode. self initCommonMode.
self initCommonMask. self initCommonMask.
self initCommonShader. self initCommonShader.
self initCommonConstants self initCommonConstants.
self initTextureConstants.
] ]

View File

@ -103,8 +103,17 @@ OpenGLSL class >> useProgram:program [
] ]
{ #category : #accessing } { #category : #accessing }
OpenGLSL class >> vertextShader [ OpenGLSL class >> vertexShader [
^ self subclassResponsibility ^ '
#ifdef GL_ES
precision mediump float;
#endif
uniform mat4 u_projection;
void main()
{
gl_Position = u_projection * vec4(gl_Vertex.xy, 0, 1);
}
'
] ]
{ #category : #initialization } { #category : #initialization }
@ -165,7 +174,7 @@ OpenGLSL >> compileFragmentShader:fragmentShaderID [
{ #category : #compiling } { #category : #compiling }
OpenGLSL >> compileVertexShader: vertexShaderID [ OpenGLSL >> compileVertexShader: vertexShaderID [
self getSourcePtr:self class vertextShader for: vertexShaderID. self getSourcePtr:self class vertexShader for: vertexShaderID.
OpenGLSL compileShader: vertexShaderID. OpenGLSL compileShader: vertexShaderID.
self checkStatus:GL_COMPILE_STATUS of: vertexShaderID self checkStatus:GL_COMPILE_STATUS of: vertexShaderID
@ -197,6 +206,7 @@ OpenGLSL >> initialize [
self addUniform: #u_time of: Uniform1F. self addUniform: #u_time of: Uniform1F.
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 setUpUniforms. self setUpUniforms.
self compile self compile
] ]

View File

@ -0,0 +1,110 @@
Class {
#name : #OpenGLTexImage2D,
#superclass : #DiyaBaseObject,
#instVars : [
'target',
'level',
'internalFormat',
'width',
'height',
'border',
'format',
'type',
'data'
],
#pools : [
'OpenGLConstants',
'OpenGLTypes'
],
#category : #'Diya-OpenGL'
}
{ #category : #accessing }
OpenGLTexImage2D >> border [
^ border
]
{ #category : #accessing }
OpenGLTexImage2D >> border: anObject [
border := anObject
]
{ #category : #accessing }
OpenGLTexImage2D >> data [
^ data
]
{ #category : #accessing }
OpenGLTexImage2D >> data: anObject [
data := anObject
]
{ #category : #accessing }
OpenGLTexImage2D >> format [
^ format
]
{ #category : #accessing }
OpenGLTexImage2D >> format: anObject [
format := anObject
]
{ #category : #accessing }
OpenGLTexImage2D >> height [
^ height
]
{ #category : #accessing }
OpenGLTexImage2D >> height: anObject [
height := anObject
]
{ #category : #accessing }
OpenGLTexImage2D >> internalFormat [
^ internalFormat
]
{ #category : #accessing }
OpenGLTexImage2D >> internalFormat: anObject [
internalFormat := anObject
]
{ #category : #accessing }
OpenGLTexImage2D >> level [
^ level
]
{ #category : #accessing }
OpenGLTexImage2D >> level: anObject [
level := anObject
]
{ #category : #accessing }
OpenGLTexImage2D >> target [
^ target
]
{ #category : #accessing }
OpenGLTexImage2D >> target: anObject [
target := anObject
]
{ #category : #accessing }
OpenGLTexImage2D >> type [
^ type
]
{ #category : #accessing }
OpenGLTexImage2D >> type: anObject [
type := anObject
]
{ #category : #accessing }
OpenGLTexImage2D >> width [
^ width
]
{ #category : #accessing }
OpenGLTexImage2D >> width: anObject [
width := anObject
]

119
Diya/OpenGLTexture.class.st Normal file
View File

@ -0,0 +1,119 @@
Class {
#name : #OpenGLTexture,
#superclass : #DiyaBaseObject,
#instVars : [
'textureID',
'unit'
],
#pools : [
'OpenGLConstants',
'OpenGLTypes'
],
#category : #'Diya-OpenGL'
}
{ #category : #'as yet unclassified' }
OpenGLTexture class >> active: unit [
^self ffiActive: GL_TEXTURE_UNIT_BASE + unit
]
{ #category : #'as yet unclassified' }
OpenGLTexture class >> bind: target texture: texture [
^ self ffiCall: #(void glBindTexture(GLenum target,GLuint texture))
]
{ #category : #'as yet unclassified' }
OpenGLTexture class >> bindDefault: target [
self bind: target texture: 0
]
{ #category : #'as yet unclassified' }
OpenGLTexture class >> delete: n pointer:textures [
^ self ffiCall: #(void glDeleteTextures(GLsizei n,const GLuint * textures))
]
{ #category : #'as yet unclassified' }
OpenGLTexture class >> ffiActive: unit [
^self ffiCall: #(void glActiveTexture( GLenum unit))
]
{ #category : #'library path' }
OpenGLTexture class >> ffiLibraryName [
^ OpenGL ffiLibraryName
]
{ #category : #'as yet unclassified' }
OpenGLTexture class >> fromUnit: unit [
^self new unit: unit; yourself
]
{ #category : #'as yet unclassified' }
OpenGLTexture class >> genTexture: n pointer: textures [
^ self ffiCall: #(void glGenTextures(GLsizei n,GLuint * textures))
]
{ #category : #'as yet unclassified' }
OpenGLTexture class >> image2D: target level: level internalformat: internalformat w: width h: height border: border format: fmt type: type data: data [
^ self ffiCall: #(void glTexImage2D( GLenum target,GLint level,GLint internalformat,GLsizei width,GLsizei height,GLint border,GLenum fmt,GLenum type,const void * data))
]
{ #category : #'as yet unclassified' }
OpenGLTexture class >> parameteri: target pname: pname param: param [
^ self ffiCall: #(void glTexParameteri( GLenum target,GLenum pname,GLint param))
]
{ #category : #initialization }
OpenGLTexture >> active [
self class active: self unit
]
{ #category : #initialization }
OpenGLTexture >> bind: target [
^ self class bind: target texture: self textureID
]
{ #category : #initialization }
OpenGLTexture >> delete [
self class delete: 1 pointer: textureID getHandle.
textureID free.
]
{ #category : #initialization }
OpenGLTexture >> initialize [
textureID := FFIExternalArray externalNewType: GLint size:1.
textureID autoRelease.
textureID at:1 put: -1.
self class genTexture: 1 pointer: textureID getHandle.
unit := 0.
]
{ #category : #initialization }
OpenGLTexture >> setImage2D: tex2D [
self active.
self bind: tex2D target.
self class image2D: tex2D target
level: tex2D level
internalformat: tex2D internalFormat
w: tex2D width
h: tex2D height
border: tex2D border
format: tex2D format
type: tex2D type
data: tex2D data
]
{ #category : #initialization }
OpenGLTexture >> textureID [
^textureID at: 1
]
{ #category : #accessing }
OpenGLTexture >> unit [
^ unit
]
{ #category : #accessing }
OpenGLTexture >> unit: anObject [
unit := anObject
]

View File

@ -74,7 +74,6 @@ OpenGLVertexArray >> bind [
{ #category : #accessing } { #category : #accessing }
OpenGLVertexArray >> delete [ OpenGLVertexArray >> delete [
OpenGLVertexArray deteleVertexArraysSize:1 arrays: vertexArrayID getHandle. OpenGLVertexArray deteleVertexArraysSize:1 arrays: vertexArrayID getHandle.
vertexArrayID at: 1 put: -1
] ]
{ #category : #'as yet unclassified' } { #category : #'as yet unclassified' }

View File

@ -46,29 +46,47 @@ OpenGLVertexBuffer class >> namedBufferData:buffer size: size data: data usage:
^self ffiCall: #(void glNamedBufferData(GLuint buffer,GLsizeiptr size,const void *data,GLenum usage)) ^self ffiCall: #(void glNamedBufferData(GLuint buffer,GLsizeiptr size,const void *data,GLenum usage))
] ]
{ #category : #'as yet unclassified' }
OpenGLVertexBuffer class >> subData:target offset: offset size: size data: data [
^self ffiCall: #(void glBufferSubData(GLenum target,GLintptr offset,GLsizeiptr size,const void * data))
]
{ #category : #'as yet unclassified' } { #category : #'as yet unclassified' }
OpenGLVertexBuffer >> bind: target [ OpenGLVertexBuffer >> bind: target [
^OpenGLVertexBuffer bind:target buffer: self vertexBufferID ^OpenGLVertexBuffer bind:target buffer: self vertexBufferID
] ]
{ #category : #'as yet unclassified' }
OpenGLVertexBuffer >> data:target data: data size: size usage: usage [
self bind: target.
^OpenGLVertexBuffer bufferData: target size: size data:(data ifNil:[data] ifNotNil: [data getHandle]) usage: usage
]
{ #category : #'as yet unclassified' } { #category : #'as yet unclassified' }
OpenGLVertexBuffer >> data:target data: data usage: usage [ OpenGLVertexBuffer >> data:target data: data usage: usage [
self bind: target.
^OpenGLVertexBuffer bufferData: target size: data size*4 data:data getHandle usage: usage ^OpenGLVertexBuffer bufferData: target size: data size*4 data:data getHandle usage: usage
] ]
{ #category : #'as yet unclassified' } { #category : #'as yet unclassified' }
OpenGLVertexBuffer >> delete [ OpenGLVertexBuffer >> delete [
OpenGLVertexBuffer deleteBuffersSize: 1 buffers: vertexBufferID getHandle. OpenGLVertexBuffer deleteBuffersSize: 1 buffers: vertexBufferID getHandle.
vertexBufferID free.
] ]
{ #category : #initialization } { #category : #initialization }
OpenGLVertexBuffer >> initialize [ OpenGLVertexBuffer >> initialize [
vertexBufferID := FFIExternalArray externalNewType: GLint size:1. vertexBufferID := FFIExternalArray externalNewType: GLint size:1.
vertexBufferID at:1 put: -1. vertexBufferID at:1 put: -1.
vertexBufferID autoRelease.
OpenGLVertexBuffer genVertexBuffersSize: 1 buffers: vertexBufferID getHandle OpenGLVertexBuffer genVertexBuffersSize: 1 buffers: vertexBufferID getHandle
] ]
{ #category : #'as yet unclassified' }
OpenGLVertexBuffer >> subData:target offset: offset data:data [
self bind: target.
^OpenGLVertexBuffer subData: target offset: offset size: data size * 4 data: data getHandle
]
{ #category : #initialization } { #category : #initialization }
OpenGLVertexBuffer >> vertexBufferID [ OpenGLVertexBuffer >> vertexBufferID [
^ vertexBufferID at: 1 ^ vertexBufferID at: 1

View File

@ -0,0 +1,96 @@
Class {
#name : #OrthoProjectionMatrix,
#superclass : #ProjectionMatrix,
#instVars : [
'viewport',
'near',
'far'
],
#category : #'Diya-Math'
}
{ #category : #'as yet unclassified' }
OrthoProjectionMatrix class >> fromDisplay: display [
^ self viewport: (Rectangle origin: 0@0 corner: (display w) @ (display h) ) near: -1.0 far: 1.0.
]
{ #category : #'as yet unclassified' }
OrthoProjectionMatrix class >> viewport:v near:n far:f [
^ self new setViewport: v near:n far:f; yourself.
]
{ #category : #accessing }
OrthoProjectionMatrix >> bottom [
^ viewport origin y
]
{ #category : #initialization }
OrthoProjectionMatrix >> calculate [
self at:1 at:1 put: (2.0 /(self right - self left)).
self at:2 at:2 put: (2.0 / (self top - self bottom)).
self at:3 at:3 put: (2.0 / (self far - self near)) negated.
self at:4 at:1 put: ((self right + self left)/ (self right - self left)) negated.
self at:4 at:2 put: ((self top + self bottom) / (self top - self bottom)) negated.
self at:4 at:3 put: ((self far + self near)/(self far - self near)) negated.
]
{ #category : #initialization }
OrthoProjectionMatrix >> default [
viewport := Rectangle origin: -1.0@ -1.0 corner: 1.0@1.0.
near := -1.0.
far := 1.0.
]
{ #category : #accessing }
OrthoProjectionMatrix >> far [
^ far
]
{ #category : #accessing }
OrthoProjectionMatrix >> far: anObject [
far := anObject
]
{ #category : #accessing }
OrthoProjectionMatrix >> left [
^ viewport origin x
]
{ #category : #accessing }
OrthoProjectionMatrix >> near [
^ near
]
{ #category : #accessing }
OrthoProjectionMatrix >> near: anObject [
near := anObject
]
{ #category : #accessing }
OrthoProjectionMatrix >> right [
^ viewport corner x
]
{ #category : #initialization }
OrthoProjectionMatrix >> setViewport: v near: n far:f [
viewport := v.
near := n.
far := f.
self update.
]
{ #category : #accessing }
OrthoProjectionMatrix >> top [
^ viewport corner y
]
{ #category : #accessing }
OrthoProjectionMatrix >> viewport [
^ viewport
]
{ #category : #accessing }
OrthoProjectionMatrix >> viewport: anObject [
viewport := anObject
]

View File

@ -0,0 +1,55 @@
Class {
#name : #ProjectionMatrix,
#superclass : #Array2D,
#instVars : [
'buffer'
],
#category : #'Diya-Math'
}
{ #category : #'instance creation' }
ProjectionMatrix class >> fromDisplay: display [
^self subclassResponsibility
]
{ #category : #accessing }
ProjectionMatrix >> buffer [
^ buffer
]
{ #category : #initialization }
ProjectionMatrix >> calculate [
self subclassResponsibility
]
{ #category : #initialization }
ProjectionMatrix >> default [
]
{ #category : #initialization }
ProjectionMatrix >> handle [
^buffer getHandle
]
{ #category : #initialization }
ProjectionMatrix >> initialize [
super initialize.
self rows:4 columns: 4 contents: ((Array new: 16) atAllPut: 0).
1 to: 4 do: [ :i| self at: i at:i put: 1.0 ].
buffer := FFIExternalArray externalNewType: #float size: self size.
buffer autoRelease.
self default.
self update
]
{ #category : #initialization }
ProjectionMatrix >> update [
|i|
self calculate.
i := 1.
self asArray do:[:e|
buffer at:i put:e.
i := i+1
].
]

36
Diya/TotoShader.class.st Normal file
View File

@ -0,0 +1,36 @@
Class {
#name : #TotoShader,
#superclass : #OpenGLSL,
#category : #'Diya-Shaders'
}
{ #category : #'as yet unclassified' }
TotoShader class >> fragmentShader [
^'
#version 330 core
in vec2 TexCoords;
uniform sampler2D text;
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;
} '
]
{ #category : #'as yet unclassified' }
TotoShader class >> vertexShader [
^'
#version 330 core
layout (location = 0) in vec4 vertex; // <vec2 pos, vec2 tex>
out vec2 TexCoords;
uniform mat4 u_projection;
void main()
{
gl_Position = u_projection * vec4(vertex.xy, 0.0, 1.0);
TexCoords = vertex.zw;
} '
]