Upgrade to Pro — share decks privately, control downloads, hide ads and more …

SER431 Lecture 22

SER431 Lecture 22

Advanced Graphics
Fractals
(201811)

Javier Gonzalez-Sanchez

November 13, 2018
Tweet

More Decks by Javier Gonzalez-Sanchez

Other Decks in Programming

Transcript

  1. 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
  2. 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
  3. 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.
  4. 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); } }
  5. Javier Gonzalez-Sanchez | SER431 | Fall 2018 | 9 jgs

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

    Code vector <Line> 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)); } }
  7. 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)) ); }
  8. 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(); // ... }
  9. Javier Gonzalez-Sanchez | SER431 | Fall 2018 | 14 jgs

    Case 1 Level = 0 Level = 1 Level = 2 Level = 3 Level = 10
  10. 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();
  11. 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(); }
  12. 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(); } }
  13. 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?
  14. Javier Gonzalez-Sanchez | SER431 | Fall 2018 | 20 jgs

    Homework https://github.com/javiergs/SER431/blob/master/Lecture22/fractals.cpp § Review the source code
  15. 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.