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 8681847
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T21:36:52+00:00 2026-06-12T21:36:52+00:00

I’m reading here . The initBuffers function is used to store the vertices of

  • 0

I’m reading here.
The initBuffers function is used to store the vertices of the object we need to draw into the Buffer.

var triangleVertexPositionBuffer;
var triangleVertexColorBuffer;
function initBuffers() {
    triangleVertexPositionBuffer = gl.createBuffer();
    gl.bindBuffer(gl.ARRAY_BUFFER, triangleVertexPositionBuffer);
    var vertices = [
         0.0, 1.0, 0.0,
        -1.0, -1.0, 0.0,
         1.0, -1.0, 0.0
    ];
    gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(vertices), gl.STATIC_DRAW);
    triangleVertexPositionBuffer.itemSize = 3;
    triangleVertexPositionBuffer.numItems = 3;

    triangleVertexColorBuffer = gl.createBuffer();
    gl.bindBuffer(gl.ARRAY_BUFFER, triangleVertexColorBuffer);
    var colors = [
        1.0, 0.0, 0.0, 1.0,
        0.0, 1.0, 0.0, 1.0,
        0.0, 0.0, 1.0, 1.0,
    ];
    gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(colors), gl.STATIC_DRAW);
    triangleVertexColorBuffer.itemSize = 4;
    triangleVertexColorBuffer.numItems = 3;
}

Now this is some kind of a “shape” defined for a triangle. Now, if I want shapes of all types and sizes I would have to keep a buffer like triangleVertexPositionBuffer for each shape ,right? And it has to be initialized right?

Is new buffer object for every new shape is a must?

I plan to create a system, where all kinds of shapes can be created during runtime say by using a button.
So is there a solution to this issue?
How do I create new Buffers during runtime? Is there a common already existing solution?

Will it hinder performance if I create an array of buffer objects?

  • 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-12T21:36:54+00:00Added an answer on June 12, 2026 at 9:36 pm

    WebGl doesn’t care if you call initBuffers every frame. Well, the performance might.

    As javascript is interpreted language, the concept of runtime vs. compile time doesn’t really exist. One advanced thing however is that the shaders required by webgl can be generated from “literal” strings, generated at runtime as well as from external files and html elements.

    There are webgl-infrastructures available e.g. three.js, but I think Javascript itself is the answer.

    EDIT: About your additional question in the comment. OpenGL and WebGL works that essentially how you described, but with matrices, which are more flexible than just giving an offset to the object. Also there are ‘index-buffers’, that help sharing vertices between triangles.

    CubeCorners = [0,0,0,   0,0,1,  0,1,0,  0,1,1,  1,0,0,  1,0,1,  1,1,0,  1,1,1 ]; 
    // There are 8 corners in a cube
    //              0----1  <-- vertices (aka corners, with x=0)
    //             /|   /|
    //            4-+--5 |
    //            | 2--|-3
    //            |/   |/
    //            6----7 
    Triangles = [0,1,2, 1,3,2, 6,4,2, 2,4,0, 5,4,7, 4,6,7,  ... + 6 other triangles ];
    //
    TransScaleMatrix = [X,0,0,0, 0,Y,0,0, 0,0,Z,0, ox,oy,oz,1];
    // scales the object and sets an internal origin (around which object is rotated)
    RotMatrix = [rx,ry,rz, 0, ux,uy,yz,0, tx,ty,tz, 0, Ox,Oy,Oz,1];
    // aligns the objects orthogonal up,right,toward vectors to r,u,t, (i.e. rotates it)
    // and translates it to new origin O.
    CameraOrientationMatrix = [  ... ... ];
    // sets cameras lookat, up and right vector + position
    PerspectiveMatrix = [ ... ...];
    // transforms viewing frustums 6 planes left,right,top,bottom,near and far planes
    // to unit cube. (objects outside this cube are clipped away)
    

    With these arrays one typically renders a hierarchical scene, where vertices and objects are reused. On can place the cube not only in two different positions, but to different scale and different orientation. Then once the scene is modeled in “World coordinates”, one places the camera to some place and some orientation and applies also a perspective matrix to transform the coordinates to “clip space” that models screen coordinates x,y and depth z.

    The beauty of these matrices is that one can premultiply each of these into a single matrixthat performs everything mentioned. Or one can split this chain of operation into the best possible amount of operations, that are either calculated on the fly in shaders, or are precalculated in the vertex buffers. One should think carefully an optimal balance of reusing objects, as the bottleneck in rendering is typically the amount of drawElements/drawArray -calls and state changes (gl.useProgram) instead of the number of vertices per object.

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

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I need a function that will clean a strings' special characters. I do NOT
I used javascript for loading a picture on my website depending on which small
I am reading a book about Javascript and jQuery and using one of the
this is what i have right now Drawing an RSS feed into the php,
I have a French site that I want to parse, but am running into
In my XML file chapters tag has more chapter tag.i need to display chapters
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out

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.