Slide 1

Slide 1 text

jgs SER 431 Advanced Graphics Lecture 22: Fractals Javier Gonzalez-Sanchez [email protected] PERALTA 230U Office Hours: By appointment

Slide 2

Slide 2 text

Javier Gonzalez-Sanchez | SER431 | Fall 2018 | 1 jgs Definition Fractal: “a fragmented geometric shape that can be split into parts, each of which is (at least approximately) a reduced-size copy of the whole.” Fundamental component of fractal geometry is recursion, i.e., fractals all have a recursive definition

Slide 3

Slide 3 text

Javier Gonzalez-Sanchez | SER431 | Fall 2018 | 2 jgs Definition Fractal: “a fragmented geometric shape that can be split into parts, each of which is (at least approximately) a reduced-size copy of the whole.” Fundamental component of fractal geometry is recursion, i.e., fractals all have a recursive definition https://goo.gl/images/XC8RWU

Slide 4

Slide 4 text

Javier Gonzalez-Sanchez | SER431 | Fall 2018 | 3 jgs https://goo.gl/images/aPyG6i

Slide 5

Slide 5 text

Javier Gonzalez-Sanchez | SER431 | Fall 2018 | 4 jgs Definition § Symmetrical fractal The parts are, in fact, exact replicas of the whole. § Stochastic fractal Built out of probabilities and randomness.

Slide 6

Slide 6 text

jgs Case 1: Symmetrical Stack boxes

Slide 7

Slide 7 text

Javier Gonzalez-Sanchez | SER431 | Fall 2018 | 6 jgs Code // create a pile of boxes as fractal void stackBoxes(float x, float y, float z) { if (y >= 0) { glPushMatrix(); glTranslatef(x, y*100, z*100/2); glCallList(display2); glPopMatrix(); stackBoxes(x-50, y-1, z-1); stackBoxes(x-50, y-1, z+1); stackBoxes(x+50, y-1, z-1); stackBoxes(x+50, y-1, z+1); } }

Slide 8

Slide 8 text

Javier Gonzalez-Sanchez | SER431 | Fall 2018 | 7 jgs Case 1

Slide 9

Slide 9 text

jgs Case 2: Stochastic ● ArrayList Lightning bolt

Slide 10

Slide 10 text

Javier Gonzalez-Sanchez | SER431 | Fall 2018 | 9 jgs Case 1 Level = 0 Level = 1 Level = 2 Level = 3 Level = 4

Slide 11

Slide 11 text

