如何在webGL和HTML5中移动整个场景



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

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

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

所以我想点击右箭头按钮,场景向右移动,直到另一个正方形出现!!使用openGL,我只需绘制它们(另一个正方形不会出现,因为我会在X轴上将gluOrtho2D设置为(01000)),然后将gluOrtho二维更改为(501500),例如,然后会出现另一个方形!!那么如何使用webGL做到这一点呢?我试过剪刀的功能,但我不会工作,因为看起来画布就是整个世界。。所以外面画的东西都会被忽略!!

有什么想法吗?

代码如下:我正在尝试绘制一个三角形,然后移动场景以完全查看第二个三角形。所以我希望结果是画布上的两个三角形完全画出来。

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>

您引入了一个投影和查看矩阵,如下所示:

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

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

最新更新