Skip to content

Commit

Permalink
Renamed PlayerRender to ModelRender. Fixed bug in PlayerRender caused…
Browse files Browse the repository at this point in the history
… by having more than 3 models in a single .mdl file
  • Loading branch information
Mathias committed Dec 2, 2013
1 parent 36025c5 commit c17579d
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 44 deletions.
55 changes: 23 additions & 32 deletions PlayerRender.js → ModelRender.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
**/

/**
Expand All @@ -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,
Expand All @@ -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();

Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -438,24 +424,25 @@ 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)];

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) {
Expand All @@ -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]);
Expand Down Expand Up @@ -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) {
Expand Down
15 changes: 7 additions & 8 deletions Player.js
Original file line number Diff line number Diff line change
Expand Up @@ -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];
Expand All @@ -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) {
Expand All @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion config.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 7 additions & 3 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
<script type="text/javascript" src="MapRender.js"></script>
<script type="text/javascript" src="Map.js"></script>
<script type="text/javascript" src="PlayerParser.js"></script>
<script type="text/javascript" src="PlayerRender.js"></script>
<script type="text/javascript" src="ModelRender.js"></script>
<script type="text/javascript" src="Player.js"></script>
<script type="text/javascript" src="lib/keyboard.js"></script>

Expand Down Expand Up @@ -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);

Expand All @@ -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
Expand Down

0 comments on commit c17579d

Please sign in to comment.