GIDForums  

Go Back   GIDForums > Computer Programming Forums > OpenGL Programming
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read
 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 10-Nov-2011, 06:42
chezmark chezmark is offline
Awaiting Email Confirmation
 
Join Date: Nov 2011
Posts: 4
chezmark is on a distinguished road
Post

OpenGL project: Create a fractal.


Hi, everybody.
Iam facing a problem and i am really looking for some help on this. I could really appreciate any help from all of you crazy programers out there.

Well my problem is this (I need to mention that I have no previous experience with OpenGL), i am trying to create a fractal as the one shown in this link (mathworld.wolfram.com), is the image on the bottom right corner (n=6, r=1/3). I have to do it with GL_POINTS so that the fractal appears after 50.000 points, and iam struggling for a week now and it's driving me crazy. I don't know what i am doing wrong so i am providing the code sample in case anyone of you guys can help me out and show me the right way to deal with this problem. Maybe somebody can provide some sample code of how this can be implemented. Thank you all in advance.


CPP / C++ / C Code:
#include <stdlib.h>
#include <GL/glut.h>

void myinit(void)
{
 
/* attributes */

      glClearColor(1.0, 1.0, 1.0, 1.0); /* white background */
      glColor3f(1.0, 0.0, 0.0); /* draw in red */

/* set up viewing */
/* 500 x 500 window with origin lower left */

      glMatrixMode(GL_PROJECTION);
      glLoadIdentity();
      gluOrtho2D(-250.0, 500.0, -250.0, 500.0);
      glMatrixMode(GL_MODELVIEW);
}

void reshape(int w, int h)
{
	 glViewport (0, 0, (GLsizei) w, (GLsizei) h);
	 glMatrixMode (GL_PROJECTION);
	 glLoadIdentity ();
	 gluOrtho2D (0.0, (GLdouble) w, 0.0, (GLdouble) h);

}

void display( void )
{

/* define a point data type */

    typedef GLfloat point[2];     

    point vertices[6] = 
						{ 
							{0.0, 0.0},
							 {0.0, 3.0},
							 {2.0, 4.5},
							 {4.0, 3.0},
							 {6.0, 1.5},
							 {2.5, 3.2}
							
						 };

    int j, k;
    point p = { 1.5, 2.3 };  /* An arbitrary initial point inside traingle */

    glClear(GL_COLOR_BUFFER_BIT);  /*clear the window */


/* compute and plots 50000 new points */
    for(k = 0; k < 50000; k++)
    {
         j = rand() % 6; /* pick a vertex at random */


     /* Compute point 1/3 between selected vertex and old point */

         p[0] = ( p[0] + vertices[j][0] ) / 3.0; 
         p[1] = ( p[1] + vertices[j][1] ) / 3.0;
   
     /* plot new point */
          glBegin(GL_POINTS);
		   glVertex2fv(p);
		   glEnd();
     }
     glFlush(); /* clear buffers */
}

int main(int argc, char** argv)
{

/* Standard GLUT initialization */

    glutInit(&argc,argv);
    glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB); /* default, not needed */
    glutInitWindowSize(500, 500); /* 500 x 500 pixel window */
    glutInitWindowPosition(0, 0); /* place window top left on display */
    glutCreateWindow("Sierpinski Gasket"); /* window title */
    glutDisplayFunc(display); /* display callback invoked when window opened */
	 //glutReshapeFunc(reshape);

    myinit(); /* set attributes */

    glutMainLoop(); /* enter event loop */

	 return 0;
}
  #2  
Old 10-Nov-2011, 19:00
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 6,160
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: Desperately need help with OpenGL project. Anybody plz. Really appreciate it.


Quote:
Originally Posted by chezmark
Hi, everybody.
...no previous experience with OpenGL), ...[/cpp]

Well, in my experience, the hardest part is getting something to show up on the screen. Anything.

Then if it's wrong, maybe I can see how to fix it. If I don't get anything, well how can you fix nothing?

Your program actually puts something on the screen. It's hard to see, but it's there

So...

Then I think the very next step should be:

Understand coordinate systems. What is meant the following statement in your init routine?
CPP / C++ / C Code:
      gluOrtho2D(-250.0, 500.0, -250.0, 500.0);

