This repository has been archived by the owner on May 16, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.cpp
165 lines (146 loc) · 3.91 KB
/
main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
//
// main.cpp
// Mandelbrot
//
// Created by Benjamin Mayes on 10/12/11.
// Copyright 2011 RIT. All rights reserved.
//
#include <iostream>
#include <sstream>
#include <cmath>
#include "mandelbrot.h"
#include "julia.h"
#ifdef __APPLE__
#include <GLUT/glut.h>
#include <OpenGL/gl.h>
#include <OpenGL/glu.h>
#else
#include <GL/glut.h>
#include <GL/gl.h>
#include <GL/glu.h>
#endif
#include "color.h"
#include "complex.h"
using namespace std;
int WINDOW_HORIZONTAL_SIZE = 800;
int WINDOW_VERTICAL_SIZE = 600;
mandelbrot m(WINDOW_HORIZONTAL_SIZE,WINDOW_VERTICAL_SIZE);//, complex(-0.8,0.156));
void glut_init(void);
void glut_display(void);
void keyboard(unsigned char,int,int);
void mouse(int,int,int,int);
void timer(int);
void test(void);
void glut_init(void) {
glClearColor(0.0,0.0,0.0,0.0);
glPointSize(1.0);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0.0, WINDOW_HORIZONTAL_SIZE, 0.0, WINDOW_VERTICAL_SIZE);
}
void glut_display(void) {
static int up = 0;
static unsigned int i = 0;
glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
glBegin(GL_POINTS);
const double ** pixels = m.get_fractal();
i++;
for( int row = 0; row < WINDOW_VERTICAL_SIZE; ++row) {
const double* r = pixels[row];
for( int col = 0; col < WINDOW_HORIZONTAL_SIZE; ++col ) {
color c = {0,0,0};
if( *r > 0 ) {
c = rgb_from_hue(1800*(0.001*i+*r));
}
r++;
glColor3f(c.r,c.g,c.b);
glVertex2d(col, row);
}
}
/*double g = m.gamma();
if( up ) {
m.set_gamma(g * 1.01);
if( g > 3.0 ) up = 0;
} else {
m.set_gamma(g / 1.01);
if( g < 0.05 ) up = 1;
}*/
glEnd();
glutSwapBuffers();
}
void mouse( int button, int state, int x, int y ) {
if( state == GLUT_UP ) {
switch( button ) {
case GLUT_LEFT_BUTTON:
m.center(x, y);
glutPostRedisplay();
break;
case GLUT_RIGHT_BUTTON:
m.center(x, y);
m.zoom_in(2.0);
glutPostRedisplay();
break;
}
}
}
void keyboard( unsigned char key, int x, int y ) {
switch( key ) {
case ']':
m.set_gamma(m.gamma() * 1.1);
break;
case '[':
m.set_gamma(m.gamma() / 1.1);
break;
case '-':
m.zoom_out(2.0);
break;
case '+':
m.zoom_in(2.0);
break;
case ';':
m.set_iters(m.iters() - 250);
cout << m.iters() << endl;
break;
case '\'':
m.set_iters(m.iters() + 250);
cout << m.iters();
break;
case 'd':
cout << m << endl;
break;
}
glutPostRedisplay();
}
void timer( int value ) {
glutPostRedisplay();
glutTimerFunc(1000/60, timer, 0);
}
int main (int argc, char ** argv)
{
if( argc == 3 ) {
stringstream ss( stringstream::in | stringstream::out );
ss.str( argv[1] );
ss >> WINDOW_HORIZONTAL_SIZE;
ss.clear();
ss.str( argv[2] );
ss >> WINDOW_VERTICAL_SIZE;
if( WINDOW_HORIZONTAL_SIZE <= 0 && WINDOW_VERTICAL_SIZE <= 0 ) {
cerr << "Usage: " << argv[0] << " [width] [height]" << endl;
return 1;
}
m.resize(WINDOW_HORIZONTAL_SIZE, WINDOW_VERTICAL_SIZE);
}
m.set_gamma(0.25);
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
glutInitWindowSize(WINDOW_HORIZONTAL_SIZE, WINDOW_VERTICAL_SIZE);
glutInitWindowPosition(100, 150);
glutCreateWindow("Fractal Viewer");
glutDisplayFunc(glut_display);
glutMouseFunc(mouse);
glutKeyboardFunc(keyboard);
glutTimerFunc(1000/60, timer, 0);
glut_init();
glutMainLoop();
return 0;
}