PDA

View Full Version : why wont this draw anything



badass
04-13-2010, 08:07 AM
Im trying to draw a fractal image based on equation

note:k is in subscript

z0=z
zk=zk-1+z0

following is the code i came up with but it wont draw anything :(
im a total newb so dont leave out obvious mistakes

#include <GL/glut.h>

GLfloat winHeight=500, winWidth=500;

GLfloat xcmin=-2.0,xcmax=2.0; //area of rectangle in complex
plane
GLfloat ycmin=-2.0,ycmax=-2.0;
GLint maxIter=1000,count=0;

struct color { GLfloat r,g,b; };

// sotre complex number
class cnum {
public:
GLfloat x,y;
};





void init()
{
glClearColor(1.0,1.0,1.0,0.0);
glPointSize(4);
}

void plotPoint(cnum z)
{
glBegin(GL_POINTS);
glVertex2f(z.x,z.y);
glEnd();
}



void test()
{
color ptColor;
cnum z,z0;
z0.x=xcmin;
z0.y=ycmin;
GLfloat xcinc=0.025,ycinc=0.025; // increments

for(z.x=xcmin; z.x<xcmax; z.x+=xcinc)
for(z.y=ycmin; z.y<ycmax; z.y+=ycinc) {
count=0;

// while magnitude < 4 and max iterations not reached
while(z.x*z.x*+z.y*z.y <=4 &amp;&amp; count <=maxIter) {
z.x=z.x*z.x+z0.x; // z.x squared + z0
z.y=z.y*z.y+z0.y;
count++;
}
if (count=0){
ptColor.r=1.0;
ptColor.g=ptColor.b=0.0;
}
else if(count > maxIter/2) {
ptColor.g=1.0;
ptColor.r=ptColor.b=0.0;
}
else if(count > maxIter) {
ptColor.b=1.0;
ptColor.r=ptColor.g=0.0;
}

glColor3f(ptColor.r,ptColor.g,ptColor.b);
plotPoint(z);
}
}

void displayFcn()
{
glClear(GL_COLOR_BUFFER_BIT);
test();
glFlush();
}

void main(int argc, char** argv)
{
glutInit(&amp;argc,argv);
glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);
glutInitWindowPosition(50,50);
glutInitWindowSize(winWidth,winHeight);
glutCreateWindow("Test");

init();
glutDisplayFunc(displayFcn);

glutMainLoop();
}

many thx
Raza

strattonbrazil
04-13-2010, 10:29 AM
You're working in the [-1,1] OpenGL box. You need to break the problem up to see what's working and what's not.

What I would do...
Give every point the color red. Are you seeing points now? If not, make sure any points are falling the the [-1,1] range and not just outside your window.

If you see points, check your color values. Are they coming in as close to white so you can't see them against your background?

Also, keep in mind glBegin() is a really slow function relatively speaking, so you'll want to call is as infrequently as possible or you'll get serious slowdown of your code.

So instead of doing this...

glBegin(...)
glVertex2f(...)
glEnd()

glBegin(...)
glVertex2f(...)
glEnd()

do this

glBegin(...)
glVertex2f(...)
glVertex2f(...)
...
glEnd()

by moving the glBegin outside your two for loops.