1

我想在 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>
4

1 回答 1

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 中查找矩阵公式。

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

于 2012-12-16T13:48:54.633 回答