Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <html>
- <head>
- <title>WebGL Application</title>
- <meta http-equiv="content-type" content="text/html; charset=ISO-8859-1">
- <script type="text/javascript" src="glMatrix-0.9.5.min.js"></script>
- <script type="text/javascript" src="webgl-utils.js"></script>
- <script id="shader-vs" type="x-shader/x-vertex">
- attribute vec3 aVertexPosition;
- //attribute vec4 aVertexColor;
- //attribute vec2 aTextCoords;
- uniform mat4 uVMatrix;
- uniform mat4 uSMatrix;
- uniform mat4 uPMatrix;
- varying vec4 curr_color;
- varying vec2 curr_textcoord;
- void main(void) {
- gl_Position = uPMatrix * uVMatrix * uSMatrix * vec4(aVertexPosition, 1.0);
- //curr_color = aVertexColor;
- //curr_textcoord = aTextCoords;
- }
- </script>
- <script id="shader-fs" type="x-shader/x-fragment">
- precision mediump float;
- //uniform sampler2D texture;
- //varying vec2 curr_textcoord;
- void main(void) {
- // gl_FragColor = texture2D(texture, curr_textcoord);
- gl_FragColor = vec4(0, 1, 0, 1);
- }
- </script>
- <script type="text/javascript">
- //VARIABLES DECARATION
- var viewMatrix = mat4.create();//Camera
- var sceneMatrix = mat4.create();//situa los objetos de la geometria en la escena
- var pMatrix = mat4.create();
- var mouseRotationMatrix = mat4.create();
- mat4.identity(mouseRotationMatrix);
- var currently_pressed_keys = {};
- var mouse_down_left = false;
- var mouse_down_wheel = false;
- var lastMouseX;
- var lastMouseY;
- var panX = 0;
- var panY = 0;
- var fov = 45;
- var gl;
- function initGL(canvas) {
- try {
- gl = canvas.getContext("experimental-webgl");
- gl.viewportWidth = canvas.width;
- gl.viewportHeight = canvas.height;
- } catch (e) {
- }
- if (!gl) {
- alert("Could not initialise WebGL, sorry :-(");
- }
- }
- 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-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, str);
- gl.compileShader(shader);
- if (!gl.getShaderParameter(shader, gl.COMPILE_STATUS)) {
- alert(gl.getShaderInfoLog(shader));
- return null;
- }
- return shader;
- }
- var shaderProgram;
- function initShaders() {
- var fragmentShader = getShader(gl, "shader-fs");
- var vertexShader = getShader(gl, "shader-vs");
- 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);
- shaderProgram.vertexPositionAttribute = gl.getAttribLocation(shaderProgram, "aVertexPosition");
- gl.enableVertexAttribArray(shaderProgram.vertexPositionAttribute);
- shaderProgram.vertexColorAttribute = gl.getAttribLocation(shaderProgram, "aVertexColor");
- gl.enableVertexAttribArray(shaderProgram.vertexColorAttribute);
- shaderProgram.vertexTexCoordAttribute = gl.getAttribLocation(shaderProgram, "aTextCoords");
- gl.enableVertexAttribArray(shaderProgram.vertexTexCoordAttribute);
- shaderProgram.pMatrixUniform = gl.getUniformLocation(shaderProgram, "uPMatrix");
- shaderProgram.vMatrixUniform = gl.getUniformLocation(shaderProgram, "uVMatrix");
- shaderProgram.sMatrixUniform = gl.getUniformLocation(shaderProgram, "uSMatrix");
- shaderProgram.textureUniform = gl.getUniformLocation(shaderProgram, "texture");
- }
- function handleMouseDown(event)
- {
- lastMouseX = event.clientX;
- lastMouseY = event.clientY;
- if(event.button == 0)
- {
- mouse_down_left = true;
- }
- else if (event.button == 1)
- {
- mouse_down_wheel = true;
- }
- }
- function deg2Rad(value)
- {
- value *= (Math.PI / 180);
- return value;
- }
- function handleMouseMove(event)
- {
- var newY = event.clientY;
- var newX = event.clientX;
- var deltaX = newX - lastMouseX;
- var deltaY = newY - lastMouseY;
- if(mouse_down_left)
- {
- var newRotationMatrix = mat4.create();
- mat4.identity(newRotationMatrix);
- mat4.rotate(newRotationMatrix, deg2Rad(deltaX / 10), [0, 1, 0]);
- mat4.rotate(newRotationMatrix, deg2Rad(deltaY / 10), [1, 0, 0]);
- mat4.multiply(newRotationMatrix, mouseRotationMatrix, mouseRotationMatrix);
- lastMouseX = newX;
- lastMouseY = newY;
- }
- if(mouse_down_wheel)
- {
- panX = (deltaX) / 100;
- panY = (deltaY) / 100;
- }
- }
- function handleMouseUp(event)
- {
- if(mouse_down_left)
- mouse_down_left = false;
- if(mouse_down_wheel)
- mouse_down_wheel = false;
- }
- function handleKeyDown(event)
- {
- currently_pressed_keys[event.keyCode] = true;
- }
- function handleKeyUp(event)
- {
- currently_pressed_keys[event.keyCode] = false;
- }
- function sendMatricesToShader() {
- gl.uniformMatrix4fv(shaderProgram.pMatrixUniform, false, pMatrix);
- gl.uniformMatrix4fv(shaderProgram.vMatrixUniform, false, viewMatrix);
- gl.uniformMatrix4fv(shaderProgram.sMatrixUniform,false, sceneMatrix);
- }
- //LOADSCENEONGPU
- var squareVertexPositionBuffer;
- var squareVertexColorBuffer;
- var myTexture;
- function loadTextureOnGPU()
- {
- myTexture = gl.createTexture();
- myTexture.image = new Image();
- myTexture.image.onload = function()
- {
- SetTextureParams(myTexture);
- }
- myTexture.image.src = "marvel.png"
- }
- function SetTextureParams(texture)
- {
- gl.bindTexture(gl.TEXTURE_2D, texture);
- gl.pixelStorei(gl.UNPACK_FLIP_Y_WEBGL, true);
- gl.texImage2D(gl.TEXTURE_2D,0,gl.RGBA,gl.RGBA, gl.UNSIGNED_BYTE, texture.image);
- gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.LINEAR);
- gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR);
- gl.bindTexture(gl.TEXTURE_2D, null);
- }
- function LoadModel()
- {
- var request = new XMLHttpRequest();
- request.open("GET", "Teapot.json");
- request.onreadystatechange = function()
- {
- if(request.readyState == 4){
- handleLoadedModel(JSON.parse(request.responseText));
- }
- }
- request.send();
- }
- function loadSceneOnGPU() {
- cubeVertexPositionBuffer = gl.createBuffer();
- gl.bindBuffer(gl.ARRAY_BUFFER, cubeVertexPositionBuffer);
- var cube_vertices = [
- // Front face
- -1.0, -1.0, 1.0,
- 1.0, -1.0, 1.0,
- 1.0, 1.0, 1.0,
- -1.0, 1.0, 1.0,
- // Back face
- -1.0, -1.0, -1.0,
- -1.0, 1.0, -1.0,
- 1.0, 1.0, -1.0,
- 1.0, -1.0, -1.0,
- // Top face
- -1.0, 1.0, -1.0,
- -1.0, 1.0, 1.0,
- 1.0, 1.0, 1.0,
- 1.0, 1.0, -1.0,
- // Bottom face
- -1.0, -1.0, -1.0,
- 1.0, -1.0, -1.0,
- 1.0, -1.0, 1.0,
- -1.0, -1.0, 1.0,
- // Right face
- 1.0, -1.0, -1.0,
- 1.0, 1.0, -1.0,
- 1.0, 1.0, 1.0,
- 1.0, -1.0, 1.0,
- // Left face
- -1.0, -1.0, -1.0,
- -1.0, -1.0, 1.0,
- -1.0, 1.0, 1.0,
- -1.0, 1.0, -1.0,
- ];
- gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(cube_vertices), gl.STATIC_DRAW);
- cubeVertexPositionBuffer.itemSize = 3;
- cubeVertexPositionBuffer.numItems = 24;
- cubeVertexIndexBuffer = gl.createBuffer();
- gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, cubeVertexIndexBuffer);
- var cube_indices = [
- 0, 1, 2, 0, 2, 3,
- 4, 5, 6, 4, 6, 7,
- 8, 9, 10, 8, 10, 11,
- 12, 13, 14, 12, 14, 15,
- 16, 17, 18, 16, 18, 19,
- 20, 21, 22, 20, 22, 23
- ];
- gl.bufferData(gl.ELEMENT_ARRAY_BUFFER, new Uint16Array(cube_indices), gl.STATIC_DRAW);
- cubeVertexIndexBuffer.itemSize = 1;
- cubeVertexIndexBuffer.numItems = 36;
- squareVertexPositionBuffer = gl.createBuffer();
- gl.bindBuffer(gl.ARRAY_BUFFER, squareVertexPositionBuffer);
- var vertices = [
- 1.0, 1.0, 0.0,
- -1.0, 1.0, 0.0,
- 1.0,-1.0, 0.0,
- - 1.0, -1.0, 0.0
- ];
- gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(vertices), gl.STATIC_DRAW);
- squareVertexPositionBuffer.itemSize = 3;
- squareVertexPositionBuffer.numItems = 4;//because we have a square
- squareVertexColorBuffer = gl.createBuffer();
- gl.bindBuffer(gl.ARRAY_BUFFER, squareVertexColorBuffer);
- var colors = [
- 1.0, 0.0, 0.0, 1.0,
- 1.0, 1.0, 0.0, 1.0,
- 1.0, 0.0, 1.0, 1.0,
- 1.0,1.0,0.0, 1.0
- ];
- gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(colors), gl.STATIC_DRAW);
- squareVertexColorBuffer.itemSize = 4;
- squareVertexColorBuffer.numItems = 4;//because we have a square
- squareAlonsoBuffer = gl.createBuffer();
- gl.bindBuffer(gl.ARRAY_BUFFER, squareAlonsoBuffer);
- var texcoord = [
- 1.0, 0.5,
- 0.5, 0.5,
- 1, 0.0,
- 0.5, 0.0
- ];
- gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(texcoord), gl.STATIC_DRAW);
- squareAlonsoBuffer.itemSize = 2;
- squareAlonsoBuffer.numItems = 4;
- }
- var modelVertexPositionBuffer;
- var modelVertexNormalBuffer;
- var modelVertexTextureCoordBuffer;
- var modelVertexIndexBuffer;
- function handleLoadedModel(modelData){
- modelVertexPositionBuffer = gl.createBuffer();
- gl.bindBuffer(gl.ARRAY_BUFFER,modelVertexPositionBuffer);
- gl.bufferData(gl.ARRAY_BUFFER,new Float32Array(modelData.vertexPositions),gl.STATIC_DRAW);
- modelVertexPositionBuffer.itemSize = 3;
- modelVertexPositionBuffer.numItems = modelData.vertexPositions/3;
- modelVertexNormalBuffer = gl.createBuffer();
- gl.bindBuffer(gl.ARRAY_BUFFER,modelVertexNormalBuffer);
- gl.bufferData(gl.ARRAY_BUFFER,new Float32Array(modelData.vertexNormals),gl.STATIC_DRAW);
- modelVertexNormalBuffer.itemSize = 3;
- modelVertexNormalBuffer.numItems = modelData.vertexNormals.length/3;
- modelVertexTextureCoordBuffer = gl.createBuffer();
- gl.bindBuffer(gl.ARRAY_BUFFER, modelVertexTextureCoordBuffer);
- gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(modelData.vertexTextureCoords),gl.STATIC_DRAW);
- modelVertexTextureCoordBuffer.itemSize = 2;
- modelVertexTextureCoordBuffer.numItems = modelData.vertexTextureCoords.length/2;
- modelVertexIndexBuffer = gl.createBuffer();
- gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER,modelVertexIndexBuffer);
- gl.bufferData(gl.ELEMENT_ARRAY_BUFFER, new Uint16Array(modelData.indices),gl.STATIC_DRAW);
- modelVertexIndexBuffer.itemSize = 1;
- modelVertexIndexBuffer.numItems = modelData.indices.length;
- }
- function drawScene()
- {
- gl.viewport(0, 0, gl.viewportWidth, gl.viewportHeight);
- gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT);
- //Type of Camera(pMatrix)
- mat4.perspective(fov, gl.viewportWidth / gl.viewportHeight, 0.1, 100.0, pMatrix);
- //Position and orientation of the camera(vMatrix)
- mat4.identity(viewMatrix);
- mat4.translate(viewMatrix, [0.0, 0.0, -10.0]);
- mat4.identity(sceneMatrix);
- mat4.translate(sceneMatrix, [panX, -panY, 0.0]);
- mat4.multiply(viewMatrix, mouseRotationMatrix);
- gl.bindBuffer(gl.ARRAY_BUFFER, cubeVertexPositionBuffer);
- gl.vertexAttribPointer(shaderProgram.vertexPositionAttribute, cubeVertexPositionBuffer.itemSize, gl.FLOAT, false, 0, 0);
- //gl.bindBuffer(gl.ARRAY_BUFFER, squareVertexColorBuffer);
- //gl.vertexAttribPointer(shaderProgram.vertexColorAttribute, squareVertexColorBuffer.itemSize, gl.FLOAT, false, 0, 0);
- // gl.bindBuffer(gl.ARRAY_BUFFER, squareAlonsoBuffer);
- // gl.vertexAttribPointer(shaderProgram.vertexTexCoordAttribute, squareAlonsoBuffer.itemSize, gl.FLOAT, false, 0, 0);
- //gl.activeTexture(gl.TEXTURE0);
- //gl.bindTexture(gl.TEXTURE_2D, myTexture);
- //gl.uniform1i(shaderProgram.textureUniform, 0);
- sendMatricesToShader();
- //in the previous line we tell the vertexshader were in memory our vertices are
- //console.log(cubeVertexIndexBuffer.numItems);
- gl.drawElements(gl.TRIANGLE_STRIP, cubeVertexIndexBuffer.numItems, gl.UNSIGNED_SHORT, 0);
- //model draw
- if(modelVertexPositionBuffer)
- {
- mat4.identity(sceneMatrix);
- mat4.translate(sceneMatrix, [0.0, 0.0, -10]);
- mat4.scale(sceneMatrix,[0.5,0.5,0.5]);
- gl.bindBuffer(gl.ARRAY_BUFFER, modelVertexPositionBuffer);
- gl.vertexAttribPointer(shaderProgram.vertexPositionAttribute,modelVertexPositionBuffer.itemSize,gl.FLOAT,false,0,0);
- gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER,modelVertexIndexBuffer),
- sendMatricesToShader();
- gl.drawElements(gl.TRIANGLES,modelVertexIndexBuffer.numItems, gl.UNSIGNED_SHORT,0);
- }
- }
- function handleKeys()
- {
- if(currently_pressed_keys[38])
- {
- if(fov > 15)
- fov -= 0.5;
- }
- if(currently_pressed_keys[40])
- {
- if(fov < 65)
- fov += 0.5;
- }
- if(currently_pressed_keys[82])
- {
- fov = 45;
- }
- }
- function reDraw()
- {
- requestAnimationFrame(reDraw);
- handleKeys();
- drawScene();
- }
- function webGLStart() {
- var canvas = document.getElementById("webGL-canvas");
- canvas.width = window.innerWidth;
- canvas.height = window.innerHeight;
- document.onkeydown = handleKeyDown;
- document.onkeyup = handleKeyUp;
- document.onmousedown = handleMouseDown;
- document.onmouseup = handleMouseUp;
- document.onmousemove = handleMouseMove;
- initGL(canvas);
- initShaders();
- LoadModel();
- loadSceneOnGPU();
- loadTextureOnGPU();
- gl.clearColor(0.0, 0.0, 0.0, 1.0);
- gl.enable(gl.DEPTH_TEST);
- reDraw();
- }
- </script>
- </head>
- <body onload="webGLStart();" onresize="webGLStart();">
- <canvas id="webGL-canvas" style="border: none; width:100%; height:100%; margin: 0 auto;"></canvas>
- </body>
- </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement