《Fundamentals of Computer Graphics》
Lecture 7 OpenGL 3D Transformation
and Fractal Geometry
Yongjin Liu
What about this lecture?
Scene layout of OpenGL
Three transformation matrix
glMatrixMode
Viewport and window of OpenGL
viewport
window
Contents
Transformation and observation control in
OpenGL
Provide a set of powerful and flexible functions
Linear algebra
Difference between viewport transformation
and projection transformation
Coordinate and matrix
Matrix operation command
Transformation pipeline
Geometry transformation pipeline
Object Coordinate
Model Transformation
World Coordinate
Projection Variable
Perspective division
ViewPort Transformation
Window Coordinate
ModelView Matrix
Clipping
Coordinate Normalization
View Transformation
Pixel
data
Vertex
data
OpenGL 3D transformation
Scene layout of OpenGL
Three transformation matrix
glMatrixMode
Viewport and window of OpenGL
viewport
window
Up direction
Observe direction
Near plane
Far plane
World coordinate
Viewing volume
Matrix transformation of OpenGL
Model transformation
View transformation
Projection transformation
Near plane
Far plane
World coordinate
Viewing volume
Matrix transformation of OpenGL
Model Transformation
View Transformation
Projection Transformation
Matrix transformation of OpenGL
Model Transformation
View Transformation
Projection Transformation
glMatrixMode(parameter)
glMatrixMode(GL_MODELVIEW)
glMatrixMode(GL_PROJECTION)
Transformation from world window to viewport
screen
World window
viewport
screen
World window
viewport
glViewport(x, y, width, height)
Transformation from world window to viewport
Example analysis
1、Initialize window
glutInit(&argc, (char**) argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(640,480);
glutInitWindowPosition(100,150);
glutCreateWindow("Sinc Function");
2、drawing function
void myDisplay(void) {// draw sinc function using world coordinate glClear( GL_COLOR_BUFFER_BIT );
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glBegin(GL_LINE_STRIP);
for(GLfloat x = -4.0; x < 4.0; x += 0.1)
glVertex2f(x, sin(pi * x) / (pi * x));
glEnd();
glFlush();
}
3、Initialize status of drawing
myInit(void) {
glClearColor(1.0,1.0,1.0,0.0);
glColor3f(0.0f,0.0f,1.0f);
glLineWidth(1.0);
}
4、Define world window
void setWindow(left, right, bottom, top) {
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(left, right, bottom, top);
//defines a 2D orthographic projection matrix
}
screen
World window
viewport
glViewport(x, y, width, height)
Transformation from world window to viewport
screen
World window Multiple viewport
Application window
Example analysis
Drawing hexagonal vortex
void render() {
glClear(GL_COLOR_BUFFER_BIT);
hexSwirl();
glutSwapBuffers();
glFlush();
}
Drawing function
void myDisplay(void) {
//set world window
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(140, 460, 40, 360);
int i, j, L=160;
for (i=0; i<4; i++) {
for (j=0; j<3; j++) {
glViewport(i*L, j*L, L, L);//set viewport } } glutSwapBuffers(); }
(140,40)
(460,360)
Width 320
Height 320
screen
World window Multiple viewport
Application window
Topic 1
Recursive method and
fractal geometry
Fractal geometry
Create object using simple program
• Using the self-similarity of real world object
• Using the mathematical recursive method
Fractal was first proposed by Mandelbrot
• Create a branch of math
• Can generate and simulate many interesting objects
• Can not be achieved using general geometry
modeling method
A simple example
Iteration rule
Koch curve and Koch snow
fractal geometry
Two feature measure
• Dependence of the unit of measurement for geometry object
• Self-similarity for geometry object
Scale and length
• Drawing resolution
• Minimal unit used in measurement
• The level of detail depends on the distance between us and the
object
Variable-length principle of Koch curve
• Segment of length 1 can be substituted by 4 off lines of
length 1/3
• For every iteration, the length of Koch curve increases 4/3
• Consider the limiting case (infinite iteration)
• Infinite curve, the first order derivative of each point is not
continuous
• It is not the usual one-dimensional curve, and not two-
dimensional object
• Fractal dimensional
Definition of fractal dimension
• Consider segment, square, cube of unit length
• The resolution of iteration h = 1/n, n is integer
• Splice the segment into k = n parts
• Splice the square into k = n2 parts
• Splice the cube into k = n3 parts
Definition of fractal dimension
• From the mathematical point of view, dimension is used
to characterize the geometric properties of the object
• To any object(segment, square, cube), there exists
equation:k = nd
• Solve k from the above equation, define d to be fractal
dimension: ln
ln
kd
n
Fractal dimension of Koch snow
• In the limiting case, the curve has infinite length, the
first derivative of each point is not continuous
• It is neither the general one dimension curve, nor the
2D object
• Segment with length 1 can be substituted with 4 off line
with 1/3 length
• n = 3, k = 4
• Fractal dimension
ln ln 41.26186
ln ln3
kd
n
Hilbert fractal curve
• Mathematician Hilbert discovered a very strange curve
• Can be proved that:
• The same as Koch curve,Hilbert curve extends
infinitely
• Hilbert curve never self-cross,but is limited in a box
• In the limiting case, Hilbert curve fill every point in the
box
• This curve is called space-filling curve
Hilbert fractal curve
• Iteration rule
Zero Hilbert curve
first Hilbert curve
first Hilbert curve
first Hilbert curve
second Hilbert curve
third Hilbert curve
third Hilbert curve
fourth Hilbert curve
David Hilbert (1862~1943)
• One of the most great mathematicians in the end of 19th
century, early of 20th century
• Fractal method,Dirichlet principal,methods of
mathematical physics
• 23 mathematical problem of Hilbert
– The second international congress of mathematicians in Pairs,
1900
– The eighth problem, prime problem,Jingrun Chen
– May 24, 2000,College de France in Pairs,announced 7 new
millennium mathematics problem, 1 million dollar bonus for each
– Poincare guess,P and NP problem, etc.
Fractal geometry
Use simple basic shape
Use simple iteration and refinement
process
Obtain complicated nature object
Uniform mathematical description
of iterative process
Iterated function system (IFS)
• d0
• d1 = f(d0)
• d2 = f(d1) = f(f(d0))
• d3 = f(f(f(d0)))
• … …
• dk = f [k](d0)
d0, d1, d2, d3, …is orbit of d0
Iterated function system (IFS)
• f (x) = 2x
• f (x) = cos(x)
• f (x) = 4x(1x)
• f (x) = x2 + x
Uniform mathematical description
of iterative process
Golden ratio and IFS
Ancient Greek Parthenon
Golden ratio
11
1 51.618033989
2
Golden ratio and IFS
1 1 1 1 ( ) 1
1 11 ( )
1 11
11
1
f x x
f xx
Golden rectangle
Golden triangle
Golden Rhombus
Golden Rhombohedron
Golden rectangle
Logarithmic spiral baer
Golden triangle a
b
Golden diamond
Golden rhombohedral
How to draw fractal geometry
shape effectively?
Turtle Graphics
turn(float angle)
forward(float dist, bool isVisible)
Example analysis polyspiral
For (some iterations) { forward(length, 1); turn(angle); length += increment; }
for(int i=1; i<3; i++) { forward(L, 1); turn(60); forward(L, 1); turn(120); forward(L, 1); turn(60); forward(L, 1); turn(120); }
L-System and instruction generation
‘F’ means forward(1, 1)
‘+’ means turn(A)
‘’ means turn(A) F FF++FF
A simple example
Iteration rule
Koch curve and Koch snow
+, no substitution
atom:b
Generative form:b->a, a->ab
In mathematics, logic and
computer science, formal
language is a precise
mathematics or machine
that can handle language
defined by formula.
The same as languages in linguistics,formal
language generally has two aspect: grammar and
semantic.
The branch of mathematics and computer
science specializing in the syntax is called
formal language theory, it only committed to
syntax of language but not semantic.
In formal language theory, formal language is
a set of strings composed by finite letter.
A formal language can contain infinite number
of strings.
Formal definition of language
Operation between languages
A formal language can limit itself by many means:
Enumerate every string(applies only to finite string set)
Generate through formal grammar(see Chomsky pedigree)
Generate through regular expression
Recognize through certain automatic machine,such as
Turing machine, finite state machine
Representation method of language
Formal grammar, formal method, formal science,
formal system
Mathematical symbol, programming language
Extension of instruction set
Add other letter to instruction set, such as
X, Y
Only play a role in the iterative generation
process
Has no effect to Turtle motion
Dragon curve
‘F’‘F’
‘X’‘X+YF+’
‘Y’‘FXY’
Original instruction ‘FX’
‘FX+YF+’
‘FX+YF++FXYF+’
Extension mode of instruction set
(original instruction, F extension mode, X extension
mode, Y extension mode, rotation angle)
Add branch into instruction set
And instruction ‘[’ and ‘]’ into instruciton set
‘[’: saveTurtle() records the current position of
turtle
‘]’: restoreTurtle() restores the position of turtle
Implement using stack
‘[’: push the position of turtle
‘]’: pop the position of turtle
Example analysis
Original instruction ‘F’
Rotation angle 22o
Extension mode of instruction set
‘F’ ‘FF-[-F+F+F]+[+F-F-F]’
Bush
bush after 4 iteration
Random and growing refinement of
object shape
Instruction set with branch is still too regular
Introduce a small random number at every
‘+’, ‘’
Set different line width according the depth
of stack
Inheritance, mutation, crossover
Application of
Stochastic L system
L system in 3D space
Control the spatial orientation of turtle
Example design of 3D bamboo (Botany)
Bamboo generally has 4~8 branches
New branch grows on every branch
New branch generally has 4 growing direction
The angle between two branches is approximately
90o
The angle between branch and trunk is
approximately 33.4o
(9033.4)/90 = 0.618
Example design of other 3D plants
Design the number of branch and trunk
Design the growing direction of new branch
Design the angle of two branches
Design the angle of branch and trunk
Design the affine transformation matrix
Design of 3D herb
Design of 3D poplar
Design of 3D willow
Other example
maple
pine
camphor
Extension of theory system
Biology organism evolution:
lower, middle, higher levels
Basic L-System
Extended L-system (X/Y instruction,branch [])
Stochastic L-System
3D L-System
Content-based L-System
Content-independent L-System(3D)
Content-based L-System
2L-System
(k,l)-system
Extension of theory system
Basic L-System
Introduce branch
Stochastic L-System
3D L-System
Content-based L-System
Parameterized L-System
Parameterized L-System
Parameterized 2L-System
Application of L-System
Modeling of Nature
• D Cohen. Computer simulation of biological pattern generation processes. Nature, 216: 246-248, 1969
• H Honda, J.B.Fisher. Tree branch angle: maximizing effective leaf area. Science, 199:888-890, 1978
Application of L-System
Modeling of tree
Modeling of herb
Application of L-System
Modeling of tree
Modeling of herb
Leaf arrangement
Defining the pattern
of different species
More flowers and fruitages
Tsinghua historic site: moonlight over the lotus pond
More fruitage: pineapple, pinenut, etc
Flower bud
Application of L-System
Modeling of tree
Modeling of herb
Leaf arrangement
Automatic modeling of architecture
L system: Inheritance, mutation, crossover
Application of L-System
Modeling of tree
Modeling of herb
Leaf arrangement
Automatic modeling of architecture
Modeling of city
L system: Inheritance, mutation, crossover
The Palace Museum
Bibliography
Michael Barnsley.
Fractals everywhere, O177.3 FB26
Fractals in multimedia, O189.12 FF79
Superfractals, TP391.41 FB26
Spiral fern leaf
Fractal landscape
Fractal Planet
Life-form in abyssal region
Ancient
Art of mathematics?
Mathematics of art?
Week 13: this week
Week 14: one lecture
Week 15: everyone gives a presentation for your
course projects
Week 16: final mark evaluation
Top Related