WebGL Support – Pyramid Position Points Not Lining Up Properly

below you will see my web gl .js file and .html and initshader.js files are correct so I did not attach. I am needing to create a pyramid that rotates about the x, y, z axis with the click function.

I am fairly sure I know where the issue in the code is and have marked that area. I have tried changing the points multiple different times and still cannot get the pyramid to line up as expected in photo.

expect output

  • .js file:
                "use strict";
                
                var canvas;
                var gl;
                
                var numPositions  = 12;
                
                var positions = [];
                var colors = [];
                
                var xAxis = 0;
                var yAxis = 1;
                var zAxis = 2;
                
                var axis = 0;
                var theta = [0, 0, 0];
                
                var thetaLoc;
                
                window.onload = function init()
                {
                    canvas = document.getElementById("gl-canvas");
                
                    gl = canvas.getContext('webgl2');
                    if (!gl) alert("WebGL 2.0 isn't available");
                
                    colorPyramid();
                
                    gl.viewport(0, 0, canvas.width, canvas.height);
                    gl.clearColor(1.0, 1.0, 1.0, 1.0);
                
                    gl.enable(gl.DEPTH_TEST);
                
                    //
                    //  Load shaders and initialize attribute buffers
                    //
                    var program = initShaders(gl, "vertex-shader", "fragment-shader");
                    gl.useProgram(program);
                
                    var cBuffer = gl.createBuffer();
                    gl.bindBuffer(gl.ARRAY_BUFFER, cBuffer);
                    gl.bufferData(gl.ARRAY_BUFFER, flatten(colors), gl.STATIC_DRAW);
                
                    var colorLoc = gl.getAttribLocation( program, "aColor" );
                    gl.vertexAttribPointer( colorLoc, 4, gl.FLOAT, false, 0, 0 );
                    gl.enableVertexAttribArray( colorLoc );
                
                    var vBuffer = gl.createBuffer();
                    gl.bindBuffer(gl.ARRAY_BUFFER, vBuffer);
                    gl.bufferData(gl.ARRAY_BUFFER, flatten(positions), gl.STATIC_DRAW);
                
                
                    var positionLoc = gl.getAttribLocation(program, "aPosition");
                    gl.vertexAttribPointer(positionLoc, 4, gl.FLOAT, false, 0, 0);
                    gl.enableVertexAttribArray(positionLoc);
                
                    thetaLoc = gl.getUniformLocation(program, "uTheta");
                
                    //event listeners for buttons
                
                    document.getElementById( "xButton" ).onclick = function () {
                        axis = xAxis;
                    };
                    document.getElementById( "yButton" ).onclick = function () {
                        axis = yAxis;
                    };
                    document.getElementById( "zButton" ).onclick = function () {
                        axis = zAxis;
                    };
                
                    render();
                }
                
                function colorPyramid()
                {
                    triple(1, 0, 3); //the issue is with these points
                    triple(2, 3, 0);
                    triple(3, 1, 2);
                    triple(0, 2, 1);
                }
                
                function triple(a, b, c)
                {
                    var vertices = [
                        vec3(-0.5, -0.2722,  0.2886),
                        vec3(0.0,  -0.2772,  -0.5773),
                        vec3(-0.5,  -0.2722,  0.2886),
                        vec3(0.0, 0.5443,  0.0)
                    ];
                
                    var vertexColors = [
                        vec4(0.0, 0.0, 0.0, 1.0),  // black
                        vec4(1.0, 0.0, 0.0, 1.0),  // red
                        vec4(1.0, 0.0, 1.0, 1.0),  // magenta
                        vec4(0.0, 1.0, 1.0, 1.0)  // cyan
                    ];
                
                    // We need to parition the quad into two triangles in order for
                    // WebGL to be able to render it.  In this case, we create two
                    // triangles from the quad indices
                
                    //vertex color assigned by the index of the vertex
                
                    var indices = [a, b, c];
                
                    for ( var i = 0; i < indices.length; ++i ) {
                        positions.push( vertices[indices[i]] );
                        //colors.push( vertexColors[indices[i]] );
                
                        // for solid colored faces use
                        colors.push(vertexColors[a]);
                    }
                }
                
                function render()
                {
                    gl.clear( gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT);
                
                    theta[axis] += 2.0;
                    gl.uniform3fv(thetaLoc, theta);
                
                    gl.drawArrays(gl.TRIANGLES, 0, numPositions);
                    requestAnimationFrame(render);
                }
                
  • .html file:
<!DOCTYPE html>
<html>

<script id="vertex-shader" type="x-shader/x-vertex">
#version 300 es

in  vec4 aPosition;
in  vec4 aColor;
out vec4 vColor;

uniform vec3 uTheta;

void main()
{
    // Compute the sines and cosines of theta for each of
    //   the three axes in one computation.
    vec3 angles = radians(uTheta);
    vec3 c = cos(angles);
    vec3 s = sin(angles);

    // Remeber: thse matrices are column-major
    mat4 rx = mat4(1.0,  0.0,  0.0, 0.0,
            0.0,  c.x,  s.x, 0.0,
            0.0, -s.x,  c.x, 0.0,
            0.0,  0.0,  0.0, 1.0);

    mat4 ry = mat4(c.y, 0.0, -s.y, 0.0,
            0.0, 1.0,  0.0, 0.0,
            s.y, 0.0,  c.y, 0.0,
            0.0, 0.0,  0.0, 1.0);


    mat4 rz = mat4(c.z, s.z, 0.0, 0.0,
            -s.z,  c.z, 0.0, 0.0,
            0.0,  0.0, 1.0, 0.0,
            0.0,  0.0, 0.0, 1.0);

    vColor = aColor;
    gl_Position = rz * ry * rx * aPosition;
    gl_Position.z = -gl_Position.z;
}
</script>

<script id="fragment-shader" type="x-shader/x-fragment">
#version 300 es

precision mediump float;

in vec4 vColor;
out vec4 fColor;

void
main()
{
    fColor = vColor;
}
</script>

<script type="text/javascript" src="../SmithLarisa Project 4/utility.js"></script>
<script type="text/javascript" src="../SmithLarisa Project 4/initShaders.js"></script>
<script type="text/javascript" src="../SmithLarisa Project 4/MV.js"></script>
<script type="text/javascript" src="../SmithLarisa Project 4/SmithLarisaPyramid.js"></script>

<body>
<canvas id="gl-canvas" width="512" height="512">
Oops ... your browser doesn't support the HTML5 canvas element
</canvas>

<br/>

<button id= "xButton">Rotate X</button>
<button id= "yButton">Rotate Y</button>
<button id= "zButton">Rotate Z</button>

</body>
</html>

I have tried changing the points multiple different times in the bolded area and still cannot get the pyramid to line up as expected in attached photo.

Please support anyway you can 🙂