Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/image/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Image 如果必须使用“新建图像”在WebGL中渲染图像_Image_Browser_Webgl_Pixel - Fatal编程技术网

Image 如果必须使用“新建图像”在WebGL中渲染图像

Image 如果必须使用“新建图像”在WebGL中渲染图像,image,browser,webgl,pixel,Image,Browser,Webgl,Pixel,我看到的WebGL绘图图像示例都使用DOM图像对象: var image = new Image(); image.src = "resources/f-texture.png"; image.addEventListener('load', function() { // Now that the image has loaded make copy it to the texture. gl.bindTexture(gl.TEXTURE_2D, texture); gl.texI

我看到的WebGL绘图图像示例都使用DOM图像对象:

var image = new Image();
image.src = "resources/f-texture.png";
image.addEventListener('load', function() {
  // Now that the image has loaded make copy it to the texture.
  gl.bindTexture(gl.TEXTURE_2D, texture);
  gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, gl.RGBA,gl.UNSIGNED_BYTE, image);
  gl.generateMipmap(gl.TEXTURE_2D);
});
想知道是否有办法将像素放在ArrayBuffer或其他东西中,而不是使用
图像
对象,然后将其绘制为图像。如果是这样的话,一般来说,代码似乎可以实现这一点。那就太好了,因为这样我就可以将像素数据用于其他事情,这样就不会重复下载图像像素数据。

您可能应该这样做

是的,可以从ArrayBuffer将数据加载到纹理

gl.bindTexure(gl.TEXTURE_2D, tex);
const data = new Uint32Array([
   255, 0, 0, 255, // red
   0, 255, 0, 255, // green
   0, 0, 255, 255, // blue
   255, 255, 0, 255, // yellow
]);
const level = 0;
const internalFormat = gl.RGBA;
const width = 2;
const height = 2;
const border = 0;
const format = gl.RGBA;
const type = gl.UNSIGNED_BYTE
gl.texImage2D(gl.TEXTURE_2D, level, internalFormat, width, height, border,
              format, type, data);
gl.generateMipmap(gl.TEXTURE_2D);