Skip to content

Commit 0a56357

Browse files
committed
Finished implementing elementary functions
1 parent 5ba94c0 commit 0a56357

5 files changed

Lines changed: 349 additions & 34 deletions

File tree

‎devlog.md‎

Lines changed: 5 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,8 @@
1-
**We can do things in 3D now!**
1+
Shorter devlog - nothing too fancy going on here!
22

3-
This might make you ask *what* are we plotting in this additional dimension, and the answer is simple: nothing that we weren't showing already
3+
Most of the plotter-related features are already implemented, the only "math-y" thing missing being a few elementary functions, and some non-elementary functions that are convenient (like Lambert-W and the Zeta function). This means that we steer into the bane of every project's existence: making it usable!
44

5-
In our plotter, we use brightness to represent magnitude (how large a number is). Black corresponds to zero, and white corresponds to a very high number, making roots of functions look like "sinks". It turns out that it's far easier to visualize these "sinks", "valleys" and "peaks" with the help of a third dimension (akin to looking at a satellite picture of a mountain range vs being there).
5+
I plan on shipping this project as a single executable rather than a zipped folder, and later compiling it into web assembly for a demo. In either case, we can't work with files, because browsers don't have file systems in the traditional sense. Virtual file systems _do_ exist, but there is a much simpler solution: turning files into strings!
6+
Now, this could be done by simply embedding a string like `const std::string vertex_source = R"({our code})"`, but if we wrote code this way, we lose our syntax highlighting and linting! The only solution is to create these strings dynamically at build-time.
67

