2022-03-08 23:30:01 +01:00
|
|
|
Class {
|
|
|
|
#name : #DiyaImageTex,
|
|
|
|
#superclass : #OpenGLTexImage2D,
|
|
|
|
#pools : [
|
|
|
|
'OpenGLConstants',
|
|
|
|
'OpenGLTypes'
|
|
|
|
],
|
|
|
|
#category : #'Diya-Graphics'
|
|
|
|
}
|
|
|
|
|
2022-08-09 03:14:23 +02:00
|
|
|
{ #category : #'instance creation' }
|
|
|
|
DiyaImageTex class >> fromDisplay: aRect as: assetName [
|
|
|
|
^ self new fromDisplay: aRect as: assetName; yourself
|
|
|
|
]
|
|
|
|
|
|
|
|
{ #category : #'instance creation' }
|
|
|
|
DiyaImageTex class >> fromDisplayAs: assetName [
|
|
|
|
^ self new fromDisplayAs: assetName; yourself
|
|
|
|
]
|
|
|
|
|
2022-03-08 23:30:01 +01:00
|
|
|
{ #category : #'instance creation' }
|
|
|
|
DiyaImageTex class >> fromFile: path [
|
|
|
|
^ self new fromFile: path; yourself
|
|
|
|
]
|
|
|
|
|
|
|
|
{ #category : #accessing }
|
|
|
|
DiyaImageTex >> drop [
|
2022-08-08 00:12:54 +02:00
|
|
|
"OpenGL
|
2022-03-08 23:30:01 +01:00
|
|
|
disable: GL_CULL_FACE;
|
2022-08-08 00:12:54 +02:00
|
|
|
disable: GL_BLEND."
|
2022-03-08 23:30:01 +01:00
|
|
|
]
|
|
|
|
|
2022-08-12 12:02:49 +02:00
|
|
|
{ #category : #conversion }
|
2022-08-09 03:14:23 +02:00
|
|
|
DiyaImageTex >> flipY [
|
2022-08-10 19:55:49 +02:00
|
|
|
|buffer size linesize top bottom|
|
|
|
|
size := self bytesSize.
|
|
|
|
linesize := width << 2.
|
|
|
|
buffer := FFIExternalArray externalNewType: GLubyte size: linesize.
|
2022-08-09 03:14:23 +02:00
|
|
|
LibC memset: buffer getHandle value: 0 size: buffer size.
|
2022-08-10 19:55:49 +02:00
|
|
|
0 to: (height >> 1) -1 do: [ :line|
|
|
|
|
top := line * linesize.
|
|
|
|
bottom := (size - (linesize * (line + 1))).
|
|
|
|
LibC memCopy: (data getHandle) + top to: buffer getHandle size: linesize.
|
|
|
|
LibC memCopy: (data getHandle) + bottom to: (data getHandle) + top size: linesize.
|
|
|
|
LibC memCopy: buffer getHandle to: (data getHandle) + bottom size: linesize.
|
2022-08-09 03:14:23 +02:00
|
|
|
].
|
2022-08-10 19:55:49 +02:00
|
|
|
buffer free
|
2022-08-09 03:14:23 +02:00
|
|
|
]
|
|
|
|
|
2022-08-12 12:02:49 +02:00
|
|
|
{ #category : #'instance creation' }
|
2022-08-09 03:14:23 +02:00
|
|
|
DiyaImageTex >> fromDisplay: aRect as: assetName [
|
2022-08-12 12:02:49 +02:00
|
|
|
data ifNotNil: [data free].
|
2022-08-10 19:55:49 +02:00
|
|
|
width := aRect extent x asInteger.
|
|
|
|
height := aRect extent y asInteger.
|
2022-08-14 15:42:59 +02:00
|
|
|
commited := false.
|
2022-08-10 19:55:49 +02:00
|
|
|
data := FFIExternalArray externalNewType: GLubyte size: self bytesSize.
|
2022-08-09 03:14:23 +02:00
|
|
|
LibC memset: data getHandle value: 0 size: data size.
|
|
|
|
data autoRelease.
|
|
|
|
OpenGL readPixelsOn: data getHandle
|
|
|
|
x: aRect origin x
|
|
|
|
y: (DiyaDisplay height) - (aRect origin y) - (aRect extent y)
|
|
|
|
w: aRect extent x
|
|
|
|
h: aRect extent y
|
|
|
|
format:GL_RGBA
|
|
|
|
type: GL_UNSIGNED_BYTE.
|
|
|
|
name := assetName.
|
|
|
|
self flipY.
|
|
|
|
]
|
|
|
|
|
2022-08-12 12:02:49 +02:00
|
|
|
{ #category : #'instance creation' }
|
2022-08-09 03:14:23 +02:00
|
|
|
DiyaImageTex >> fromDisplayAs: assetName [
|
|
|
|
self fromDisplay: (Rectangle origin: 0@0 extent: DiyaDisplay extent ) as: assetName
|
|
|
|
]
|
|
|
|
|
2022-03-08 23:30:01 +01:00
|
|
|
{ #category : #'instance creation' }
|
2022-03-21 18:03:15 +01:00
|
|
|
DiyaImageTex >> fromFile: aPath [
|
2022-08-12 12:02:49 +02:00
|
|
|
|surface|
|
2022-08-13 15:55:51 +02:00
|
|
|
self stdlog: 'Loading texture from ', aPath fullName.
|
2022-08-12 12:02:49 +02:00
|
|
|
data ifNotNil: [ data free ].
|
|
|
|
name := aPath fullName.
|
|
|
|
surface := self surfaceFromFile: aPath.
|
2022-08-09 00:22:18 +02:00
|
|
|
width := surface w.
|
|
|
|
height := surface h.
|
2022-08-12 12:02:49 +02:00
|
|
|
data := FFIExternalArray externalNewType: GLubyte size: self bytesSize.
|
|
|
|
LibC memset: data getHandle value: 0 size: data size.
|
|
|
|
data autoRelease.
|
|
|
|
LibC memCopy: surface pixels getHandle to: data getHandle size: data size.
|
|
|
|
SDL2
|
|
|
|
freeSurface: surface.
|
2022-08-14 15:42:59 +02:00
|
|
|
commited := false.
|
2022-08-13 15:55:51 +02:00
|
|
|
self stdlog: 'Loaded ', aPath fullName.
|
2022-03-08 23:30:01 +01:00
|
|
|
]
|
|
|
|
|
2022-08-12 12:02:49 +02:00
|
|
|
{ #category : #'instance creation' }
|
|
|
|
DiyaImageTex >> fromForm: aForm name: aName [
|
|
|
|
|surface|
|
|
|
|
name := aName.
|
|
|
|
data ifNotNil: [ data free ].
|
|
|
|
surface := self surfaceFromForm: aForm.
|
|
|
|
width := surface w.
|
|
|
|
height := surface h.
|
2022-08-14 15:42:59 +02:00
|
|
|
commited := false.
|
2022-08-12 12:02:49 +02:00
|
|
|
data := FFIExternalArray externalNewType: GLubyte size: self bytesSize.
|
|
|
|
LibC memset: data getHandle value: 0 size: data size.
|
|
|
|
data autoRelease.
|
|
|
|
LibC memCopy: surface pixels getHandle to: data getHandle size: data size.
|
|
|
|
SDL2
|
|
|
|
freeSurface: surface.
|
|
|
|
]
|
|
|
|
|
2022-03-08 23:30:01 +01:00
|
|
|
{ #category : #'instance creation' }
|
|
|
|
DiyaImageTex >> initialize [
|
|
|
|
super initialize.
|
|
|
|
level := 0.
|
|
|
|
border := 0.
|
|
|
|
format := GL_RGBA.
|
|
|
|
internalFormat := GL_RGBA.
|
|
|
|
type := GL_UNSIGNED_BYTE.
|
|
|
|
target := GL_TEXTURE_2D.
|
|
|
|
]
|
|
|
|
|
|
|
|
{ #category : #accessing }
|
|
|
|
DiyaImageTex >> mipmap [
|
|
|
|
^false
|
|
|
|
]
|
|
|
|
|
|
|
|
{ #category : #accessing }
|
|
|
|
DiyaImageTex >> setup [
|
|
|
|
OpenGLTexture
|
|
|
|
parameteri: GL_TEXTURE_2D pname: GL_TEXTURE_WRAP_S param: GL_CLAMP_TO_EDGE;
|
|
|
|
parameteri: GL_TEXTURE_2D pname: GL_TEXTURE_WRAP_T param: GL_CLAMP_TO_EDGE;
|
|
|
|
parameteri: GL_TEXTURE_2D pname: GL_TEXTURE_MIN_FILTER param: GL_LINEAR;
|
|
|
|
parameteri: GL_TEXTURE_2D pname: GL_TEXTURE_MAG_FILTER param: GL_LINEAR.
|
|
|
|
]
|
2022-08-09 00:22:18 +02:00
|
|
|
|
2022-08-12 12:02:49 +02:00
|
|
|
{ #category : #conversion }
|
|
|
|
DiyaImageTex >> surfaceFromFile: aPath [
|
|
|
|
aPath exists ifFalse: [ ^DiyaCoreAPIError signal:'File not found ', aPath fullName ].
|
|
|
|
[^ SDL2Image SDLImgLoad: aPath fullName] on: Error do: [
|
|
|
|
^ self surfaceFromForm: (ImageReadWriter formFromFileNamed: aPath)
|
|
|
|
].
|
|
|
|
]
|
|
|
|
|
|
|
|
{ #category : #conversion }
|
|
|
|
DiyaImageTex >> surfaceFromForm: aForm [
|
|
|
|
|srcSurface destSurface form|
|
|
|
|
form := aForm asFormOfDepth: 32.
|
|
|
|
srcSurface :=
|
|
|
|
SDL2 createRGBSurfaceFromPixels: form bits
|
|
|
|
width: form width height: form height
|
|
|
|
depth: 32 pitch: (form width << 2)
|
|
|
|
rmask: 16r00ff0000
|
|
|
|
gmask: 16r000ff00
|
|
|
|
bmask: 16r00000ff
|
|
|
|
amask: 16rff000000.
|
|
|
|
destSurface :=
|
|
|
|
SDL2 createRGBSurfaceFromPixels: srcSurface pixels getHandle
|
|
|
|
width: form width height: form height
|
|
|
|
depth: 32 pitch: (form width << 2)
|
|
|
|
rmask: 16r000000ff
|
|
|
|
gmask: 16r0000ff00
|
|
|
|
bmask: 16r00ff0000
|
|
|
|
amask: 16rff000000.
|
|
|
|
SDL2 SDLBlitSurface: srcSurface srcRect: nil dest: destSurface dstRect: nil.
|
|
|
|
SDL2
|
|
|
|
freeSurface: srcSurface.
|
|
|
|
^ destSurface
|
2022-08-09 00:22:18 +02:00
|
|
|
]
|