Anim8or Community

Please login or register.

Login with username, password and session length
Advanced search  

News:

Ian Ross has just released a book on Anim8or. It's perect for a beginner and a good reference for experienced users. It contains detailed chapters on every aspect, with many examples. Get your own copy here: "Anim8or Tutorial Book"

Author Topic: flat planes : generate in rows and column  (Read 10977 times)

BOB_I_Ts

  • Full Member
  • ***
  • Posts: 157
    • View Profile
    • Anim8rOrg
flat planes : generate in rows and column
« on: March 11, 2010, 05:20:38 pm »

Hello Anim8ors been long while since i have been involved with code or 3d modeling ,*shakes fist at MMO's for making me forget every thing

any way i thought i try refresh my mind on a8s
i want to create a simple flat plane with division attributes

just example of 3 x 3 grid and vertice # order
current script which attempted to do above failed in console

Code: [Select]
#plugin("object", "mesh", "gridtest");
#parameter("x division", int, 3, 1, 10);
#parameter("z division", int, 3, 1, 10);
#parameter("X scale", float, 40, 2, 1000.0,scale,scale_x);
#parameter("z scale", float, 40, 2, 1000.0,scale,scale_z);
#return($grid);
#button(24, 24, 2, 0x0000000000, 0x0000000000, 0x0000000000, 0x0000000000,

0x0000001800,
0x0000001800, 0x0000000000, 0x0000000000, 0x0000001800, 0x0000001800,

0x0000001800,
0x0000001800, 0x0000001800, 0x0000001800, 0x0000001800, 0x0000001800,

0x0000001800,
0x0000001800, 0x0000001800, 0x0000001800, 0x0000001800, 0x0000000000,

0x0000000000,
0x0000000000);

shape $grid;
float $xs,$zs,$stepx,$stepz; 
int $xd,$zd,$index[201],$i,$j,$k,$l;
point3 $p;

$xd = parameter("x division");
$zd = parameter("z division");
$xs = parameter("X scale");
$zs = parameter("z scale");

$stepx = $xs/$xd;
$stepz = $zs/$zd;
$l = 1;

$grid.Open();

$index[$k] = $grid.AddPoint($p);

for $i = 1 to ($xd + 1) do {

$index[$k] = $grid.AddPoint($p);

for $j = 1 to $zd do {
$p.z = $p.z + $stepz;
$index[$k] = $grid.AddPoint($p);
$i = 1;


}

$p.x = $l * $stepx;
$l = + 1;
$p.z = 0;
}


/* temp code faces should fill first cell */
$grid.OpenFace(0, 4);
$grid.VertexN($index[0]);
$grid.VertexN($index[1]);
$grid.VertexN($index[5]);
$grid.VertexN($index[4]);
$grid.CloseFace();
$grid.Close();
edit : fixed typos in code

How would we go about fixing this code !?.
« Last Edit: March 11, 2010, 08:46:54 pm by BOB_I_Ts »
Logged

BOB_I_Ts

  • Full Member
  • ***
  • Posts: 157
    • View Profile
    • Anim8rOrg
Re: flat planes : generate in rows and column
« Reply #1 on: March 11, 2010, 10:13:03 pm »

obvious i was thinking of the sequence in reverse

Code: [Select]
#plugin("object", "mesh", "gridtest");
#parameter("x division", int, 3, 1, 10);
#parameter("z division", int, 3, 1, 10);
#parameter("X scale", float, 40, 2, 1000.0,scale,scale_x);
#parameter("z scale", float, 40, 2, 1000.0,scale,scale_z);
#return($grid);
#button(24, 24, 2, 0x0000000000, 0x0000000000, 0x0000000000, 0x0000000000, 0x0000001800,
0x0000001800, 0x0000000000, 0x0000000000, 0x0000001800, 0x0000001800, 0x0000001800,
0x0000001800, 0x0000001800, 0x0000001800, 0x0000001800, 0x0000001800, 0x0000001800,
0x0000001800, 0x0000001800, 0x0000001800, 0x0000001800, 0x0000000000, 0x0000000000,
0x0000000000);

shape $grid;
float $xs,$zs,$stepx,$stepz; 
int $xd,$zd,$index[201],$i,$j,$k,$l,$face,$total;
point3 $p;

$xd = parameter("x division");
$zd = parameter("z division");
$xs = parameter("X scale");
$zs = parameter("z scale");

$stepx = $xs/$xd;
$stepz = $zs/$zd;
$l = 1;
$total = $xd * $zd;
$face = 1;

