微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何为 webgl 模型实现 gouraud 着色

如何解决如何为 webgl 模型实现 gouraud 着色

这是我用来加载茶壶模型的代码

function loadTeapot() {
        var request = new XMLHttpRequest();
        request.open("GET","./model/Teapot.json");
        request.onreadystatechange = function () {
            if (request.readyState == 4) {
                handleLoadedTeapot(JSON.parse(request.responseText));
            }
        }
        request.send();
    }

这是我加载茶壶后使用的函数

function handleLoadedTeapot(teapotData) {
        teapotVertexPositionBuffer = gl.createBuffer();
        gl.bindBuffer(gl.ARRAY_BUFFER,teapotVertexPositionBuffer);
        gl.bufferData(gl.ARRAY_BUFFER,new Float32Array(teapotData.vertexPositions),gl.STATIC_DRAW);
        teapotVertexPositionBuffer.itemSize = 3;
        teapotVertexPositionBuffer.numItems = teapotData.vertexPositions.length / 3;

        teapotVertexnormalBuffer = gl.createBuffer();
        gl.bindBuffer(gl.ARRAY_BUFFER,teapotVertexnormalBuffer);
        gl.bufferData(gl.ARRAY_BUFFER,new Float32Array(teapotData.vertexnormals),gl.STATIC_DRAW);
        teapotVertexnormalBuffer.itemSize = 3;
        teapotVertexnormalBuffer.numItems = teapotData.vertexnormals.length / 3;

        teapotVertexFrontColorBuffer = gl.createBuffer();
        gl.bindBuffer(gl.ARRAY_BUFFER,teapotVertexFrontColorBuffer);
        gl.bufferData(gl.ARRAY_BUFFER,new Float32Array(teapotData.vertexFrontcolors),gl.STATIC_DRAW);
        teapotVertexFrontColorBuffer.itemSize = 3;
        teapotVertexFrontColorBuffer.numItems = teapotData.vertexFrontcolors.length / 3;
    }

这是我用来初始化着色器的函数

function initShaders() {
        var fragmentShader = getShader(gl,"fragmentShader");
        var vertexShader   = getShader(gl,"vertexShader");

        shaderProgram = gl.createProgram();
        gl.attachShader(shaderProgram,vertexShader);
        gl.attachShader(shaderProgram,fragmentShader);
        gl.linkProgram(shaderProgram);

        if (!gl.getProgramParameter(shaderProgram,gl.LINK_STATUS)) {
            alert("Could not initialise shaders");
        }

        gl.useProgram(shaderProgram);

        shaderProgram.vertexPositionAttribute = gl.getAttribLocation(shaderProgram,"aVertexPosition");
        gl.enabLevertexAttribArray(shaderProgram.vertexPositionAttribute);
        shaderProgram.vertexFrontColorAttribute = gl.getAttribLocation(shaderProgram,"aFrontColor");
        gl.enabLevertexAttribArray(shaderProgram.vertexFrontColorAttribute);

        shaderProgram.pMatrixUniform  = gl.getUniformlocation(shaderProgram,"uPMatrix");
        shaderProgram.mvMatrixUniform = gl.getUniformlocation(shaderProgram,"uMVMatrix");
    }

我想对这个茶壶模型应用古罗着色。你能帮我吗。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。