PDA

View Full Version : Display Problems



PhilMag
09-05-2002, 04:06 AM
Hi,
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:

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

class GLintPoint{
public:

GLint x, y;
};

void myInit(void)
{
glClearColor(1.0,1.0,1.0,0.0);
glColor3f(0.0f, 0.0f, 0.0f);
glPointSize(4.0);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0.0, 640.0, 0.0, 480.0);
}

void drawDot(GLint x , GLint y)
{
glBegin(GL_POINTS);
glVertex2i(x, y);
glEnd();
}

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 < 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);
}
glFlush();

}

void main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(640, 480);
glutInitWindowPosition(100, 150);
glutCreateWindow("The Sierpinski Gasket");
glutDisplayFunc(Sierpinski);
myInit();
glutMainLoop();

}

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

Phil

coredump
09-05-2002, 05:14 AM
you need to load the MODELVIEW matrix when displaying geometry. put this after your call to gluOrtho2D:

glMatrixMode(GL_MODELVIEW);

Bob
09-05-2002, 05:30 AM
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.

PhilMag
09-05-2002, 05:46 AM
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.

Phil

PhilMag
09-05-2002, 06:07 AM
Have just checked my code and found mistake in the maths http://www.opengl.org/discussion_boards/ubb/smile.gif

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,

Phil