-
-
Save Kuranes/8aadb58f953f11e198ce to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE HTML> | |
<html lang="en"> | |
<head> | |
<title>Listing 2-2, Load Shaders From DOM</title> | |
<meta charset="utf-8"> | |
<script id="shader-vs" type="x-shader/x-vertex"> | |
attribute vec3 aVertexPosition; | |
void main() { | |
gl_Position = vec4(aVertexPosition, 1.0); | |
} | |
</script> | |
<script id="shader-fs" type="x-shader/x-fragment"> | |
precision mediump float; | |
void main() { | |
gl_FragColor = vec4(0.5, 1.0, 1.0, 1.0); | |
} | |
</script> | |
<script src="webgl-debug.js"></script> | |
<script type="text/javascript"> | |
var gl; | |
var canvas; | |
var shaderProgram; | |
var vertexBuffer; | |
function createGLContext(canvas) { | |
var names = ["webgl", "experimental-webgl"]; | |
var context = null; | |
for (var i=0; i < names.length; i++) { | |
try { | |
context = canvas.getContext(names[i]); | |
} catch(e) {} | |
if (context) { | |
break; | |
} | |
} | |
if (context) { | |
context.viewportWidth = canvas.width; | |
context.viewportHeight = canvas.height; | |
} else { | |
alert("Failed to create WebGL context!"); | |
} | |
return context; | |
} | |
function loadShaderFromDOM(id) { | |
var shaderScript = document.getElementById(id); | |
// If we don't find an element with the specified id | |
// we do an early exit | |
if (!shaderScript) { | |
return null; | |
} | |
// Loop through the children for the found DOM element and | |
// build up the shader source code as a string | |
var shaderSource = ""; | |
var currentChild = shaderScript.firstChild; | |
while (currentChild) { | |
if (currentChild.nodeType == 3) { // 3 corresponds to TEXT_NODE | |
shaderSource += currentChild.textContent; | |
} | |
currentChild = currentChild.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, shaderSource); | |
gl.compileShader(shader); | |
if (!gl.getShaderParameter(shader, gl.COMPILE_STATUS)) { | |
alert(gl.getShaderInfoLog(shader)); | |
return null; | |
} | |
return shader; | |
} | |
function setupShaders() { | |
vertexShader = loadShaderFromDOM("shader-vs"); | |
fragmentShader = loadShaderFromDOM("shader-fs"); | |
shaderProgram = gl.createProgram(); | |
gl.attachShader(shaderProgram, vertexShader); | |
gl.attachShader(shaderProgram, fragmentShader); | |
gl.linkProgram(shaderProgram); | |
if (!gl.getProgramParameter(shaderProgram, gl.LINK_STATUS)) { | |
alert("Failed to setup shaders"); | |
} | |
gl.useProgram(shaderProgram); | |
shaderProgram.vertexPositionAttribute = gl.getAttribLocation(shaderProgram, "aVertexPosition"); | |
} | |
function setupBuffers() { | |
vertexBuffer = gl.createBuffer(); | |
gl.bindBuffer(gl.ARRAY_BUFFER, vertexBuffer); | |
var triangleVertices = [ | |
0.0, 0.5, 0.0, | |
-0.5, -0.5, 0.0, | |
0.5, -0.5, 0.0 | |
]; | |
gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(triangleVertices), gl.STATIC_DRAW); | |
vertexBuffer.itemSize = 3; | |
vertexBuffer.numberOfItems = 3; | |
} | |
function draw() { | |
gl.viewport(0, 0, gl.viewportWidth, gl.viewportHeight); | |
gl.clear(gl.COLOR_BUFFER_BIT); | |
gl.vertexAttribPointer(shaderProgram.vertexPositionAttribute, | |
vertexBuffer.itemSize, gl.FLOAT, false, 0, 0); | |
gl.enableVertexAttribArray(shaderProgram.vertexPositionAttribute); | |
gl.drawArrays(gl.TRIANGLES, 0, vertexBuffer.numberOfItems); | |
} | |
function startup() { | |
canvas = document.getElementById("myGLCanvas"); | |
gl = WebGLDebugUtils.makeDebugContext(createGLContext(canvas)); | |
setupShaders(); | |
setupBuffers(); | |
gl.clearColor(0.0, 0.0, 0.0, 1.0); | |
draw(); | |
} | |
</script> | |
</head> | |
<body onload="startup();"> | |
<canvas id="myGLCanvas" width="500" height="500"></canvas> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment