gifts/Printf
This is an example from the book "WebGL Programming Guide" by Kouichi Matsuda and Rodger Lea
by Ivan Enzhaev
HTML
<canvas id="webgl" width="400" height="400">
Please use a browser that supports "canvas"
</canvas>
<script type="x-shader/x-vertex" id="VertexShader">
attribute vec4 a_Position;
attribute vec2 a_TexCoord;
varying vec2 v_TexCoord;
void main()
{
gl_Position = a_Position;
v_TexCoord = a_TexCoord;
}
</script>
<script type="x-shader/x-fragment" id="FragmentShader">
precision mediump float;
uniform sampler2D u_Sampler;
varying vec2 v_TexCoord;
// Get the number at the specified digit
float getNumber(float value, float digit)
{
int thisDigit = int(value / digit);
int upperDigit = int(thisDigit / 10) * 10;
return float(thisDigit - upperDigit);
}
void main()
{
float testValue = 57.2957; // The value to be displayed
vec2 texCoord = v_TexCoord;
if (texCoord.x < (1.0/16.0)) // 10
{
if (testValue >= 10.0)
{
float xOffset = mod(texCoord.x, 1.0/16.0);
texCoord.x = getNumber(testValue, 10.0) / 16.0 + xOffset;
}
else
{
texCoord.x = 12.0/16.0;
}
}
else if (texCoord.x < (2.0/16.0)) // 1
{
float xOffset = mod(texCoord.x, 1.0/16.0);
texCoord.x = getNumber(testValue, 1.0) / 16.0 + xOffset;
}
else if (texCoord.x < (3.0/16.0)) // Decimal point
{
float xOffset = mod(texCoord.x, 1.0/16.0);
texCoord.x = (10.0/16.0) + xOffset; // Decimal point is located at 10/16
}
else if (texCoord.x < (4.0/16.0)) // 0.1
{
float xOffset = mod(texCoord.x, 1.0/16.0);
texCoord.x =...
JavaScript
// Printf.js 2012 (c) itami & matsuda
// This is an example from the book "WebGL Programming Guide"
// by Kouichi Matsuda and Rodger Lea
main();
//window.onload = main;
function main()
{
// Retrieve <canvas> element
var canvas = document.getElementById('webgl');
// Get the rendering context for WebGL
var gl = getWebGLContext(canvas);
if (!gl)
{
console.log('Failed to get the rendering context for WebGL');
return;
}
// Get shader elements
var vShaderElement = document.getElementById("VertexShader");
var fShaderElement = document.getElementById("FragmentShader");
if (vShaderElement == null)
{
console.log("Failed to get the vertex shader element");
return;
}
if (fShaderElement == null)
{
console.log("Failed to get the fragment shader element");
return;
}
// Get shader sources
var vShaderSource = vShaderElement.firstChild.textContent;
var fShaderSource = fShaderElement.firstChild.textContent;
// Initialize shaders
if (!initShaders(gl, vShaderSource, fShaderSource))
{
console.log('Failed to intialize shaders.');
return;
}
// Set the vertex information
var n = initVertexBuffers(gl);
if (n < 0)
{
console.log('Failed to set the vertex information');
return;
}
// Set the clear color
gl.clearColor(0, 0, 0, 1);
// Set texture
if (!initTextures(gl, n))
{
console.log('Failed to intialize the texture.');
return;
}
}
function initVertexBuffers(gl)
{
var verticesTexCoords = new Float32Array([
-0.8, 0.05, 0.0, 1.0,
-0.8, -0.05, 0.0, 0.0,
0.8, 0.05, 1.0, 1.0,
0.8, -0.05, 1.0, 0.0,
]);
var n = 4; // The number of vertices
// Create a buffer object
var vertexTexCoordBuffer = gl.createBuffer();
if (!vertexTexCoordBuffer)
{
console.log('Failed to create a buffer object');
...