Skip to content

Commit ecb469c

Browse files
authored
update yippie
no epilepsy! one line at a time rendering for high iter counts optimizations
1 parent 52fed2f commit ecb469c

File tree

1 file changed

+78
-53
lines changed

1 file changed

+78
-53
lines changed

mandel.cpp

Lines changed: 78 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -8,27 +8,39 @@ double xPos=-2.0;
88
double yPos=-1.0;
99
double zoom=300;
1010
double startX,startY,start_X,start_Y;
11-
int maxIter=50;
11+
int maxIter =100;
12+
int maxIter2=100;
1213
double zoomFactor=1.0;
13-
double mouseX,mouseY;
14-
float escapesmooth(double real, double imag){ // Optimized Escape Time Algorithm
14+
float period=0;
15+
bool render=true;
16+
float escapesmooth(double real, double imag){ // Optimized Escape Time Algorithm + smooth colors + perodicity checks
1517
double r0=real;
1618
double i0=imag;
1719
float iter=0.0;
18-
double tempreal;
20+
double tempreal,io,ro;
1921
while(iter< maxIter&&real*real+imag*imag<=4){
2022
tempreal=real*real-imag*imag+r0;
2123
imag=2*real*imag+i0;
2224
real=tempreal;
2325
iter++;
26+
if(real==ro&&imag==io){
27+
iter=maxIter;
28+
break;
29+
}
30+
period++;
31+
if(period>20){
32+
period=0;
33+
ro=real;
34+
io=imag;
35+
}
2436
}
2537
if(iter<maxIter){
2638
float log_zn=log((real*real)+(imag*imag))/2;
2739
float nu=log(log_zn/log(2))/log(2);
2840
iter=iter+1.0-nu;}
2941
return iter;
30-
}
31-
/*int escape(double real, double imag){ // Escape time algorithm
42+
}/*
43+
int escape(double real, double imag){ // Escape time algorithm
3244
double r0=real;
3345
double ig0=imag;
3446
int iter=0;
@@ -44,43 +56,56 @@ float escapesmooth(double real, double imag){ // Optimized Escape Time Algorithm
4456

4557
void display(){
4658
glClear(GL_COLOR_BUFFER_BIT);
47-
for(int x=0;x<WIDTH;x++){
48-
glBegin(GL_POINTS);
49-
for(int y=0;y<HEIGHT;y++){
50-
float iterations=escapesmooth(x/zoom+xPos,y/zoom+yPos);
51-
glVertex2i(x,y);
52-
if(iterations==maxIter){
53-
glColor3f(0,0,0);
54-
}
55-
else{
56-
float r1=0.5+sin(floor(iterations)/10)/2;
57-
float r2=0.5+sin((floor(iterations)+1)/10)/2;
58-
float g1=floor(iterations)/maxIter;
59-
float g2=(floor(iterations)+1)/maxIter;
60-
float b1=1-floor(iterations)/maxIter;
61-
float b2=1-(floor(iterations)+1)/maxIter;
62-
/*
63-
float r1=floor(iterations)/32;
64-
float r2=floor(iterations+1)/32;
65-
float g1=floor(iterations)/64;
66-
float g2=floor(iterations+1)/64;
67-
float b1=floor(iterations)/128;
68-
float b2=floor(iterations+1)/128;*/
69-
70-
float t=fmod(iterations,1);
71-
glColor3f(lerp(r1,r2,t),lerp(g1,g2,t),lerp(b1,b2,t));
72-
//glColor3f(r1,g1,b1);
59+
if(render){
60+
for(int x=0;x<WIDTH;x++){
61+
glBegin(GL_POINTS);
62+
for(int y=0;y<HEIGHT;y++){
63+
float iterations=escapesmooth(x/zoom+xPos,y/zoom+yPos);
64+
glVertex2i(x,y);
65+
if(iterations==maxIter){
66+
glColor3f(0,0,0);
7367
}
68+
else{
69+
float r1=0.5+sin(floor(iterations)/10)/2;
70+
float r2=0.5+sin((floor(iterations)+1)/10)/2;
71+
float g1=floor(iterations)/maxIter;
72+
float g2=(floor(iterations)+1)/maxIter;
73+
float b1=1-floor(iterations)/maxIter;
74+
float b2=1-(floor(iterations)+1)/maxIter;
75+
/*Sunburst:
76+
float r1=floor(iterations)/32;
77+
float r2=floor(iterations+1)/32;
78+
float g1=floor(iterations)/64;
79+
float g2=floor(iterations+1)/64;
80+
float b1=floor(iterations)/128;
81+
float b2=floor(iterations+1)/128;*/
82+
/*test
83+
float r1=iterations/32;
84+
float r2=iterations/32;
85+
float g1=iterations/64;
86+
float g2=iterations/64;
87+
float b1=iterations/128;
88+
float b2=iterations/128;*/
89+
90+
float t=fmod(iterations,1);
91+
glColor3f(lerp(r1,r2,t),lerp(g1,g2,t),lerp(b1,b2,t));
92+
//glColor3f(r1,g1,b1);
93+
}
94+
}
95+
if(maxIter>100000){
96+
glEnd();
97+
glFlush();}
7498
}
7599
glEnd();
76-
//glFlush();
100+
glFlush();
101+
if(maxIter2>1999999){
102+
render=false;
103+
}
77104
}
78-
glFlush();
79-
glutSwapBuffers();
105+
maxIter*=1.2;
106+
if(render){glutSwapBuffers();}
80107
}
81108
void reshape(int w,int h){glutReshapeWindow(WIDTH,HEIGHT);}
82-
83-
84109
bool isDragging = false;
85110
int lastX,lastY;
86111

