texSubImage2D
不是你想要的功能。你遇到了三个问题:
texSubImage2D
不会将 的子集复制image
到 GL 纹理中。它以给定的偏移量复制整个image
GL 纹理。
texSubImage2D
texImage2D
只能修改现有的纹理数据,除非首先为 GL 纹理调用,否则会搞砸。
- 您使用的调用样式
texSubImage2D
需要一个像素数组而不是HTMLImageElement
.
这些方法有四种可能的签名:
// These two accept the normal HTMLImageElement, etc. for the last param.
texImage2D(enum target, int level, enum internalformat, enum format,
enum type, Object object);
texSubImage2D(enum target, int level, int xoffset, int yoffset,
enum format, enum type, Object object);
// These two accept a Uint8Array[] of pixels as the last parameter, despite
// being documented as wanting an ImageData object. The only reason these
// have a width/height param is *because* they take a pixel array, and GL
// doesn't know how large the image is.
texImage2D(enum target, int level, enum internalformat, long width,
long height, int border, enum format, enum type, Object pixels);
texSubImage2D(enum target, int level, int xoffset, int yoffset,
long width, long height, enum format, enum type, Object pixels);
要使用图像的前 32x32 像素创建纹理,请改为执行以下操作:
var spriteCanvas = document.createElement('canvas');
spriteCanvas.width = 32;
spriteCanvas.height = 32;
var spriteContext = spriteCanvas.getContext('2d');
spriteContext.drawImage(image, 0, 0);
gl.bindTexture(gl.TEXTURE_2D, texture);
gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, gl.RGBA, gl.UNSIGNED_BYTE, spriteCanvas);