Last active
September 29, 2023 15:57
-
-
Save haehn/93efff9e7f9aefa7de15f531fb2bad18 to your computer and use it in GitHub Desktop.
CS460 Fall 2023 Hello Rectangle in Vanilla WebGL!
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
<html> | |
<head> | |
<meta content="text/html;charset=utf-8" http-equiv="Content-Type"> | |
<meta content="utf-8" http-equiv="encoding"> | |
<title>Default WebGL!</title> | |
<style> | |
html, body { | |
background-color:#000; | |
margin: 0; | |
padding: 0; | |
height: 100%; | |
overflow: hidden !important; | |
} | |
#c { | |
width: 100%; | |
height: 100%; | |
} | |
</style> | |
</head> | |
<script type="text/javascript" src="https://cs460.org/js/glmatrix.js"></script> | |
<script id="vertexshader" type="glsl"> | |
attribute vec3 position; | |
void main(void) { | |
gl_Position = vec4( position, 1.); | |
} | |
</script> | |
<script id="fragmentshader" type="glsl"> | |
precision mediump float; | |
void main(void) { | |
gl_FragColor = vec4(1., 1., 1., 1.); | |
} | |
</script> | |
<script> | |
// VARIABLE DECLARATIONS | |
window.onload = function() { | |
//************************************************************// | |
// | |
// INITIALIZE WEBGL | |
// | |
c = document.getElementById( 'c' ); // setup canvas | |
c.width = window.innerWidth; | |
c.height = window.innerHeight; | |
gl = c.getContext( 'webgl' ); // setup GL context | |
gl.viewport(0, 0, c.width, c.height ); | |
//************************************************************// | |
// | |
// SHADERS | |
// | |
v_shader = gl.createShader( gl.VERTEX_SHADER ); | |
f_shader = gl.createShader( gl.FRAGMENT_SHADER ); | |
// compile vertex shader | |
gl.shaderSource( v_shader, document.getElementById( 'vertexshader' ).innerText ); | |
gl.compileShader( v_shader ); | |
if (!gl.getShaderParameter( v_shader, gl.COMPILE_STATUS)) { | |
console.log(gl.getShaderInfoLog( v_shader )); | |
} else { | |
console.log('Vertex Shader compiled!'); | |
} | |
// compile fragment shader | |
gl.shaderSource( f_shader, document.getElementById( 'fragmentshader' ).innerText ); | |
gl.compileShader( f_shader ); | |
if (!gl.getShaderParameter( f_shader, gl.COMPILE_STATUS)) { | |
console.log(gl.getShaderInfoLog( f_shader )); | |
} else { | |
console.log('Fragment Shader compiled!'); | |
} | |
// attach and link the shaders | |
shaderprogram = gl.createProgram(); | |
gl.attachShader( shaderprogram, v_shader ); | |
gl.attachShader( shaderprogram, f_shader ); | |
gl.linkProgram( shaderprogram ); | |
gl.useProgram( shaderprogram ); | |
//************************************************************// | |
// | |
// CREATE GEOMETRY | |
// | |
vertices = new Float32Array( [ | |
-0.5, 0.5, 0.0, // V0 | |
-0.5, -0.5, 0.0, // V1 | |
0.5, 0.5, 0.0, // V2 | |
0.5, 0.5, 0.0, // V3 | |
-0.5, -0.5, 0.0, // V4 | |
0.5, -0.5, 0.0 // V5 | |
] ); | |
v_buffer = gl.createBuffer(); // create | |
gl.bindBuffer( gl.ARRAY_BUFFER, v_buffer ); // bind | |
gl.bufferData( gl.ARRAY_BUFFER, vertices, gl.STATIC_DRAW ); // put data in | |
gl.bindBuffer( gl.ARRAY_BUFFER, null ); // unbind | |
//************************************************************// | |
// | |
// CONNECT SHADER WITH GEOMETRY | |
// | |
gl.bindBuffer( gl.ARRAY_BUFFER, v_buffer ); | |
// find the attribute in the shader source | |
var a_position = gl.getAttribLocation( shaderprogram, 'position' ); | |
gl.vertexAttribPointer( a_position, 3, gl.FLOAT, false, 0, 0 ); | |
gl.enableVertexAttribArray ( a_position ); | |
//************************************************************// | |
// | |
// DRAW! | |
// | |
gl.clearColor( 0., 0., 0., 0.) | |
gl.clear( gl.COLOR_BUFFER_BIT ); | |
gl.drawArrays( gl.TRIANGLES, 0, 6 ); | |
}; | |
</script> | |
<body> | |
<canvas id="c"></canvas> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment