Skip to content

Commit 8c67399

Browse files
committed
Added newTextureInit.
1 parent db77709 commit 8c67399

File tree

2 files changed

+51
-8
lines changed

2 files changed

+51
-8
lines changed

src/Graphics/WebGLTexture.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@
55

66
"use strict";
77

8+
exports.asArrayBufferView_ = function (it) {
9+
return (it);
10+
};
11+
812
exports.loadImage_ = function(name,continuation)
913
{return function()
1014
{var i = new Image();

src/Graphics/WebGLTexture.purs

Lines changed: 47 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -32,21 +32,30 @@ module Graphics.WebGLTexture
3232
, handleSubLoad2D
3333
, createTexture
3434
, newTexture
35+
, newTextureInit
3536

3637
, targetTypeToConst
3738

3839
)where
3940

40-
import Prelude (Unit, return, bind, otherwise, (+), (<), unit, (==), ($))
41+
import Prelude
4142
import Control.Monad.Eff.WebGL (WebGl, EffWebGL)
4243
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)
4451
import Data.Int.Bits ((.&.),(.|.))
4552
import Control.Monad.Eff (Eff)
4653
import Control.Monad (when)
4754
import Extensions (fail)
4855
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)
5059

5160
newtype WebGLTex = WebGLTex WebGLTexture
5261

@@ -197,6 +206,23 @@ newTexture width height filterSpec = do
197206
unbindTexture TEXTURE_2D
198207
return texture
199208

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+
200226
texParameteri :: forall eff. TexTarget -> TexParName -> GLint -> EffWebGL eff Unit
201227
texParameteri target pname param = runFn3 texParameteri_ (texTargetToConst target) (texParNameToConst pname) param
202228

@@ -223,17 +249,27 @@ texImage2D target level internalFormat format typ pixels =
223249
runFn6 texImage2D__ (targetTypeToConst target) level (internalFormatToConst internalFormat)
224250
(internalFormatToConst format) (textureTypeToConst typ) pixels
225251

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-
231252
texImage2DNull :: forall eff. TargetType -> GLint -> InternalFormat -> GLsizei -> GLsizei -> InternalFormat -> TextureType
232253
-> EffWebGL eff Unit
233254
texImage2DNull target level internalFormat width height format typ =
234255
runFn8 texImage2DNull_ (targetTypeToConst target) level (internalFormatToConst internalFormat)
235256
width height 0 (internalFormatToConst format) (textureTypeToConst typ)
236257

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+
237273
activeTexture :: forall eff. Int -> Eff (webgl :: WebGl | eff) Unit
238274
activeTexture n | n < _MAX_COMBINED_TEXTURE_IMAGE_UNITS = runFn1 activeTexture_ (_TEXTURE0 + n)
239275
| otherwise = fail "WebGLTexture>>activeTexture: wrong argument!"
@@ -246,6 +282,8 @@ createTexture = do
246282
uniform1i :: forall eff. WebGLUniformLocation -> GLint -> Eff (webgl :: WebGl | eff) Unit
247283
uniform1i = runFn2 uniform1i_
248284

285+
foreign import asArrayBufferView_ :: forall a . ArrayView a -> ArrayBufferView
286+
249287
foreign import loadImage_ :: forall a eff. Fn2 String
250288
(CanvasImageSource -> EffWebGL eff a)
251289
(EffWebGL eff Unit)
@@ -277,5 +315,6 @@ foreign import texImage2DNull_ :: forall eff. Fn8 GLenum
277315
GLenum
278316
(Eff (webgl :: WebGl | eff) Unit)
279317

318+
280319
foreign import bindTexture__ :: forall eff. Fn1 GLenum
281320
(Eff (webgl :: WebGl | eff) Unit)

0 commit comments

Comments
 (0)