Interactive_Computer_Graphi.../Abgabe_3/common/objects3D.js

887 lines
18 KiB
JavaScript
Raw Permalink Normal View History

2023-11-27 21:27:13 +00:00
class Object3D
{
constructor()
{
this.posVBO = gl.createBuffer();
this.colorVBO = gl.createBuffer();
this.indexVBO = gl.createBuffer();
this.positions = [];
this.indices = [];
this.colors = [];
this.position = [0, 0, 0];
this.orientation = [0, 0, 0];
this.scale = [1, 1, 1];
this.modelMatrix;
this.SetModelMatrix();
}
getRandomFloat(min, max) {
return Math.random() * (max-min) + min;
}
2023-11-27 21:27:13 +00:00
initBuffers()
{
// Create VBO for positions and activate it
gl.bindBuffer(gl.ARRAY_BUFFER, this.posVBO);
// Fill VBO with positions
gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(this.positions), gl.STATIC_DRAW);
// Create VBO for colors and activate it
gl.bindBuffer(gl.ARRAY_BUFFER, this.colorVBO);
// Fill VBO with colors
gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(this.colors), gl.STATIC_DRAW);
// Create VBO for indices and activate it
gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, this.indexVBO);
// Fill VBO with indices
gl.bufferData(gl.ELEMENT_ARRAY_BUFFER, new Uint16Array(this.indices), gl.STATIC_DRAW);
}
SetModelMatrix(position = this.position, orientation = this.orientation, scale = this.scale)
{
this.position = position;
this.orientation = [orientation[0] * Math.PI / 180, orientation[1] * Math.PI / 180, orientation[2] * Math.PI / 180]; //Convert the orientation to RAD
this.scale = scale;
this.modelMatrix = mat4.create();
mat4.scale(this.modelMatrix, this.modelMatrix, scale);
mat4.rotate(this.modelMatrix, this.modelMatrix, orientation[1], [0, 1, 0]); // orientation nimmt eine zahl, keinen vektor wie in den todos vorgegeben
mat4.translate(this.modelMatrix, this.modelMatrix, position);
}
UpdateUniforms ()
{
const modelLoc = gl.getUniformLocation(program, "modelMatrix");
gl.uniformMatrix4fv(modelLoc, false, this.modelMatrix);
}
render()
{
// Link data in VBO to shader variables
gl.bindBuffer(gl.ARRAY_BUFFER, this.posVBO);
gl.enableVertexAttribArray(posLoc);
gl.vertexAttribPointer(posLoc, 3, gl.FLOAT, false, 0, 0);
this.UpdateUniforms();
// Link data in VBO to shader variables
gl.bindBuffer(gl.ARRAY_BUFFER, this.colorVBO);
gl.enableVertexAttribArray(colorLoc);
gl.vertexAttribPointer(colorLoc, 4, gl.FLOAT, false, 0, 0);
// Render
gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, this.indexVBO);
gl.drawElements(gl.TRIANGLES, this.indices.length, gl.UNSIGNED_SHORT, 0);
}
}
class Insel extends Object3D
{
constructor()
{
super();
this.positions =
[
-0.344503,-0.106899,-2.313329,
0.254658,0.065420,-2.430170,
0.506020,-0.293147,-2.207084,
1.415955,0.064912,-1.957500,
1.489208,-0.318759,-1.320762,
1.685851,0.084184,-1.268915,
2.014675,-0.126122,-0.782958,
0.957233,-1.089151,-0.905606,
0.454708,-1.256676,-0.897711,
0.810208,-0.709404,-1.141590,
0.739686,-1.249709,-0.541415,
0.231851,-1.904112,-0.621845,
0.052641,-1.633897,-0.758536,
-0.020753,-0.743329,-1.377161,
1.364762,-0.324582,0.647285,
2.074500,0.090083,0.068582,
1.608835,0.037777,0.559365,
1.955554,-0.191974,0.132568,
1.393132,0.037777,1.383312,
1.210661,-0.322174,1.134609,
0.514977,-0.286422,1.576757,
0.507135,-1.581211,0.348922,
0.342563,-2.052501,-0.027325,
0.620568,-1.491335,-0.146730,
0.745909,-0.723539,0.485115,
0.089439,-1.345612,0.360167,
1.089500,-0.770651,-0.314462,
-0.123718,-0.298339,1.611730,
-0.930959,0.037777,1.550149,
-0.866234,-0.291222,1.300128,
-0.414258,0.037777,1.808618,
-1.444312,-0.282136,0.794076,
-1.591571,0.097928,0.827036,
-1.839477,-0.177971,0.145553,
-0.936850,-0.751093,-0.176985,
-0.611777,-1.317411,-0.262516,
-0.326162,-1.310280,0.225784,
0.289486,-0.743127,0.679448,
0.020776,-1.770522,0.330532,
-1.675519,-0.336500,-0.606829,
-1.607249,0.010755,-1.680275,
-0.625318,-0.725192,-1.039731,
-2.011082,0.043485,-0.744422,
-1.247727,0.059337,-1.332687,
-0.934732,-0.314137,-1.943344,
-0.184171,-0.491687,-2.096484,
-0.707720,0.065178,-2.205832,
-0.350659,-1.373304,-0.758204,
-0.299529,-2.155551,-0.308846,
-0.086080,-2.478361,-0.004677,
0.091215,-2.267556,0.130842,
-0.260030,-2.240649,0.060849,
-0.470828,-1.954876,0.024811,
-0.278168,0.037777,-0.209083,
-0.914878,0.210568,-1.267986,
-0.862298,0.231412,-0.781910,
-0.302799,0.520691,-1.301783,
-1.068480,0.101026,-0.360536,
-1.736910,0.103204,0.106553,
0.236277,0.037777,0.701510,
0.656348,0.037777,-0.098120,
0.501631,0.037777,1.688231,
1.034499,0.275328,-1.269756,
0.471459,0.267036,-1.698558,
0.637660,0.376390,-1.207861,
0.812177,0.491673,-0.567514,
1.244986,0.398908,-0.324291,
1.002755,0.639249,-0.717163,
1.381545,0.288786,-0.765328,
1.348978,0.284964,-1.076072,
-0.505260,0.213488,-1.704558,
-0.221831,0.306144,-1.838428,
-0.274246,0.065420,-2.359878,
2.054775,0.091828,-0.677912,
-0.148262,0.773865,-0.947432,
0.101020,0.922236,-0.839739,
0.312442,0.779166,-1.109731,
-0.262315,0.721321,-1.533752,
-0.404757,0.197895,-0.782219,
0.568488,0.537125,-0.913448,
0.115069,0.760356,-1.450921,
-0.061115,0.654189,-0.643377,
0.205810,0.801703,-0.757537,
0.344937,0.262088,-0.492076,
0.077232,0.934717,-1.202342,
-0.102879,0.422805,-0.489015,
-0.269697,0.566033,-0.874217,
-0.186512,0.743264,-1.152426
];
this.indices =
[
0,1,2,
3,4,2,
5,6,4,
7,8,9,
8,10,11,
9,12,13,
14,15,16,
14,6,17,
6,15,17,
14,18,19,
18,20,19,
21,22,23,
24,25,21,
24,23,26,
27,28,29,
20,30,27,
28,31,29,
32,33,31,
34,35,36,
36,25,37,
36,38,25,
39,40,41,
40,42,43,
33,42,39,
40,44,41,
44,0,45,
40,46,44,
46,0,44,
41,12,47,
47,12,48,
41,35,34,
48,35,47,
48,11,49,
11,50,49,
50,51,49,
50,52,51,
51,48,49,
53,32,28,
54,55,56,
57,58,32,
59,16,60,
28,59,53,
59,61,18,
62,63,64,
65,66,67,
68,5,69,
70,43,54,
63,1,71,
71,72,46,
13,45,2,
9,4,7,
4,26,7,
26,14,24,
26,6,14,
19,37,24,
37,29,36,
29,34,36,
44,13,41,
2,1,3,
2,45,0,
0,72,1,
9,2,4,
3,5,4,
5,73,6,
7,10,8,
7,26,10,
9,8,12,
8,11,12,
14,17,15,
6,73,15,
14,16,18,
18,61,20,
24,21,23,
21,38,22,
24,37,25,
21,25,38,
26,23,10,
23,22,10,
27,30,28,
20,61,30,
28,32,31,
32,58,33,
36,35,52,
74,75,76,
36,52,38,
40,39,42,
33,58,42,
40,43,46,
46,72,0,
41,13,12,
41,47,35,
48,52,35,
48,12,11,
11,22,50,
11,10,22,
50,38,52,
22,38,50,
48,51,52,
53,57,32,
43,42,57,
57,42,58,
59,18,16,
28,30,59,
59,30,61,
5,62,69,
60,16,15,
66,73,68,
71,70,77,
63,3,1,
71,1,72,
13,2,9,
4,6,26,
14,19,24,
19,20,37,
37,27,29,
37,20,27,
29,31,34,
31,33,34,
34,39,41,
34,33,39,
44,45,13,
55,78,56,
59,60,53,
79,60,65,
77,56,80,
81,82,75,
82,76,75,
60,76,83,
76,80,84,
53,83,85,
86,74,87,
64,76,79,
74,84,87,
53,60,83,
78,85,86,
67,64,79,
67,79,65,
67,66,68,
67,69,62,
67,68,69,
60,66,65,
67,62,64,
62,3,63,
68,73,5,
5,3,62,
66,15,73,
79,76,60,
64,63,76,
60,15,66,
76,82,83,
85,82,81,
85,83,82,
74,85,81,
56,78,86,
56,87,84,
56,86,87,
56,84,80,
71,80,63,
53,55,57,
56,77,70,
56,70,54,
43,55,54,
70,46,43,
71,46,70,
76,63,80,
86,85,74,
74,76,84,
78,53,85,
71,77,80,
53,78,55,
43,57,55,
74,81,75
];
this.colors = [];
for(var i = 0; i < this.positions.length; i += 3)
{
let r = this.getRandomFloat(0.4, 0.7);
let g = this.getRandomFloat(0.4, 0.6);
let b = this.getRandomFloat(0.4, 0.45);
this.colors.push(r, g, b, 1);
2023-11-27 21:27:13 +00:00
}
this.initBuffers();
}
}
class River extends Object3D
{
constructor()
{
super();
this.positions =
[
0.0, 0.0, 14.0, // index 0
0.75, 0.0, 12.5, // index 1
1.25, 0.0, 12.5, // index 2
1.0, 0.0, 11.0, // index 3
2.0, 0.0, 11.0, // index 4
0.75, 0.0, 9.5, // index 5
2.25, 0.0, 9.5, // index 6
0.0, 0.0, 8.0, // index 7
2.0, 0.0, 8.0, // index 8
-2.25, 0.0, 6.1875, // index 9
0.25, 0.0, 6.1875, // index 10
-3.0, 0.0, 4.0, // index 11
0.0, 0.0, 4.0, // index 12
-2.25, 0.0, 1.8125, // index 13
1.25, 0.0, 1.8125, // index 14
0.0, 0.0, 0.0, // index 15
4.0, 0.0, 0.0, // index 16
0.0, -7.0, 0.0, // index 17 -> additional for waterfall
4.0, -6.0, 0.0 // index 18 -> additional for waterfall
];
this.indices =
[
0, 1, 2,
1, 2, 3,
2, 3, 4,
3, 4, 5,
4, 5, 6,
5, 6, 7,
6, 7, 8,
7, 8, 9,
8, 9, 10,
9, 10, 11,
10, 11, 12,
11, 12, 13,
12, 13, 14,
13, 14, 15,
14, 15, 16,
15, 16, 17, // additional for waterfall
16, 17, 18 // additional for waterfall
];
this.colors = [];
for(var i = 0; i < this.positions.length; i += 3) {
let r = this.getRandomFloat(0.1, 0.2);
let g = this.getRandomFloat(0.2, 0.6);
let b = this.getRandomFloat(0.6, 0.7);
this.colors.push(r, g, b, 1);
2023-11-27 21:27:13 +00:00
}
this.initBuffers();
}
}
class Tree extends Object3D
{
constructor()
{
super();
this.positions = [
-0.056969,0.301313,0.059775,
-0.056969,0.301313,-0.040876,
-0.055153,0.236174,-0.050744,
-0.055153,0.236174,-0.050744,
0.045498,0.236174,-0.050744,
0.045498,0.236174,-0.050744,
0.183358,0.293767,-0.010892,
0.183358,0.293767,0.016715,
-0.055153,0.236174,0.049907,
0.045498,0.236174,0.049907,
-0.055153,0.051740,0.049907,
0.045498,0.051740,0.049907,
0.043682,0.301313,0.059775,
-0.037898,0.415271,0.149025,
-0.093419,0.415271,0.149025,
-0.037898,0.415271,0.093504,
-0.093419,0.415271,0.093504,
0.043682,0.301313,-0.040876,
0.008874,0.422507,-0.242378,
0.069244,0.422507,-0.242378,
0.043682,0.301313,-0.040876,
-0.056969,0.301313,-0.040876,
0.007785,0.461577,-0.236459,
0.068155,0.461577,-0.236459,
0.182859,0.311634,0.019422,
0.182859,0.311634,-0.008185,
-0.079447,0.009962,0.074201,
0.045498,0.051740,-0.050744,
-0.055153,0.051740,-0.050744,
-0.079447,0.009962,-0.075038,
0.069792,0.009962,-0.075038,
0.069792,0.009962,0.074201,
-0.023604,0.642852,-0.220543,
-0.139997,0.499304,-0.220543,
0.131748,0.574773,-0.100629,
0.281596,0.451252,-0.218185,
0.128755,0.410586,-0.109847,
0.070558,0.502573,-0.052865,
-0.127460,0.423836,-0.288955,
-0.139997,0.499304,-0.357368,
0.164721,0.355756,-0.357368,
0.070558,0.497457,-0.498071,
0.202360,0.582881,-0.462644,
0.258592,0.543865,-0.243667,
0.239636,0.235546,-0.001961,
0.322547,0.294289,0.056497,
0.207968,0.275752,0.097434,
0.290802,0.292476,-0.067686,
0.137152,0.290442,0.003639,
0.207968,0.279664,-0.113150,
0.305492,0.374381,-0.000033,
0.271305,0.366839,0.086037,
0.156725,0.378237,0.060204,
0.156725,0.378237,-0.060271,
0.271305,0.378237,-0.097501,
0.239636,0.412997,0.003822,
-0.036268,0.361529,0.163458,
0.091404,0.426523,0.255533,
-0.091304,0.397046,0.313238,
0.173261,0.530724,0.313832,
0.211194,0.504732,0.151857,
-0.199772,0.427673,0.169213,
-0.091304,0.415891,0.027848,
0.084198,0.366894,0.069797,
0.241174,0.620887,0.254329,
-0.125322,0.487354,0.416054,
0.033749,0.468629,0.412686,
-0.042797,0.620887,0.521313,
-0.309859,0.487355,0.162063,
-0.248280,0.463910,0.311354,
-0.326769,0.617539,0.192386,
-0.125322,0.487354,-0.091928,
-0.248280,0.463910,0.012771,
-0.218301,0.620887,-0.049390,
0.173261,0.487354,0.005089,
0.032055,0.467037,-0.060038,
0.132707,0.620887,-0.079497,
0.132707,0.620887,0.403623,
-0.218301,0.620887,0.339866,
-0.326769,0.620887,0.069796,
-0.042797,0.615104,-0.156346,
0.241174,0.620887,0.069796,
0.053601,0.742690,0.364031,
0.168557,0.738438,0.269407,
0.005710,0.823999,0.349044,
-0.258855,0.754420,0.319037,
-0.121286,0.777864,0.403622,
-0.169792,0.874880,0.254329,
-0.258855,0.754420,0.005089,
-0.296788,0.777864,0.162063,
-0.169792,0.874880,0.069797,
0.010880,0.723391,-0.015800,
-0.121286,0.777864,-0.079496,
0.005710,0.846612,0.024079,
0.224265,0.754419,0.162063,
0.145085,0.753424,0.056032,
0.114178,0.853428,0.145975,
-0.042797,0.900194,0.183269
];
this.indices = [
0,1,2,
2,3,4,
5,6,7,
0,8,9,
8,10,11,
12,13,14,
15,16,14,
17,15,13,
17,1,16,
1,0,14,
3,18,19,
5,4,20,
17,20,21,
1,21,3,
22,23,19,
20,4,19,
21,20,23,
21,22,18,
24,7,6,
12,9,7,
12,24,25,
5,17,25,
11,10,26,
9,11,27,
5,27,28,
2,28,10,
29,30,31,
11,31,30,
28,27,30,
10,28,29,
32,33,34,
35,34,36,
33,36,37,
33,38,36,
36,34,37,
33,39,38,
38,39,40,
41,32,42,
36,38,40,
34,35,43,
39,32,41,
41,42,40,
40,39,41,
42,34,43,
34,33,37,
42,35,40,
32,39,33,
32,34,42,
43,35,42,
35,36,40,
44,45,46,
45,44,47,
44,46,48,
44,48,49,
44,49,47,
45,47,50,
46,45,51,
48,46,52,
49,48,53,
47,49,54,
45,50,51,
46,51,52,
48,52,53,
49,53,54,
47,54,50,
51,50,55,
52,51,55,
53,52,55,
54,53,55,
50,54,55,
56,57,58,
59,57,60,
56,58,61,
56,61,62,
56,62,63,
59,60,64,
65,66,67,
68,69,70,
71,72,73,
74,75,76,
59,64,77,
65,67,78,
68,70,79,
71,73,80,
74,76,81,
82,83,84,
85,86,87,
88,89,90,
91,92,93,
94,95,96,
58,66,65,
58,57,66,
57,59,66,
60,63,74,
60,57,63,
57,56,63,
61,69,68,
61,58,69,
58,65,69,
62,72,71,
62,61,72,
61,68,72,
63,75,74,
63,62,75,
62,71,75,
64,81,94,
64,60,81,
60,74,81,
67,77,82,
67,66,77,
66,59,77,
70,78,85,
70,69,78,
69,65,78,
73,79,88,
73,72,79,
72,68,79,
76,80,91,
76,75,80,
75,71,80,
77,83,82,
77,64,83,
64,94,83,
78,86,85,
78,67,86,
67,82,86,
79,89,88,
79,70,89,
70,85,89,
80,92,91,
80,73,92,
73,88,92,
81,95,94,
81,76,95,
76,91,95,
84,96,97,
84,83,96,
83,94,96,
87,84,97,
87,86,84,
86,82,84,
90,87,97,
90,89,87,
89,85,87,
93,90,97,
93,92,90,
92,88,90,
96,93,97,
96,95,93,
95,91,93,
18,22,19,
8,0,2,
5,2,4,
9,5,7,
12,0,9,
9,8,11,
0,12,14,
13,15,14,
12,17,13,
15,17,16,
16,1,14,
4,3,19,
17,5,20,
1,17,21,
2,1,3,
23,20,19,
22,21,23,
3,21,18,
25,24,6,
24,12,7,
17,12,25,
6,5,25,
31,11,26,
5,9,27,
2,5,28,
8,2,10,
26,29,31,
27,11,30,
29,28,30,
26,10,29
];
this.colors = [];
for(var i = 0; i < this.positions.length; i += 3) {
let r = this.getRandomFloat(0.3, 0.4);
let g = this.getRandomFloat(0.4, 0.6);
let b = this.getRandomFloat(0.1, 0.2);
this.colors.push(r, g, b, 1);
2023-11-27 21:27:13 +00:00
}
this.initBuffers();
}
}
class Cloud extends Object3D
{
constructor() {
super();
this.positions = [
-0.308265,-0.282990,-0.001417,
0.101554,-0.243033,0.459730,
-0.309308,-0.125191,0.744740,
0.505238,-0.184567,0.783138,
0.286346,-0.089592,-0.001417,
-0.681349,-0.318752,-0.001417,
-0.309308,-0.233630,-0.747573,
0.101554,-0.065793,-0.462563,
0.469048,0.098790,0.415557,
-0.193767,-0.184567,1.268031,
0.183178,-0.259167,1.205894,
-0.000570,0.000000,1.517511,
-0.963457,-0.122647,-0.001417,
-0.676801,-0.160374,0.744743,
-0.860548,0.000000,0.459731,
-0.193767,-0.184567,-1.270864,
-0.676801,-0.135416,-0.747576,
-0.411437,0.000000,-1.208735,
0.310058,-0.184567,-0.785971,
0.183178,-0.123648,-1.208727,
0.410298,0.000000,-1.208735,
0.410298,0.000000,1.345405,
-0.384104,0.000000,1.205901,
-0.860548,0.000000,-0.462565,
-0.000570,0.000000,-1.598620,
0.384591,0.090319,-0.462565,
0.192628,0.380510,1.268031,
0.480481,0.216971,0.744743,
-0.002332,0.213378,0.631682,
-0.624286,0.240846,0.783138,
-0.184317,0.365906,1.039482,
-0.605569,0.380903,0.277674,
-0.701558,0.184567,-0.640105,
-0.902876,0.216971,-0.001417,
-0.352763,0.239456,-0.319506,
0.192628,0.247332,-1.168216,
-0.184317,0.414270,-1.208727,
-0.014225,0.327900,-0.747573,
0.272043,0.363024,-0.001417,
0.344697,0.335558,-0.747576,
0.012647,0.279427,-0.001417,
-0.280090,0.522819,0.081168
];
this.indices = [
0,1,2,
3,1,4,
0,2,5,
0,5,6,
0,6,7,
3,4,8,
9,10,11,
12,13,14,
15,16,17,
18,19,20,
3,8,21,
9,11,22,
12,14,23,
15,17,24,
18,20,25,
26,27,28,
29,30,31,
32,33,34,
35,36,37,
38,39,40,
2,10,9,
2,1,10,
1,3,10,
4,7,18,
4,1,7,
1,0,7,
5,13,12,
5,2,13,
2,9,13,
6,16,15,
6,5,16,
5,12,16,
7,19,18,
7,6,19,
6,15,19,
8,25,38,
8,4,25,
4,18,25,
11,21,26,
11,10,21,
10,3,21,
14,22,29,
14,13,22,
13,9,22,
17,23,32,
17,16,23,
16,12,23,
20,24,35,
20,19,24,
19,15,24,
21,27,26,
21,8,27,
8,38,27,
22,30,29,
22,11,30,
11,26,30,
23,33,32,
23,14,33,
14,29,33,
24,36,35,
24,17,36,
17,32,36,
25,39,38,
25,20,39,
20,35,39,
28,40,41,
28,27,40,
27,38,40,
31,28,41,
31,30,28,
30,26,28,
34,31,41,
34,33,31,
33,29,31,
37,34,41,
37,36,34,
36,32,34,
40,37,41,
40,39,37,
39,35,37
];
this.colors = [];
for(var i = 0; i < this.positions.length; i += 3) {
let gray = this.getRandomFloat(0.8, 0.9);
this.colors.push(gray, gray, gray, 1);
2023-11-27 21:27:13 +00:00
}
this.initBuffers();
}
}