$grid.Open();

$index[$k] = $grid.AddPoint($p);

for $i = 1 to ($zd + 1) do {

$index[$k] = $grid.AddPoint($p);

for $j = 1 to $xd do {
$p.x = $p.x + $stepx;
$index[$k] = $grid.AddPoint($p);
$i = 1;
}
$p.z = $l * $stepz;
$l = $l + 1;
$p.x = 0;
}

/*draw face's start here*/
if ($face < $total) {
for $face = 1 to $total do {
$grid.OpenFace(0,4);
$grid.VertexN($face);
$grid.VertexN($face + 1);
$grid.VertexN($xd + ($face + 2));
$grid.VertexN($xd + ($face + 1));
$grid.CloseFace();
$face = $face + 1;
}
}
$grid.Close();

my next problem solving the mystery of the normals
seems that the faces break on the last row hmmm.....
Logged

headwax

  • Sr. Member
  • ****
  • Posts: 600
    • View Profile
    • Headwax's Website
Re: flat planes : generate in rows and column
« Reply #2 on: March 11, 2010, 11:59:30 pm »

hmm sorry, can't help

just wanted to say it's good to see a legend returning ;)
Logged

BOB_I_Ts

  • Full Member
  • ***
  • Posts: 157
    • View Profile
    • Anim8rOrg
Re: flat planes : generate in rows and column
« Reply #3 on: March 12, 2010, 06:47:59 am »

wow never be refereed as a legend thank you for the complement headwax your awesome

current code change in face generation code is as follows
Code: [Select]
/* temp code faces should fill first cell */

int $face,$cut,$total;

$cut =  1;
$face = 1;
$total = $xd * $zd ;

if ($face < ($total + 1) ) {
for $face = 1 to ($total + 1) do {
$grid.OpenFace(0,0);
if ($face == (($cut * $xd) + $cut)) {
$cut = $cut + 1;
} else {
$grid.VertexN($face);
$grid.VertexN($face + 1);
$grid.VertexN($xd + ($face + 2));
$grid.VertexN($xd + ($face + 1));
$grid.CloseFace();
$face = $face + 1;
}

}
}
$grid.Close();

Issue is even though if cut command removes the diagonal lines the face cells don't equal the total ! im missing something yet again meh
Logged

hihosilver

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 1294
    • View Profile
Re: flat planes : generate in rows and column
« Reply #4 on: March 12, 2010, 11:36:39 am »

Well if it helps I believe the same.  Too bad I can't help you with coding either ;)
Good to see you back mate!
Logged

Kubajzz

  • Flying Platypus
  • Global Moderator
  • Sr. Member
  • *****
  • Posts: 548
  • Little doggie ate my avatar...
    • View Profile
Re: flat planes : generate in rows and column
« Reply #5 on: March 12, 2010, 01:18:33 pm »

Hi, it's really good to see you back!

It looks like you need to practice ASL more often ;)



I looked into your code and I found a few problems:

#1
Code: [Select]
if ($face < $total) {
  for $face = 1 to $total do {
    $grid.OpenFace(0,4);
    $grid.VertexN($face);
    $grid.VertexN($face + 1);
    $grid.VertexN($xd + ($face + 2));
    $grid.VertexN($xd + ($face + 1));
    $grid.CloseFace();
    $face = $face + 1; /* Useless... */
  }
}
The "if ($face < $total)" condition is useless, because it is checked implicitly by the "for" cycle. Also the last line inside the cycle makes no sense to me - you don't have to increment the "$face" variable yourself, the "for" cycle does it for you...

Anyway, I think it would be better to write 2 nested cycles, the inner cycle will build one row and the outer cycle will build the whole plane... The reason is simple - your cycle goes through all points, which also includes for example face {3, 4, 8, 7} (see your picture) and that makes no sense... Maybe the code in your last post solves this issue, but I really can't understand what that script is doing...

So this is what I think this piece of code should look like:
Code: [Select]
int $NumPointsInRow;
$NumPointsInRow = $xd + 1;

/* The outer cycle builds all rows */
for $i = 0 to $NumPointsInRow * ($zd - 1) step $NumPointsInRow do {

  /* The inner cycle builds all faces in one row */
  for $j = 0 to $xd-1 do {

    $grid.OpenFace(0,4);
    $grid.VertexN($i + $j);
    $grid.VertexN($i + $j + 1);
    $grid.VertexN(($i + $NumPointsInRow) + $j + 1);
    $grid.VertexN(($i + $NumPointsInRow) + $j);
    $grid.CloseFace();

  }
}


