Display Problems

I am trying to write a program to draw the Serpinski Gasket. The code compiles fine but then when I run the program all I get is an empty, see-through window (not a pretty fractal).
Why is this?
Here is my code:

#include <windows.h>
#include <stdlib.h>
#include <gl/gl.h>
#include <gl/glut.h>

class GLintPoint{

 GLint x, y;


void myInit(void)
glColor3f(0.0f, 0.0f, 0.0f);
gluOrtho2D(0.0, 640.0, 0.0, 480.0);

void drawDot(GLint x , GLint y)
glVertex2i(x, y);

void Sierpinski(void)
GLintPoint T[3]= {(10,10), (300,30), (200, 300)};

 int index = random(3);
 GLintPoint point = T[index];
 drawDot(point.x, point.y);
 for(int i = 0; i &lt; 1000; i++)
	 index = random(3);
	 point.x = (point.x + T[index].x) / 2;
	 point.y = (point.y = T[index].y) / 2;
	 drawDot(point.x, point.y);


void main(int argc, char** argv)
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(640, 480);
glutInitWindowPosition(100, 150);
glutCreateWindow(“The Sierpinski Gasket”);


Its my first proper OpenGL program so its probably something really simple.


you need to load the MODELVIEW matrix when displaying geometry. put this after your call to gluOrtho2D:


glMatrixMode(GL_MODELVIEW) is, in this case, not needed. Once the projection matrix is set, he don’t touch the matrix stack at all.

As for the problem, i suspect it’s black lines on a black window. You set the clear color to white, but you don’t call glClear(GL_COLOR_BUFFER_BIT) to clear the buffer.

Thanks for that I have a white b/g now.
Unfortunately the program doesnt create the picture its supposed to. I think its because the random number part isn’t doing what I want it to.

What does the command random(3) actually do?

What I want to do is randomly generate numbers 1,2 or 3 so I randomly select one of the points in the array T.

I would guess from the output I am getting that I am always selecting T[1] and never any of the others.


Have just checked my code and found mistake in the maths

I also think I remember reading that the first position in an array is 0 not 1 so I’ll rephrase what I wrote above.

Having corrected the maths (It should have read “point.y = (point.y + T[index].y) / 2;”) I get an output more like what I expected. However, I still don’t think the code ever selects T[2] since all the dots are at the bottom of the screen.

Since I don’t know much about C++ an explanation of what random(3) does might help here.

Cheers and sorry bout the previous post,