Thread: 3d triangle

  1. #1
    Registered User rogster001's Avatar
    Join Date
    Aug 2006
    Location
    Liverpool UK
    Posts
    1,472

    3d triangle

    The code below is a function to draw a '3d' version of the Sierpinski sieve at the moment it just draws a 2d scene with 3dperspective, the plotting of the points is all there to create a 3d model but i have no idea what needs to be implemented to get this going, should i be creating more surfaces or something? and lighting direction i suppose but maybe running before can walk.

    i have posted a screenshot of how it looks after a couple of iterations so you get an idea

    thanks for any suggestions.

    Code:
    void DrawScene(SDL_Surface *screen)
    {
    int count;
    int maxrand = 5;                
    int num;                      
    
                              // verticies of the "3d" triangle 
      
    int v1acr = 200; int v1dwn = 25;                     // top middle point
    int v2acr = 360; int v2dwn = 250;                   // right corner
    int v3acr = 40; int v3dwn = 250;                     // left corner
    int v4acr = 200; int v4dwn = 300;                   // bottom middle point
    int v5acr = 200; int v5dwn = 250;                   // back middle point
    
    int plotacr, plotdwn;                                        // variables to store x & y coordinates //
    int currentacr = 275;           
    int currentdwn = 112;
    
         
         srand(time(NULL));             
           
         SDL_WM_SetCaption("3D-ish Sierpinski Gasket","3D-ish Sierpinski Gasket" );   
    
    	for (count = 0; count < 20000; count++)
    	{
    	num = rand() % maxrand; 
    	
    		switch(num)
    		{
    		case 0:
    		plotacr = v1acr + currentacr / 2; plotdwn = v1dwn + currentdwn / 2;
    		break;
    		case 1:
    		plotacr = v2acr + currentacr / 2; plotdwn = v2dwn + currentdwn / 2;
    		break;
    		case 2:
    		plotacr = v3acr + currentacr / 2; plotdwn = v3dwn + currentdwn / 2;
    		break;
    		case 3:
    		plotacr = v4acr + currentacr / 2; plotdwn = v4dwn + currentdwn / 2;
    		break;
    		case 4:
    		plotacr = v5acr + currentacr / 2; plotdwn = v5dwn + currentdwn / 2;
    		break;
    		}
    
    		 DrawPixel(screen, plotacr,plotdwn,100,50,50);
    		currentacr = plotacr; currentdwn = plotdwn;                    //set last point to new start point 
    	 }
    
      Sulock(screen);
    SDL_Flip(screen);
    }
    Last edited by rogster001; 09-09-2009 at 10:08 AM.

  2. #2
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    A simple way to "connect the dots" and have a wireframe model instead of a collection of points is to draw a line between each point of a triangle. I'm not really sure it would improve the look of your program, though.

    You could also simulate lighting by pretending that there is a light source at the observer's position, and using the Z coordinate (if you can access it) to determine the alpha component of the pixel's colour; making farther points seem darker might lend a more 3D feel to the output.

    Maybe you could implement gravity between the points in the Sierpinski's trangle. Might make for some interesting effects . . . . Or you could just rotate the entire thing, perhaps based on user input or just automatically. A little bit of trigonometry is involved in this. A rotation about the Y axis, for example, can be achieved with something like this:
    Code:
        result->x = (origin->x * cos(y_angle)) - (origin->z * sin(y_angle));
        result->y = origin->y;
        result->z = (origin->z * cos(y_angle)) + (origin->x * sin(y_angle));
    Of course, it's faster if you only call sin() and cos() once, and cache the value.

    Zooming's easier and would be interesting, too.

    Also, some inspiration: File:Sierpinski pyramid.jpg - Wikimedia Commons
    Have fun.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  3. #3
    Registered User rogster001's Avatar
    Join Date
    Aug 2006
    Location
    Liverpool UK
    Posts
    1,472
    thanks! , i am going to have a crack at some of those ideas, zooming is a good spin on it maybe, and i have just completed a neat mandlebrot zoom function i could perhaps work into it

  4. #4
    Registered User rogster001's Avatar
    Join Date
    Aug 2006
    Location
    Liverpool UK
    Posts
    1,472
    That image posted is awesome, this is what i am talking about, i am not equipped to produce something like that myself yet but it would be interesting to know what was going on to render the pyramid thus.

  5. #5
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    I suspect that that particular image was rendered using DirectX (based on the author's other programs), but you can achieve the same results with other libraries like OpenGL. If you're wanting to get into this 3D stuff, I highly recommend looking into OpenGL. The best part is that OpenGL plays well with the SDL, so you don't have to abandon it.

    Nehe's OpenGL tutorials: NeHe Productions: Main Page
    Lazy Foo's SDL and OpenGL tutorial: Lazy Foo' Productions

    There are other good sites around too, just search Google.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  6. #6
    Registered User rogster001's Avatar
    Join Date
    Aug 2006
    Location
    Liverpool UK
    Posts
    1,472
    thanks!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Formula for Area of a triangle in 3D??
    By Zeeshan in forum A Brief History of Cprogramming.com
    Replies: 2
    Last Post: 04-06-2008, 03:27 AM
  2. Right Triangle Program
    By BSmith4740 in forum C# Programming
    Replies: 9
    Last Post: 02-27-2008, 12:24 AM
  3. Odd 3D Invis Objects?
    By Zeusbwr in forum Game Programming
    Replies: 4
    Last Post: 12-07-2004, 07:01 PM
  4. Just in case: "Odd" Triangle Challenge (for me)
    By BB18 in forum C Programming
    Replies: 3
    Last Post: 10-09-2004, 12:02 AM
  5. 3D starfield
    By VirtualAce in forum Game Programming
    Replies: 6
    Last Post: 06-26-2003, 12:40 PM