-
Notifications
You must be signed in to change notification settings - Fork 23
/
model.html
157 lines (126 loc) · 5.3 KB
/
model.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
<!DOCTYPE html>
<html>
<head>
<title>Model Viewer</title>
<!-- Common Utilities -->
<script type="text/javascript" src="js/glMatrix.js"></script>
<script type="text/javascript" src="js/js-struct.js"></script>
<script type="text/javascript" src="js/webgl-util.js"></script>
<script type="text/javascript" src="js/webgl-demo-camera.js"></script>
<!-- Source engine specific -->
<script type="text/javascript" src="js/source-vmt.js"></script>
<script type="text/javascript" src="js/source-mdl-struct.js"></script>
<script type="text/javascript" src="js/source-mdl.js"></script>
<!-- Main Javascript -->
<script type="text/javascript">
"use strict";
var model, camera;
var frame = 0;
// Set up basic GL State up front
function initGL(gl, canvas) {
gl.viewport(0, 0, canvas.width, canvas.height);
gl.clearColor(0.0, 0.0, 0.0, 1.0);
gl.clearDepth(1.0);
gl.enable(gl.DEPTH_TEST);
gl.depthFunc(gl.LEQUAL);
gl.enable(gl.CULL_FACE);
gl.cullFace(gl.FRONT);
gl.enable(gl.BLEND);
gl.blendFunc(gl.SRC_ALPHA, gl.ONE_MINUS_SRC_ALPHA);
glUtil.loadDefaultTexture(gl, "root/no-shader.jpg");
camera = Object.create(ModelDemoCamera).init(canvas);
camera.distance = 130;
camera.center = [0, 0, 45];
initMesh(gl);
}
// Load the model
function initMesh(gl) {
model = Object.create(SourceModel).load(gl, 'root/tf/models/player/heavy');
}
// Draw a single frame
function drawFrame(gl, time) {
// Clear back buffer
gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT);
if(!model) { return; }
camera.update(16);
model.draw(gl, camera.viewMat, camera.projectionMat);
}
// shim layer with setTimeout fallback
window.requestAnimFrame = (function(){
return window.requestAnimationFrame ||
window.webkitRequestAnimationFrame ||
window.mozRequestAnimationFrame ||
window.oRequestAnimationFrame ||
window.msRequestAnimationFrame
})();
function setFrameCallback(onFrame, element) {
function animloop(time){
onFrame(time);
window.requestAnimFrame(animloop, element);
};
window.requestAnimFrame(animloop, element);
}
function main() {
var canvas = document.getElementById('viewport');
// Fit the canvas to the document
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
// Get the GL Context
var gl = glUtil.getContext(canvas);
if(!gl) { return; }
initGL(gl, canvas);
// Draw Frames in quick succession
setFrameCallback(function(time){
drawFrame(gl, time);
frame++;
}, canvas);
// FPS Counter
setInterval(function(){
document.getElementById('fps').innerHTML = frame;
frame = 0;
}, 1000); // TODO: This is pretty clunky. Find a better way...
}
window.addEventListener("load", main);
</script>
<style type="text/css">
body {
font: 0.8em Verdana,sans-serif;
}
#fps-counter {
position: absolute;
top: 5px;
left: 5px;
z-index: 2;
color: white;
}
#controls {
position: absolute;
bottom: 5px;
left: 5px;
z-index: 2;
color: white;
}
canvas {
position: absolute;
cursor: move;
top: 0;
left: 0;
text-align: center;
}
</style>
</head>
<body>
<div id="viewport-frame">
<canvas id="viewport">
Sorry, but your browser does not support WebGL or does not have it enabled.
To get a WebGL-enabled browser, please see:<br/>
<a href="https://www.khronos.org/webgl/wiki/Getting_a_WebGL_Implementation" target="_blank">
Getting a WebGL Implementation
</a>
</canvas>
<div id="fps-counter">
FPS: <span id="fps">0</span>
</div>
</div>
</body>
</html>