# Random points in OpenGL

Hi all,

As most on this site I am brand new to OpenGL, and a project requires that I pick a constellation of stars to visualize on the screen. I have all of the points plotted and the lines connected at my verticies and it looks fine. I am having trouble with scattering all of the other stars (maybe 100 or so of different sizes) randomly.

We were told that we could use this code:

int random(int m)
{
return rand()%m;
}

I guess my question is where would this go in the program and how would I generate my 100 random dots to represent my stars.

Thanks,
1nuprogrammer

This is not much of an OpenGL question, this is more normal C stuff, if you would just look up the rand() function in your reference and then have a peek at the % operator you should not have a ny problem to figure this out.

Your random function will return a “random” value between 0 and m-1 so you could do something like:

glVertex3d( -500.0 + random(1000), -500.0 + random(1000), -500.0 + random(1000));

To create a set of random stars in a box where each side is 1000 units long and aligned around the origin.

Of course you will have to add glBegin and a loop to the code above.

Mikael

i don’t think it’s a good idea to use the random() func in the glVertex call, because everytime your window is repainted you’ll get different positions.

you can avoid this by

1. using the random() func in glVertex, but encapsulating the glVertex calls in a display list.

or

1. filling a vertex array with the random values.

Hi guys,

Thanks for your responses, but I’m afraid they are still a little advanced. I’ll add the code that I have so far and maybe someone could suggest a place for the random stars…I have searched all over for some code to create this but I have been running into problems.

Here it is:

#include <windows.h> // use as needed for your system
#include <gl/Gl.h>
#include <gl/glut.h>
//<<<<<<<<<<<<<<<<<<<<<<< myInit >>>>>>>>>>>>>>>>>>>>
void myInit(void)
{
glClearColor(0.0,0.0,0.0,0.0); // set black background color
glColor3f(1.0f, 1.0f, 1.0f); // set the drawing color to black
//glPointSize(4.0); // a ‘dot’ is 4 by 4 pixels
glMatrixMode(GL_PROJECTION);
gluOrtho2D(0.0, 640.0, 0.0, 480.0);
}

//<<<<<<<<<<<<<<<<<<<<<<<< myDisplay >>>>>>>>>>>>>>>>>
void myDisplay(void)
{
glClear(GL_COLOR_BUFFER_BIT); // clear the screen

// Star #1 Alpheratz
glPointSize(9.0);
glBegin(GL_POINTS);
glVertex2i(400,155);
glEnd();
glFlush();

// Star #2
glPointSize(4.0);
glBegin(GL_POINTS);
glVertex2i(300,165);
glEnd();
glFlush();

// Star #3
glBegin(GL_POINTS);
glVertex2i(250,145);
glEnd();
glFlush();

// Star #4
glPointSize(3.0);
glBegin(GL_POINTS);
glVertex2i(260,200);
glEnd();
glFlush();

// Star #5
glBegin(GL_POINTS);
glVertex2i(250,225);
glEnd();
glFlush();

// Star #6
glBegin(GL_POINTS);
glVertex2i(150,185);
glEnd();
glFlush();

// Star #7 Markab - The Brightest Star of Pegasus
glPointSize(13.0);
glBegin(GL_POINTS);
glVertex2i(275,300);
glEnd();
glFlush();

// Star #8
glPointSize(9.0);
glBegin(GL_POINTS);
glVertex2i(400,300);
glEnd();
glFlush();

// Star #9
glBegin(GL_POINTS);
glVertex2i(225,350);
glEnd();
glFlush();

// Star #10
glBegin(GL_POINTS);
glVertex2i(100,400);
glEnd();
glFlush();

// Star #11 - Enif
glBegin(GL_POINTS);
glVertex2i(60,350);
glEnd();
glFlush();

// Star #12 - M15
glBegin(GL_POINTS);
glVertex2i(50,300);
glEnd();
glFlush();

// Connect the lines
glBegin(GL_LINE_LOOP);
glVertex2i(300,165);
glVertex2i(400,155);
glVertex2i(400,300);
glVertex2i(275,300);
glEnd();
glFlush();

// Connect the other verticies
glBegin(GL_LINES);
glVertex2i(300,165);
glVertex2i(250,145);
glEnd();
glFlush();

glBegin(GL_LINES);
glVertex2i(250,225);
glVertex2i(260,200);
glEnd();
glFlush();

glBegin(GL_LINES);
glVertex2i(300,165);
glVertex2i(260,200);
glEnd();
glFlush();

glBegin(GL_LINES);
glVertex2i(250,225);
glVertex2i(150,185);
glEnd();
glFlush();

glBegin(GL_LINES);
glVertex2i(275,300);
glVertex2i(225,350);
glEnd();
glFlush();

glBegin(GL_LINES);
glVertex2i(225,350);
glVertex2i(100,400);
glEnd();
glFlush();

glBegin(GL_LINES);
glVertex2i(100,400);
glVertex2i(60,350);
glEnd();
glFlush();

}

