-
Notifications
You must be signed in to change notification settings - Fork 0
/
tp1.html
executable file
·207 lines (144 loc) · 5.96 KB
/
tp1.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
200
201
202
203
204
205
206
207
<!doctype html>
<html>
<head>
<title></title>
<link rel="stylesheet" href="style.css">
</head>
<body>
<canvas id="my-canvas" width="1000" height="800">
Your browser does not support the HTML5 canvas element.
</canvas>
<script type="text/javascript" src="js/gl-matrix.js"></script>
<script id="shader-vs" type="x-shader/x-vertex">
precision highp float;
attribute vec3 aVertexPosition;
attribute vec3 aVertexNormal;
uniform mat4 modelMatrix;
uniform mat4 viewMatrix;
uniform mat4 projMatrix;
uniform mat4 normalMatrix;
varying vec3 vNormal;
varying vec3 vPosWorld;
void main(void) {
gl_Position = projMatrix * viewMatrix * modelMatrix * vec4(aVertexPosition, 1.0);
vPosWorld=(modelMatrix*vec4(aVertexPosition,1.0)).xyz; //la posicion en coordenadas de mundo
vNormal=(normalMatrix*vec4(aVertexNormal,1.0)).xyz; //la normal en coordenadas de mundo
}
</script>
<script id="shader-fs" type="x-shader/x-fragment">
precision highp float;
varying vec3 vNormal;
varying vec3 vPosWorld;
void main(void) {
vec3 lightVec=normalize(vec3(0.0,3.0,5.0)-vPosWorld);
vec3 diffColor=mix(vec3(0.7,0.7,0.7),vNormal,0.4);
vec3 color=dot(lightVec,vNormal)*diffColor+vec3(0.2,0.2,0.2);
gl_FragColor = vec4(color,1.0);
}
</script>
<script src="js/geometry.js"></script>
<script src="js/parametric-surface.js"></script>
<script src="js/translation-surface.js"></script>
<script>
var mat4 = glMatrix.mat4;
var vec3 = glMatrix.vec3;
var gl = null,
canvas = null,
glProgram = null,
fragmentShader = null,
vertexShader = null;
var sceneObjects = [];
var modelMatrix = mat4.create();
var viewMatrix = mat4.create();
var projMatrix = mat4.create();
var normalMatrix = mat4.create();
var rotate_angle = -1.57078;
function initWebGL() {
canvas = document.getElementById("my-canvas");
try {
gl = canvas.getContext("webgl");
} catch (e) {
alert("Error: Your browser does not appear to support WebGL.");
}
if (gl) {
setupWebGL();
initShaders();
setupBuffers();
tick();
} else {
alert("Error: Your browser does not appear to support WebGL.");
}
}
function setupWebGL() {
gl.enable(gl.DEPTH_TEST);
//set the clear color
gl.clearColor(0.1, 0.1, 0.2, 1.0);
gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT);
gl.viewport(0, 0, canvas.width, canvas.height);
// Matrix de Proyeccion Perspectiva
mat4.perspective(projMatrix, 45, canvas.width / canvas.height, 0.1, 100.0);
// mat4.identity(modelMatrix);
// mat4.rotate(modelMatrix, modelMatrix, -1.57078, [1.0, 0.0, 0.0]);
mat4.identity(viewMatrix);
mat4.translate(viewMatrix, viewMatrix, [0.0, 0.0, -5.0]);
}
function initShaders() {
//get shader source
var fs_source = document.getElementById('shader-fs').innerHTML,
vs_source = document.getElementById('shader-vs').innerHTML;
//compile shaders
vertexShader = makeShader(vs_source, gl.VERTEX_SHADER);
fragmentShader = makeShader(fs_source, gl.FRAGMENT_SHADER);
//create program
glProgram = gl.createProgram();
//attach and link shaders to the program
gl.attachShader(glProgram, vertexShader);
gl.attachShader(glProgram, fragmentShader);
gl.linkProgram(glProgram);
if (!gl.getProgramParameter(glProgram, gl.LINK_STATUS)) {
alert("Unable to initialize the shader program.");
}
//use program
gl.useProgram(glProgram);
}
function makeShader(src, type) {
//compile the vertex shader
var shader = gl.createShader(type);
gl.shaderSource(shader, src);
gl.compileShader(shader);
if (!gl.getShaderParameter(shader, gl.COMPILE_STATUS)) {
console.log("Error compiling shader: " + gl.getShaderInfoLog(shader));
}
return shader;
}
function setupBuffers() {
var model1 = ParametricSurface.generateCartesian(new ParametricSurface.TuboSenoidal(.2, .6, .8, 1.2), 32, 32);
sceneObjects.push(new Geometry.Object3D(model1, modelMatrix));
var model2 = ParametricSurface.generateSpherical(new ParametricSurface.StrangeBall(), 128, 256);
sceneObjects.push(new Geometry.Object3D(model2, modelMatrix));
}
function drawScene() {
for (const object3D of sceneObjects) {
object3D.draw(gl, glProgram);
}
}
function animate() {
rotate_angle += 0.01;
mat4.identity(modelMatrix);
mat4.rotate(modelMatrix, modelMatrix, rotate_angle, [1.0, 0.0, 1.0]);
mat4.identity(modelMatrix);
mat4.rotate(modelMatrix, modelMatrix, rotate_angle, [1.0, 0.0, 1.0]);
mat4.identity(normalMatrix);
mat4.multiply(normalMatrix, viewMatrix, modelMatrix);
mat4.invert(normalMatrix, normalMatrix);
mat4.transpose(normalMatrix, normalMatrix);
}
function tick() {
requestAnimationFrame(tick);
drawScene();
animate();
}
window.onload = initWebGL;
</script>
</body>
</html>