7-
There's many ways to render functions in 3D, but the one I chose is by laying out a large 256x256 grid of "mini-planes" that are molded to look like the function (there are methods with 'infinite' precision but they are horribly slow and, if we want more precision, we can just add more segments, anyway!). This required me to create a `Mesh` struct and functions to handle their creation and use.
8-
Also, since all this logic operates on vertices, we have to move the 3D code into a vertex shader. This required massive refactoring of pretty much *all* of our preprocessing functions, since they were all rigged to work with the fragment shader. We also had to do more dependency injection trickery to make sure the 2D and 3D shaders remain synchronized (of course).
9-
Finally, we had to create a custom camera to actually move around our 3D plot, and update our state variables to see if the plot is 3D.
10-
11-
Attached, the third dimension! :O
12-
13-
**Related Issues:**
14-
[Issue #29](https://url.jam06452.uk/ou8zyn): 3D Plotting
15-
16-
**Related Commits:**
17-
[Commit 4a0ab12](https://url.jam06452.uk/dv60mh): Fragment and vertex shader for 3D
18-
[Commit 0a17f71](https://url.jam06452.uk/zsu6bw): Refactor of preprocessor logic
19-
[Commit 614238a](https://url.jam06452.uk/u896ze): Created mesh grid
20-
[Commit 4bfd2ed](https://url.jam06452.uk/fo0yqb): 3D Rendering done
21-
[Commit 0a17f71](https://url.jam06452.uk/a8oszc): Working camera!
8+
And that's pretty much it! We just now refactor any file access we wanted to use our `get_source` function, that secretely fetches the built string in compile-time if it's known.

‎src/higher_order/derivative.cpp‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@ unique_ptr<AstNode> differentiate(AstNode* node, const Operator var) {
77
m[Operator::COS] = build_expression("-sin(uplaceholder)");
88
m[Operator::TAN] = build_expression("sec(uplaceholder)^2");
99
m[Operator::LOG] = build_expression("1/uplaceholder");
10+
11+
m[Operator::COSEC] = build_expression("-csc(uplaceholder) * cot(uplaceholder)");
12+
1013
return m;
1114
}();
1215
if (node->op.op == Operator::CONSTANT) {

‎src/shaders/embedded_shaders.h‎

Lines changed: 146 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,4 @@
1-
#pragma once
2-
#include <string>
3-
4-
5-
std::string SRC_PLOTTER_FRAG = R"(#version 330
1+
std::string SRC_PLOTTER_FRAG = R"(#version 330
62
73
// Other useful constants
84
@@ -13,6 +9,10 @@ const float PI = 3.141591f;
139
const float TWO_PI_OVER_3 = 2*PI*0.66666f;
1410
const float TWO_OVER_PI = 2.0f / PI;
1511
12+
const vec2 CPI = vec2(PI,0.0f);
13+
const vec2 ONE = vec2(1.0f,0.0f);
14+
const vec2 I = vec2(0.0f,1.0f);
15+
1616
#define END_CONSTANT_DEFINITIONS HERE
1717
1818
in vec2 pos;
@@ -117,6 +117,84 @@ vec2 ctanh(vec2 a) {
117117
return vec2(sinh(2.0f * a.x) / div, sin(2.0f * a.y) / div);
118118
}
119119
120+
vec2 csqrt(vec2 a) {
121+
return cpow(a,vec2(0.5f,0.0f));
122+
}
123+
124+
vec2 reciprocal(vec2 z){
125+
return cdiv(ONE,z);
126+
}
127+
128+
// Inverse trigonometric
129+
vec2 casin(vec2 z){
130+
vec2 iz = vec2(-z.y,z.x);
131+
vec2 root = csqrt(csub(vec2(1.0f,0.0f),cmult(z,z)));
132+
return cmult(vec2(0.0f,-1.0f),clog(cadd(iz,root)));
133+
}
134+
135+
vec2 cacos(vec2 z){
136+
return csub(vec2(PI/2.0f, 0.0f), casin(z));
137+
}
138+
139+
vec2 catan(vec2 z){
140+
vec2 iz = vec2(-z.y,z.x);
141+
vec2 term = cdiv(csub(ONE,iz),cadd(ONE,iz));
142+
return cmult(vec2(0.0f,0.5f),clog(term));
143+
}
144+
145+
vec2 cacsc(vec2 z){
146+
return casin(reciprocal(z));
147+
}
148+
149+
vec2 casec(vec2 z){
150+
return cacos(reciprocal(z));
151+
}
152+
153+
vec2 cacot(vec2 z){
154+
return catan(reciprocal(z));
155+
}
156+
157+
// Reciprocal hyperbolic
158+
159+
vec2 csech(vec2 z){
160+
return reciprocal(ccosh(z));
161+
}
162+
163+
vec2 ccsch(vec2 z){
164+
return reciprocal(csinh(z));
165+
}
166+
167+
vec2 ccoth(vec2 z){
168+
return cdiv(ccosh(z),csinh(z));
169+
}
170+
171+
// Inverse hyperbolic
172+
vec2 casinh(vec2 z){
173+
vec2 root = csqrt(cadd(cmult(z,z),ONE));
174+
return clog(cadd(z,root));
175+
}
176+
177+
vec2 cacosh(vec2 z){
178+
vec2 root = csqrt(csub(cmult(z,z),ONE));
179+
return clog(cadd(z,root));
180+
}
181+
182+
vec2 catanh(vec2 z){
183+
vec2 term = cdiv(cadd(ONE,z),csub(ONE,z));
184+
return cmult(vec2(0.5f,0.0f),clog(term));
185+
}
186+
187+
vec2 cacsch(vec2 z){
188+
return casinh(reciprocal(z));
189+
}
190+
191+
vec2 casech(vec2 z){
192+
return cacosh(reciprocal(z));
193+
}
194+
195+
vec2 cacoth(vec2 z){
196+
return catanh(reciprocal(z));
197+
}
120198
121199
vec2 cneg(vec2 a){
122200
return -a;
@@ -156,6 +234,21 @@ vec2 cneg(vec2 a){
156234
#define LOG SHADER_LOG
157235
#define ARG SHADER_ARG
158236
#define MAG SHADER_MAG
237+
#define ASIN SHADER_ASIN
238+
#define ACOS SHADER_ACOS
239+
#define ATAN SHADER_ATAN
240+
#define ACSC SHADER_ACSC
241+
#define ASEC SHADER_ASEC
242+
#define ACOT SHADER_ACOT
243+
#define CSCH SHADER_CSCH
244+
#define SECH SHADER_SECH
245+
#define COTH SHADER_COTH
246+
#define ASINH SHADER_ASINH
247+
#define ACOSH SHADER_ACOSH
248+
#define ATANH SHADER_ATANH
249+
#define ACSCH SHADER_ACSCH
250+
#define ASECH SHADER_ASECH
251+
#define ACOTH SHADER_ACOTH
159252
160253
#ifndef SHADER_NULL_SYMBOL
161254
#define SHADER_NULL_SYMBOL 0u
@@ -222,6 +315,21 @@ vec2 cneg(vec2 a){
222315
#define SHADER_POW 22u
223316
#define SHADER_ARG 23u
224317
#define SHADER_MAG 24u
318+
#define SHADER_ASIN 25u
319+
#define SHADER_ACOS 26u
320+
#define SHADER_ATAN 27u
321+
#define SHADER_ACSC 28u
322+
#define SHADER_ASEC 29u
323+
#define SHADER_ACOT 30u
324+
#define SHADER_CSCH 31u
325+
#define SHADER_SECH 32u
326+
#define SHADER_COTH 33u
327+
#define SHADER_ASINH 34u
328+
#define SHADER_ACOSH 35u
329+
#define SHADER_ATANH 36u
330+
#define SHADER_ACSCH 37u
331+
#define SHADER_ASECH 38u
332+
#define SHADER_ACOTH 39u
225333
#endif
226334
227335
@@ -280,8 +388,36 @@ vec2 evaluate_unary_operator(in uint operator, inout vec2 stack[16], inout int s
280388
return cmag(param);
281389
case ARG:
282390
return carg(param);
283-
284-
391+
case ASIN:
392+
return casin(param);
393+
case ACOS:
394+
return cacos(param);
395+
case ATAN:
396+
return catan(param);
397+
case ACSC:
398+
return cacsc(param);
399+
case ASEC:
400+
return casec(param);
401+
case ACOT:
402+
return cacot(param);
403+
case CSCH:
404+
return ccsch(param);
405+
case SECH:
406+
return csech(param);
407+
case COTH:
408+
return ccoth(param);
409+
case ASINH:
410+
return casinh(param);
411+
case ACOSH:
412+
return cacosh(param);
413+
case ATANH:
414+
return catanh(param);
415+
case ACSCH:
416+
return cacsch(param);
417+
case ASECH:
418+
return casech(param);
419+
case ACOTH:
420+
return cacoth(param);
285421
}
286422
return param;
287423
}
@@ -379,7 +515,7 @@ void main(){
379515
FragColor = vec4(hsl2rgb(hsl),1.0f);
380516
})";
381517

382-
std::string SRC_PLOTTER_VERT = R"(#version 330
518+
std::string SRC_PLOTTER_VERT = R"(#version 330
383519
384520
out vec2 pos;
385521
@@ -390,7 +526,7 @@ void main(){
390526
gl_Position = vec4(x,y,0.0f,1.0f);
391527
})";
392528

393-
std::string SRC_PLOTTER3D_FRAG = R"(#version 330 core
529+
std::string SRC_PLOTTER3D_FRAG = R"(#version 330 core
394530
395531
in vec2 f_z;
396532
out vec4 FragColor;
@@ -415,7 +551,7 @@ void main(){
415551
FragColor = vec4(hsl2rgb(hsl),1.0f);
416552
})";
417553

418-
std::string SRC_PLOTTER3D_VERT = R"(#version 330 core
554+
std::string SRC_PLOTTER3D_VERT = R"(#version 330 core
419555
420556
#define HERE ;
421557

0 commit comments

Comments
 (0)