# Grid with For loop.. Working but I'm sure it's...

### #1 o0Illusionist0o

o0Illusionist0o

GMC Member

• New Member
• 7 posts
• Version:GM8

Posted 29 July 2012 - 08:20 PM

Hey forum, Would just like to say howdy as this is my first post in a looong time (new acc ^.^)

So anyway, I'm working with a piece of code..which i should really know by heart..considering how many times i've written it in the past. So I'm wanting to draw a 3D grid using a for loop (and 3d vertex's in my case) Now, the code works.. but I'm sure I remember it being simpler.. I don't want to waste resources.

Thanks allot!

```{  xx = 0;
yy = 0;

d3d_primitive_begin_texture( pr_trianglestrip, tex.grid );

for (xx = 0; yy <= 320; xx+= 64) {

d3d_vertex_texture( xx, yy, 50, 0, 0 ) ;
d3d_vertex_texture( xx, yy + 32, 50, 0, 1 ) ;
d3d_vertex_texture( xx + 32, yy, 50, 1, 0 ) ;
d3d_vertex_texture( xx + 32, yy + 32, 50, 1, 1 ) ;

if xx = 320 {
d3d_vertex_texture( xx + 32, yy + 32, 50, 1, 1 ) ;
xx = -64;
yy += 32 ;
}
}
d3d_primitive_end();
}
```

Edited by o0Illusionist0o, 29 July 2012 - 08:25 PM.

• 0

### #2 torigara

torigara

GMC Member

• GMC Member
• 6483 posts

Posted 30 July 2012 - 03:39 AM

That's a curious way to write a double loop with a single for, but I guess the straightforward way is simpler.
```d3d_primitive_begin_texture( pr_trianglestrip, tex.grid );
for (yy = 0; yy <= 320; yy += 32) {
for (xx = 0; xx <= 320; xx+= 64) {
d3d_vertex_texture( xx, yy, 50, 0, 0 ) ;
d3d_vertex_texture( xx, yy + 32, 50, 0, 1 ) ;
d3d_vertex_texture( xx + 32, yy, 50, 1, 0 ) ;
d3d_vertex_texture( xx + 32, yy + 32, 50, 1, 1 ) ;
}
d3d_vertex_texture( 320 + 32, yy + 32, 50, 1, 1 ) ; // dummy vertex to break a row?
}
d3d_primitive_end();
```
Or even simpler by using a repeated texture.
```texture_set_repeat(true);
d3d_primitive_begin_texture( pr_trianglestrip, tex.grid );
d3d_vertex_texture( 0,   0,   50, 0,  0 ) ;
d3d_vertex_texture( 0,   352, 50, 0,  11 ) ; // 11 = (320+32)/32
d3d_vertex_texture( 352, 0,   50, 11, 0 ) ;
d3d_vertex_texture( 352, 352, 50, 11, 11 ) ;
d3d_primitive_end();
texture_set_repeat(false); // reset to default
```

Edited by torigara, 30 July 2012 - 03:43 AM.

• 0

#### 0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users