WebGL 3d Cylinder和3d Cone ontop彼此

时间:2015-10-03 06:20:11

标签: opengl webgl

我正在制作一个Webgl程序,需要使用三角形条创建一个Cylinder,并使用三角形扇形为它创建一个简单的游戏类型程序树。

我可以用来做这个的任何提示或方法,因为我只知道如何制作矩形棱镜。我是WebGL的初学者,所有帮助都表示赞赏:)

// The Block class that creates a rectangular prism............used as steps/obstacles for player to avoid
// The constructor function for a block when its created
// Arguments: a vec3 location, a floating-point angle (degrees) and a vec3 scales
function Block(location, angle, scales) {
    var rs = mult(rotate(angle, [0,0,1]), scalem(scales));
    this.trs = mult(translate(location), rs);
}

// A block's render function
// Arguments:
//   offset - offset of vertices into current vertex attribute array
//   worldview - current worldview transformation
Block.prototype.render = function(offset, worldview) {
    gl.uniformMatrix4fv(mvLoc, false, flatten(mult(worldview, this.trs)));
    gl.drawArrays(gl.TRIANGLES, offset, Block.NV);
};

// The number of vertices to represent a cube (NV)
Block.NV = 36;

//Makes the vertices for the block/rectangle
Block.initModel = function() {

    // The 8 raw vertices of a cube
    var rawverts = [
        vec3(-0.5, -0.5,  0.5),
        vec3(-0.5,  0.5,  0.5),
        vec3( 0.5,  0.5,  0.5),
        vec3( 0.5, -0.5,  0.5),
        vec3(-0.5, -0.5, -0.5),
        vec3(-0.5,  0.5, -0.5),
        vec3( 0.5,  0.5, -0.5),
        vec3( 0.5, -0.5, -0.5)
    ];

    // A local array in which to develop the 36 vertices
    var vertices = [];

    // A nested function generating the vertices for each face
    function quad(a, b, c, d) {
        // if abcd is an anticlockwise winding on a face
        // then abc and acd are anticlockwise windings on its triangles
        var indices = [a, b, c, a, c, d];

        for (var i = 0; i < indices.length; ++i) {
            vertices.push(rawverts[indices[i]]);
        }
    }

    //Generates the cube's faces
    function doCube() {
        // Use anticlockwise windings
        quad(1, 0, 3, 2);
        quad(2, 3, 7, 6);
        quad(3, 0, 4, 7);
        quad(6, 5, 1, 2);
        quad(4, 5, 6, 7);
        quad(5, 4, 0, 1);
    }

    doCube();
    return vertices;
}//end of block.initial model function

//Vertices to be intialised
Block.vertices = Block.initModel();
//end of block/rectangle class-------------------------------------------

0 个答案:

没有答案