Skip to main content
GameDev.net gamedev.net
🔒 Locked

Generate random patterns

Started by flyjason Mar 16, 2007 at 1:42 PM 9 replies 1.9k views
Original Post
flyjason
flyjason
Hi, I would like to know how to create patterns randomly in OpenGL. What I mean is that let say I use a square and a triangle, and then mix them up so that it creates a kind of pattern. The pattern generated should be unique. That is, next time a pattern is generated, it should be different from the previous one. Perhaps OpenGL has some specific functions for performing these??... I need to know also how to save each newly generated pattern as a picture file (JPEG, GIF, etc). Thanks for helping.
NerdInHisShoe
NerdInHisShoe
I still don't understand what you are trying to do. You mean randomly lay out polygons on a plane and get a random sort of design? Create a random sequence of shapes in a row?
Johnny was a chemist's son by Johnny is no more, for what Johnny thought was H2O was HO4
flyjason
flyjason
I want to get a random sort of design yeah.

Laying out some polygons (possibly squares) and then create the design. The design should then be saved in JPEG/GIF format (3x3 inches).

When the program is executed again...a new design is created but should be unique in the sense that the previously created design should be different from the newly created ones. In other words, this imply that all design should be unique.

Any idea?
JohnBolton
JohnBolton
Quote:
Original post by flyjason
I would like to know how to create patterns randomly in OpenGL. What I mean is that let say I use a square and a triangle, and then mix them up so that it creates a kind of pattern. The pattern generated should be unique. That is, next time a pattern is generated, it should be different from the previous one.
...
I need to know also how to save each newly generated pattern as a picture file (JPEG, GIF, etc).

This is more of a question about generating unique random patterns than a question about OpenGL.

You need to write an algorithm that uses a random number generator (such as rand) to generate a random image, then write code to create the image in memory or use OpenGL to draw the image in memory, then use an image library (such as DevIL) to write the image to a file.

As another alternative, you could use OpenGL to draw the image on the screen and then use screen capture to save it to a file.
John BoltonLocomotive Games (THQ)Current Project: Destroy All Humans (Wii). IN STORES NOW!
flyjason
flyjason
Yeah I was thinking about using rand()...I know it generates random numbers.

How can it be of help to produce random images?

Any pseudocode?
Spoonbender
Spoonbender
Just call rand() for each pixel in the image, would be a simple answer.
Pseudocode:
foreach (column c in image){  foreach (pixel p in c){    p.red = rand()%256;    p.green = rand()%256;    p.blue = rand()%256;  }}



If you need anything more constructive or detailed, you have to provide some more information. What are you trying to achieve (at a higher level, that is. What do you need the generated shapes for, what are they meant to represent)

Knowing that is pretty important in order to be able to suggest ways to generate them.
Rand
Rand
Or because those patterns were just black and white squares.

for(each pixel){   bool bWhite=rand()%2;   r=g=b=bWhite*255;}

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.