Skip to content

Commit

Permalink
Add zelview support
Browse files Browse the repository at this point in the history
  • Loading branch information
magcius committed Oct 30, 2016
1 parent 73d8c00 commit b665ef2
Show file tree
Hide file tree
Showing 13 changed files with 3,952 additions and 45 deletions.
13 changes: 13 additions & 0 deletions .vscode/launch.json
@@ -0,0 +1,13 @@
{
"version": "0.1.0",
"configurations": [
{
"name": "Launch index.html",
"type": "chrome",
"request": "launch",
"url": "http://localhost:8000/index.html",
"webRoot": "${workspaceRoot}",
"sourceMaps": true
}
]
}
3 changes: 1 addition & 2 deletions .vscode/settings.json
@@ -1,7 +1,6 @@
// Place your settings in this file to overwrite default and user settings.
{
"files.exclude": {
"**/*.js.map": true,
"**/*.js": true
"**/*.js.map": true
}
}
1,877 changes: 1,860 additions & 17 deletions build/main.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion build/main.js.map

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions src/main.ts
@@ -1,6 +1,7 @@

import { Viewer, Scene, SceneDesc, SceneGroup } from 'viewer';
import * as SM64DS from 'sm64ds/scenes';
import * as ZELVIEW from 'zelview/scenes';

export class Main {
viewer:Viewer;
Expand All @@ -15,6 +16,7 @@ export class Main {

// The "plugin" part of this.
this.groups.push(SM64DS.sceneGroup);
this.groups.push(ZELVIEW.sceneGroup);

this.makeUI();
}
Expand Down
32 changes: 10 additions & 22 deletions src/sm64ds/render.ts
Expand Up @@ -5,8 +5,9 @@ import * as LZ77 from 'lz77';
import * as Viewer from 'viewer';
import * as NITRO_BMD from './nitro_bmd';
import * as NITRO_GX from './nitro_gx';
import { fetch } from 'util';

var DL_VERT_SHADER_SOURCE = `
const DL_VERT_SHADER_SOURCE = `
precision mediump float;
uniform mat4 u_modelView;
uniform mat4 u_localMatrix;
Expand All @@ -25,7 +26,7 @@ var DL_VERT_SHADER_SOURCE = `
}
`;

var DL_FRAG_SHADER_SOURCE = `
const DL_FRAG_SHADER_SOURCE = `
precision mediump float;
varying vec2 v_uv;
varying vec4 v_color;
Expand Down Expand Up @@ -61,8 +62,8 @@ class NITRO_Program extends Viewer.Program {
}

// 3 pos + 4 color + 2 uv
var VERTEX_SIZE = 9;
var VERTEX_BYTES = VERTEX_SIZE * Float32Array.BYTES_PER_ELEMENT;
const VERTEX_SIZE = 9;
const VERTEX_BYTES = VERTEX_SIZE * Float32Array.BYTES_PER_ELEMENT;

enum RenderPass {
OPAQUE = 0x01,
Expand Down Expand Up @@ -95,7 +96,6 @@ class Texture implements Viewer.Texture {
}

class Scene implements Viewer.Scene {
path:string;
textures:Viewer.Texture[];
modelFuncs:Function[];
program:NITRO_Program;
Expand Down Expand Up @@ -199,7 +199,11 @@ class Scene implements Viewer.Scene {
const localMatrix = window.mat4.create();
const bmd = this.bmd;

window.mat4.scale(localMatrix, localMatrix, [bmd.scaleFactor, bmd.scaleFactor, bmd.scaleFactor]);
// Local fudge factor so that all the models in the viewer "line up".
const localScaleFactor = 100;
const scaleFactor = bmd.scaleFactor * localScaleFactor;

window.mat4.scale(localMatrix, localMatrix, [scaleFactor, scaleFactor, scaleFactor]);
const batches = bmdm.batches.map((batch) => this.translateBatch(gl, batch));
return (pass:RenderPass) => {
gl.uniformMatrix4fv(this.program.localMatrixLocation, false, localMatrix);
Expand Down Expand Up @@ -229,22 +233,6 @@ class Scene implements Viewer.Scene {
}
}

function fetch(path):PromiseLike<ArrayBuffer> {
var request = new XMLHttpRequest();
request.open("GET", path, true);
request.responseType = "arraybuffer";
request.send();

return new window.Promise((resolve, reject) => {
request.onload = () => {
resolve(request.response);
};
request.onerror = () => {
reject();
};
});
}

export class SceneDesc implements Viewer.SceneDesc {
name:string;
path:string;
Expand Down
2 changes: 1 addition & 1 deletion src/sm64ds/scenes.ts
Expand Up @@ -53,7 +53,7 @@ const sceneDescs:SceneDesc[] = [
'water_city_all.bmd',
'water_land_all.bmd',
].map((filename:string):SceneDesc => {
const path = 'data/sm64ds/' + filename;
const path = `data/sm64ds/${filename}`;
return new SceneDesc(filename, path);
});

Expand Down
16 changes: 16 additions & 0 deletions src/util.ts
@@ -0,0 +1,16 @@

export function fetch(path):PromiseLike<ArrayBuffer> {
var request = new XMLHttpRequest();
request.open("GET", path, true);
request.responseType = "arraybuffer";
request.send();

return new window.Promise((resolve, reject) => {
request.onload = () => {
resolve(request.response);
};
request.onerror = () => {
reject();
};
});
}
6 changes: 4 additions & 2 deletions src/viewer.ts
Expand Up @@ -19,7 +19,7 @@ function compileShader(gl:WebGLRenderingContext, str:string, type:number) {

if (!gl.getShaderParameter(shader, gl.COMPILE_STATUS)) {
console.error(gl.getShaderInfoLog(shader));
return null;
throw new Error();
}

return shader;
Expand Down Expand Up @@ -56,6 +56,7 @@ export class Program {
}

export class RenderState {
gl:WebGLRenderingContext;
viewport:Viewport;
currentProgram:Program = null;

Expand All @@ -64,6 +65,7 @@ export class RenderState {

constructor(viewport:Viewport) {
this.viewport = viewport;
this.gl = this.viewport.gl;

this.projection = window.mat4.create();
window.mat4.perspective(this.projection, Math.PI / 4, viewport.canvas.width / viewport.canvas.height, 0.2, 50000);
Expand Down Expand Up @@ -185,7 +187,7 @@ export class Viewer {
var dt = nt - t;
t = nt;

var mult = .1;
var mult = 10;
if (keysDown[SHIFT])
mult *= 5;
mult *= (dt / 16.0);
Expand Down

0 comments on commit b665ef2

Please sign in to comment.