Created
December 3, 2017 19:08
-
-
Save esmitt/62c416be4d61e8b02b7c14ad6757e98d to your computer and use it in GitHub Desktop.
Single mandelbrot drawing function in C++ using GLUT
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdlib.h> | |
#include "GL/glut.h" | |
#include <iostream> | |
#pragma comment(lib, "glut32.lib") | |
#define WINDOW_TITLE "Project" | |
using namespace std; | |
const float ANGLE = 45.f; | |
const float FOV = 70.f; | |
const float NCP = 0.001f; | |
const float FCP = 40.f; | |
int m_iWidth = 800; //width of the viewport | |
int m_iHeight = 600; //height of the viewport | |
/// | |
/// Function designed to be use in the glutKeyboardFunc | |
/// @param key represents the key pressed in the keyboard | |
/// | |
void keyboardDown(unsigned char key, int x, int y) | |
{ | |
switch(key) { | |
case 'Q': | |
case 'q': | |
case 27: // ESC | |
exit(0); | |
} | |
} | |
void idle(void) | |
{ | |
glutPostRedisplay(); | |
} | |
#define NUM 100 | |
int dwell(double cx, double cy) | |
{ | |
double tmp, dx = cx, dy = cy, fsq = cx*cx + cy*cy; | |
int count = 0; | |
for (count = 0; count <= NUM && fsq <= 4; count++) | |
{ | |
tmp = dx; | |
dx = dx*dx - dy*dy + cx; | |
dy = 2.0 * tmp * dy + cy; | |
fsq = dx*dx + dy*dy; | |
} | |
return count; | |
} | |
void Color(int iterations) | |
{ | |
if (iterations < 10) | |
glColor3ub(64, 128, 128); | |
else if (iterations < 30) | |
glColor3ub(0, 0, 255); | |
else if (iterations < 50) | |
glColor3ub(0, 255, 0); | |
else if (iterations < 70) | |
glColor3ub(255, 0, 0); | |
else if (iterations < 90) | |
glColor3ub(255, 255,0); | |
else | |
glColor3ub(0, 128, 0); | |
} | |
double interpolate(double a, double b, double value) | |
{ | |
return a + ((b - a)* value); | |
} | |
void draw() | |
{ | |
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); | |
glClearColor(0.6, 0.6, 0.6, 1); | |
glMatrixMode(GL_MODELVIEW); | |
glLoadIdentity(); | |
//begin - rendering a simple triangle (testing) | |
float xstart = -2.25; | |
float xend = 0.75; | |
float ystart = -1.5; | |
float yend = 1.5; | |
for (int y = 0; y < m_iHeight; y++) | |
{ | |
for (int x = 0; x < m_iWidth; x++) | |
{ | |
double mapX = x / (double)m_iWidth; | |
double mapY = y / (double)m_iHeight; | |
glBegin(GL_POINTS); | |
Color(dwell(interpolate(xstart, xend, mapX), interpolate(ystart, yend, mapY))); | |
glVertex2i(x, y); | |
glEnd(); | |
} | |
} | |
//begin - rendering a simple triangle | |
glutSwapBuffers(); | |
} | |
void reshape(int width, int height) | |
{ | |
glViewport (0, 0, (GLsizei) width, (GLsizei) height); | |
glMatrixMode (GL_PROJECTION); | |
glLoadIdentity(); | |
m_iWidth = width; | |
m_iHeight = height; | |
gluOrtho2D(0, width, 0, height); | |
glMatrixMode(GL_MODELVIEW); | |
glLoadIdentity(); | |
} | |
bool initialize() | |
{ | |
glEnable(GL_DEPTH_TEST); | |
return true; | |
} | |
int main(int argc, char** argv) | |
{ | |
glutInit(&argc, argv); | |
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA | GLUT_DEPTH); | |
glutInitWindowSize(m_iWidth, m_iHeight); | |
glutCreateWindow(WINDOW_TITLE); | |
glutCreateMenu(NULL); | |
if(!initialize()) return 1; | |
glutDisplayFunc(draw); | |
glutIdleFunc(idle); | |
glutReshapeFunc(reshape); | |
glutKeyboardFunc(keyboardDown); | |
//GUI construction | |
glutMainLoop(); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment