PDA

View Full Version : rand() and exit() not working in c++ tool?


Dogcow
09-27-2002, 01:56 PM
In a never-ending quest to get a simple OpenGL program working so I can build on it, I decided to try the Three-Dimensional Sierpinski Gasket.

Three things from the code didn't work:

• rand()
• exit() or exit(0)
• NULL

Project Builder complained about all three being undeclared. I was under the impression that all of these should be built in... I have OpenGL.framework, GLUT.framework, and Foundation.framework imported into my project. If anyone can help me I'd really appreciate it...if I could just get one of these fricking examples working I'd be able to actually start on my program...

Thanks in advance,
-Dogcow "moof!"
Visit The Underground (http://theunderground.eon.com.au)


The code I'm working with:

/* 3D Sierpinski Gasket GL program */

#include <OpenGL/gl.h>
#include <OpenGL/glu.h>
#include <GLUT/glut.h>

/* Define a point data type */

typedef struct { float x,y,z;} point3;

point3 vertices[4]={{0,0,0},{250,500,100},{500,250,250},{250,100,250}}; /* A tetrahedon */

int j;
//point3 new, old={250,100,250};
point3 newt={250,100,250};
point3 old={250,100,250};
void clear()
{
glClear(GL_COLOR_BUFFER_BIT);
}

/* compute and plot a single point */
void display()
{
long rand();
//int i;
j=rand()%4; /* pick a vertex at random */

/* compute point halfway between vertex and old point */
newt.x = (old.x+vertices[j].x)/2;
newt.y = (old.y+vertices[j].y)/2;
newt.z = (old.z+vertices[j].z)/2;

/* plot point */
glBegin(GL_POINTS);
glColor3f(1.0-newt.z/250.,newt.z/250.,0.);
glVertex3f(newt.x,newt.y,newt.z);
glEnd();

/* replace old point by new */
old.x=newt.x;
old.y=newt.y;
old.z=newt.z;

glFlush();
}

void mouse(int btn, int state, int x, int y)
{
if(btn==GLUT_LEFT_BUTTON&state==GLUT_DOWN) glutIdleFunc(display);
if(btn==GLUT_MIDDLE_BUTTON&state==GLUT_DOWN) glutIdleFunc(NULL);
if(btn==GLUT_RIGHT_BUTTON&state==GLUT_DOWN) exit();
}

int main(int argc, char** argv)
{
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(500,500);
glutInitWindowPosition(0,0);
glutCreateWindow("Sierpinksi Gasket");

glutIdleFunc(display);
glutMouseFunc(mouse);
glClearColor(1.0,1.0,1.0,0.0); /* white background */
glColor3f(1.0,0.0,0.0);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0.0,500.0,0.0,500.0,-500.0,500.0);
glMatrixMode(GL_MODELVIEW);
glutDisplayFunc(clear);

glutMainLoop();
}

hayne
09-27-2002, 02:27 PM
Functions in C++ are not "built in" - they exist in pre-compiled form in a library.
And you need to supply declarations for all functions that you call. Usually you do this via the #include of a header file containing the declarations.
For any given function, the 'man' page (or other documentation) tells you which header file you need to include. In the case of 'rand' and 'exit', it is stdlib.h