Why do you want lower left = (-250,-250) and upper right (500,500)?

I think things may be easier to handle mathematically if I use a centered coordinate system, but let's continue with your setup for now.

Anyhow...

Now, with your system you have a window that shows points from x = -250 to +500, and same for y.

However...

You generate points with x values and y valued going from zero to something like 5 or 6. That makes a small red blob on the screen, right? Look very carefully. It's really small, like a dot, and it's not centered, but it's there. I mean, you have 50,000 points, but they all lie within a pixel or two of each other, so the picture is not very edifying, right?

So, without changing anything else in your program give the points a scale factor of something like 750/6 so that the individual points will be more visible.

Maybe something like:
CPP / C++ / C Code:
void display(void)
{

    /* define a point data type */
    typedef GLfloat point[2];

    /*
       Are these supposed to be the vertices of a regular
       hexagon?  Really?  In what universe?
    */
    point vertices[6] = {
        {0.0, 0.0},
        {0.0, 3.0},
        {2.0, 4.5},
        {4.0, 3.0},
        {6.0, 1.5},
        {2.5, 3.2}

    };


    /* An arbitrary initial point inside traingle */
    point p = { 1.5, 2.3 };

    /*clear the window */
    glClear(GL_COLOR_BUFFER_BIT);


    /*
       Since points on the screen will plot for values of x and y
       over a range of -250 to +500, pick a scale factor that
       might make the dots (and the pattern that they make)
       somewhat visible.  This is just for experimentation.
    */
    double scale_factor = 750.0/6.0;
    /* compute and plots 50000 new points */
    for (int k = 0; k < 50000; k++) {
        /* pick a vertex at random */
        int j = rand() % 6;

        /* Compute point 1/3 between selected vertex and old point */
        p[0] = (p[0] + vertices[j][0]) / 3.0;
        p[1] = (p[1] + vertices[j][1]) / 3.0;

        /* plot new point */
        glBegin(GL_POINTS);
        glVertex2f(scale_factor*p[0], scale_factor*p[1]);
        glEnd();
    }
    /* clear buffers */
    glFlush();
}

Now, I get something that I can see. Furthermore, the pattern looks a little Sierpenski-ish, but certainly nothing like the neat little starflake in your link.

I have indicated the problem in a comment: Your vertex values don't come close to defining a regular hexagon. Really. Not Close. (See Footnote.)


Here's a way to find them mathematically
CPP / C++ / C Code:
// gen_hexagon.cpp
//
// Will generate vertex points for a regular hexagon
// centered at (0.0, 0.0) and side length = 1.0.
//
// davekw7x
//

#include <iostream>
#include <iomanip>
#include <cmath>

using namespace std;

int main()
{

    int number_of_sides = 6;
    double vertices[6][2];
    double side = 1.0;
    // If your cmath doesn't have M_PI, then just define it with something like
    // #define M_PI   3.14159265358979323846

    double factor = 2.0 * M_PI / number_of_sides;
    double x0 = 0.0;
    double y0 = 0.0;
    cout << fixed << setprecision(6);
    for (int i = 0; i < number_of_sides; i++) {
        vertices[i][0] = x0 + side*sin(i*factor);
        vertices[i][1] = y0 + side*cos(i*factor);
        cout << "vertex[" << i << "]: (" 
             << showpos << vertices[i][0]
             << "," << vertices[i][1] << ")" 
             << noshowpos << endl;
    }
    return 0;
}

Output:
CPP / C++ / C Code:
vertex[0]: (+0.000000,+1.000000)
vertex[1]: (+0.866025,+0.500000)
vertex[2]: (+0.866025,-0.500000)
vertex[3]: (+0.000000,-1.000000)
vertex[4]: (-0.866025,-0.500000)
vertex[5]: (-0.866025,+0.500000)

