From c17579d395fc6e38d36a25c94e993b30e374a7bd Mon Sep 17 00:00:00 2001 From: Mathias Date: Mon, 2 Dec 2013 17:40:14 +0100 Subject: [PATCH] Renamed PlayerRender to ModelRender. Fixed bug in PlayerRender caused by having more than 3 models in a single .mdl file --- PlayerRender.js => ModelRender.js | 55 +++++++++++++------------------ Player.js | 15 ++++----- config.js | 2 +- index.html | 10 ++++-- 4 files changed, 38 insertions(+), 44 deletions(-) rename PlayerRender.js => ModelRender.js (93%) diff --git a/PlayerRender.js b/ModelRender.js similarity index 93% rename from PlayerRender.js rename to ModelRender.js index bc69e51..b8422e2 100644 --- a/PlayerRender.js +++ b/ModelRender.js @@ -2,8 +2,7 @@ This file contains all the code needed in order to render a textured .mdl version 10 - TODO: Fix mvMatrix so it looks CS-ish - Provide easy-to-use public functions for choosing the animations + TODO: Provide easy-to-use public functions for choosing the animations **/ /** @@ -30,7 +29,7 @@ quat.fromAngles = function(angles) { return [x, y, z, w]; }; -cs.PlayerRender = function(gl, player) { +cs.ModelRender = function(gl, player) { var constants = { valid: 0, total: 1, @@ -44,12 +43,9 @@ cs.PlayerRender = function(gl, player) { STUDIO_RLOOP: 0x8000 }; - this.player = player; - this.gl = gl; + this.sequenceIndex = 1; var frame = 0; - var controllers = [0, 0, 0, 0]; - var mouth = 0; var vertexBuffer = gl.createBuffer(); @@ -125,36 +121,26 @@ cs.PlayerRender = function(gl, player) { var value; if(i <= 3) { if(boneController.type & constants.RLOOP) { - //TODO: Woah magic constant ftw - value = controllers[i] * 1.40625 + boneController.start; + value = boneController.start; } - else { - value = controllers[i] / 255.0; - if (value < 0) - value = 0; - if (value > 1.0) - value = 1.0; - - value = (1.0 - value) * boneController.start + value * boneController.end; + else { + value = boneController.start + value * boneController.end; } } else { - value = mouth / 64.0; - if (value > 1.0) - value = 1.0; - value = (1.0 - value) * boneController.start + value * boneController.end; + value = boneController.start + value * boneController.end; } //Chrome refuses to optimize the function due to non constant switch labels switch(boneController.type & constants.STUDIO_TYPES) { - case 0x0008: //STUDIO_XR - case 0x0010: //STUDIO_YR - case 0x0020: //STUDIO_ZR + case constants.STUDIO_XR: + case constants.STUDIO_YR: + case constants.STUDIO_ZR: adj[j] = value * (Math.PI / 180.0); break; - case 0x0001: //STUDIO_X - case 0x0002: //STUDIO_Y - case 0x0004: //STUDIO_Z + case constants.STUDIO_X: + case constants.STUDIO_Y: + case constants.STUDIO_Z: adj[j] = value; break; } @@ -438,6 +424,7 @@ cs.PlayerRender = function(gl, player) { transforms[n] = vectorTransform([vertices[i], vertices[i+1], vertices[i+2]], transformations[model.transformIndices[n]]); } + var k = 0; for(var i = 0; i < model.numMesh; ++i) { var mesh = model.mesh[i]; var texture = player.textures[DataReader.readSignedShort(player.data, player.header.skinIndex + 2*mesh.skinRef)]; @@ -445,17 +432,17 @@ cs.PlayerRender = function(gl, player) { var s = 1.0/texture.width; var t = 1.0/texture.height; - var index = mesh.triIndex + i; + var index = mesh.triIndex; gl.bindTexture(gl.TEXTURE_2D, texture.id); gl.uniform1i(shaderProgram.samplerUniform, 0); while(true) { var j = DataReader.readSignedShort(player.data, index); - index += 2; if(j === 0) { break; } + index += 2; var fanMode = false; if(j < 0) { @@ -473,6 +460,11 @@ cs.PlayerRender = function(gl, player) { //Add vertex var vertex = transforms[vertIndex]; + ++k; + if(k === 130) { + var g = 0; + } + buffer.push(vertex[0]); buffer.push(vertex[1]); buffer.push(vertex[2]); @@ -506,14 +498,13 @@ cs.PlayerRender = function(gl, player) { gl.enableVertexAttribArray(shaderProgram.vertexPositionAttribute); gl.enableVertexAttribArray(shaderProgram.texCoordAttribute); - //TODO: Find the magic mvMatrix that looks correct + mat4.rotateX(cs.mvMatrix, cs.mvMatrix, -Math.PI/2); mat4.rotateZ(cs.mvMatrix, cs.mvMatrix, Math.PI/2); gl.uniformMatrix4fv(shaderProgram.pMatrixUniform, false, cs.pMatrix); gl.uniformMatrix4fv(shaderProgram.mvMatrixUniform, false, cs.mvMatrix); - var sequenceIndex = 1; - var sequence = player.sequences[sequenceIndex]; + var sequence = player.sequences[this.sequenceIndex]; var transformations = setupBones(frame, sequence); for(var i = 0; i < player.header.numBodyParts; ++i) { diff --git a/Player.js b/Player.js index b7e9709..7e33d82 100644 --- a/Player.js +++ b/Player.js @@ -5,17 +5,17 @@ **/ window.cs = window.cs || { }; -cs.Player = function(gl, x, y, z, data, speed) { +cs.Player = function(gl, x, y, z, data) { this.x = x; this.y = y; this.z = z; this.yAngle = 0; this.xAngle = 0; + this.speed = 5; //X and Y direction. Not necessarily normalized var dir = [0, 0]; - this.speed = speed; var playerData = cs.PlayerParser.parse(gl, data); - var playerRender = new cs.PlayerRender(gl, playerData); + var playerRender = new cs.ModelRender(gl, playerData); this.position = function() { return [this.x, this.y, this.z]; @@ -28,13 +28,13 @@ cs.Player = function(gl, x, y, z, data, speed) { var normalDir = [0, 0]; vec2.normalize(normalDir, dir); //Move forward - var newX = this.x + speed*normalDir[0]*Math.cos(this.yAngle); - var newY = this.y - speed*normalDir[0]*Math.sin(this.yAngle); + var newX = this.x + this.speed*normalDir[0]*Math.cos(this.yAngle); + var newY = this.y - this.speed*normalDir[0]*Math.sin(this.yAngle); var newZ = this.z; //Strafe - newY -= speed*normalDir[1]*Math.cos(Math.PI - this.yAngle); - newX += speed*normalDir[1]*Math.sin(Math.PI - this.yAngle); + newY -= this.speed*normalDir[1]*Math.cos(Math.PI - this.yAngle); + newX += this.speed*normalDir[1]*Math.sin(Math.PI - this.yAngle); //Apply gravity if we're not on the ground. TODO: Accelerate instead of subtracting a constant if(!onGround) { @@ -52,7 +52,6 @@ cs.Player = function(gl, x, y, z, data, speed) { var PI_HALF = Math.PI/2.0; var PI_TWO = Math.PI*2.0; - //Hardcoded sensitivity. TODO: Read off some future "Settings" class this.yAngle += xDelta * cs.config.MOUSE_SENSITIVITY; //Make sure we're in the interval [0, 2*pi] while (this.yAngle < 0) { diff --git a/config.js b/config.js index c58d22f..6b7fc69 100644 --- a/config.js +++ b/config.js @@ -6,7 +6,7 @@ window.cs = window.cs || { }; cs.config = { MAP_PATH: "data/maps/cs_assault.bsp", - PLAYER_PATH: "data/models/gign.mdl", + PLAYER_PATH: "data/models/equipment/v_ak47.mdl", NEAR_CLIPPING: 0.1, FAR_CLIPPING: 10000.0, FIELD_OF_VIEW: 59.0, //In degrees diff --git a/index.html b/index.html index 931d207..f0695a5 100644 --- a/index.html +++ b/index.html @@ -12,7 +12,7 @@ - + @@ -99,7 +99,7 @@ download(cs.config.PLAYER_PATH, function(data) { //Hardcoded cs_assault position. TODO: Read this from the entity set - cs.player = new cs.Player(gl, -320, 352, 48, data, 5); + cs.player = new cs.Player(gl, -320, 352, 48, data); gl.clearColor(0.0, 0.0, 0.0, 1.0); @@ -116,7 +116,11 @@ //Listen for clicks on the canvas canvas.addEventListener("click", function() { - PointerLock.requestPointerLock(canvas); + //is the mouse not currently locked? + if(!PointerLock.pointerLockElement()) { + //Nope. Request locking + PointerLock.requestPointerLock(canvas); + } }, false); //Listen for pointer locking