将新对象添加到现有JavaScript代码时出错

时间:2016-12-07 20:20:56

标签: javascript html

当我尝试将我创建的新对象添加到我的javascript文件中时,我正在尝试解决无法读取未定义错误属性的问题。

以前,我能够在html的场景中成功获得3个对象(牛,飞机和茶壶)并与它们进行交互。当我用我自己的对象替换飞机时(在我的情况下它被称为willis),我收到了错误。

错误含蓄地说明:

prog4.js:131未捕获的TypeError:无法读取未定义的属性“位置”(...) 在里面 @ prog4.js:131 负载 @ prog4.html:12

除了将'plane'重命名为'willis'并确保文件位于正确的位置之外,我对代码没有任何改变。

非常感谢任何帮助!我的目标是从原来的3中获得场景中的3个不同的对象,我希望一次只能做一个以查看进度。

<html>
<head>
    <script type="text/javascript" src="webgl-utils.js"></script>
    <script type="text/javascript" src="webgl-debug.js"></script>
    <script type="text/javascript" src="cuon-utils.js"></script>
    <script type="text/javascript" src="cuon-matrix.js"></script>
    <script type="text/javascript" src="teapot.js"></script>
    <script type="text/javascript" src="willis.js"></script>
    <script type="text/javascript" src="cow.js"></script>
    <script type="text/javascript" src="prog4.js"></script>
</head>
<body onload="init()">
    <script id="vertexShader" type="x-shader/x-vertex">
        precision mediump float;

        uniform mat4 projectionMatrix;
        uniform mat4 viewMatrix;
        uniform mat4 modelMatrix;
        uniform vec4 lightPosition;

        attribute vec4 vertexPosition;
        attribute vec3 vertexNormal;

        varying vec3 fragmentNormal;
        varying vec3 fragmentLight;
        varying vec3 fragmentView;
        varying vec4 fragmentPosition;

        void main() {        
            mat4 modelViewMatrix = viewMatrix * modelMatrix;

            vec4 p = vec4 (modelViewMatrix * vertexPosition);
            vec4 q = vec4 (viewMatrix * lightPosition);

            fragmentNormal = normalize(mat3(modelViewMatrix) * vertexNormal);
            fragmentLight  = normalize(vec3(q - p));
            fragmentView   = normalize(vec3(-p));
            fragmentPosition = vertexPosition;

            gl_Position = projectionMatrix * modelViewMatrix * vertexPosition;

        }
    </script>
    <script id="lightingFragmentShader" type="x-shader/x-fragment">
        precision mediump float;

        varying vec3 fragmentNormal;
        varying vec3 fragmentLight;
        varying vec3 fragmentView;

        uniform vec3 modelColor;
        uniform vec3 lightColor;


        void main() {      
          vec3 n = normalize(fragmentNormal);
          vec3 l = normalize(fragmentLight);
          vec3 v = normalize(fragmentView);
          vec3 h = normalize(l+v);

          float d = max(dot(l,n),0.0);
          float s = pow(max(dot(h,n),0.0), 10.0);

          vec3 fragmentColor = modelColor * lightColor * d + lightColor * s;

          gl_FragColor = vec4(fragmentColor, 1.0);

        }
    </script>
    <script id="rainbowFragmentShader" type="x-shader/x-fragment">
        precision mediump float;

        varying vec3 fragmentNormal;
        varying vec3 fragmentLight;
        varying vec3 fragmentView;
        varying vec4 fragmentPosition;

        uniform vec3 lightColor;

        void main() {

            vec3 modelColor;

            if(fragmentPosition.y > 0.5) {
                modelColor = vec3(1.0,0.0,0.0);
            }
            else if (fragmentPosition.y > 0.3) {
                modelColor = vec3(0.0,0.0,1.0);
            }
            else if (fragmentPosition.y > 0.1) {
                modelColor = vec3(0.0,1.0,0.1);
            }
            else {
                modelColor = vec3(0.0,1.0,1.0);
            }

        vec3 fragmentColor = modelColor;

        gl_FragColor = vec4(fragmentColor, 1.0);
    }
    </script>
    <script id="goochFragmentShader" type="x-shader/x-fragment">
        precision mediump float;

        varying vec3 fragmentNormal;
        varying vec3 fragmentLight;
        varying vec3 fragmentView;

        uniform vec3 modelColor;
        uniform vec3 lightColor;

        void main() {      
          vec3 n = normalize(fragmentNormal);
          vec3 l = normalize(fragmentLight);
          vec3 v = normalize(fragmentView);
          vec3 h = normalize(l+v);

          float kg = max(dot(n,v), 0.0);

          vec3 fragmentColor = mix(vec3(0.0,0.0,1.0), vec3(1.0,1.0,0.0), kg);

          gl_FragColor = vec4(fragmentColor, 1.0);

        }
    </script>
    <canvas id="webgl" width="500px" height="500px" >
        This content requires <a href="http://get.webgl.org/">WebGL</a>.
    </canvas>
