@@ -32,21 +32,30 @@ module Graphics.WebGLTexture
32
32
, handleSubLoad2D
33
33
, createTexture
34
34
, newTexture
35
+ , newTextureInit
35
36
36
37
, targetTypeToConst
37
38
38
39
)where
39
40
40
- import Prelude ( Unit , return , bind , otherwise , (+), (<), unit , (==), ($))
41
+ import Prelude
41
42
import Control.Monad.Eff.WebGL (WebGl , EffWebGL )
42
43
import Graphics.WebGL (Uniform (Uniform))
43
- import Graphics.WebGLRaw (GLenum , GLint , GLsizei , WebGLUniformLocation , WebGLTexture , uniform1i_ , createTexture_ , _TEXTURE0 , activeTexture_ , _MAX_COMBINED_TEXTURE_IMAGE_UNITS , bindTexture_ , pixelStorei_ , texParameteri_ , _TEXTURE_2D , generateMipmap_ , _CLAMP_TO_EDGE , _LINEAR_MIPMAP_NEAREST , _LINEAR , _NEAREST , _TEXTURE_WRAP_T , _TEXTURE_WRAP_S , _TEXTURE_MAG_FILTER , _TEXTURE_MIN_FILTER , _TEXTURE_CUBE_MAP , _UNPACK_COLORSPACE_CONVERSION_WEBGL , _UNPACK_PREMULTIPLY_ALPHA_WEBGL , _UNPACK_FLIP_Y_WEBGL , _UNPACK_ALIGNMENT , _PACK_ALIGNMENT , _UNSIGNED_SHORT_5_5_5_1 , _UNSIGNED_SHORT_4_4_4_4 , _UNSIGNED_SHORT_5_6_5 , _FLOAT , _RGBA , _UNSIGNED_BYTE , _RGB , _LUMINANCE_ALPHA , _LUMINANCE , _ALPHA , _TEXTURE_CUBE_MAP_NEGATIVE_Z , _TEXTURE_CUBE_MAP_POSITIVE_Z , _TEXTURE_CUBE_MAP_NEGATIVE_Y , _TEXTURE_CUBE_MAP_POSITIVE_Y , _TEXTURE_CUBE_MAP_NEGATIVE_X , _TEXTURE_CUBE_MAP_POSITIVE_X )
44
+ import Graphics.WebGLRaw (texImage2D_ , GLenum , GLint , GLsizei , WebGLUniformLocation , WebGLTexture , uniform1i_ , createTexture_ ,
45
+ _TEXTURE0 , activeTexture_ , _MAX_COMBINED_TEXTURE_IMAGE_UNITS , bindTexture_ , pixelStorei_ , texParameteri_ , _TEXTURE_2D , generateMipmap_ ,
46
+ _CLAMP_TO_EDGE , _LINEAR_MIPMAP_NEAREST , _LINEAR , _NEAREST , _TEXTURE_WRAP_T , _TEXTURE_WRAP_S , _TEXTURE_MAG_FILTER , _TEXTURE_MIN_FILTER ,
47
+ _TEXTURE_CUBE_MAP , _UNPACK_COLORSPACE_CONVERSION_WEBGL , _UNPACK_PREMULTIPLY_ALPHA_WEBGL , _UNPACK_FLIP_Y_WEBGL , _UNPACK_ALIGNMENT ,
48
+ _PACK_ALIGNMENT , _UNSIGNED_SHORT_5_5_5_1 , _UNSIGNED_SHORT_4_4_4_4 , _UNSIGNED_SHORT_5_6_5 , _FLOAT , _RGBA , _UNSIGNED_BYTE , _RGB , _LUMINANCE_ALPHA ,
49
+ _LUMINANCE , _ALPHA , _TEXTURE_CUBE_MAP_NEGATIVE_Z , _TEXTURE_CUBE_MAP_POSITIVE_Z , _TEXTURE_CUBE_MAP_NEGATIVE_Y , _TEXTURE_CUBE_MAP_POSITIVE_Y ,
50
+ _TEXTURE_CUBE_MAP_NEGATIVE_X , _TEXTURE_CUBE_MAP_POSITIVE_X , ArrayBufferView )
44
51
import Data.Int.Bits ((.&.),(.|.))
45
52
import Control.Monad.Eff (Eff )
46
53
import Control.Monad (when )
47
54
import Extensions (fail )
48
55
import Graphics.Canvas (CanvasImageSource ())
49
- import Data.Function (Fn1 , Fn8 , Fn7 , Fn6 , Fn2 , runFn2 , runFn0 , runFn1 , runFn8 , runFn7 , runFn6 , runFn3 )
56
+ import Data.Function (Fn1 , Fn8 , Fn7 , Fn6 , Fn2 , runFn2 , runFn0 , runFn1 , runFn7 , runFn9 , runFn8 , runFn6 , runFn3 )
57
+ import Data.TypedArray (newInt8Array , newUint8Array )
58
+ import Data.ArrayBuffer.Types (ArrayView )
50
59
51
60
newtype WebGLTex = WebGLTex WebGLTexture
52
61
@@ -197,6 +206,23 @@ newTexture width height filterSpec = do
197
206
unbindTexture TEXTURE_2D
198
207
return texture
199
208
209
+ newTextureInit :: forall eff . Int -> Int -> TexFilterSpec -> EffWebGL eff WebGLTex
210
+ newTextureInit width height filterSpec = do
211
+ texture <- createTexture
212
+ let pixels = newUint8Array (width * height * 4 )
213
+ bindTexture TEXTURE_2D texture
214
+ texParameteri TTEXTURE_2D TEXTURE_MAG_FILTER (texFilterSpecToMagConst filterSpec)
215
+ texParameteri TTEXTURE_2D TEXTURE_MIN_FILTER (texFilterSpecToMinConst filterSpec)
216
+ when (((width .|. height) .&. 1 ) == 1 ) $ do
217
+ texParameteri TTEXTURE_2D TEXTURE_WRAP_S _CLAMP_TO_EDGE
218
+ texParameteri TTEXTURE_2D TEXTURE_WRAP_T _CLAMP_TO_EDGE
219
+ texImage2DPixels TEXTURE_2D 0 IF_RGBA width height IF_RGBA UNSIGNED_BYTE (asArrayBufferView_ pixels)
220
+ case filterSpec of
221
+ MIPMAP -> runFn1 generateMipmap_ _TEXTURE_2D
222
+ _ -> return unit
223
+ unbindTexture TEXTURE_2D
224
+ return texture
225
+
200
226
texParameteri :: forall eff . TexTarget -> TexParName -> GLint -> EffWebGL eff Unit
201
227
texParameteri target pname param = runFn3 texParameteri_ (texTargetToConst target) (texParNameToConst pname) param
202
228
@@ -223,17 +249,27 @@ texImage2D target level internalFormat format typ pixels =
223
249
runFn6 texImage2D__ (targetTypeToConst target) level (internalFormatToConst internalFormat)
224
250
(internalFormatToConst format) (textureTypeToConst typ) pixels
225
251
226
- texSubImage2D :: forall eff a . TargetType -> GLint -> GLint -> GLint -> InternalFormat -> TextureType -> a
227
- -> EffWebGL eff Unit
228
- texSubImage2D target level x y format typ pixels =
229
- runFn7 texSubImage2D__ (targetTypeToConst target) level x y (internalFormatToConst format) (textureTypeToConst typ) pixels
230
-
231
252
texImage2DNull :: forall eff . TargetType -> GLint -> InternalFormat -> GLsizei -> GLsizei -> InternalFormat -> TextureType
232
253
-> EffWebGL eff Unit
233
254
texImage2DNull target level internalFormat width height format typ =
234
255
runFn8 texImage2DNull_ (targetTypeToConst target) level (internalFormatToConst internalFormat)
235
256
width height 0 (internalFormatToConst format) (textureTypeToConst typ)
236
257
258
+ texImage2DPixels :: forall eff . TargetType -> GLint -> InternalFormat -> GLsizei -> GLsizei -> InternalFormat -> TextureType -> ArrayBufferView
259
+ -> EffWebGL eff Unit
260
+ texImage2DPixels target level internalFormat width height format typ pixels =
261
+ runFn9 texImage2D_ (targetTypeToConst target) level (internalFormatToConst internalFormat)
262
+ width height 0 (internalFormatToConst format) (textureTypeToConst typ) pixels
263
+
264
+
265
+
266
+ texSubImage2D :: forall eff a . TargetType -> GLint -> GLint -> GLint -> InternalFormat -> TextureType -> a
267
+ -> EffWebGL eff Unit
268
+ texSubImage2D target level x y format typ pixels =
269
+ runFn7 texSubImage2D__ (targetTypeToConst target) level x y (internalFormatToConst format) (textureTypeToConst typ) pixels
270
+
271
+
272
+
237
273
activeTexture :: forall eff . Int -> Eff (webgl :: WebGl | eff ) Unit
238
274
activeTexture n | n < _MAX_COMBINED_TEXTURE_IMAGE_UNITS = runFn1 activeTexture_ (_TEXTURE0 + n)
239
275
| otherwise = fail " WebGLTexture>>activeTexture: wrong argument!"
@@ -246,6 +282,8 @@ createTexture = do
246
282
uniform1i :: forall eff . WebGLUniformLocation -> GLint -> Eff (webgl :: WebGl | eff ) Unit
247
283
uniform1i = runFn2 uniform1i_
248
284
285
+ foreign import asArrayBufferView_ :: forall a . ArrayView a -> ArrayBufferView
286
+
249
287
foreign import loadImage_ :: forall a eff . Fn2 String
250
288
(CanvasImageSource -> EffWebGL eff a )
251
289
(EffWebGL eff Unit )
@@ -277,5 +315,6 @@ foreign import texImage2DNull_ :: forall eff. Fn8 GLenum
277
315
GLenum
278
316
(Eff (webgl :: WebGl | eff ) Unit )
279
317
318
+
280
319
foreign import bindTexture__ :: forall eff . Fn1 GLenum
281
320
(Eff (webgl :: WebGl | eff ) Unit )
0 commit comments