forked from imtumbleweed/WebGLTutorials
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index19.php
177 lines (126 loc) · 6.11 KB
/
index19.php
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
<!doctype html>
<html>
<head>
<title>Tutorial 19 - Drawing 2D Sprites in WebGL</title>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
<script src = 'http://www.tigrisgames.com/js/jquery.js' type = 'text/javascript'></script>
<script src = 'http://www.tigrisgames.com/js/ui.js' type = 'text/javascript'></script>
<script src = 'http://www.tigrisgames.com/fx/gl.js'></script>
<script src = 'http://www.tigrisgames.com/fx/shaders.js?v=6'></script>
<script src = 'http://www.tigrisgames.com/fx/primitives.js?v=2'></script>
<script src = 'http://www.tigrisgames.com/fx/texture.js'></script>
<script src = 'http://www.tigrisgames.com/fx/vector3.js?v=1'></script>
<script src = 'http://www.tigrisgames.com/fx/matrix.js?v=2'></script>
<script src = 'http://www.tigrisgames.com/fx/collision.js?v=3'></script>
<script src = 'http://www.tigrisgames.com/fx/ply-multi.js?v=1'></script>
<script src = 'http://www.tigrisgames.com/fx/model5.js?v=5'></script>
<script src = 'http://www.tigrisgames.com/fx/keyboard.js'></script>
<script src = 'http://www.tigrisgames.com/fx/mouse.js'></script>
<!--<script src = 'http://www.tigrisgames.com/fx/collision.js'></script>//-->
<script type = "text/javascript">
/* -- Gl functions -- */
var canvas = null;
var gl = null;
var model_indices = null;
var car_angle = 0;
$(document).ready(function() {
var canvas = document.getElementById('gl');
gl = GetWebGLContext(canvas);
if (!gl)
console.log('Failed to set up WebGL.');
else { // Load a shader from "shaders" folder
// Enable depth test
gl.enable(gl.DEPTH_TEST);
gl.depthFunc(gl.LESS);
InitializeKeyboard();
InitializeMouse();
CreateShadersFromFile( gl );
LoadTextures();
LoadModels();
}
});
var x = 0;
var y = 0.09;
var z = 0;
// An event that fires when all shader resources finish loading in CreateShadersFromFile
window.webGLResourcesLoaded = function() {
console.log("webGLResourcesLoaded(): All WebGL shaders have finished loading!");
for (var i = 0; i < 1; i++)
BindModel( i );
// Use our standard shader program for rendering this triangle
gl.useProgram( Shader.spriteProgram );
// Create storage for our matrices
var Projection = new CanvasMatrix4();
var ModelView = new CanvasMatrix4();
var ObserverView = new CanvasMatrix4();
var Model = new CanvasMatrix4();
var View = new CanvasMatrix4();
var model_angle = -150;
// Size of our canvas
var width = 800;
var height = 600;
$("#gl").css( { "width" : width + "px", "height" : height + "px" } );
// Start main drawing loop
var T = setInterval(function() {
if (!gl)
return;
if (key.left) x -= 0.01;
if (key.right) x += 0.01;
if (key.up) z += 0.01;
if (key.down) z -= 0.01;
if (key.w) y += 0.01;
if (key.s) y -= 0.01;
var scale = 0.5;
// Clear WebGL canvas
gl.clearColor(0.0, 0.0, 0.0, 1.0);
gl.clear(gl.COLOR_BUFFER_BIT);
gl.clear(gl.DEPTH_BUFFER_BIT);
// Set "brick.png" as active texture to pass into the shader
gl.activeTexture(gl.TEXTURE0);
gl.bindTexture(gl.TEXTURE_2D, star.texture);
gl.uniform1i(gl.getUniformLocation(Shader.spriteProgram, 'image'), 0);
// Indices of cube
// var indices_cube = window.ref_arrayMDL[1][4];
// Create camera perspective matrix
Projection.makeIdentity();
Projection.perspective(45, width / height, 0.05, 1000);
//Projection.ortho(0, 0, 100, 100, -100, 100);
gl.uniformMatrix4fv(gl.getUniformLocation(Shader.spriteProgram, "Projection"), false, Projection.getAsFloat32Array());
var View = new CanvasMatrix4();
View.makeIdentity();
View.translate(0, -0.25, -10);
// Set viewport to Upper Left corner
gl.viewport(0, 0, width, height);
// Default ambient color set to "white"
rgb = [1.0, 1.0, 0.7];
LightPosition = [x, y, z]; // some angle
LightDirection = [0,-1,0]; // some other angle
LightColor = [1, 1, 1]; // white-yellowish
BindModel(0);
gl.uniformMatrix4fv(gl.getUniformLocation(Shader.spriteProgram, "View"), false, View.getAsFloat32Array());
for (var yy = 0; yy < 10; yy++) {
for (var xx = 0; xx < 10; xx++) {
Model.makeIdentity();
Model.rotate(xx + yy, 0, 1, 0);
var rx = -10 + Math.random() * 20;
var ry = -10 + Math.random() * 20;
Model.translate(rx, ry, 0);
Model.scale(0.5, 0.5, 0.5);
Model.rotate(Math.random() * 360, 0, 0, 1);
gl.uniform3fv(gl.getUniformLocation(Shader.spriteProgram, "rgb"), rgb);
gl.uniformMatrix4fv(gl.getUniformLocation(Shader.spriteProgram, "Model"), false, Model.getAsFloat32Array());
gl.drawElements(gl.TRIANGLES, model_indices.length, gl.UNSIGNED_SHORT, 0);
}
}
});
}
</script>
</head>
<style>
#fx { position: relative; margin: 0 auto; width: 1000px; height: 500px; border: 1px solid gray; }
#gl { width: 800px; height: 600px; }
</style>
<body style = "background: #202020; padding: 32px;">
<canvas id = "gl" width = "800" height = "600"></canvas>
</body>
</html>