# 3d triangle

This is a discussion on 3d triangle within the C Programming forums, part of the General Programming Boards category; The code below is a function to draw a '3d' version of the Sierpinski sieve at the moment it just ...

1. ## 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));

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);
}```

2. 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.

3. 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. 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. 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.

6. thanks!

Popular pages Recent additions