-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbague.js
executable file
·160 lines (121 loc) · 4.16 KB
/
bague.js
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
"use strict";
let vertex_anneau=`#version 300 es
uniform mat4 projectionMatrix;
uniform mat4 viewMatrix;
uniform mat3 normalMatrix;
in vec3 position_in;
in vec3 normal_in;
in vec2 texcoord_in;
out vec3 vertPos;
out vec3 normalInterp;
out vec2 tc;
void main()
{
vec4 Po4 = viewMatrix * vec4(position_in,1);
vertPos = vec3(Po4) / Po4.w;
normalInterp = normalMatrix * normal_in;
tc = texcoord_in;
gl_Position = projectionMatrix * Po4;
}`;
// Effet phong Adapté selon https://www.cs.toronto.edu/~jacobson/phong-demo/
// pour obtenir un anneau doré plus brillant
let fragment_anneau=`#version 300 es
precision highp float;
uniform sampler2D TU0;
in vec3 normalInterp; // Surface normal
in vec3 vertPos; // Vertex position
in vec2 tc;
const float Ka = 1.0; // Ambient reflection coefficient
const float Kd = 1.0; // Diffuse reflection coefficient
const float Ks = 1.0; // Specular reflection coefficient
const float shininessVal = 8.0; // Shininess
const vec3 ambientColor = vec3(0.2, 0.09, 0.0);
const vec3 specularColor = vec3(1,1,1);
const vec3 lightPos = vec3(0.0,0.5,1.0); // Light position
out vec4 frag_out;
void main()
{
vec3 col = texture(TU0,tc).rgb;
vec3 N = normalize(normalInterp);
vec3 L = normalize(lightPos - vertPos);
// Lambert's cosine law
float lambertian = max(dot(N, L), 0.0);
float specular = 0.0;
if(lambertian > 0.0) {
vec3 R = reflect(-L, N); // Reflected light vector
vec3 V = normalize(-vertPos); // Vector to viewer
// Compute the specular term
float specAngle = max(dot(R, V), 0.0);
specular = pow(specAngle, shininessVal);
}
frag_out = vec4(Ka * ambientColor + Kd * lambertian * col + Ks * specular * specularColor, 1.0);
}`;
let vertex_diamant =`#version 300 es
uniform mat4 projectionMatrix;
uniform mat4 viewMatrix;
in vec3 position_in;
in vec2 texcoord_in;
out vec2 tc;
void main()
{
tc = texcoord_in;
gl_Position = projectionMatrix * viewMatrix * vec4(position_in,1);
}`;
let fragment_diamant=`#version 300 es
precision highp float;
uniform sampler2D TU0;
in vec2 tc;
out vec4 frag_out;
void main()
{
vec3 col = texture(TU0, tc).rgb;
frag_out = vec4(col,1.0);
}`;
//Declaration
let mesh_anneau = null;
let prg_anneau = null;
let mesh_diamant = null;
let prg_diamant = null;
let diamant_tex = null;
let gold_tex = null;
function init_wgl(){
//Mise en place interface
UserInterface.begin();
UserInterface.adjust_width();
// Texture
gold_tex = Texture2d();
diamant_tex = Texture2d();
Promise.all([
gold_tex.load("texture/pink-gold-texture.jpg", gl.RGB8, gl.RGB),
diamant_tex.load("texture/diamond-texture.jpg", gl.RGB8, gl.RGB)
]).then(update_wgl);
//
prg_anneau = ShaderProgram(vertex_anneau,fragment_anneau,'anneau');
let anneau = Mesh.Ring(50);
mesh_anneau = anneau.renderer(true,true,true);
//
prg_diamant= ShaderProgram(vertex_diamant,fragment_diamant,'diamant');
let diamant = Mesh.Diamant(7);
mesh_diamant = diamant.renderer(true,false,true);
scene_camera.set_scene_radius(anneau.BB.radius);
scene_camera.set_scene_center(anneau.BB.center);
}
function draw_wgl(){
gl.clearColor( 0.84314, 0.62353, 0.86667, 1);
gl.enable(gl.DEPTH_TEST);
gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT);
const projection_matrix = scene_camera.get_projection_matrix();
const view_matrix = scene_camera.get_view_matrix();
prg_anneau.bind();
prg_anneau.uniform.viewMatrix = mmult(view_matrix, scale(0.5, 0.5, 1.2));
prg_anneau.uniform.projectionMatrix = projection_matrix;
prg_anneau.uniform.normalMatrix = view_matrix.inverse3transpose();
prg_anneau.uniform.TU0 = gold_tex.bind();
mesh_anneau.draw(gl.TRIANGLES);
prg_diamant.bind();
prg_diamant.uniform.viewMatrix = mmult(view_matrix, translate(0.0, 0.176, 0.0), rotateX(90), scale(0.1));
prg_diamant.uniform.projectionMatrix = projection_matrix;
prg_diamant.uniform.TU0 = diamant_tex.bind();
mesh_diamant.draw(gl.TRIANGLES);
}
launch_3d();