Javier Gonzalez-Sanchez | SER431 | Fall 2018 | 10 jgs Code vector thunderbolt; void createBolt(Vec3f p1, Vec3f p2, int level) { thunderbolt.push_back(Line(p1.x,p1.y,p1.z, p2.x,p2.y, p2.z)); for (int t = 0; t < level; t++) { int tam = thunderbolt.size(); Vec3f middle; int i; for (i = 0; i < tam; i++) { p1.x = thunderbolt[0].x1; p1.y = thunderbolt[0].y1; p1.z = thunderbolt[0].z1; p2.x = thunderbolt[0].x2; p2.y = thunderbolt[0].y2; p2.z = thunderbolt[0].z2; thunderbolt.erase(thunderbolt.begin()); middle = calculateMiddle(p1, p2, t); thunderbolt.push_back(Line(p1.x, p1.y, p1.z, middle.x,middle.y, middle.z)); thunderbolt.push_back(Line(middle.x, middle.y, middle.z, p2.x,p2.y, p2.z)); } // extension line Vec3f direction = middle - p1; Vec3f pin = middle + direction * 0.7; thunderbolt.push_back(Line(middle.x, middle.y, middle.z, pin.x, pin.y, pin.z)); } }

Slide 12

Slide 12 text

Javier Gonzalez-Sanchez | SER431 | Fall 2018 | 11 jgs // calculate middle points with a displacement for the thunderbolt Vec3f calculateMiddle(Vec3f p1, Vec3f p2, int level) { Vec3f O = (p2+p1)/2; float m1 = (p2.y - p1.y) / (p2.x - p1.x); int signo = (random_number()>0.5) ? 1 : -1; // higher level, small displacement float x = O.x + signo*((cos(PI/2 + atan(m1)))/ pow(2, level)); float y = O.y + signo*((sin(PI/2 + atan(m1)))/ pow(2, level)); return Vec3f(x, y, (signo*random_number() / pow(2, level)) ); }

Slide 13

Slide 13 text

Javier Gonzalez-Sanchez | SER431 | Fall 2018 | 12 jgs void display(void) { // ... glPushMatrix(); glTranslatef(-300,0,300); glScalef(100, 100, 100); for (Line line : thunderbolt) { glBegin(GL_QUADS); glVertex3f(line.x1, line.y1, line.z1); glVertex3f(line.x1+0.1, line.y1, line.z1 + 0.1); glVertex3f(line.x2, line.y2, line.z2); glVertex3f(line.x2 +0.1, line.y2, line.z2 + 0.1); glEnd(); } glPopMatrix(); // ... }

Slide 14

Slide 14 text

jgs Case 3 Stochastic ● DisplayList Tree

Slide 15

Slide 15 text

Javier Gonzalez-Sanchez | SER431 | Fall 2018 | 14 jgs Case 1 Level = 0 Level = 1 Level = 2 Level = 3 Level = 10

Slide 16

Slide 16 text

Javier Gonzalez-Sanchez | SER431 | Fall 2018 | 15 jgs Code // Create display lists for a leaf, a set of leaves, and a stem void CreateTreeLists(void) { // steam glNewList(STEM, GL_COMPILE); glPushMatrix(); glRotatef(-90, 1, 0, 0); gluCylinder(cylquad, 0.1, 0.08, 1, 10, 2); glPopMatrix(); glEndList(); // leaf glNewList(LEAF, GL_COMPILE); glBegin(GL_TRIANGLES); // vertex here glEnd(); glEndList();

Slide 17

Slide 17 text

Javier Gonzalez-Sanchez | SER431 | Fall 2018 | 16 jgs // steam & leaf glNewList(STEMANDLEAVES, GL_COMPILE); glPushMatrix(); glCallList(STEM); for (i = 0; i < 3; i++) { glTranslatef(0, 0.333, 0); glRotatef(90, 0, 1, 0); glPushMatrix(); glRotatef(0, 0, 1, 0); glRotatef(50, 1, 0, 0); glCallList(LEAF); glPopMatrix(); glPushMatrix(); glRotatef(180, 0, 1, 0); glRotatef(60, 1, 0, 0); glCallList(LEAF); glPopMatrix(); } glPopMatrix(); glEndList(); // full tree glNewList(FULLTREE, GL_COMPILE); glPushMatrix(); glTranslatef(0, -1, 0); fractalTree(0); glPopMatrix(); glEndList(); }

Slide 18

Slide 18 text

Javier Gonzalez-Sanchez | SER431 | Fall 2018 | 17 jgs void fractalTree(int level) { long savedseed; if (level == MAX_LEVEL) { glPushMatrix(); glRotatef(random_number() * 180, 0, 1, 0); glCallList(STEMANDLEAVES); glPopMatrix(); } else { glCallList(STEM); glPushMatrix(); glRotatef(random_number() * 180, 0, 1, 0); glTranslatef(0, 1, 0); glScalef(0.7, 0.7, 0.7); glPushMatrix(); glRotatef(110 + random_number() * 40, 0, 1, 0); glRotatef(30 + random_number() * 20, 0, 0, 1); fractalTree(level + 1); glPopMatrix();glPushMatrix(); glRotatef(-130 + random_number() * 40, 0, 1, 0); glRotatef(30 + random_number() * 20, 0, 0, 1); fractalTree(level + 1); glPopMatrix();glPushMatrix(); glRotatef(-20 + random_number() * 40, 0, 1, 0); glRotatef(30 + random_number() * 20, 0, 0, 1); fractalTree(level + 1); glPopMatrix(); glPopMatrix(); } }

Slide 19

Slide 19 text

jgs Test Yourselves

Slide 20

Slide 20 text

Javier Gonzalez-Sanchez | SER431 | Fall 2018 | 19 jgs Test Yourselves § Create a forest It could be possible to have 2 or 3 slightly different trees § Create a lightning bolt Is is possible to add glow? What about animation? How to create a Koch snowflake?

Slide 21

Slide 21 text

Javier Gonzalez-Sanchez | SER431 | Fall 2018 | 20 jgs Homework https://github.com/javiergs/SER431/blob/master/Lecture22/fractals.cpp § Review the source code

Slide 22

Slide 22 text

jgs SER431 Advanced Graphics Javier Gonzalez-Sanchez [email protected] Fall 2018 Disclaimer. These slides can only be used as study material for the class SER431 at ASU. They cannot be distributed or used for another purpose.