View Full Version : Insufficient dot density

08-13-2009, 04:54 PM
I am trying to get opengl code to generate a gingerman fractal that is supposed to look like this


But looks like this instead


(You can see 30 dots but very sparsely distributed)

This is the code


The equation for the gingerman fractal is:


Any advice would be great

08-13-2009, 06:50 PM
The problem is not with openGL but rather with the use of the C "abs()" function. The abs() has a prototype "int abs(int)" -- ie it returns an integer and throws away the fractional part.

Try using "fabs()" instead which returns a double. This will probably require including the <math.h> header.

#include <math.h>

point.x = 1.0 - prevPoint.y + fabs(prevPoint.x);

This should also be reflected in the drawDot function to take GLfloat arguments (x,y)
and glVertex2i to glVertex2f since you need to plot GLfloats (not plotting GLints anymore)

void drawDot(GLfloat x, GLfloat y)
glVertex2f(x, y);

Here is the entire code with the above changes and some header stuff to make it cross-platform -- checked on my linux box.

#if defined(_WIN32)
#include <windows.h> // use as needed for your system
#include <gl/Gl.h>
#include <gl/glu.h>
#include <glut/glut.h>
#elif defined(__APPLE__) || defined(MACOSX)
#include <OpenGL/gl.h>
#include <OpenGL/glu.h>
#include <GLUT/glut.h>
#else //linux as default
#include <GL/glut.h>

#include <stdlib.h> // for rand()
#include <time.h>
#include <math.h>

struct GLpoint {
GLfloat x;
GLfloat y;

//<<<<<<<<<<<<<<<<<<<<<<< drawDot >>>>>>>>>>>>>>>>>>>>
void drawDot(GLfloat x, GLfloat y)
glVertex2f(x, y); // draw some points (don't know how many)

//<<<<<<<<<<<<<<<<<<<<<<< myInit >>>>>>>>>>>>>>>>>>>>
void myInit(void)
glClearColor(1.0, 1.0, 1.0, 0.0); // set the bg color to a bright white
glColor3f(0.0f, 0.0f, 0.0f); // set the drawing color to black
glPointSize(1.0); //set the point size to 4 by 4 pixels
glMatrixMode(GL_PROJECTION);// set up appropriate matrices- to be explained
gluOrtho2D(-3.0, 8.0, -3.0, 8.0);

//<<<<<<<<<<<<<<<<<<<<<<<< myDisplay >>>>>>>>>>>>>>>>>
// the redraw function
void myDisplay(void)
// displays Gingerbread Man Fractal
glClear(GL_COLOR_BUFFER_BIT); // clear the screen
GLpoint prevPoint;
prevPoint.x = -0.1;
prevPoint.y = 0;

glVertex2f(prevPoint.x, prevPoint.y);

GLpoint point;

const int num = 10000;
for (int i=0; i< num; i++) {
point.y = prevPoint.x;
point.x = 1.0 - prevPoint.y + fabs(prevPoint.x);
glVertex2f(point.x, point.y); // draw some points (don't know how many)
prevPoint.x = point.x;
prevPoint.y = point.y;

glFlush(); // send all output to display

//<<<<<<<<<<<<<<<<<<<<<<<< main >>>>>>>>>>>>>>>>>>>>>>
int main(int argc, char **argv)
glutInit(&amp;argc, argv); // initialize the toolkit
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB); // set the display mode
glutInitWindowSize(640,480); // set the window size
glutInitWindowPosition(100, 150); // set the window position on the screen
glutCreateWindow("Gingerbread Man Fractal"); // open the screen window(with its exciting title)
glutDisplayFunc(myDisplay); // register the redraw function
glutMainLoop(); // go into a perpetual loop
return 0;