当我调整四个画布的大小(实际宽度和高度,而不仅仅是CSS,以便在全屏时适应屏幕比例)时,使用webgl的画布不会更新。虽然我打电话
gl.viewport( 0, 0, gl.drawingBufferWidth, gl.drawingBufferHeight );
它保持原始比例,因此它偏离其他画布。
我错过了什么?
我在互联网上找到的唯一信息是更新我的矩阵,但我不使用任何矩阵,我只是在两个三角形上使用纹理。
创作代码:
gl.viewport(0, 0, game.w, game.h);
gl.vertexShader = createShaderFromScriptElement(gl, "2d-vertex-shader");
gl.fragmentShader = createShaderFromScriptElement(gl, "2d-fragment-shader");
gl.program = createProgram(gl, [gl.vertexShader, gl.fragmentShader]);
gl.useProgram( gl.program);
gl.positionLocation = gl.getAttribLocation( gl.program, "a_position");
gl.texCoordLocation = gl.getAttribLocation( gl.program, "a_texCoord");
gl.texCoordBuffer = gl.createBuffer();
gl.bindBuffer( gl.ARRAY_BUFFER, gl.texCoordBuffer );
gl.bufferData( gl.ARRAY_BUFFER, new Float32Array([
0.0, 0.0,
1.0, 0.0,
0.0, 1.0,
0.0, 1.0,
1.0, 0.0,
1.0, 1.0]), gl.STATIC_DRAW);
gl.enableVertexAttribArray( gl.texCoordLocation );
gl.vertexAttribPointer( gl.texCoordLocation , 2, gl.FLOAT, false, 0, 0);
gl.posBuffer = gl.createBuffer();
gl.bindBuffer( gl.ARRAY_BUFFER , gl.posBuffer );
gl.bufferData( gl.ARRAY_BUFFER , new Float32Array([
0.0, 0.0,
1.0, 0.0,
0.0, 1.0,
0.0, 1.0,
1.0, 0.0,
1.0, 1.0]), gl.STATIC_DRAW);
gl.enableVertexAttribArray( gl.positionLocation );
gl.vertexAttribPointer( gl.positionLocation, 2, gl.FLOAT, false, 0, 0 );
gl.resolutionLocation = gl.getUniformLocation( gl.program, "u_resolution" );
webglalpha = gl.getUniformLocation( gl.program, "alpha" );
答案 0 :(得分:0)
我自己发现,决议制服也必须更新:
gl.uniform2f( gl.resolutionLocation , game.width , game.height );