Now, you can put the generation code in your opengl program or you can just initialize your vertices array with these values:
CPP / C++ / C Code:
void display(void)
{

    /* define a point data type */
    typedef GLfloat point[2];

    double scale_factor = 500/1.2;
    point vertices[6] = {
         {+0.000000,+1.000000},
         {+0.866025,+0.500000},
         {+0.866025,-0.500000},
         {+0.000000,-1.000000},
         {-0.866025,-0.500000},
         {-0.866025,+0.500000}
    };

    /* An arbitrary initial point inside traingle */
    point p = { 1.5, 2.3 };
    

    /*Clear the window. */
    glClear(GL_COLOR_BUFFER_BIT);


    /* Compute and plot 50000 new points. */
    for (int k = 0; k < 10000; k++)
    {
        /* pick a vertex at random */
        int j = rand() % 6;

        /* Compute point 1/3 between selected vertex and old point */
        p[0] = (p[0] + vertices[j][0]) / 3.0;
        p[1] = (p[1] + vertices[j][1]) / 3.0;

        /* plot new point */
        glBegin(GL_POINTS);
        glVertex2f(scale_factor*p[0], scale_factor*p[1]);
        glEnd();
    }
    /* clear buffers */
    glFlush();
}

Summary: Here are my suggestions.

Change the coordinate system to be zero-centered. That is in myinit(), do something like
CPP / C++ / C Code:
    gluOrtho2D(-250.0, 250.0, -250.0, 250.0);

Now the points will be shown for x and y values between -250 and 250. (This doesn't have to be the same as the dimensions of the opengl window, but making the coordinate system have similar dimensions to the window size in pixels might make some debugging simpler...)


Then, since x any y values for the hexagon are all between -1 and 1, I use a scale factor of 500/1.2 so that the figure won't crowd the edges.

Now when I try again, I get something more like that cute little snowflake, nicely centered on the screen.


Regards,

Dave

Footnote: To get a feel for coordinates and scaling, I suggest that you try something simpler for starters: Use GL_LINES to plot the hexagon. Or some such thing.

There are, perhaps, more elegant ways of doing just about everything that I mention above, but I like to start with fundamentals.

Draw a triangle using GL_LINES. It doesn't have to be equilateral. Experiment with different vertices. Make sure you can tell up from down and left from right in your coordinate system.

Then it's onward and upward to some real fun stuff!
__________________
Sometimes I just can't help myself...
  #3  
Old 13-Nov-2011, 06:03
chezmark chezmark is offline
Awaiting Email Confirmation
 
Join Date: Nov 2011
Posts: 4
chezmark is on a distinguished road

Re: OpenGL project: Create a fractal.


Dave many many many thanks, you probably should be teaching those stuff instead of my dumn *** teacher. Thanks dude.
  #4  
Old 13-Nov-2011, 08:18
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 6,160
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: OpenGL project: Create a fractal.


Quote:
Originally Posted by chezmark
you probably should be teaching ...
Here's the thing: I tried teaching once, at a small university in central Florida, and I thought I was actually doing OK, but then it started to get to me.

I mean, all those poison-pen anonymous e-mails. Late-night harassing phone calls with death threats. Stuff like that. Really bad mojo, doncha know?

Anyhow...

Once they discovered that it was me doing that (to my students), they figured that there enough extenuating circumstances for them to defer charges, but they made me promise not to have direct contact with freshmen C.S. students again.


Regards,

Dave

Footnote:
If my probation officer ever figures out my secret identity on this forum, I may have to go under-cover for a while, so if you don't hear from me again, well...
__________________
Sometimes I just can't help myself...
  #5  
Old 02-Aug-2017, 03:05
pravin007techno pravin007techno is offline
New Member
 
Join Date: Sep 2014
Posts: 10
pravin007techno is an unknown quantity at this point

Re: OpenGL project: Create a fractal.


Thanks to all
 


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
My second java project. nstone Java Forum 1 19-Jan-2011 08:41
2d/3d And 3d/2d Project......(fltk) rohan_iitd FLTK Forum 1 23-Nov-2004 01:15
nVidia & OpenGL Problems in windows XP marjasin Computer Hardware Forum 21 30-Aug-2004 00:57
Help with project. anignna C Programming Language 12 27-Apr-2004 07:51
Help: Eagle Scout Project BobbyDouglas Open Discussion Forum 3 17-Nov-2003 08:17

Network Sites: GIDNetwork · GIDApp · GIDBlog · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 10:16.


vBulletin, Copyright © 2000 - 2017, Jelsoft Enterprises Ltd.