"use strict"; function start() { window.addEventListener("keydown", onKeyDown, false); window.addEventListener("keyup", onKeyUp, false); Render.initialize(); Render.run = function () { setInterval(function () { Render.update(); Render.draw(); }, 1000 / Render.fps); }; Render.run(); } var Render = {} var KeyQ = false; var KeyW = false; var KeyE = false; var KeyA = false; var KeyS = false; var KeyD = false; var g_xRot = 0; var g_yRot = 0; var g_zRot = 0; var lightx = -0.86;// 0.9; var lighty = -0.56; var lightz = 1; var fov = 50 * Math.PI / 180.0; var zNear = 0.1; var zFar = 100; Render.fps = 30; window.onresize = function () { var canvas = document.getElementById("glcanvas"); resize(canvas) Render.context.pMatrix = new Float32Array([ (canvas.height / canvas.width) / Math.tan(fov), 0, 0, 0, 0, 1 / Math.tan(fov), 0, 0, 0, 0, (zNear + zFar) / (zNear - zFar), -1, 0, 0, 2 * zNear * zFar / (zNear - zFar), 0]); Render.context.viewport(0, 0, Render.canvas.width, Render.canvas.height); }; Render.initialize = function () { var c; this.canvas = document.getElementById("glcanvas"); resize(this.canvas); var w = this.canvas.width; var h = this.canvas.height; try { c = this.canvas.getContext("webgl") || this.canvas.getContext("experimental-webgl"); c = WebGLDebugUtils.makeDebugContext(c, throwOnGLError, logAndValidate); } catch (e) { } if (!c) { alert("WebGL konnte nicht initialisiert werden."); return; } this.context = c; c.pMatrix = new Float32Array([ (this.canvas.height / this.canvas.width) / Math.tan(fov), 0, 0, 0, 0, 1 / Math.tan(fov), 0, 0, 0, 0, (zNear + zFar) / (zNear - zFar), -1, 0, 0, 2 * zNear * zFar / (zNear - zFar), 0]); createShader(c); c.enable(c.DEPTH_TEST); c.depthFunc(c.LEQUAL); initObjBuffers(c); this.frame = 1; } Render.update = function () { var c = this.context; var f = (this.frame / 50); g_xRot = Math.sin(f * 1.2) * 5 + 90; g_yRot = Math.cos(f * 0.9) * 5; lightx = (Math.cos(f) + 1) / 2 - 0.86 / 2; if (KeyQ) { lightz -= 0.01; } if (KeyW) { lighty += 0.01; } if (KeyE) { lightz += 0.01; } if (KeyA) { lightx -= 0.01; } if (KeyS) { lighty -= 0.01; } if (KeyD) { lightx += 0.01; } } Render.draw = function () { var c = this.context; var f = this.frame / 1000 % 1; var rgb = hslToRgb(f, 0.8, 0.2); c.clearColor(rgb[0], rgb[1], rgb[2], 1.0); c.clear(c.COLOR_BUFFER_BIT | c.DEPTH_BUFFER_BIT); var mvMatrix = new mat4.create(); mat4.identity(mvMatrix); //Modell um 3 Einheiten auf der z-Achse verschieben: mat4.translate(mvMatrix, [0, 0.3, -7]); //Model rotieren: mat4.rotateX(mvMatrix, g_xRot * Math.PI / 180.0); mat4.rotateY(mvMatrix, g_yRot * Math.PI / 180.0); mat4.rotateZ(mvMatrix, g_zRot * Math.PI / 180.0); //Beleuchtung c.uniform3f(c.getUniformLocation(c.shaderProgram, "uAmbientColor"), 0.0, 0.05, 0.2); //var vLightDirection = vec3.create([-2.0, -2.0, -2.0]); //var vAdjustedLDir = vec3.normalize(vLightDirection); //var flatLD = [vAdjustedLDir.e(1),vAdjustedLDir.e(2),vAdjustedLDir.e(3)]; c.uniform3f(c.getUniformLocation(c.shaderProgram, "uLightingDirection"), lightx, lighty, lightz); c.uniform3f(c.getUniformLocation(c.shaderProgram, "uDirectionalColor"), 0.0, 0.25, 0.05); //Ende Beleuchtung c.bindBuffer(c.ARRAY_BUFFER, c.g_backgroundNormalBufferID); c.vertexAttribPointer(c.g_vertexNormalAttribute, c.g_backgroundNormalBufferID.itemSize, c.FLOAT, false, 0, 0); c.bindBuffer(c.ARRAY_BUFFER, c.g_backgroundVertexBufferID); c.vertexAttribPointer(c.g_vertexPositionAttribute, c.g_backgroundVertexBufferID.itemSize, c.FLOAT, false, 0, 0); c.bindBuffer(c.ELEMENT_ARRAY_BUFFER, c.g_backgroundIndexBufferID); var pUniform = c.getUniformLocation(c.shaderProgram, "uPMatrix"); c.uniformMatrix4fv(pUniform, false, c.pMatrix); var mvUniform = c.getUniformLocation(c.shaderProgram, "uMVMatrix"); c.uniformMatrix4fv(mvUniform, false, mvMatrix); var normalMatrix = mat4.inverse(mvMatrix); normalMatrix = mat4.transpose(normalMatrix); var nUniform = c.getUniformLocation(c.shaderProgram, "uNMatrix"); c.uniformMatrix4fv(nUniform, false, normalMatrix); c.drawElements(c.TRIANGLES, c.g_backgroundIndexBufferID.numItems, c.UNSIGNED_SHORT, 0); //////////////////// //Beleuchtung c.uniform3f(c.getUniformLocation(c.shaderProgram, "uAmbientColor"), 0.00, 0.00, 0.00); //var vLightDirection = vec3.create([-2.0, -2.0, -2.0]); //var vAdjustedLDir = vec3.normalize(vLightDirection); //var flatLD = [vAdjustedLDir.e(1),vAdjustedLDir.e(2),vAdjustedLDir.e(3)]; c.uniform3f(c.getUniformLocation(c.shaderProgram, "uLightingDirection"), lightx, lighty, lightz); c.uniform3f(c.getUniformLocation(c.shaderProgram, "uDirectionalColor"), 0.5, 0.2, 0.0); //Ende Beleuchtung c.bindBuffer(c.ARRAY_BUFFER, c.g_nameNormalBufferID); c.vertexAttribPointer(c.g_vertexNormalAttribute, c.g_nameNormalBufferID.itemSize, c.FLOAT, false, 0, 0); c.bindBuffer(c.ARRAY_BUFFER, c.g_nameVertexBufferID); c.vertexAttribPointer(c.g_vertexPositionAttribute, c.g_nameVertexBufferID.itemSize, c.FLOAT, false, 0, 0); c.bindBuffer(c.ELEMENT_ARRAY_BUFFER, c.g_nameIndexBufferID); // var pUniform = c.getUniformLocation(c.shaderProgram, "uPMatrix"); // c.uniformMatrix4fv(pUniform, false, c.pMatrix); // // var mvUniform = c.getUniformLocation(c.shaderProgram, "uMVMatrix"); // c.uniformMatrix4fv(mvUniform, false, mvMatrix); //var normalMatrix = mat4.inverse(mvMatrix); //normalMatrix = mat4.transpose(normalMatrix); //var nUniform = c.getUniformLocation(c.shaderProgram, "uNMatrix"); //c.uniformMatrix4fv(nUniform, false, normalMatrix); c.drawElements(c.TRIANGLES, c.g_nameIndexBufferID.numItems, c.UNSIGNED_SHORT, 0); this.frame++; } function hslToRgb(h, s, l) { var r, g, b; if (s == 0) { r = g = b = l; // achromatic } else { var hue2rgb = function hue2rgb(p, q, t) { if (t < 0) t += 1; if (t > 1) t -= 1; if (t < 1 / 6) return p + (q - p) * 6 * t; if (t < 1 / 2) return q; if (t < 2 / 3) return p + (q - p) * (2 / 3 - t) * 6; return p; } var q = l < 0.5 ? l * (1 + s) : l + s - l * s; var p = 2 * l - q; r = hue2rgb(p, q, h + 1 / 3); g = hue2rgb(p, q, h); b = hue2rgb(p, q, h - 1 / 3); } return [r, g, b]; } function initObjBuffers(c) { //background c.g_backgroundVertexBufferID = c.createBuffer(); c.bindBuffer(c.ARRAY_BUFFER, c.g_backgroundVertexBufferID); c.bufferData(c.ARRAY_BUFFER, new Float32Array(background_vertices), c.STATIC_DRAW); c.g_backgroundVertexBufferID.itemSize = 3; c.g_backgroundVertexBufferID.numItems = background_vertices / c.g_backgroundVertexBufferID.itemSize; c.g_backgroundNormalBufferID = c.createBuffer(); c.bindBuffer(c.ARRAY_BUFFER, c.g_backgroundNormalBufferID); c.bufferData(c.ARRAY_BUFFER, new Float32Array(background_normals), c.STATIC_DRAW); c.g_backgroundNormalBufferID.itemSize = 3; c.g_backgroundNormalBufferID.numItems = background_normals / c.g_backgroundNormalBufferID.itemSize; c.g_backgroundIndexBufferID = c.createBuffer(); c.bindBuffer(c.ELEMENT_ARRAY_BUFFER, c.g_backgroundIndexBufferID); c.bufferData(c.ELEMENT_ARRAY_BUFFER, new Uint16Array(background_indices), c.STATIC_DRAW); c.g_backgroundIndexBufferID.itemSize = 1; c.g_backgroundIndexBufferID.numItems = background_indices.length; //name c.g_nameVertexBufferID = c.createBuffer(); c.bindBuffer(c.ARRAY_BUFFER, c.g_nameVertexBufferID); c.bufferData(c.ARRAY_BUFFER, new Float32Array(name_vertices), c.STATIC_DRAW); c.g_nameVertexBufferID.itemSize = 3; c.g_nameVertexBufferID.numItems = name_vertices / c.g_nameVertexBufferID.itemSize; c.g_nameNormalBufferID = c.createBuffer(); c.bindBuffer(c.ARRAY_BUFFER, c.g_nameNormalBufferID); c.bufferData(c.ARRAY_BUFFER, new Float32Array(name_normals), c.STATIC_DRAW); c.g_nameNormalBufferID.itemSize = 3; c.g_nameNormalBufferID.numItems = name_normals / c.g_nameNormalBufferID.itemSize; c.g_nameIndexBufferID = c.createBuffer(); c.bindBuffer(c.ELEMENT_ARRAY_BUFFER, c.g_nameIndexBufferID); c.bufferData(c.ELEMENT_ARRAY_BUFFER, new Uint16Array(name_indices), c.STATIC_DRAW); c.g_nameIndexBufferID.itemSize = 1; c.g_nameIndexBufferID.numItems = name_indices.length; } function createShader(c) { c.shaderProgram = c.createProgram(); // Vertex-Shader anlegen: var vertexShaderSource = 'attribute vec3 aVertexNormal; \n\ attribute vec3 aVertexPosition; \n\ uniform mat4 uMVMatrix; \n\ uniform mat4 uPMatrix; \n\ uniform mat4 uNMatrix; \n\ // Licht: \n\ uniform vec3 uAmbientColor; \n\ uniform vec3 uLightingDirection; \n\ uniform vec3 uDirectionalColor; \n\ varying vec3 vLightWeighting; \n\ \n\ void main(void) \n\ { \n\ gl_Position = uPMatrix * uMVMatrix * vec4(aVertexPosition, 1.0); \n\ //BEGINN BELEUCHTUNG \n\ vec4 transformedNormal = uNMatrix * vec4(aVertexNormal, 1.0); \n\ float fDirectionalLightWeighting = max(dot(transformedNormal.xyz, -uLightingDirection), 0.0); \n\ vLightWeighting = uAmbientColor + uDirectionalColor * fDirectionalLightWeighting; \n\ //ENDE BELEUCHTUNG \n\ } \n'; var vertexShader = c.createShader(c.VERTEX_SHADER); c.shaderSource(vertexShader, vertexShaderSource); c.compileShader(vertexShader); if (!c.getShaderParameter(vertexShader, c.COMPILE_STATUS)) { alert("vertexShader-Compile-Fehler:\n" + c.getShaderInfoLog(vertexShader)); return null; } c.attachShader(c.shaderProgram, vertexShader); var fragmentShaderSource = '\n\ //this ifdef is a temporary work-around for the (upcoming) strict shader validator\n\ #ifdef GL_ES\n\ precision highp float;\n\ #endif\n\ varying vec3 vLightWeighting;\n\ void main(void)\n\ {\n\ gl_FragColor = vec4(vLightWeighting,1.0)*vec4(1.0, 1.0, 1.0, 1.0);\n\ }'; var fragmentShader = c.createShader(c.FRAGMENT_SHADER); c.shaderSource(fragmentShader, fragmentShaderSource); c.compileShader(fragmentShader); if (!c.getShaderParameter(fragmentShader, c.COMPILE_STATUS)) { alert("fragmentShader-Compile-Fehler:\n" + c.getShaderInfoLog(fragmentShader)); return null; } c.attachShader(c.shaderProgram, fragmentShader); c.linkProgram(c.shaderProgram); if (!c.getProgramParameter(c.shaderProgram, c.LINK_STATUS)) { alert("Fehler beim Linken des Shader-Programms."); } c.useProgram(c.shaderProgram); c.g_vertexPositionAttribute = c.getAttribLocation(c.shaderProgram, "aVertexPosition"); c.g_vertexNormalAttribute = c.getAttribLocation(c.shaderProgram, "aVertexNormal"); c.enableVertexAttribArray(c.g_vertexPositionAttribute); c.enableVertexAttribArray(c.g_vertexNormalAttribute); } function logAndValidate(functionName, args) { alert(functionName, args); } function throwOnGLError(err, funcName, args) { alert(err); }; function resize(canvas) { var displayWidth = canvas.clientWidth; var displayHeight = canvas.clientHeight; if (canvas.width != displayWidth || canvas.height != displayHeight) { canvas.width = displayWidth; canvas.height = displayHeight; } } function onKeyDown(event) { var keyCode = event.keyCode; switch (keyCode) { case 81: //q KeyQ = true; break; case 87: //w KeyW = true; break; case 69: //e KeyE = true; break; case 65: //a KeyA = true; break; case 83: //s KeyS = true; break; case 68: //d KeyD = true; break; } } function onKeyUp(event) { var keyCode = event.keyCode; switch (keyCode) { case 81: //q KeyQ = false; break; case 87: //w KeyW = false; break; case 69: //e KeyE = false; break; case 65: //a KeyA = false; break; case 83: //s KeyS = false; break; case 68: //d KeyD = false; break; } }