</body>

var canvas
var gl

var willis
var teapot
var cow

// Interaction
var modelRotationX =0;
var modelRotationY =0;

var dragging = false;
var lastClientX = 0;
var lastClientY = 0;

// Mouse commands
function onmousedown(event) {  
    dragging = true;
    lastClientX = event.clientX;
    lastClientY = event.clientY;
}

function onmouseup(event) {
    dragging = false;
}

function onmousemove(event) {
    if (dragging) {
        dX = event.clientX - lastClientX;
        dY = event.clientY - lastClientY;

        modelRotationY = modelRotationY + dX;
        modelRotationX = modelRotationX + dY;

        if (modelRotationX > 90.0) {
            modelRotationX = 90.0;
        }
        if (modelRotationX < -90.0) {
            modelRotationX = -90.0;
        }
        requestAnimationFrame(draw);
    }
    lastClientX = event.clientX;
    lastClientY = event.clientY;
}

// FLatten
function flatten(a) {
    return a.reduce(function (b, v) { b.push.apply(b, v); return b }, [])
}

// Create shaders for objects
function Shader (vertexId, fragmentId) {
    this.program = createProgram(gl, document.getElementById(vertexId).text,
                                     document.getElementById(fragmentId).text);
    this.projectionMatrixLocation = gl.getUniformLocation(this.program, "projectionMatrix");
    this.viewMatrixLocation = gl.getUniformLocation(this.program, "viewMatrix");
    this.modelMatrixLocation = gl.getUniformLocation(this.program, "modelMatrix");

    this.lightPositionLocation = gl.getUniformLocation(this.program, "lightPosition");
    this.modelColorLocation = gl.getUniformLocation(this.program, "modelColor");
    this.lightColorLocation = gl.getUniformLocation(this.program, "lightColor");

    this.vertexPositionLocation = gl.getAttribLocation(this.program, 'vertexPosition');
    this.vertexNormalLocation = gl.getAttribLocation(this.program, 'vertexNormal');

    gl.enableVertexAttribArray(this.vertexPositionLocation);
    gl.enableVertexAttribArray(this.vertexNormalLocation);
}

// Execution for shaders
Shader.prototype.use = function(projectionMatrix, viewMatrix, modelMatrix) {
    gl.useProgram(this.program);

    gl.uniformMatrix4fv(this.projectionMatrixLocation, false, projectionMatrix.elements);
    gl.uniformMatrix4fv(this.viewMatrixLocation, false, viewMatrix.elements);
    gl.uniformMatrix4fv(this.modelMatrixLocation, false, modelMatrix.elements);

    gl.uniform4f(this.lightPositionLocation,0,8,8,1);
    gl.uniform3f(this.modelColorLocation,1,1,1);
    gl.uniform3f(this.lightColorLocation,0.6,0.4,0.2);
}