#2
The piece of code where you build your points was all pretty messy, too complicated and unreadable... The following code does the same and is in my opinion much easier to understand:
Code: [Select]
for $i = 0 to $zd do {
  for $j = 0 to $xd do {
    $p.x = $j * $stepx;
    $p.z = $i * $stepz;
    $grid.AddPoint($p);
  }
}
...compared to your original code...
Code: [Select]
$index[$k] = $grid.AddPoint($p);

for $i = 1 to ($zd + 1) do {

$index[$k] = $grid.AddPoint($p);

for $j = 1 to $xd do {
$p.x = $p.x + $stepx;
$index[$k] = $grid.AddPoint($p);
$i = 1;
}
$p.z = $l * $stepz;
$l = $l + 1;
$p.x = 0;
}



I did a few more minor changes to make the whole thing more readable. I also noticed a few things in your code that might be considered "bad practice" (I hope you don't mind if I point them out...). For example:
  • The "for" cycles should start from 0 in most cases, because most of things in ASL (arrays, points, faces) are indexed from 0
  • It's generally considered "bad" and "dirty" programming style to edit the iteration variable inside a "for" cycle and it's a big source of impossible-to-find bugs
  • As you surely know, it always helps to give your variables meaningful names...
  • I personally like indenting the code inside enclosed blocks (such as "if" and "for") to make it more readable
  • The most important rule: The code should always be readable and easy to understand. This does not necessarily mean "the shorter, the better". For example your script could be made much shorter by combining the point-building and face-building routines into one cycle, which might also increase the performance, but the result would be very difficult to read and debug. However, in most cases "short = simple = easy to read"...

This is what the final product looks like:
Code: [Select]
#plugin("object", "mesh", "gridtest");
#parameter("x division", int, 3, 1, 10);
#parameter("z division", int, 3, 1, 10);
#parameter("X scale", float, 40, 2, 1000.0,scale,scale_x);
#parameter("z scale", float, 40, 2, 1000.0,scale,scale_z);
#return($grid);
#button(24, 24, 2, 0x0000000000, 0x0000000000, 0x0000000000, 0x0000000000, 0x0000001800, 0x0000001800, 0x0000000000, 0x0000000000, 0x0000001800, 0x0000001800, 0x0000001800, 0x0000001800, 0x0000001800, 0x0000001800, 0x0000001800, 0x0000001800, 0x0000001800, 0x0000001800, 0x0000001800, 0x0000001800, 0x0000001800, 0x0000000000, 0x0000000000, 0x0000000000);

shape $grid;
float $xs,$zs,$stepx,$stepz; 
int $xd,$zd,$i,$j,$NumPointsInRow;
point3 $p;

$xd = parameter("x division");
$zd = parameter("z division");
$xs = parameter("X scale");
$zs = parameter("z scale");

$stepx = $xs/$xd;
$stepz = $zs/$zd;

$grid.Open();

/* Build points here */
for $i = 0 to $zd do {
  for $j = 0 to $xd do {
    $p.x = $j * $stepx;
    $p.z = $i * $stepz;
    $grid.AddPoint($p);
  }
}

/* Build faces here */
$NumPointsInRow = $xd + 1;
for $i = 0 to $NumPointsInRow * ($zd - 1) step $NumPointsInRow do {
  for $j = 0 to $xd-1 do {
    $grid.OpenFace(0,4);
    $grid.VertexN($i + $j);
    $grid.VertexN($i + $j + 1);
    $grid.VertexN(($i + $xd + 1) + $j + 1);
    $grid.VertexN(($i + $xd + 1) + $j);
    $grid.CloseFace();
  }
}

$grid.Close();

That's all. I hope I helped you, should you have any questions I'm here for you ;)
Logged

BOB_I_Ts

  • Full Member
  • ***
  • Posts: 157
    • View Profile
    • Anim8rOrg
Re: flat planes : generate in rows and column
« Reply #6 on: March 13, 2010, 12:26:02 am »


Thank you so much for demonstrating working example and the correct method Kubajzz
I never have been natural typing in languages but as always i jump in deep end.... but if i didn't
i wouldn't get help of and information from great people like you ;D.
i was also unaware of the "for"  "to" and "for" "step" condition's

i will redownload as many a8s as i can hopfully i can make some tools maybe eventually even something useful and improve as i go
Logged