【问题标题】:How to move the whole scene in webGL and HTML5?如何在 webGL 和 HTML5 中移动整个场景?
【发布时间】:2012-12-16 21:48:54
【问题描述】:

我想在 webGL 的画布区域中移动整个场景。

我的意思是,如果我使用的是 openGL。我会更改剪辑窗口,但我认为在 webGL 中,大世界就是画布,所以我不能在 webGL 中做同样的事情!!

例如: 我有一个画布宽度:1000,高度:500 2 个正方形:第一个正方形在 X 轴上介于 0 和 1000 之间,因此在画布区域中,第二个正方形在 X 轴上大于 1000。

所以我想点击右箭头按钮,场景向右移动,直到出现另一个方块!使用openGL我会简单地绘制它们(另一个正方形不会出现,因为我会在X轴上将gluOrtho2D设置为(0,1000))然后将gluOrtho2D更改为(500, 1500)然后另一个正方形将出现!!那么如何使用 webGL 做到这一点?我尝试了剪刀功能,但我不会工作,因为画布似乎是整个世界..所以外面画的任何东西都会被忽略!!

有什么想法吗?

这是代码:我正在尝试绘制 1 个三角形,然后移动场景以完全查看第二个三角形。所以我希望结果是完全绘制的画布中的 2 个三角形。

var gl;
var triangleBuffer;
var triangleBuffer2;
var squareBuffer;
var shaderprogram;

function initGL(canvas) {
    try {
      gl = canvas.getContext("experimental-webgl");
      gl.viewportWidth = canvas.width;
      gl.viewportHeight = canvas.height;
    } catch(e) { }
    if (!gl) {
      alert("Could not initialise WebGL!");
    }
}

function getShader(gl, id) {
    var shaderScript = document.getElementById(id);
    if (!shaderScript) {
        return null;
    }

    var str = "";
    var k = shaderScript.firstChild;
    while (k) {
        if (k.nodeType == 3) {
    str += k.textContent;
        }
        k = k.nextSibling;
    }

    var shader;
    if (shaderScript.type == "x-shader/x-fragment") {
        shader = gl.createShader(gl.FRAGMENT_SHADER);
    } else if (shaderScript.type == "x-shader/x-vertex") {
        shader = gl.createShader(gl.VERTEX_SHADER);
    } else {
        return null;
    }

    gl.shaderSource(shader, str);
    gl.compileShader(shader);

    if (!gl.getShaderParameter(shader, gl.COMPILE_STATUS)) {
        alert(gl.getShaderInfoLog(shader));
        return null;
    }

    return shader;
}

function initShaders(){
    var fragmentShader = getShader(gl, "shader-fs");
    var vertexShader = getShader(gl, "shader-vs");

    // Create a program (piece of code to run on the GPU)
    shaderProgram = gl.createProgram();
    // Attach the vertext and fragment shaders to the program then link them
    gl.attachShader(shaderProgram, vertexShader);
    gl.attachShader(shaderProgram, fragmentShader);
    gl.linkProgram(shaderProgram);

    // If the linking failed
    if (!gl.getProgramParameter(shaderProgram, gl.LINK_STATUS)) {
        alert("Could not initialise shaders");
    }

    // Set the current program
    gl.useProgram(shaderProgram);

    // look up where the vertex data needs to go.
    gl.vertexPositionAttribute = gl.getAttribLocation(shaderProgram, "a_position");

    gl.enableVertexAttribArray(shaderProgram.vertexPositionAttribute);

    var resolutionLocation = gl.getUniformLocation(shaderProgram, "u_resolution");
    gl.uniform2f(resolutionLocation, gl.viewportWidth, gl.viewportHeight);
}

function initBuffers(){
    // Create buffer for the triangle
    triangleBuffer = gl.createBuffer();
    // Set the current buffer to the triangle buffer, so any action or transformation will be done on this current buffer
    gl.bindBuffer(gl.ARRAY_BUFFER, triangleBuffer);

    // One triangle inside the canvas coordinates, the other is not
    var vertices = [500, 250,
            600, 150,
            550, 250,
            900, 100,
            1100, 300,
            900, 200];
    // load the vertices array into the current buffer
    gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(vertices), gl.STATIC_DRAW);

    // This array consists of 3 items, each item consists of 3 numbers
    triangleBuffer.numItems = 6;
    triangleBuffer.itemSize = 2;
}

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

    gl.bindBuffer(gl.ARRAY_BUFFER, triangleBuffer);
    // call the code the will operate on the current buffer
    gl.vertexAttribPointer(shaderProgram.vertexPositionAttribute, triangleBuffer.itemSize, gl.FLOAT, false, 0, 0);
    // Draw the first triangle
    gl.drawArrays(gl.TRIANGLES, 0, 3);

    /* ======================================= here goes the moving of the scene ================== */

    // Draw the second triangle
    gl.drawArrays(gl.TRIANGLES, 3, 3);

}

function start() {

    // get the canvas element source
    var canvas = document.getElementById("game");

    initGL(canvas);
    initShaders();
    initBuffers();

    // Clear the canvas
    gl.clearColor(0.0, 0.0, 0.0, 1.0);

    drawScene();
}

这里是着色器:

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

    void main(void) {
        gl_FragColor = vec4(0.0, 0.0, 1.0, 1.0);
    }
</script>

<script id="shader-vs" type="x-shader/x-vertex">
    attribute vec2 a_position;
    uniform vec2 u_resolution;

    void main(void) {
        // convert from normal coordinates to clipspace coordinates(-1 to 1)
        vec2 clipSpace = ((a_position / u_resolution) * 2.0) - 1.0;
        gl_Position = vec4(clipSpace, 0, 1);

    }
</script>

【问题讨论】:

  • 取决于你在画什么,怎么画。
  • 我只想移动场景,而不查看场景的内容。例如,如果我想制作一个超级马里奥游戏。我希望当角色向右移动时,场景移动并出现关卡的新对象。
  • 我明白你想要什么,我要告诉你的是这取决于你要渲染什么,以及你如何渲染它。渲染场景的方法有很多种,而您如何移动它取决于您最初是如何渲染它的。你在使用场景图库吗?你实现了场景图吗?你的着色器是什么样的? What have you tried?

标签: javascript html webgl


【解决方案1】:

你像这样引入一个投影和观察矩阵:

attribute vec3 position;
uniform mat4 proj, view;

void main(void){
    gl_Position = proj * view * vec4(position, 1);
}

glMatrix 库具有设置这些 4x4 矩阵的例程https://github.com/toji/gl-matrix

或者,您可以在 opengl 编程指南(蓝皮书)的附录 F 中查找矩阵公式。

通常您选择投影矩阵作为透视投影,将视图矩阵选择为平移/旋转的组合。

【讨论】:

    猜你喜欢
    • 2016-02-11
    • 1970-01-01
    • 2017-07-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多