PDA

View Full Version : How to create a polygon and its normal?

patipano
28th January 2012, 17:41
How to create a polygon with Superscope and its normal vector?

Warrior of the Light
2nd February 2012, 21:31
there's a list of tutorials here (http://forums.winamp.com/showthread.php?t=209060). I suggest you read through PAK-9's one

QOAL
2nd February 2012, 22:18
I'd probably recommend using the triangle APE, if you want nice filled polys, rather than a superscope. (Not that I can remember how to use that ape)

patipano
3rd February 2012, 05:23
The PAK-9's Tutorial didn't show me how to create polygon with Triangle(Render) :(.

Warrior of the Light
3rd February 2012, 12:36
I never really used it myself but it's basically a SSC with 3 X's and 3 Y's instead of one I think.

patipano
3rd February 2012, 14:38
Thank you everyone! i got it! (using SSC) :D
Any example code for solid pentagon (SSC)?

Warrior of the Light
3rd February 2012, 15:45
solids are not possible since there only is a line. There are tricks though.
The most commonly used is 'zigzagging' This is the code for a square, do your best to turn this into a pentagon:

init:
n=h

point:
linesize=2;
sw=bnot(sw);
x=sw*1-0.5;
y=i*1-0.5

patipano
10th February 2012, 17:14
Is this okay for pentagon?
"la" is amount of layer(density)

Init:
la=7;n=la*6;a=72/360*2*\$pi;asp=h/w;
x0=1;x1=cos(a);x2=cos(2*a);x3=x2;x4=x1;
y0=0;y1=-sin(a);y2=-sin(2*a);y3=-y2;y4=-y1;

Frame:
p=0;c=0;

Point:
x=(equal(p,0)*x0+equal(p,1)*x1+equal(p,2)*x2+equal(p,3)*x3+equal(p,4)*x4+equal(p,5)*x0);
y=(equal(p,0)*y0+equal(p,1)*y1+equal(p,2)*y2+equal(p,3)*y3+equal(p,4)*y4+equal(p,5)*y0);

sc=1-floor(c/6)*(1/la);
sc=if(below(sc,0),0,sc);
skip=if(equal(p%6,0),1,0);

x=x*asp*sc;
y=y*sc;

p=(p+1)%6;c=c+1;

Warrior of the Light
10th February 2012, 21:36
Looks great! the code you managed to create is more advanced than I had expected.

I would increase la to (for instance) w*0.5 and make the linewidth 2 or 3.

some optimization tips:
You could remove the if() statement for skip; skip=equal(p%6,0); will work too.
The extra line for sc is not needed. Simply use sc=max(1-floor(c/6)*(1/la),0); instead.

patipano
11th February 2012, 04:41
Oh thank you for cleaning my code! :up: i have to learn lots about minimizing the code.

What's the difference between longer code and shorter code? only FPS rate?

Warrior of the Light
11th February 2012, 12:14
Yes. the CPU always runs at 100% with AVS and both reading the code and rendering the image is done by the processor. So the less code it has to read, the faster the image is rendered. Especially in the Point box because it is calculated for every point of the scope over and over again.

For this example it isn't really important but once your preset starts 'growing' you can see the framerate dropping fast.

patipano
12th February 2012, 06:22
Why this scope is so mess? I just added some 3D rotation to my pentagon.

Point:
x1=(equal(p,0)*x0+equal(p,1)*x1+equal(p,2)*x2+equal(p,3)*x3+equal(p,4)*x4+equal(p,5)*x0);
y1=(equal(p,0)*y0+equal(p,1)*y1+equal(p,2)*y2+equal(p,3)*y3+equal(p,4)*y4+equal(p,5)*y0);

z1=0
y2=y1*xc-z1*xs;z2=y1*xs+z1*xc;
x2=z2*ys+x1*yc;z3=z2*yc-x1*ys;
x3=x2*zc-y2*zs;y3=y2*zc+x2*zs;

sc=1-floor(c/6)*(1/la);
skip=equal(p%6,0);
z=1/(z3+1);
x=x3*asp*sc*z;
y=y3*sc*z;
p=(p+1)%6;c=c+1;

Warrior of the Light
12th February 2012, 14:25
you forgot a ; after z1=0

patipano
12th February 2012, 15:19
It also messy. It isn't 3D pentagon :cry:

Grandchild
13th February 2012, 21:00
I really suggest you spend your time learning how to use Triangle. It's just that much more clean than approximating stuff with zig-zagging them in an SSC.
That being said, here's a few pointers to get you going with your pentagon:

First you have to ask, How can a <enter your shape here> be created with triangles? In the case of a pentagon that's pretty easy once you draw a pentagon on paper:
http://s15.postimage.org/d8jevke6v/pentagon2triangles.png
Blue numbers identify triangles and red numbers identify vertices (corners).

So you are going to have 3 triangles.
So in your Init box goes
n = 3;

You also need the coordinates for those triangles, preferably in x, y and z. So I would suggest to use megabuf() since that can be indexed and you can access it more easily later with the help of counters (more about them later).
So write down the coordinates for your pentagon-triangles (or calculate them live - which I will do below) and write the values to a megabuffer array, like so:
crcl5th = 2*\$PI/5; // precalculate the step size around a circle
size = 1;
point1x = sin(crcl5th*0)*size; point1y = cos(crcl5th*0)*size;
point2x = sin(crcl5th*1)*size; point2y = cos(crcl5th*1)*size;
point3x = sin(crcl5th*2)*size; point3y = cos(crcl5th*2)*size;
point4x = sin(crcl5th*3)*size; point4y = cos(crcl5th*3)*size;
point5x = sin(crcl5th*4)*size; point5y = cos(crcl5th*4)*size;
// x coordinate ------------- y coordinate ------------- z coordinate
// triangle 1
assign(megabuf(00),point1x);assign(megabuf(01),point1y);assign(megabuf(02),0);
assign(megabuf(03),point4x);assign(megabuf(04),point4y);assign(megabuf(05),0);
assign(megabuf(06),point5x);assign(megabuf(07),point5y);assign(megabuf(08),0);
// triangle 2
assign(megabuf(09),point1x);assign(megabuf(10),point1y);assign(megabuf(11),0);
assign(megabuf(12),point4x);assign(megabuf(13),point4y);assign(megabuf(14),0);
assign(megabuf(15),point3x);assign(megabuf(16),point3y);assign(megabuf(17),0);
// triangle 3
assign(megabuf(18),point1x);assign(megabuf(19),point1y);assign(megabuf(20),0);
assign(megabuf(21),point2x);assign(megabuf(22),point2y);assign(megabuf(23),0);
assign(megabuf(24),point3x);assign(megabuf(25),point3y);assign(megabuf(26),0);
All this goes into your init section as well, except if you want to modify the coordinates (for example if your pentagon should rotate, or move in any other way.

Now comes the tricky part. You need to access these 3 triangles in your point/triangle code
Every time the triangle section gets evaluated it expects you to give it 3 points in 2D, that is:
x1 = someval; y1 = someval;
x2 = someval; y2 = someval;
x3 = someval; y3 = someval;

Additionally you can specify the triangles color with:
red1 = redval;
blue1 = blueval;
green1 = greenval;
(There is no red2/blue2/green2 and red3/blue3/green3, don't bother...)

Now how do you access the coordinates from the megabuf()?
Well you use a counter. That counter should count in steps of 9, because each time in the triangle section, you want to use 9 different values (we have Z-coordinates, but we throw them away for now, when you add 3D-rotation you will need them again). So, in the frame section set your counter to zero:
counter = 0;
so that every frame the counter will be reset and the pentagon drawn correctly.

Now in the triangle section you read your values from the megabuffer into the coordinates, increase the counter and you're done:
x1 = megabuf(counter); y1 = megabuf(counter+1); // you can add z1/2/3 here if needed
x2 = megabuf(counter+3); y2 = megabuf(counter+4);
x3 = megabuf(counter+6); y3 = megabuf(counter+7);
counter = counter+9;
Voila, if everything went well it should show you a white pentagon now.

I have to admit, I haven't tested this, but I've done it so many times I'm pretty sure it works. If you think something is off, tell me, but check first if you followed the steps correctly.

patipano
14th February 2012, 06:30
Wow, lots of coding but I'd have to check it out! Thanks :up:

patipano
15th February 2012, 17:07
I think this triangle coding is harder than using SSC to coloring from center (gradient).
Any idea for coloring (from center with Triangle)?