forked from CIS565-Fall-2014/Project5-WebGL
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvert_wave_custom.html
More file actions
209 lines (195 loc) · 7.32 KB
/
vert_wave_custom.html
File metadata and controls
209 lines (195 loc) · 7.32 KB
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
200
201
202
203
204
205
206
207
208
209
<html>
<head>
<title>Vertex Wave</title>
<meta charset ="utf-8">
<meta http-equiv="X-UA-Compatible" content="chrome=1"> <!-- Use Chrome Frame in IE -->
</head>
<body>
<div id="message" style="position:absolute;top:100px"></div> <!-- Pixel offset to avoid FPS counter -->
<canvas id="canvas" style="border: none;" width="1024" height="768" tabindex="1"></canvas>
<script src ="js/lib/gl-matrix.js" type ="text/javascript"></script>
<script src ="js/webGLUtility.js" type ="text/javascript"></script>
<script src="js/lib/dat.gui.min.js" type="text/javascript"></script>
<script id="vs" type="x-shader/x-vertex">
attribute vec2 position;
uniform mat4 u_modelViewPerspective;
uniform float u_time;
uniform vec3 u_mincolor;
uniform vec3 u_maxcolor;
varying vec3 fs_color; //input
void main(void)
{
// NOTE : according to the WebGL standard, 0.0f is not accepted
float dis = sqrt( (position.x-0.5)*(position.x-0.5) + (position.y-0.5)*(position.y-0.5) );
float s_contrib = sin(5.0*dis*3.14159 + u_time);
//float t_contrib = cos(position.y*2.0*3.14159 + u_time);
float height = s_contrib / (1.0 + 5.0 * dis);;
float alpha = (height+1.0)/2.0;
fs_color = mix(u_mincolor/255.0, u_maxcolor/255.0,alpha );
// NOTE : gl_Position is always a vec4
gl_Position = u_modelViewPerspective * vec4(vec3(position, height), 1.0);
}
</script>
<script id="fs" type="x-shader/x-fragment">
precision mediump float;
uniform vec4 u_color;
varying vec3 fs_color; //output
void main(void)
{
// NOTE : gl_FragColor is always a vec4
gl_FragColor = vec4(fs_color,1.0);
}
</script>
<script>
// Globals
var positionLocation = 0;
var heightLocation = 1;
var u_modelViewPerspectiveLocation;
var u_color;
var heights;
var numberOfIndices;
var eye = [2.0, 1.0, 3.0];
var center = [0.0, 0.0, 0.0];
var up = [0.0, 0.0, 1.0];
var NUM_WIDTH_PTS = 32;
var NUM_HEIGHT_PTS = 32;
var message;
var canvas;
var context;
var persp = mat4.create();
var view = mat4.create();
//Added
var time = 0.0;
var u_timeLocation;
var u_mincolorLocation;
var u_maxcolorLocation;
var Colors = {
mincolor : [100, 100, 255],
maxcolor : [255, 100, 100]
};
// Function called when the window is loaded
window.onload = function () {
// Add GUI component
var gui = new dat.GUI();
gui.addColor(Colors, 'mincolor');
gui.addColor(Colors, 'maxcolor');
init();
animate();
};
function init() {
message = document.getElementById("message");
canvas = document.getElementById("canvas");
context = createWebGLContext(canvas, message);
if (!context) {
return;
}
// SET UP WEBGL CONTEXT
context.viewport(0, 0, canvas.width, canvas.height);
context.clearColor(1.0, 1.0, 1.0, 1.0);
context.enable(context.DEPTH_TEST);
mat4.perspective(45.0, 0.5, 0.1, 100.0, persp);
mat4.lookAt(eye, center, up, view);
initializeShader();
initializeGrid();
}
function animate(){
// Update
var model = mat4.create();
mat4.identity(model);
mat4.translate(model, [-0.5, -0.5, 0.0]);
var mv = mat4.create();
mat4.multiply(view, model, mv);
var mvp = mat4.create();
mat4.multiply(persp, mv, mvp);
//Added
time = time + 0.03;
if (time == 3.14159 * 100.0)
time = 0.0;
// Render
context.clear(context.COLOR_BUFFER_BIT | context.DEPTH_BUFFER_BIT);
context.uniformMatrix4fv(u_modelViewPerspectiveLocation, false, mvp);
context.drawElements(context.LINES, numberOfIndices, context.UNSIGNED_SHORT, 0);
//Added
context.uniform1f(u_timeLocation, time);
context.uniform3f(u_mincolorLocation, Colors.mincolor[0], Colors.mincolor[1], Colors.mincolor[2]);
context.uniform3f(u_maxcolorLocation, Colors.maxcolor[0], Colors.maxcolor[1], Colors.maxcolor[2]);
window.requestAnimFrame(animate);
}
function initializeShader() {
var program;
var vs = getShaderSource(document.getElementById("vs"));
var fs = getShaderSource(document.getElementById("fs"));
var program = createProgram(context, vs, fs, message);
context.bindAttribLocation(program, positionLocation, "position");
u_modelViewPerspectiveLocation = context.getUniformLocation(program,"u_modelViewPerspective");
u_colorLocation = context.getUniformLocation(program, "u_color");
//Added
u_timeLocation = context.getUniformLocation(program, "u_time");
u_mincolorLocation = context.getUniformLocation(program, "u_mincolor");
u_maxcolorLocation = context.getUniformLocation(program, "u_maxcolor");
context.useProgram(program);
}
function initializeGrid() {
function uploadMesh(positions, heights, indices) {
// Positions
var positionsName = context.createBuffer();
context.bindBuffer(context.ARRAY_BUFFER, positionsName);
context.bufferData(context.ARRAY_BUFFER, positions, context.STREAM_DRAW);
context.vertexAttribPointer(positionLocation, 2, context.FLOAT, false, 0, 0);
context.enableVertexAttribArray(positionLocation);
if (heights){
// Heights
var heightsName = context.createBuffer();
context.bindBuffer(context.ARRAY_BUFFER, heightsName);
context.bufferData(context.ARRAY_BUFFER, heights.length *
heights.BYTES_PER_ELEMENT, context.STATIC_DRAW);
context.vertexAttribPointer(heightLocation, 1, context.FLOAT, false, 0, 0);
context.enableVertexAttribArray(heightLocation);
}
// Indices
var indicesName = context.createBuffer();
context.bindBuffer(context.ELEMENT_ARRAY_BUFFER, indicesName);
context.bufferData(context.ELEMENT_ARRAY_BUFFER, indices, context.STATIC_DRAW);
}
var WIDTH_DIVISIONS = NUM_WIDTH_PTS - 1;
var HEIGHT_DIVISIONS = NUM_HEIGHT_PTS - 1;
var numberOfPositions = NUM_WIDTH_PTS * NUM_HEIGHT_PTS;
var positions = new Float32Array(2 * numberOfPositions);
var indices = new Uint16Array(2 * ((NUM_HEIGHT_PTS * (NUM_WIDTH_PTS - 1)) +
(NUM_WIDTH_PTS * (NUM_HEIGHT_PTS - 1))));
var positionsIndex = 0;
var indicesIndex = 0;
var length;
for (var j = 0; j < NUM_WIDTH_PTS; ++j){
positions[positionsIndex++] = j /(NUM_WIDTH_PTS - 1);
positions[positionsIndex++] = 0.0;
if (j>=1){
length = positionsIndex / 2;
indices[indicesIndex++] = length - 2;
indices[indicesIndex++] = length - 1;
}
}
for (var i = 0; i < HEIGHT_DIVISIONS; ++i){
var v = (i + 1) / (NUM_HEIGHT_PTS - 1);
positions[positionsIndex++] = 0.0;
positions[positionsIndex++] = v;
length = (positionsIndex / 2);
indices[indicesIndex++] = length - 1;
indices[indicesIndex++] = length - 1 - NUM_WIDTH_PTS;
for (var k = 0; k < WIDTH_DIVISIONS; ++k){
positions[positionsIndex++] = (k + 1) / (NUM_WIDTH_PTS - 1);
positions[positionsIndex++] = v;
length = positionsIndex / 2;
var new_pt = length - 1;
indices[indicesIndex++] = new_pt - 1; // Previous side
indices[indicesIndex++] = new_pt;
indices[indicesIndex++] = new_pt - NUM_WIDTH_PTS; // Previous bottom
indices[indicesIndex++] = new_pt;
}
}
uploadMesh(positions, heights, indices);
numberOfIndices = indices.length;
}
</script>
</body>
</html>