简体   繁体   English

如何使用WebGl在画布上呈现二进制数据?

[英]How to render binary data on canvas using WebGl?

I am using PNaCl ffmpeg to open, read and decode RTSP stream. 我正在使用PNaCl ffmpeg打开,读取和解码RTSP流。 I am now having raw video frames which I need to transfer to WebGl to render on the canvas. 我现在有原始视频帧,需要将其传输到WebGl以便在画布上进行渲染。

How can I render binary data on the canvas? 如何在画布上呈现二进制数据?

I am running the following code: I presume that I should get a grey canvas after running this code, because I am passing RGBA values of (120,120,120,1) to the synthetic data. 我正在运行以下代码:我假设运行此代码后应该得到灰色画布,因为我正在将(120,120,120,1)的RGBA值传递给合成数据。

 var canvas = document.getElementById('canvas'); var gl = initWebGL(canvas); //function initializes webgl initViewport(gl, canvas); //initializes view port console.log('viewport initialized'); var data = []; for (var i = 0 ; i < 256; i++){ data.push(120,120,120,1.0); } console.log(data); var pixels = new Uint8Array(data); // 16x16 RGBA image var texture = gl.createTexture(); gl.bindTexture(gl.TEXTURE_2D, texture); gl.texImage2D( gl.TEXTURE_2D, // target 0, // mip level gl.RGBA, // internal format 16, 16, // width and height 0, // border gl.RGBA, //format gl.UNSIGNED_BYTE, // type pixels // texture data ); console.log('pixels'); console.log(pixels); 
 <canvas id="canvas"></canvas> 

I should get a grey 16x16 box being represented on the canvas, but I am not getting that. 我应该在画布上看到一个灰色的16x16框,但是我没有得到。 What additional steps do I need to take to correctly render the 2D bitmap on the canvas? 我需要采取什么其他步骤才能在画布上正确呈现2D位图?

PS. PS。 I am taking help from this article . 我正在从这篇文章中寻求帮助。

Console output: 控制台输出:

As pointed out in the comments, alpha in WebGL in the type of texture you're creating is 0 to 255. You're putting in 1.0 which = 1/255 or an alpha of 0.004 如评论中所指出,WebGL中要创建的纹理类型的Alpha为0到255。您要输入1.0,即= 1/255或Alpha为0.004

But on top of that you say 但最重要的是,你说

I am running the following code: I presume that I should get a grey canvas after running this code 我正在运行以下代码:我认为运行此代码后应该得到一个灰色画布。

That code is not enough for WebGL. 对于WebGL而言,该代码还不够。 WebGL requires you to supply a vertex shader and fragment shader , vertex data for vertices and then call either gl.drawArrays or gl.drawElements to render something. WebGL要求您提供顶点着色器和片段着色器 ,为顶点提供顶点数据,然后调用gl.drawArraysgl.drawElements进行渲染。 The code you provided doesn't do those things and without those things we can't tell what else you're doing. 您提供的代码不会执行这些操作,没有这些操作,我们将无法判断您在做什么。

You're also only supplying mip level 0. You either need to supply mips or set texture filtering so only the first level is used otherwise the texture is unrenderable (you'll get a warning about it in the JavaScript console of most browsers). 您还只提供了Mip级别0。您要么需要提供Mip级别,要么设置纹理过滤,以便仅使用第一级,否则纹理将无法渲染(您会在大多数浏览器的JavaScript控制台中收到有关它的警告)。

Here's a working example 这是一个有效的例子

 var canvas = document.getElementById('canvas'); var gl = canvas.getContext("webgl"); var data = []; for (var i = 0 ; i < 256; i++){ data.push(120,120,120,255); } var pixels = new Uint8Array(data); // 16x16 RGBA image var texture = gl.createTexture(); gl.bindTexture(gl.TEXTURE_2D, texture); gl.texImage2D( gl.TEXTURE_2D, // target 0, // mip level gl.RGBA, // internal format 16, 16, // width and height 0, // border gl.RGBA, //format gl.UNSIGNED_BYTE, // type pixels // texture data ); gl.generateMipmap(gl.TEXTURE_2D); // you need to do this or set filtering // compiles and links the shaders and looks up uniform and attribute locations var programInfo = twgl.createProgramInfo(gl, ["vs", "fs"]); var arrays = { position: [ -1, -1, 0, 1, -1, 0, -1, 1, 0, -1, 1, 0, 1, -1, 0, 1, 1, 0, ], }; // calls gl.createBuffer, gl.bindBuffer, gl.bufferData for each array var bufferInfo = twgl.createBufferInfoFromArrays(gl, arrays); var uniforms = { u_texture: texture, }; gl.useProgram(programInfo.program); // calls gl.bindBuffer, gl.enableVertexAttribArray, gl.vertexAttribPointer twgl.setBuffersAndAttributes(gl, programInfo, bufferInfo); // calls gl.activeTexture, gl.bindTexture, gl.uniformXXX twgl.setUniforms(programInfo, uniforms); // calls gl.drawArrays or gl.drawElements twgl.drawBufferInfo(gl, gl.TRIANGLES, bufferInfo); 
 canvas { border: 1px solid black; } 
 <script id="vs" type="notjs"> attribute vec4 position; varying vec2 v_texcoord; void main() { gl_Position = position; // Since we know we'll be passing in -1 to +1 for position v_texcoord = position.xy * 0.5 + 0.5; } </script> <script id="fs" type="notjs"> precision mediump float; uniform sampler2D u_texture; varying vec2 v_texcoord; void main() { gl_FragColor = texture2D(u_texture, v_texcoord); } </script> <script src="https://twgljs.org/dist/twgl.min.js"></script> <canvas id="canvas"></canvas> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM