Original Post
Hey there... I have just made some mandelbrot thingy, but it runs slow as an old man with a cane. So I was wondering if anyone had any tips for optimizing it? Code is here:
#include <iostream> #include <cmath> #include <GL/glut.h> struct compnumber { public: float x; float y; }; float a=6.0f; float diff=0.001f; float distance(compnumber z) { return sqrt(z.x*z.x+z.y*z.y); } float add_real(compnumber z, compnumber q) { return q.x+z.x; } float add_complex(compnumber z, compnumber q) { return q.y+z.y; } float times_real(compnumber z, compnumber q) { return z.x*q.x-q.y*z.y; } float times_complex(compnumber z, compnumber q) { return q.x*z.y+q.y*z.x; } compnumber c,z,z1; void changeSize(int w, int h) { // Prevent a divide by zero, when window is too short // (you cant make a window of zero width). if(h == 0) h = 1; float ratio = 1.0* w / h; // Reset the coordinate system before modifying glMatrixMode(GL_PROJECTION); glLoadIdentity(); // Set the viewport to be the entire window glViewport(0, 0, w, h); // Set the correct perspective. gluPerspective(45,ratio,1,1000); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); gluLookAt(0.0,0.0,4.0, 0.0,0.0,0.0, 0.0f,1.0f,0.0f); } void processNormalKeys(unsigned char key, int x, int y) { if (key == 27) exit(0); } void renderScene(void) { float i,k; float step=0.005; int iter=15; int count; glClearColor(1.0f,1.0f,1.0,1.0f); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // x axis glColor3f(0.0f,0.0f,0.0f); glBegin(GL_LINES); glVertex3f(-2.0,0.0,0.0); glVertex3f(2.0,0.0,0.0); glEnd(); // y axis glBegin(GL_LINES); glVertex3f(0.0,-2.0,0.0); glVertex3f(0.0,2.0,0.0); glEnd(); // Plot function glBegin(GL_POINTS); for(i=-2.0;i<2.1;i+=step) { for(k=-2.0;k<2.1;k+=step) { c.x=i; c.y=k; z.x=0; z.y=0; //We do this for all points from (-2.0,-2.0,0.0) to (2.0,2.0,0.0) for(count=0;count<iter;count++) { z1.x=times_real(z,z)+c.x; z1.y=times_complex(z,z)+c.y; z.x=z1.x; z.y=z1.y; } if(distance(z1)>distance(c)) { glColor3f(1.0f,0.0f,0.0); glVertex3f(c.x,c.y,0.0); } if(distance(z1)<distance(c)) { glColor3f(0.0f,1.0f,0.0); glVertex3f(c.x,c.y,0.0); } } } glEnd(); glutSwapBuffers(); } int main(int argc, char **argv) { c.x=1.0f; c.y=2.0f; glutInit(&argc, argv); glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGBA); glutInitWindowPosition(100,100); glutInitWindowSize(320,320); glutCreateWindow("Functions"); glutKeyboardFunc(processNormalKeys); glutDisplayFunc(renderScene); glutIdleFunc(renderScene); glutReshapeFunc(changeSize); glutMainLoop(); return 0; } I'd really appreciate your help. thanks. *EDIT* Or here if you want it indented: http://cpp.sourceforge.net/?show=33623 *EDIT*