Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class MyTriangle extends MyPrimitive {
- constructor (scene, id, x1, y1, z1, x2, y2, z2, x3, y3, z3) {
- super (scene);
- this.x1 = x1;
- this.y1 = y1;
- this.z1 = z1;
- this.x2 = x2;
- this.y2 = y2;
- this.z2 = z2;
- this.x3 = x3;
- this.y3 = y3;
- this.z3 = z3;
- this.origCoords = [];
- this.initBuffers();
- };
- initBuffers() {
- this.vertices = [
- this.x1, this.y1, this.z1,
- this.x2, this.y2, this.z2,
- this.x3, this.y3, this.z3
- ];
- // https://www.khronos.org/opengl/wiki/Calculating_a_Surface_Normal
- let u = [this.x2-this.x1, this.y2-this.y1, this.z2-this.z1]; // a p1p2
- let v = [this.x3-this.x1, this.y3-this.y1, this.z3-this.z1]; // c p3p1
- let w = [this.x3-this.x2, this.y3-this.y2, this.z3-this.z2]; // b p2p3
- let nx = u[1]*v[2] - u[2]*v[1];
- let ny = u[2]*v[0] - u[0]*v[2];
- let nz = u[0]*v[1] - u[1]*v[0];
- let vec = [nx, ny, nz];
- let norm = this.norm(vec);
- this.normals = [
- nx/norm, ny/norm, nz/norm,
- nx/norm, ny/norm, nz/norm,
- nx/norm, ny/norm, nz/norm
- ];
- this.indices = [
- 0, 1, 2
- ];
- let a = this.norm(u);
- let b = this.norm(w);
- let c = this.norm(v);
- let cosAlfa = (Math.pow(a, 2) - Math.pow(b, 2) + Math.pow(c, 2)) / (2 * a *c);
- let sinAlfa = Math.sqrt(1 - Math.pow (cosAlfa, 2));
- this.tex2 = a;
- this.tex3 = [c*cosAlfa, c*sinAlfa];
- this.texCoords = [
- 0,0,
- a,0,
- this.tex3[0], this.tex3[1]
- ];
- this.texCoords = [...this.origCoords];
- this.primitiveType=this.scene.gl.TRIANGLES;
- this.initGLBuffers();
- }
- norm(vector){
- let norm = 0;
- vector.forEach(v => {
- norm += Math.pow(v, 2);
- });
- norm = Math.sqrt(norm);
- return norm;
- }
- updateTexCoords(length_s, length_t) {
- this.texCoords = [
- 0, 0,
- this.tex2/length_s, 0,
- this.tex3[0]/length_s, this.tex3[1]/length_t
- ];
- this.updateTexCoordsGLBuffers();
- }
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement