Skip to content

Commit b212378

Browse files
author
Dylan McGannon
committed
Add julia sets selectable using 'z' key.
1 parent 9729053 commit b212378

File tree

3 files changed

+113
-28
lines changed

3 files changed

+113
-28
lines changed

js/gl.js

Lines changed: 93 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,24 @@ export class GL {
2020
this.canvas = canvas;
2121
this.gl = canvas.getContext("webgl2");
2222
this.gl.get_super = () => this;
23+
2324
this.objects = [];
25+
2426
this.projection_matrix = Matrix.identity();
2527
this.view_matrix = Matrix.identity();
28+
2629
this.camera_position = new Vector(0, 0, 0, 1);
30+
this.mouse_position = new Vector(0, 0, 0, 1);
31+
2732
this.zoom_target = new Vector(0, 0, 0, 1);
2833
this.zoom_speed = 0;
2934
this.zoom_level = 1;
35+
36+
this.desired_julia = 0;
37+
this.current_julia = -1;
38+
3039
this.color_cycle = 0;
40+
3141
this.extreme_mode = false;
3242

3343
this.drag_active = false;
@@ -40,23 +50,34 @@ export class GL {
4050

4151
// Add keyboard event listener
4252
window.addEventListener('keypress', (event) => {
43-
if (event.key === 'x') {
44-
this.extreme_mode = !this.extreme_mode;
45-
event.preventDefault();
46-
} else if (event.key === 'c') {
47-
if (this.zoom_speed > 0) {
48-
this.zoom_speed += 1;
49-
} else {
50-
this.zoom_speed = 1;
51-
}
52-
event.preventDefault();
53-
} else if (event.key === 'v') {
54-
if (this.zoom_speed > 0) {
55-
this.zoom_speed = -1;
56-
} else {
57-
this.zoom_speed -= 1;
58-
}
59-
event.preventDefault();
53+
switch (event.key) {
54+
case 'z':
55+
this.desired_julia = (this.desired_julia + 1) % 10;
56+
event.preventDefault();
57+
break;
58+
case 'x':
59+
this.extreme_mode = !this.extreme_mode;
60+
event.preventDefault();
61+
break;
62+
case 'c':
63+
this.zoom_target = this.mouse_position;
64+
if (this.zoom_speed > 0) {
65+
this.zoom_speed += 1;
66+
} else {
67+
this.zoom_speed = 1;
68+
}
69+
event.preventDefault();
70+
break;
71+
case 'v':
72+
this.zoom_target = this.mouse_position;
73+
if (this.zoom_speed > 0) {
74+
this.zoom_speed = -1;
75+
} else {
76+
this.zoom_speed -= 1;
77+
}
78+
event.preventDefault();
79+
default:
80+
break;
6081
}
6182
});
6283

@@ -77,7 +98,7 @@ export class GL {
7798
});
7899
canvas.addEventListener('mousemove', (event) => {
79100
// Unproject mouse coords into scene coords.
80-
this.zoom_target = this.view_matrix.inverse().multiply_vector(
101+
this.mouse_position = this.view_matrix.inverse().multiply_vector(
81102
this.unproject(event.layerX, this.canvas.clientHeight - event.layerY, 0.5)
82103
);
83104

@@ -88,24 +109,18 @@ export class GL {
88109

89110
// Stop any zooming going on.
90111
this.zoom_speed = 0;
91-
/*this.last_mouse_position = this.view_matrix.inverse().multiply_vector(
92-
this.unproject(
93-
event.layerX,
94-
this.canvas.clientHeight - event.layerY,
95-
0
96-
)
97-
);*/
98112

99113
// Set the camera position to: current pos - mouse pos + initial click pos
100114
this.set_camera_position(
101-
this.camera_position.x - this.zoom_target.x + this.drag_point.x,
102-
this.camera_position.y - this.zoom_target.y + this.drag_point.y,
115+
this.camera_position.x - this.mouse_position.x + this.drag_point.x,
116+
this.camera_position.y - this.mouse_position.y + this.drag_point.y,
103117
this.camera_position.z
104118
);
105119
});
106120

107121
// Add mouse wheel listener
108122
canvas.addEventListener('wheel', (event) => {
123+
this.zoom_target = this.mouse_position;
109124
if (event.deltaY > 0) {
110125
if (this.zoom_speed > 0) {
111126
this.zoom_speed += 1;
@@ -406,6 +421,56 @@ export class GL {
406421
return true;
407422
}
408423

424+
/**
425+
* Switch the rendered set.
426+
*/
427+
switch() {
428+
if (this.current_julia === this.desired_julia) {
429+
return false;
430+
}
431+
432+
this.current_julia = this.desired_julia;
433+
434+
let c;
435+
switch (this.current_julia) {
436+
case 0:
437+
c = new Vector(0, 0, 0, 0);
438+
break;
439+
case 1:
440+
c = new Vector(-0.4, 0.6, 0, 0);
441+
break;
442+
case 2:
443+
c = new Vector(0.285, 0, 0, 0);
444+
break;
445+
case 3:
446+
c = new Vector(0.285, 0.01, 0, 0);
447+
break;
448+
case 4:
449+
c = new Vector(0.45, 0.1428, 0, 0);
450+
break;
451+
case 5:
452+
c = new Vector(-0.70176, -0.3842, 0, 0);
453+
break;
454+
case 6:
455+
c = new Vector(-0.835, -0.2321, 0, 0);
456+
break;
457+
case 7:
458+
c = new Vector(-0.8, 0.156, 0, 0);
459+
break;
460+
case 8:
461+
c = new Vector(-0.7269, 0.1889, 0, 0);
462+
break;
463+
case 9:
464+
default:
465+
c = new Vector(0, -0.8, 0, 0);
466+
break;
467+
}
468+
469+
this.get_shader_program().set_uniform_vec2('julia_constant', c);
470+
471+
return true;
472+
}
473+
409474
/**
410475
* The event loop executed for each tick.
411476
*/
@@ -424,6 +489,7 @@ export class GL {
424489
scene_dirty |= this.resize();
425490
scene_dirty |= this.zoom(frame_delta);
426491
scene_dirty |= this.cycle(frame_delta);
492+
scene_dirty |= this.switch();
427493

428494
if (scene_dirty) {
429495
this.render();

js/shader.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,18 @@ export class ShaderProgram {
8383
this.gl.uniformMatrix4fv(location, false, value);
8484
}
8585

86+
/**
87+
* Set a 2x1 vector uniform value.
88+
*
89+
* @param {string} name
90+
* @param {Vector} value
91+
*/
92+
set_uniform_vec2(name, value) {
93+
const location = this.gl.getUniformLocation(this.program, name);
94+
this.gl.useProgram(this.program);
95+
this.gl.uniform2f(location, value.x, value.y);
96+
}
97+
8698
/**
8799
* Set a float uniform value.
88100
*

shaders/fragment.glsl

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,17 @@ precision highp float;
55
in vec4 vertex;
66
out vec4 output_colour;
77
uniform float continuous_cycle;
8+
uniform vec2 julia_constant;
89

910
void main() {
1011
vec2 p = vertex.xy;
11-
vec2 c = p;
12+
vec2 c;
13+
14+
if (julia_constant.x == 0.0 && julia_constant.y == 0.0) {
15+
c = p;
16+
} else {
17+
c = julia_constant;
18+
}
1219

1320
vec3 color = vec3(0.0, 0.0, 0.0);
1421

0 commit comments

Comments
 (0)