//<<<<<<<<<<<<<<<<<<<<<<<< main >>>>>>>>>>>>>>>>>>>>>>
int main(int argc, char** argv)
{
glutInit(&argc, argv); // initialize the toolkit
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB); // set display mode
glutInitWindowSize(640,480); // set window size
glutInitWindowPosition(100, 150); // set window position on screen
glutCreateWindow(“Pegasus, The Flying Horse”); // open the screen window
glutDisplayFunc(myDisplay); // register redraw function
myInit();
glutMainLoop(); // go into a perpetual loop
}

Come on now, you are already rendering stars on your own, wright ?

Just add a loop at the end of your display function (before glEnd and glFlush of course) like I said in my first post.

glVertex2i( random( 400), random( 400)); for example.

If you do not want to mess around with display lists then just add a srand( 100); or what ever before you start render your random stars and they will all end up in the same position each time display is called.

Mikael

First off I can’t use parenthesis for some reason now.

True, I did figure out how to do my stars but it was easier because this program started out as a display of three dots. I just added more and different size dots. OK back to business, here is what I did to try and get the random stars…

At my last star #12 I added:

glBegin GL_POINTS;
glVertex2i 50,300;
for int i=0; i<100; i++ // shouldn’t this create 100 stars?
glVertex2i random 400,random 400; // at the random x,y coordinates ranging from 0-399 respectively?
glEnd;
glFlush;

Well when I tried to compile it said that random couldn’t be used as a function.

Then you did something wrong when you defined/declared the random function, is all the source code in one file ?, in that case do you think you could post it here ?

random doesn’t take arguments, so you should use

random()%400

random(400)

Remeber it is not letting me use parenthesis for some reason so I couldn’t post my code-does anyone know why this is happening? I don’t know if there is a way to attach files in here so I will say that the program is exactly the same as one of my earlier posts except I took the suggestion that the random function doesn’t take arguments so I added this to star #12

// Star #12 - M15
glBegin GL_POINTS;
glVertex2i 50,300;
for int i=0; i<100; i++
glVertex2i random%400,random%400;
glEnd;
glFlush;

Here are the errors I received when trying to compile:

Compiler: Default compiler
Executing g++.exe…
g++.exe “C:\Dev-Cpp\Pegasus.cpp” -o “C:\Dev-Cpp\Pegasus.exe” -I"C:\Dev-Cpp\include\c++" -I"C:\Dev-Cpp\include\c++\mingw32" -I"C:\Dev-Cpp\include\c++\backward" -I"C:\Dev-Cpp\include" -L"C:\Dev-Cpp\lib"
C:/Dev-Cpp/Pegasus.cpp: In function void myDisplay': C:/Dev-Cpp/Pegasus.cpp:96:random’ undeclared first use this function
C:/Dev-Cpp/Pegasus.cpp:96: Each undeclared identifier is reported only once
for each function it appears in.

Execution terminated

Do I need to declare the random function?

Hello all,

FINALLY SUCCESS!!!

Here is what I did:

I figured out that I needed to declare random at the beginning of the program like this:

remember no parenthesis in replies…

int random int m
{
return rand%m;
}

So I did this:

glPointSize 1.0;
glBegin GL_POINTS;
for i=0; i=500; i++
glVertex2i random700, random700;
glEnd;
glFlush;

AND IT WORKED!!!

I feel like one of you now. Anyways, thanks for everyone’s help.

Peace,
1nuprogrammer