Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9263191
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T13:31:35+00:00 2026-06-18T13:31:35+00:00

In OpenGL you can draw define points like this: glBegin(GL_POINTS); for(float theta=0, radius=60.0; radius>1.0;

  • 0

In OpenGL you can draw define points like this:

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();

How do you accomplish this same functionality in WebGL?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T13:31:36+00:00Added an answer on June 18, 2026 at 1:31 pm

    The code you wrote really doesn’t do much except define some points. To do that in WebGL could do it like this

    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;
    

    That would make 2 JavaScript arrays. You’d then need to put them to 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);
    

    After that though you need to write a shader and set it up. Shaders are a huge topic. For your particular data I’m guessing these shader would do

    A vertex shader

    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;
    }
    

    A fragment shader

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

    Next you need to initialize the shaders and parameters. I’m going to assume I put the shaders in script tags with ids “vshader” and “fshader” and use this boilerplate code to load them.

    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);
    

    and finally draw the points

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

    Here’s a snippet

    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>

    you might find these WebGL tutorials helpful.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am picking up the opengl es 2.0 with GLKit. I can successfully draw
I want to draw my opengl es animation in a customed UIView, how can
In OpenGL, you can actually draw text with an XYZ position, and it will
Considering QGLWidget (or OpenGL in general), what can be the easiest solution to draw
To draw a dotted line in OpenGL I can use glLineStipple, but how do
I'm doing a method to draw this using OpenGL, the drawing is in 2D.
Short Version How can I draw short text labels in an OpenGL mapping application
i'm learning opengl and I want to draw 50 texture(from one variable) this is
How can I draw a 2D crescent or moon shape in OpenGL? I have
Apple example of freehand drawing GLPaint , painting using OpenGL ES can't draw with

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.