// Create models
function Model (positions, normals, triangles) {
    this.positionArray = new Float32Array(flatten(positions));
    this.normalArray = new Float32Array(flatten(normals));
    this.triangleArray = new Uint16Array(flatten(triangles));

    this.normalBuffer = gl.createBuffer();
    this.positionBuffer = gl.createBuffer();
    this.triangleBuffer = gl.createBuffer();

    gl.bindBuffer(gl.ARRAY_BUFFER, this.normalBuffer);
    gl.bufferData(gl.ARRAY_BUFFER, this.normalArray, gl.STATIC_DRAW);

    gl.bindBuffer(gl.ARRAY_BUFFER, this.positionBuffer);
    gl.bufferData(gl.ARRAY_BUFFER, this.positionArray, gl.STATIC_DRAW);

    gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, this.triangleBuffer);
    gl.bufferData(gl.ELEMENT_ARRAY_BUFFER, this.triangleArray, gl.STATIC_DRAW);
}

// Draw shaders on objects
Model.prototype.draw = function (shader_entry) {
    gl.bindBuffer(gl.ARRAY_BUFFER, this.normalBuffer);
    gl.vertexAttribPointer(shader_entry.vertexNormalLocation, 3, gl.FLOAT, false, 0, 0);

    gl.bindBuffer(gl.ARRAY_BUFFER, this.positionBuffer);
    gl.vertexAttribPointer(shader_entry.vertexPositionLocation, 3, gl.FLOAT, false, 0, 0);

    gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, this.triangleBuffer);
    gl.drawElements(gl.TRIANGLES, this.triangleArray.length, gl.UNSIGNED_SHORT, 0);   
}

// Initialization
function init() {
    // Initialize the GL context
    canvas = document.getElementById('webgl');
    gl = getWebGLContext(canvas, false);

    lightingShader = new Shader('vertexShader', 'lightingFragmentShader');
    rainbowShader = new Shader('vertexShader', 'rainbowFragmentShader');
    goochShader = new Shader('vertexShader', 'goochFragmentShader');

    teapotModel = new Model(teapot.positions, teapot.normals, teapot.triangles);
    willisModel = new Model(willis.positions, willis.normals, willis.triangles);
    cowModel = new Model(cow.positions, cow.normals, cow.triangles);

    // Make mouse functions "properties of the canvas entity."
    canvas.onmousedown = onmousedown;  
    canvas.onmouseup = onmouseup;
    canvas.onmousemove = onmousemove;

    // Depth test
    gl.enable(gl.DEPTH_TEST);

    // Request animation frame
    requestAnimationFrame(draw);
}

// Draw
function draw() {

    // Create colored background
    gl.clearColor(0.2, 0.1, 0.0, 0.3);
    gl.clear(gl.COLOR_BUFFER_BIT);

    // Declaration of variables
    var modelMatrix = new Matrix4();
    var viewMatrix = new Matrix4();
    var projectionMatrix = new Matrix4();

    // Cow and rotation
    var modelMatrix = new Matrix4();
    modelMatrix.rotate(modelRotationX,1,0,0);
    modelMatrix.rotate(modelRotationY,0,1,0);
    viewMatrix.translate(0,0,-5)

    projectionMatrix.perspective(60,1,1,10);

    gl.clear(gl.COLOR_BUFFER_BIT|gl.DEPTH_BUFFER_BIT);
    gl.enable(gl.DEPTH_TEST);

    // Cow shader
    lightingShader.use(projectionMatrix, viewMatrix, modelMatrix);
    cowModel.draw(lightingShader);

    // willis
    var modelMatrix = new Matrix4();
    modelMatrix.rotate(modelRotationX,1,0,0);
    modelMatrix.rotate(modelRotationY,0,1,0);
    viewMatrix.translate(2,0,-1)

    // willis shader
    goochShader.use(projectionMatrix, viewMatrix, modelMatrix);
    willisModel.draw(goochShader);

    //Teapot
    var modelMatrix = new Matrix4();
    modelMatrix.rotate(modelRotationX,1,0,0);
    modelMatrix.rotate(modelRotationY,0,1,0);
    viewMatrix.translate(-5,0,-2)

    // Teapot shader
    rainbowShader.use(projectionMatrix, viewMatrix, modelMatrix);
    teapotModel.draw(rainbowShader);
}

0 个答案:

没有答案