Permalink
Browse files

Building the Game Part 5

  • Loading branch information...
toji committed Nov 28, 2011
1 parent cf9ca05 commit b27e446098eb48edb820cd583322b3591927d436
View
@@ -1 +1,4 @@
-.DS_Store
+.DS_Store
+public/root/level/*
+public/root/texture/*
+public/root/model/*
View
Binary file not shown.
View
Binary file not shown.
View
@@ -136,10 +136,10 @@ define([
this._angles = vec3.create();
this._position = vec3.create();
- this.speed = 100;
+ this.speed = 25;
this._pressedKeys = new Array(128);
this._viewMat = mat4.create();
- this._cameraMat = mat4.create();
+ this._rotMat = mat4.create();
this._dirty = true;
// Set up the appropriate event hooks
@@ -160,14 +160,18 @@ define([
}, false);
canvas.addEventListener('mousemove', function (event) {
+ if(!event) { throw new Error("BAD SHIT HAPPENED!"); }
if (moving) {
var xDelta = event.pageX - lastX,
- yDelta = event.pageY - lastY;
+ yDelta = event.pageY - lastY,
+ rot = self._rotMat;
lastX = event.pageX;
lastY = event.pageY;
self._angles[1] += xDelta * 0.025;
+ // Keep our rotation in the range of [0, 2*PI]
+ // (Prevents numeric instability if you spin around a LOT.)
while (self._angles[1] < 0) {
self._angles[1] += Math.PI * 2.0;
}
@@ -176,12 +180,18 @@ define([
}
self._angles[0] += yDelta * 0.025;
- while (self._angles[0] < -Math.PI * 0.5) {
+ // Clamp the up/down rotation to prevent us from flipping upside-down
+ if (self._angles[0] < -Math.PI * 0.5) {
self._angles[0] = -Math.PI * 0.5;
}
- while (self._angles[0] > Math.PI * 0.5) {
+ if (self._angles[0] > Math.PI * 0.5) {
self._angles[0] = Math.PI * 0.5;
}
+
+ // Update the directional matrix
+ mat4.identity(rot);
+ mat4.rotateY(rot, -self._angles[1]);
+ mat4.rotateX(rot, -self._angles[0]);
self._dirty = true;
}
@@ -216,9 +226,9 @@ define([
if (this._dirty) {
var mv = this._viewMat;
mat4.identity(mv);
- mat4.rotateX(mv, this._angles[0] - Math.PI / 2.0);
- mat4.rotateZ(mv, this._angles[1]);
- mat4.rotateY(mv, this._angles[2]);
+ mat4.rotateX(mv, this._angles[0]);
+ mat4.rotateY(mv, this._angles[1]);
+ mat4.rotateZ(mv, this._angles[2]);
mat4.translate(mv, [-this._position[0], -this._position[1], -this._position[2]]);
this._dirty = false;
}
@@ -228,15 +238,14 @@ define([
FlyingCamera.prototype.update = function (frameTime) {
var dir = vec3.create(),
- speed = (this.speed / 1000) * frameTime,
- cam;
+ speed = (this.speed / 1000) * frameTime;
// This is our first person movement code. It's not really pretty, but it works
if (this._pressedKeys['W'.charCodeAt(0)]) {
- dir[1] += speed;
+ dir[2] -= speed;
}
if (this._pressedKeys['S'.charCodeAt(0)]) {
- dir[1] -= speed;
+ dir[2] += speed;
}
if (this._pressedKeys['A'.charCodeAt(0)]) {
dir[0] -= speed;
@@ -245,22 +254,15 @@ define([
dir[0] += speed;
}
if (this._pressedKeys[32]) { // Space, moves up
- dir[2] += speed;
+ dir[1] += speed;
}
if (this._pressedKeys[17]) { // Ctrl, moves down
- dir[2] -= speed;
+ dir[1] -= speed;
}
if (dir[0] !== 0 || dir[1] !== 0 || dir[2] !== 0) {
- cam = this._cameraMat;
- mat4.identity(cam);
- mat4.rotateX(cam, this._angles[0]);
- mat4.rotateZ(cam, this._angles[1]);
- mat4.inverse(cam);
-
- mat4.multiplyVec3(cam, dir);
-
// Move the camera in the direction we are facing
+ mat4.multiplyVec3(this._rotMat, dir);
vec3.add(this._position, dir);
this._dirty = true;
View
@@ -24,21 +24,21 @@
define([
"camera",
"model",
+ "skinned-model",
"animation",
+ "level",
"util/gl-util",
"js/util/gl-matrix.js",
-], function(camera, model, animation, glUtil) {
+], function(camera, model, skinnedModel, animation, level, glUtil) {
"use strict";
var GameRenderer = function (gl, canvas) {
var i, instance;
-
// To get a camera that gives you a flying first-person perspective, use camera.FlyingCamera
// To get a camera that rotates around a fixed point, use camera.ModelCamera
- this.camera = new camera.ModelCamera(canvas);
- this.camera.distance = 75;
- this.camera.setCenter([0, 0, 0]);
+ this.camera = new camera.FlyingCamera(canvas);
+ this.camera.setPosition([-2, 3, 15]);
this.fov = 45;
this.projectionMat = mat4.create();
@@ -47,42 +47,11 @@ define([
gl.clearColor(0.0, 0.0, 0.2, 1.0);
gl.clearDepth(1.0);
gl.enable(gl.DEPTH_TEST);
+ gl.enable(gl.CULL_FACE);
gl.pixelStorei(gl.UNPACK_FLIP_Y_WEBGL, true);
- this.model1 = new model.Model();
- this.model1.load(gl, "root/model/vat");
-
- this.model2 = new model.Model();
- this.model2.load(gl, "root/model/crateSmall");
-
- this.model3 = new model.Model();
- this.model3.load(gl, "root/model/crateMedium");
-
- this.model4 = new model.Model();
- this.model4.load(gl, "root/model/barrelSmall");
-
- function createInstances(modelType, count) {
- for(i = 0; i < count; ++i) {
- instance = modelType.createInstance();
-
- // Generate a random rotation and position for this instance
- mat4.rotateX(instance.matrix, Math.random() * Math.PI);
- mat4.rotateY(instance.matrix, Math.random() * Math.PI);
- mat4.rotateZ(instance.matrix, Math.random() * Math.PI);
-
- // Generate a random rotation and position for this instance
- mat4.translate(instance.matrix,
- [(Math.random()-0.5) * 100.0,
- (Math.random()-0.5) * 100.0,
- (Math.random()-0.5) * 100.0]
- );
- }
- }
-
- createInstances(this.model1, 250);
- createInstances(this.model2, 250);
- createInstances(this.model3, 250);
- createInstances(this.model4, 250);
+ this.level = new level.Level();
+ this.level.load(gl, "root/level/level1");
};
GameRenderer.prototype.resize = function (gl, canvas) {
@@ -98,10 +67,7 @@ define([
gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT);
- this.model1.drawInstances(gl, viewMat, projectionMat);
- this.model2.drawInstances(gl, viewMat, projectionMat);
- this.model3.drawInstances(gl, viewMat, projectionMat);
- this.model4.drawInstances(gl, viewMat, projectionMat);
+ this.level.draw(gl, viewMat, projectionMat);
};
return {
View
@@ -0,0 +1,109 @@
+/*
+ * Copyright (c) 2011 Brandon Jones
+ *
+ * This software is provided 'as-is', without any express or implied
+ * warranty. In no event will the authors be held liable for any damages
+ * arising from the use of this software.
+ *
+ * Permission is granted to anyone to use this software for any purpose,
+ * including commercial applications, and to alter it and redistribute it
+ * freely, subject to the following restrictions:
+ *
+ * 1. The origin of this software must not be misrepresented; you must not
+ * claim that you wrote the original software. If you use this software
+ * in a product, an acknowledgment in the product documentation would be
+ * appreciated but is not required.
+ *
+ * 2. Altered source versions must be plainly marked as such, and must not
+ * be misrepresented as being the original software.
+ *
+ * 3. This notice may not be removed or altered from any source
+ * distribution.
+ */
+
+define([
+ "util/gl-util",
+ "model",
+ "texture",
+ "util/q",
+ "js/util/gl-matrix.js"
+], function (glUtil, model, texture, Q) {
+
+ "use strict";
+
+ var Level = function () {
+ this.props = [];
+ this.lightmaps = [];
+ this.complete = false;
+ };
+
+ Level.prototype.load = function (gl, url, callback) {
+ var self = this;
+
+ // Load the json portion of the model
+ var jsonXhr = new XMLHttpRequest();
+ jsonXhr.open('GET', url + ".wgllevel", true);
+ jsonXhr.onload = function() {
+ // TODO: Error Catch!
+ var level = JSON.parse(this.responseText);
+ self._parseLevel(level);
+ self._compileLevel(gl);
+
+ self.complete = true;
+ if (callback) { callback(self); }
+ };
+ jsonXhr.send(null);
+ };
+
+ Level.prototype._parseLevel = function (doc) {
+ this.lightmapPaths = doc.lightmaps;
+ this.props = doc.props;
+ };
+
+ Level.prototype._compileLevel = function (gl) {
+ var i, j, prop, instance, url, lightmap;
+ for(i in this.lightmapPaths) {
+ lightmap = this.lightmapPaths[i];
+ this._loadLightmap(gl, i, this.lightmapPaths[i]);
+ }
+
+ for (i in this.props) {
+ prop = this.props[i];
+ url = prop.model;
+ prop.model = new model.Model();
+ prop.model.load(gl, url);
+
+ for(j in prop.instances) {
+ instance = prop.instances[j];
+ instance.modelInstance = prop.model.createInstance();
+ instance.modelInstance.matrix = mat4.fromRotationTranslation(instance.rot, instance.pos);
+ mat4.scale(instance.modelInstance.matrix, [instance.scale, instance.scale, instance.scale]);
+ instance.modelInstance.lightmap = instance.lightmap;
+ instance.modelInstance.lightmap.scale = new Float32Array(instance.lightmap.scale);
+ instance.modelInstance.lightmap.offset = new Float32Array(instance.lightmap.offset);
+ }
+ }
+ };
+
+ Level.prototype._loadLightmap = function(gl, id, url) {
+ var self = this;
+ this.lightmaps[id] = null;
+ Q.when(texture.TextureManager.getInstance(gl, url), function(tex) {
+ self.lightmaps[id] = tex;
+ });
+ };
+
+ Level.prototype.draw = function (gl, viewMat, projectionMat) {
+ var i, prop;
+ if (!this.complete) { return; }
+
+ for (i in this.props) {
+ prop = this.props[i];
+ prop.model.drawLightmappedInstances(gl, viewMat, projectionMat, this.lightmaps);
+ }
+ };
+
+ return {
+ Level: Level
+ };
+});
Oops, something went wrong.

0 comments on commit b27e446

Please sign in to comment.