Forum archive
drawing fast squares
- what is the fastes way to draw voxels that are represented by 2D squares ?
No matter what I do my code always gets slowdown by the drawing routine, this is ofcourse somewhat normal,
but I was hoping to speed it up. What do you guys suggest ?
so I'm searching for something like this :
for(y = 0; y < 4; y++)
{
for(x = 0; x < 4; x++)
{
pixel(x,y,color);
}
}
or
for(y = 0; y < 4; y++)
{
pixel(0,y,color);
pixel(1,y,color);
pixel(2,y,color);
pixel(3,y,color);
}
I was hoping somebody had a damn sweet trick for it. Re: drawing fast squares
Why, the fastest way to draw bounded cubes is to use Ken's Voxlap / Slab6 optimized rendering algorithm.
I suggest you skim through Ken's Voxlap / Slab6 source:
http://advsys.net/ken/slab6.zip <--- src included
http://advsys.net/ken/voxlap/voxlap_src.zip <---- src included- I've read the source before. But it's not too handy for my situation.
Since his drawing code is optimized for PC in ASM, and I'm searching for C/C++ code.
Basically I need a fast routine for C/C++ to plot a square directly to the VRAM.
In case of my examples the pixel function would be :
VRAM[y * width + x] = color; - I'm pretty sure the second way is more faster!
- You are losing most of your speed by calling a function for every single pixel. Here's how I would write it:
typedef struct { long topleft, pitch, xres, yres; } frame_t;
static void drawrect_32bpp (frame_t *dd, long x0, long y0, long x1, long y1, long col)
{
long *lptr;
x0 = max(x0,0); x1 = min(x1,dd->xres);
y0 = max(y0,0); y1 = min(y1,dd->yres);
lptr = (long *)(y0*dd->pitch + dd->topleft);
for(y1-=y0;y1>0;y1--,lptr=(long *)(((long)lptr)+dd->pitch))
for(y0=x0;y0<x1;y0++) lptr[y0] = col;
}
You could use a lookup table for the initial calculation of lptr, but I've found it to not always be faster. - thanks Ken, I'm going to try your routine :)
Btw; I'm not calling pixel every time, It was just to make the example look more clear.
As I wrote in my second post; I write directly to VRAM. - Sounds interesting, I like to see where this goes.
- And what do you guys use for zbuffer ?
my current routine (inside my voxel render function) looks something like this :
where :
256 is the width of the screen and int posy = y * 256;
if(newz < zbuffer[(posy) + x]) vram[(posy) + x] = color;
if(newz < zbuffer[(posy) + x + 1]) vram[(posy) + x + 1] = color;
if(newz < zbuffer[(posy + 256) + x]) vram[(posy + 256) + x] = color;
if(newz < zbuffer[(posy + 256) + x + 1]) vram[(posy + 256) + x + 1] = color;
I basically write everything out, no loops, no multiplies. yet I'm still not happy.