WebGL中的OpenGL点功能

时间:2013-02-08 03:01:26

标签: opengl-es webgl

在OpenGL中,你可以 draw 定义这样的点:

glBegin(GL_POINTS);
for(float theta=0, radius=60.0; radius>1.0; theta+=0.1, radius-=0.3){
   glColor3f(radius/60.0,0.3,1-(radius/60.0));
   glVertex2i(200+radius*cos(theta),200+radius*sin(theta));
}
glEnd();

如何在WebGL中实现相同的功能?

2 个答案:

答案 0 :(得分:13)

你写的代码除了定义一些要点之外没什么用。要在WebGL中执行此操作,可以像这样做

var colors = [];
var verts = [];
var theta=0 
for(var radius=60.0; radius>1.0; radius-=0.3) {
  colors.push(radius/60.0, 0.3, 1-(radius/60.0));
  verts.push(200+radius*Math.cos(theta),200+radius*Math.sin(theta));
  theta+=0.1;
}
var numPoints = colors.length / 3;

这会产生2个JavaScript数组。然后,您需要将它们放到WebGLBuffers

var colorBuffer = gl.createBuffer();
gl.bindBuffer(gl.ARRAY_BUFFER, colorBuffer);
gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(colors), gl.STATIC_DRAW);

var vertBuffer = gl.createBuffer();
gl.bindBuffer(gl.ARRAY_BUFFER, vertBuffer);
gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(verts), gl.STATIC_DRAW);

之后,您需要编写着色器并进行设置。着色器是一个很大的话题。对于您的特定数据,我猜这些着色器会做

顶点着色器

uniform mat4 u_matrix;
attribute vec4 a_vertex;
attribute vec4 a_color;
varying vec4 v_color;

void main() {
  // Set the size of the point
  gl_PointSize = 3.0;

  // multiply each vertex by a matrix.
  gl_Position = u_matrix * a_vertex;

  // pass the color to the fragment shader
  v_color = a_color;
}

片段着色器

precision mediump float;
varying vec4 v_color;
void main() {
  gl_FragColor = v_color;
}

接下来,您需要初始化着色器和参数。我将假设我将着色器放在带有id“vshader”和“fshader”以及use this boilerplate code的脚本标签中以加载它们。

var program = createProgramFromScriptTags(gl, "vshader", "fshader");
gl.useProgram(program);

// look up the locations for the inputs to our shaders.
var u_matLoc = gl.getUniformLocation(program, "u_matrix");
var colorLoc = gl.getAttribLocation(program, "a_color");
var vertLoc = gl.getAttribLocation(program, "a_vertex");

// Set the matrix to some that makes 1 unit 1 pixel.
gl.uniformMatrix4fv(u_matLoc, false, [
  2 / width, 0, 0, 0,
  0, 2 / height, 0, 0,
  0, 0, 1, 0,
  0, 0, 0, 1
]);

// Tell the shader how to get data out of the buffers.
gl.bindBuffer(gl.ARRAY_BUFFER, colorBuffer);
gl.vertexAttribPointer(colorLoc, 3, gl.FLOAT, false, 0, 0);
gl.enableVertexAttribArray(colorLoc);
gl.bindBuffer(gl.ARRAY_BUFFER, vertBuffer);
gl.vertexAttribPointer(vertLoc, 2, gl.FLOAT, false, 0, 0);
gl.enableVertexAttribArray(vertLoc);

最后画出分数

gl.drawArrays(gl.POINTS, 0, numPoints);

这是一个片段

var canvas = document.getElementById("c");
var gl = canvas.getContext("webgl");
if (!gl) {
    alert("no WebGL");
    //return;
}

var colors = [];
var verts = [];
var theta=0 
for(var radius=160.0; radius>1.0; radius-=0.3) {
    colors.push(radius/160.0, 0.3, 1-(radius/160.0));
    verts.push(radius*Math.cos(theta),radius*Math.sin(theta));
    theta+=0.1;
}
var numPoints = colors.length / 3;

var colorBuffer = gl.createBuffer();
gl.bindBuffer(gl.ARRAY_BUFFER, colorBuffer);
gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(colors), gl.STATIC_DRAW);

var vertBuffer = gl.createBuffer();
gl.bindBuffer(gl.ARRAY_BUFFER, vertBuffer);
gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(verts), gl.STATIC_DRAW);

var program = twgl.createProgramFromScripts(gl, ["vshader", "fshader"]);
gl.useProgram(program);

// look up the locations for the inputs to our shaders.
var u_matLoc = gl.getUniformLocation(program, "u_matrix");
var colorLoc = gl.getAttribLocation(program, "a_color");
var vertLoc = gl.getAttribLocation(program, "a_vertex");

function draw() {
    gl.clear(gl.COLOR_BUFFER_BIT);
    gl.clearColor(1.0, 1.0, 1.0, 1.0);
    
 // Set the matrix to some that makes 1 unit 1 pixel.
gl.uniformMatrix4fv(u_matLoc, false, [
    2 / canvas.width, 0, 0, 0,
    0, -2 / canvas.height, 0, 0,
    0, 0, 1, 0,
    0, 0, 0, 1
]);
    
    gl.bindBuffer(gl.ARRAY_BUFFER, colorBuffer);
    gl.vertexAttribPointer(colorLoc, 3, gl.FLOAT, false, 0, 0);
    gl.enableVertexAttribArray(colorLoc);
    gl.bindBuffer(gl.ARRAY_BUFFER, vertBuffer);
    gl.vertexAttribPointer(vertLoc, 2, gl.FLOAT, false, 0, 0);
    gl.enableVertexAttribArray(vertLoc);
    
    gl.drawArrays(gl.POINTS, 0, numPoints);
    
    requestAnimationFrame(draw, canvas);
}

draw();
canvas { border: 1px solid black; }
<script src="https://twgljs.org/dist/3.x/twgl.min.js"></script>
<script id="vshader" type="whatever">
    uniform mat4 u_matrix;
    attribute vec4 a_vertex;
    attribute vec4 a_color;
    varying vec4 v_color;

    void main() {
      // Set the size of the point
      gl_PointSize = length(a_vertex) * 0.1;
    
      // multiply each vertex by a matrix.
      gl_Position = u_matrix * a_vertex;

      // pass the color to the fragment shader
      v_color = a_color;
    }    
</script>
<script id="fshader" type="whatever">
precision mediump float;
varying vec4 v_color;
void main() {
    gl_FragColor = v_color;
}
</script>
<canvas id="c" width="400" height="400"></canvas>

您可能会找到these WebGL tutorials helpful

答案 1 :(得分:2)

WebGL基于OpenGL ES 2.0(参见here),它降低了对即时模式的支持。

  

此规范描述了HTML 5 canvas元素[CANVAS]的附加呈现上下文和支持对象。此上下文允许使用符合OpenGL ES 2.0 API的API进行渲染。

您需要使用顶点缓冲区来存储顶点数据。有关保留模式下工作原理的详细说明,请参阅here 1 。并there为一个很好的小例子让你开始。

1 :感谢在此发布此内容的人。