-
Notifications
You must be signed in to change notification settings - Fork 103
/
octree.html
200 lines (172 loc) · 5.72 KB
/
octree.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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN">
<html>
<head>
<title>Octree Test</title>
<style type="text/css">
#viewport
{
float:left;
width: 640px;
}
#stats
{
float:right;
width: 600px;
}
</style>
<script src="../../CubicVR.js" type="text/javascript"></script>
<script id="core-shader-vs" srcUrl="../../CubicVR_Core.vs" type="x-shader/x-vertex"></script>
<script id="core-shader-fs" srcUrl="../../CubicVR_Core.fs" type="x-shader/x-fragment"></script>
<script type='text/javascript'>
var gl;
var objects = [];
var lights = [];
var octree;
var test_object;
var frames = 0;
var map_context;
var scene;
var map_toggle = false;
var duration = 0;
/************************************************
* init_gl
************************************************/
function init_gl(canvas)
{
gl = null;
try
{
gl = canvas.getContext("experimental-webgl");
gl.viewport(0, 0, canvas.width, canvas.height);
}
catch(e)
{
console.log(e);
} //try
if (!gl)
{
alert("Could not initialise WebGL, sorry :-(");
} //if
CubicVR.GLCore.init(gl,"core-shader-vs","core-shader-fs");
} //init_gl
/************************************************
* main
************************************************/
function main()
{
var canvas = document.getElementById("cubicvr-canvas");
init_gl(canvas);
octree = new CubicVR.Octree(400, 6);
scene = new CubicVR.Scene(canvas.width, canvas.height, 40, 0.1, 200, octree);
box_material = new CubicVR.Material("test");
box_material.color = [1, 0, 0];
var box_object = new CubicVR.Mesh();
CubicVR.genBoxObject(box_object, .5, box_material);
box_object.calcNormals();
box_object.triangulateQuads();
box_object.compile();
for (var i = 0; i < 150; ++i)
{
objects[i] = new CubicVR.SceneObject(box_object);
objects[i].position = [rand(-150, 150), rand(-5, 5), rand(-150, 150)];
scene.bindSceneObject(objects[i]);
} //for
for (var i = 0; i < 2; ++i)
{
lights[i] = new CubicVR.Light(CubicVR.enums.light.type.POINT);
lights[i].position = [rand(-5, 5), rand(-5, 5), rand(-5, 5)];
lights[i].distance = 200.0;
lights[i].intensity = 3.0;
scene.bindLight(lights[i]);
} //for
scene.camera.position = [0, 0, 0];
scene.camera.target = [0, 0, 1];
scene.camera.setFOV(40);
scene.camera.setDimensions(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);
window.addEventListener("keydown", on_key_down, false);
window.addEventListener("keyup", on_key_up, false);
map_canvas = document.getElementById("map-canvas");
map_context = map_canvas.getContext("2d");
setInterval(render, 15);
render();
} //main
/************************************************
* on_key_down
************************************************/
function on_key_down(e)
{
if (e.keyCode == 77)
map_toggle = ! map_toggle;
} //on_key_down
/************************************************
* on_key_up
************************************************/
function on_key_up(e)
{
} //on_key_up
/************************************************
* render
************************************************/
var xp = Math.random()*6;
var start_time;
var sample_frames = 5;
var fps = 0;
function render()
{
if (frames % sample_frames === 0)
start_time = new Date().getTime();
xp += 0.01;
gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT);
if (map_toggle)
{
map_context.fillStyle = "#000000";
map_context.fillRect(0, 0, 400, 400);
scene.octree.draw_on_map(map_context);
scene.camera.frustum.draw_on_map(map_context);
} //if
scene.camera.target = [Math.sin(xp), 0, Math.cos(xp)];
scene.render();
if (frames % sample_frames === sample_frames - 1)
{
var end_time = new Date().getTime();
var delta_time = end_time - start_time;
fps = Math.round(1000/delta_time*sample_frames, 4);
} //if
var stats_div = document.getElementById("stats-text");
stats_div.innerHTML = "Press M to toggle the map.<br /><br />FPS: " + fps + "<br/>Frame: " + frames + " " + "Nodes: " + scene.objects_rendered;
++frames;
} //render
function draw_line(context, startx, starty, endx, endy)
{
context.beginPath();
context.moveTo(startx, starty);
context.lineTo(endx, endy);
context.closePath();
context.stroke();
} //draw_line
/************************************************
* render
************************************************/
function rand(min, max)
{
return Math.random() * (max-min) + min;
} //rand
</script>
</head>
<body onLoad="main();">
<div id="viewport">
<canvas id="cubicvr-canvas" style="border: none;" width="640" height="480"></canvas>
</div>
<div id="stats">
<div id="stats-text"></div>
<br />
<div id="stats-map">
<canvas id="map-canvas" style="border: none;" width="400" height="400"></canvas>
</div>
</div>
</body>
</html>