Question: A slight variation on generating the Sierpinski gasket with Problem Description: Refer to Exercise 2 . 1 . A slight variation on generating the Sierpinski

A slight variation on generating the Sierpinski gasket with Problem Description:
Refer to Exercise 2.1. A slight variation on generating the Sierpinski gasket with
triangular polygons yields the fractal mountains used in computer-generated animations. After
you find the midpoint of each side of the triangle, perturb this location before sub-division.
Generate these triangles without fill. Later, you can do this exercise in three dimensions and
add shading. After a few subdivisions, you should have generated sufficient detail that your
triangles look like a mountain.
Step by Step Instructions:
Create a folder named CSCl431ProjectTwo. Copy gasket2.html, gasket2.js, a
triangular polygons yields the fractal mountains used in computer-generated animations.After
you find the midpoint of each side of the triangle,perturb this location before sub-division.
Generate these triangles without fill.Later,you can do this exercise in three dimensions and
add shading.After a few subdivisions,you should have generated sufficient detail that your
triangles look like a mountain.
Step by Step Instructions:
4. Now, follow the instruction in problem description: After you find the midpoint of each
side of the triangle,perturb this location before sub-division.You may do this by simply
move this midpoint to a random point on the same side of triangle.
5. Change color set as green.
6. Run your project, you shall get something looks like a mountain (the green graph
above).
Fix my code:
"use strict";
let canvas;
let gl;
let positions =[];
let numTimesToSubdivide =5;
window.onload = function init()
{
canvas = document.getElementById("gl-canvas" );
gl = canvas.getContext('webgl2');
if (!gl){ alert( "WebGL 2.0 isn't available" ); }
//
// Initialize our data for the Sierpinski Gasket
//
// First, initialize the corners of our gasket with three positions.
let vertices =[
vec2(-1,-1),
vec2(0,1),
vec2(1,-1)
];
divideTriangle( vertices[0], vertices[1], vertices[2],
numTimesToSubdivide);
//
// Configure WebGL
//
gl.viewport(0,0, canvas.width, canvas.height );
gl.clearColor(1.0,1.0,1.0,1.0);
// Load shaders and initialize attribute buffers
let program = initShaders( gl, "vertex-shader", "fragment-shader" );
gl.useProgram(program);
// Load the data into the GPU
let bufferId = gl.createBuffer();
gl.bindBuffer( gl.ARRAY_BUFFER, bufferId );
gl.bufferData( gl.ARRAY_BUFFER, flatten(positions), gl.STATIC_DRAW );
// Associate out shader variables with our data buffer
let positionLoc = gl.getAttribLocation( program, "aPosition" );
gl.vertexAttribPointer( positionLoc, 2, gl.FLOAT, false, 0,0);
gl.enableVertexAttribArray( positionLoc );
render();
};
function triangle(a, b, c)
{
positions.push(a, b, c);
}
function divideTriangle(a, b, c, count)
{
// check for end of recursion
if ( count ===0){
triangle(a, b, c);
}
else {
//bisect the sides
let ab = mix( a, b,0.5);
let ac = mix( a, c,0.5);
let bc = mix( b, c,0.5);
--count;
// three new triangles
divideTriangle( a, ab, ac, count );
divideTriangle( c, ac, bc, count );
divideTriangle( b, bc, ab, count );
}
}
function midpointDisplacement(x1, x2, y1, y2, displacement, roughness)
{
let mx =(x1+x2)/2;
let my =(y1+y2)/2;
let distance = Math.sqrt((x2-x1)**2+(y2-y1)**2);
if(distance=1){
return;
}
let randomDisplacement=(Math.random()*2-1)*displacement;
my+=randomDisplacement;
let maxDisplacement=distance/roughness;
if(displacement>maxDisplacement){
displacement =maxDisplacement;
}
midpointDisplacement(x1, y1, mx, my, displacement, roughness);
midpointDisplacement(mx, my, x2, y2, displacement, roughness);
line.push(x1, y1, x2, y2);
}
function render()
{
gl.clear( gl.COLOR_BUFFER_BIT );
gl.drawArrays( gl.TRIANGLES, 0, positions.length);
}
A slight variation on generating the Sierpinski

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!