我很抱歉粘贴一个代码列表,但它非常简单。我不知道为什么,没有警告没有错误,它在沉默中崩溃。这里是源代码,它引用了一个utils.js文件,我保证外部文件中没有错误。因为其他程序很好地基于这项工作。
<!DOCTYPE html>
<html>
<head>
<title>Test</title>
<style>
body { background: pink }
canvas { background: orange }
</style>
<script src="utils.js"></script>
<script id="shader-vs" type="x-shader/x-vertex">
attribute vec4 aVertexPosition;
uniform float uCosB, uSinB;
void main() {
gl_Position.x = aVertexPosition.x * uCosB - aVertexPosition.y * uSinB;
gl_Position.y = aVertexPosition.x * uCosB + aVertexPosition.y * uSinB;
gl_Position.z = aVertexPosition.z;
gl_Position.w = 1.0;
}
</script>
<script id="shader-fs" type="x-shader/x-fragment">
void main() {
gl_FragColor = vec4(1.0, 0.0, 0.0, 1.0);
}
</script>
<script>
var gl, canvas, shaderProgram;
window.onload = function() {
initCanvasAndGL("canvas");
initProgram();
initBuffer();
gl.clearColor(0.0, 0.0, 0.0, 1.0);
gl.clear(gl.COLOR_BUFFER_BIT);
var angle = 90 / 180 * Math.PI;
var uSinB = gl.getUniformLocation(shaderProgram, "uSinB");
var uCosB = gl.getUniformLocation(shaderProgram, "uCosB");
gl.uniform1f(uSinB, Math.sin(angle));
gl.uniform1f(uCosB, Math.cos(angle));
gl.drawArrays(gl.TRIANGLES, 0, 3);
}
function initBuffer() {
var vertices = [0.0, 0.5, -0.5, -0.5, 0.5, -0.5];
var vertexBuffer = gl.createBuffer();
gl.bindBuffer(gl.ARRAY_BUFFER, vertexBuffer);
gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(vertices), gl.STATIC_DRAW);
var aVertexPosition = gl.getAttribLocation(shaderProgram, "aVertexPosition");
gl.vertexAttribPointer(aVertexPosition, 2, gl.FLOAT, false, 0, 0);
gl.enableVertexAttribArray(aVertexPosition);
}
</script>
</head>
<body>
<canvas id="canvas" width="400" height="400">
Canvas Not Supported! :(
</canvas>
</body>
If you do suspect the utils.js, Here is the source code.
function initCanvasAndGL(canvasID) {
canvas = document.getElementById(canvasID);
var names = [
"webgl", "experimental-webgl",
"webkit-3d", "moz-webgl"
];
for(var i = 0; i < names.length; ++i) {
try {
gl = canvas.getContext(names[i]);
} catch(e) {}
if(gl) {
break;
}
}
if(!gl) {
alert("WebGL is not available! :(");
}
}
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-vertex") {
shader = gl.createShader(gl.VERTEX_SHADER);
} else if(shaderScript.type == "x-shader/x-fragment") {
shader = gl.createShader(gl.FRAGMENT_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 initProgram() {
var vertexShader = getShader(gl, "shader-vs");
var fragmentShader = getShader(gl, "shader-fs");
shaderProgram = gl.createProgram();
gl.attachShader(shaderProgram, vertexShader);
gl.attachShader(shaderProgram, fragmentShader);
gl.linkProgram(shaderProgram);
if(!gl.getProgramParameter(shaderProgram, gl.LINK_STATUS)) {
alert("Could not initialise shaders! :(");
}
gl.useProgram(shaderProgram);
}
您可以将它放在一起运行该程序。但结果确实让我发疯了。
答案 0 :(得分:0)
是的,正如另一个人评论的那样 - 你需要将你的gl_Position.y更改为
gl_Position.y = aVertexPosition.x * uSinB + aVertexPosition.y * uCosB;
然后你会看到一个红色三角形 享受