WebGL相机对象似乎没有跟随角色

时间:2015-10-11 06:04:48

标签: javascript 3d camera

我一直在使用来自this book的webGL进行游戏开发的指导,在关于相机的章节中,它讨论并给出了一个非常有限的轨道相机示例,但继续其余的本书专注于简单的第一人称免费相机。

轨道相机工作得非常好,但是如果我尝试创建一个可控制的角色对象,并设置相机的轨道点来跟随它,相机似乎......超前了?它以某种方式移动得比角色更快。

以下是OrbitalCamera对象的相关方法:

OrbitCamera.prototype.setOrbitPoint = function (orbitPoint)
{
  // get the distance the camera was from the orbit point.
    var orbitPointToCam =vec3.create();
    vec3.multiply(orbitPointToCam,this.dir, -this.getDistance());
    this.orbitPoint[0] = orbitPoint[0];
    this.orbitPoint[1] = orbitPoint[1];
    this.orbitPoint[2] = orbitPoint[2];
    vec3.add(this.pos,this.orbitPoint, orbitPointToCam);
}

这里是调用函​​数的地方:

                function animate(){
                var lSpeed = 0;
                var uSpeed = 0;
                var distance = cam.getDistance();

                if(stage.stageObjects.length != 0){
                var pos = stage.stageObjects[0].location;

                    if (left) {
                        lSpeed = +0.05;
                    }
                    if (right) {
                        lSpeed = -0.05;
                    }
                    if (up) {
                        uSpeed = 0.05;
                    }
                    if (down) {
                        uSpeed = -0.05;
                    }   

                    stage.stageObjects[0].location = [(uSpeed*Math.round(cam.dir[0]))+(lSpeed*Math.round(cam.left[0]))+pos[0], pos[1], (uSpeed*Math.round(cam.dir[2]))+(lSpeed*Math.round(cam.left[2]))+pos[2]]
                    cam.setOrbitPoint([stage.stageObjects[0].location[0],0,stage.stageObjects[0].location[2]]);
                    cam.setDistance(distance);                        

                    if (cup) {
                        cam.pitch(Math.PI*0.01);   
                    }
                    if (cdown) {
                        cam.pitch(-Math.PI*0.01);   
                    }
                    if (cleft) {
                        cam.yaw(-Math.PI*0.01);   
                    }
                    if (cright) {
                        cam.yaw(Math.PI*0.01);   
                    }

                }


            }

以下是平局电话:

                function drawScene(){
                gl.viewport(0,0, gl.viewportWidth, gl.viewportHeight);
                gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT); 

                setLightUniform();

                pushMatrix();
                calculateMvMatrix();
                for (var i = 0; i<stage.stageObjects.length; i++){

                    pushMatrix();
                    mat4.translate(mvMatrix, mvMatrix, stage.stageObjects[i].location);
                    mat4.rotateX(mvMatrix, mvMatrix, stage.stageObjects[i].rotationX);
                    mat4.rotateY(mvMatrix, mvMatrix, stage.stageObjects[i].rotationY);
                    mat4.rotateZ(mvMatrix, mvMatrix, stage.stageObjects[i].rotationZ);
                    setMatrixUniforms();

                    gl.uniform3f(shaderProgram.materialDiffuseColor, stage.stageObjects[i].diffuseColor[0],stage.stageObjects[i].diffuseColor[1],stage.stageObjects[i].diffuseColor[2]);
                    gl.uniform3f(shaderProgram.materialAmbientColor, stage.stageObjects[i].ambientColor[0],stage.stageObjects[i].ambientColor[1],stage.stageObjects[i].ambientColor[2]);

                    gl.bindBuffer(gl.ARRAY_BUFFER, stage.stageObjects[i].vbo);
                    gl.vertexAttribPointer(shaderProgram.vertexPositionAttribute, stage.stageObjects[i].vbo.itemSize, gl.FLOAT, false, 0, 0);

                    gl.bindBuffer(gl.ARRAY_BUFFER, stage.stageObjects[i].nbo);
                    gl.vertexAttribPointer(shaderProgram.vertexNormalAttribute, stage.stageObjects[i].nbo.itemSize, gl.FLOAT, false, 0,0);

                    gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, stage.stageObjects[i].ibo);
                    gl.drawElements(gl.TRIANGLES, stage.stageObjects[i].geometry.indices.length, gl.UNSIGNED_SHORT,0);
                    popMatrix();
                }
                popMatrix();



            }

如果问题不在这里,我可以发布Camera对象的整个代码。非常感谢任何帮助,这一直让我疯狂

0 个答案:

没有答案