@@ -92,29 +117,29 @@ void mouse(int button, int state, int x, int y){
92117
lastX=x;
93118
lastY=y;
94119
} else if(button==GLUT_LEFT_BUTTON&&state==GLUT_UP){
95-
isDragging=false;}
120+
isDragging=false;
121+
}
96122
}
97123
void motion(int x, int y) {
98-
mouseX = x;
99-
mouseY = y;
100124
if (isDragging) {
101125
double deltaX=(x-lastX)/zoom;
102126
double deltaY=(y-lastY)/zoom;
103127
xPos-=deltaX;
104128
yPos+=deltaY;
105129
lastX=x;
106130
lastY=y;
131+
maxIter=maxIter2;
107132
glutPostRedisplay();
108133
}
109134
}
110135
void key(unsigned char key,int x,int y){
111136
switch(key){
112-
case 'q':{zoom*=1.1; break;}
113-
case 'e':{zoom*=0.9; break;}
114-
case '=':{maxIter+=10;break;}
115-
case '+':{maxIter+=10;break;}
116-
case '-':{maxIter-=10;break;}
117-
case 'r':{zoom=300;xPos=-2.0;yPos=-1.0;maxIter=50;break;}
137+
case 'q':{zoom*=1.1;maxIter=maxIter2;render=true;break;}
138+
case 'e':{zoom*=0.9;maxIter=maxIter2;render=true;break;}
139+
case '=':{maxIter2+=1;maxIter=maxIter2;render=true;break;}
140+
case '+':{maxIter2+=1;maxIter=maxIter2;render=true;break;}
141+
case '-':{maxIter2-=1;maxIter=maxIter2;render=true;break;}
142+
case 'r':{zoom=300;xPos=-2.0;yPos=-1.0;maxIter=50;render=true;break;}
118143
}
119144
}
120145

@@ -124,17 +149,17 @@ int main(int argc, char** argv){
124149
glutInitWindowSize(WIDTH,HEIGHT);
125150
glutCreateWindow("mandel badandel");
126151

127-
glMatrixMode(GL_PROJECTION)
128-
glLoadIdentity();
129-
gluOrtho2D(0, WIDTH, 0, HEIGHT);
130-
glMatrixMode(GL_MODELVIEW);
131-
glLoadIdentity();
152+
glMatrixMode(GL_PROJECTION);
153+
glLoadIdentity();
154+
gluOrtho2D(0, WIDTH, 0, HEIGHT);
155+
glMatrixMode(GL_MODELVIEW);
156+
glLoadIdentity();
132157
glClearColor(0.0,0.0,0.0,1.0);
133158

134159
glutDisplayFunc(display);
135160
glutReshapeFunc(reshape);
136-
glutMouseFunc(mouse);
137-
glutMotionFunc(motion);
161+
glutMouseFunc(mouse);
162+
glutMotionFunc(motion);
138163
glutKeyboardFunc(key);
139164

140165
glutMainLoop();

0 commit comments

Comments
 (0)