Просмотр исходного кода

improved, but still weird/wrong camera.

based on [tut][], but somehow still wrong. the interesting stuff is in
[shooter.js][], in the functions `rotateView` and `moveView`.

[tut]: http://www.html5rocks.com/en/tutorials/pointerlock/intro
[shooter.js]: http://www.html5rocks.com/en/tutorials/pointerlock/intro/shooter.js
Lucas Stadler лет назад: 12
Родитель
Сommit
53e7b41dc1
1 измененных файлов с 46 добавлено и 21 удалено
  1. 46 21
      js/pixl/public/trixl.html

+ 46 - 21
js/pixl/public/trixl.html

@ -86,6 +86,46 @@
86 86
		//gl.enable(gl.CULL_FACE);
87 87
		gl.enable(gl.DEPTH_TEST);
88 88
89
		trixl.camera = { pos: [0, 0, 5], focus: [0, 0, 10], up: [0, 1, 0] };
90
		trixl.camera.front = function() {
91
			var front = vec3.subtract([], trixl.camera.focus, trixl.camera.pos);
92
			return vec3.normalize(front, front);
93
		}
94
		trixl.camera.strafe = function(front) {
95
			var strafe = vec3.cross([], front || trixl.camera.front(), trixl.camera.up);
96
			return vec3.normalize(strafe, strafe);
97
		}
98
		trixl.camera.forward = function(scale) {
99
			var front = trixl.camera.front();
100
			vec3.scale(front, front, scale);
101
102
			vec3.add(trixl.camera.pos, trixl.camera.pos, front);
103
			vec3.add(trixl.camera.focus, trixl.camera.focus, front);
104
		}
105
		trixl.camera.sideways = function(scale) {
106
			var front = trixl.camera.front();
107
			var strafe = trixl.camera.strafe(front);
108
			vec3.scale(strafe, strafe, scale);
109
110
			vec3.add(trixl.camera.pos, trixl.camera.pos, strafe);
111
			vec3.add(trixl.camera.focus, trixl.camera.focus, strafe);
112
		}
113
		trixl.camera.rotate = function(dx, dy) {
114
			var front = trixl.camera.front();
115
			var strafe = trixl.camera.strafe(front);
116
117
			trixl.camera.rotateAround(dx, trixl.camera.up);
118
			trixl.camera.rotateAround(dy, strafe);
119
		}
120
		trixl.camera.rotateAround = function(angle, axis) {
121
			var front = trixl.camera.front();
122
123
			var q = quat.setAxisAngle([], axis, angle);
124
			vec3.transformQuat(front, front, q);
125
126
			vec3.add(trixl.camera.focus, trixl.camera.pos, front);
127
		}
128
89 129
		var vertexPosBuffer = gl.createBuffer();
90 130
		gl.bindBuffer(gl.ARRAY_BUFFER, vertexPosBuffer);
91 131
		gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(geometry.cube()), gl.STATIC_DRAW);
@ -102,30 +142,16 @@
102 142
		program.color = gl.getUniformLocation(program, 'color');
103 143
		gl.uniform4f(program.color, -1, -1, -1, -1);
104 144
105
		trixl.pos = {x: 0, y: 0, z: 5, a: 0};
106
		trixl.focus = {x: 0, y: 0, z: 5, a: 0};
107
108 145
		var angle = {x: 0, y: 0, z: 0};
109 146
		var offset = {x: 0, y: 0, z: 0};
110 147
		program.transform = gl.getUniformLocation(program, 'transform');
111 148
		trixl.redraw = function() {
112
			var camera = mat4.multiplyMany(
113
				matrix.translate(trixl.pos.x, trixl.pos.y, trixl.pos.z),
114
				matrix.rotateY(trixl.pos.a)
115
			);
116
			var camera_pos = camera.slice(12, 15);
117
			var focus_pos = mat4.multiplyMany(
118
				matrix.translate(trixl.pos.x + trixl.focus.x, trixl.pos.y + trixl.focus.y, trixl.pos.z + trixl.focus.z),
119
				matrix.rotateY(trixl.pos.a + trixl.focus.a)
120
			).slice(12, 15);
121
			var up = [0, 1, 0];
122
			camera = mat4.lookAt([], camera_pos, focus_pos, up);
149
			var camera = mat4.lookAt([], trixl.camera.pos, trixl.camera.focus, trixl.camera.up);
123 150
			var view = mat4.invert([], camera);
124 151
125 152
			var aspect = trixl.window.w / trixl.window.h;
126 153
			var transform = function(pos) {
127 154
				return mat4.multiplyMany(
128
					//matrix.scale(0.6, 0.6, 0.6),
129 155
					matrix.translate(-0.5, -0.5, -0.5),
130 156
					matrix.rotateZ(angle.z / 10),
131 157
					//matrix.rotateY(angle.y / 10),
@ -207,16 +233,16 @@
207 233
			}
208 234
209 235
			if (isPressed(37) || isPressed(65)) { // left || a
210
				trixl.pos.x += 0.1;
236
				trixl.camera.sideways(-0.05);
211 237
			}
212 238
			if (isPressed(38) || isPressed(87)) { // up || w
213
				trixl.pos.z -= 0.1;
239
				trixl.camera.forward(-0.05);
214 240
			}
215 241
			if (isPressed(39) || isPressed(68)) { // right || d
216
				trixl.pos.x -= 0.1;
242
				trixl.camera.sideways(0.05);
217 243
			}
218 244
			if (isPressed(40) || isPressed(83)) { // down || s
219
				trixl.pos.z += 0.1;
245
				trixl.camera.forward(0.05);
220 246
			}
221 247
		});
222 248
@ -233,8 +259,7 @@
233 259
			}
234 260
235 261
			var diff = { x: mouse.last.x - ev.clientX, y: mouse.last.y - ev.clientY };
236
			trixl.focus.a += diff.x * 0.01;
237
			trixl.focus.y += diff.y * 0.01;
262
			trixl.camera.rotate(diff.x * 0.01, diff.y * 0.01);
238 263
239 264
			mouse.last = {x: ev.clientX, y: ev.clientY};
240 265
		});