webgl adding projection doesnt display object

Posted by dazed3confused on Stack Overflow See other posts from Stack Overflow or by dazed3confused
Published on 2012-03-23T20:24:11Z Indexed on 2012/03/23 23:29 UTC
Read the original article Hit count: 186

Filed under:

I am having a look at web gl, and trying to render a cube, but I am having a problem when I try to add projection into the vertex shader. I have added an attribute, but when I use it to multiple the modelview and position, it stops displaying the cube. Im not sure why and was wondering if anyone could help? Ive tried looking at a few examples but just cant get this to work

vertex shader
    attribute vec3 aVertexPosition;

    uniform mat4 uMVMatrix;
    uniform mat4 uPMatrix;

    void main(void) {
        gl_Position = uPMatrix * uMVMatrix * vec4(aVertexPosition, 1.0);
        //gl_Position = uMVMatrix * vec4(aVertexPosition, 1.0);
    }

fragment shader
    #ifdef GL_ES
    precision highp float; // Not sure why this is required, need to google it
    #endif

    uniform vec4 uColor;

    void main() {
            gl_FragColor = uColor;
    }


    function init() {

        // Get a reference to our drawing surface
        canvas = document.getElementById("webglSurface");
        gl = canvas.getContext("experimental-webgl");

        /** Create our simple program **/
        // Get our shaders
        var v = document.getElementById("vertexShader").firstChild.nodeValue;
        var f = document.getElementById("fragmentShader").firstChild.nodeValue;

        // Compile vertex shader
        var vs = gl.createShader(gl.VERTEX_SHADER);
        gl.shaderSource(vs, v);
        gl.compileShader(vs);

        // Compile fragment shader
        var fs = gl.createShader(gl.FRAGMENT_SHADER);
        gl.shaderSource(fs, f);
        gl.compileShader(fs);

        // Create program and attach shaders
        program = gl.createProgram();
        gl.attachShader(program, vs);
        gl.attachShader(program, fs);
        gl.linkProgram(program);

        // Some debug code to check for shader compile errors and log them to console
        if (!gl.getShaderParameter(vs, gl.COMPILE_STATUS))
            console.log(gl.getShaderInfoLog(vs));

        if (!gl.getShaderParameter(fs, gl.COMPILE_STATUS))
                console.log(gl.getShaderInfoLog(fs));

        if (!gl.getProgramParameter(program, gl.LINK_STATUS))
                console.log(gl.getProgramInfoLog(program));


        /* Create some simple VBOs*/
        // Vertices for a cube
        var vertices = new Float32Array([
            -0.5, 0.5, 0.5, // 0
            -0.5, -0.5, 0.5, // 1
            0.5, 0.5, 0.5, // 2
            0.5, -0.5, 0.5, // 3
            -0.5, 0.5, -0.5, // 4
            -0.5, -0.5, -0.5, // 5 
            -0.5, 0.5, -0.5, // 6
            -0.5,-0.5, -0.5 // 7  
        ]);

        // Indices of the cube
        var indicies = new Int16Array([
            0, 1, 2, 1, 2, 3, // front
            5, 4, 6, 5, 6, 7, // back
            0, 1, 5, 0, 5, 4, // left
            2, 3, 6, 6, 3, 7, // right
            0, 4, 2, 4, 2, 6, // top
            5, 3, 1, 5, 3, 7 // bottom
        ]);

        // create vertices object on the GPU
        vbo = gl.createBuffer();
        gl.bindBuffer(gl.ARRAY_BUFFER, vbo);
        gl.bufferData(gl.ARRAY_BUFFER, vertices, gl.STATIC_DRAW);

        // Create indicies object on th GPU
        ibo = gl.createBuffer();
        gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, ibo);
        gl.bufferData(gl.ELEMENT_ARRAY_BUFFER, indicies, gl.STATIC_DRAW);

        gl.clearColor(0.0, 0.0, 0.0, 1.0);
        gl.enable(gl.DEPTH_TEST);
        // Render scene every 33 milliseconds
        setInterval(render, 33);

    }

    var mvMatrix = mat4.create();
    var pMatrix = mat4.create();

    function render() {

        // Set our viewport and clear it before we render
        gl.viewport(0, 0, canvas.width, canvas.height);
        gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT);

        gl.useProgram(program);

        // Bind appropriate VBOs
        gl.bindBuffer(gl.ARRAY_BUFFER, vbo);
        gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, ibo);

        // Set the color for the fragment shader
        program.uColor = gl.getUniformLocation(program, "uColor");
        gl.uniform4fv(program.uColor, [0.3, 0.3, 0.3, 1.0]);

        // 
        // code.google.com/p/glmatrix/wiki/Usage

        program.uPMatrix = gl.getUniformLocation(program, "uPMatrix");
        program.uMVMatrix = gl.getUniformLocation(program, "uMVMatrix");

        mat4.perspective(45, gl.viewportWidth / gl.viewportHeight, 1.0, 10.0, pMatrix);

        mat4.identity(mvMatrix);

        mat4.translate(mvMatrix, [0.0, -0.25, -1.0]);

        gl.uniformMatrix4fv(program.uPMatrix, false, pMatrix);
        gl.uniformMatrix4fv(program.uMVMatrix, false, mvMatrix);


        // Set the position for the vertex shader
        program.aVertexPosition = gl.getAttribLocation(program, "aVertexPosition");
        gl.enableVertexAttribArray(program.aVertexPosition);

        gl.vertexAttribPointer(program.aVertexPosition, 3, gl.FLOAT, false, 3*4, 0); // position

        // Render the Object         
        gl.drawElements(gl.TRIANGLES, 36, gl.UNSIGNED_SHORT, 0);
    }

Thanks in advance for any help

© Stack Overflow or respective owner

Related posts about webgl