Dynamic array and OpenGL problem

Jarrett Billingsley kb3ctd2 at yahoo.com
Tue Jan 15 05:15:22 PST 2008


"Mikko Ronkainen" <mikoro at iki.fi> wrote in message 
news:fmi0gi$2b99$1 at digitalmars.com...
> Ok, I'm still trying to optimize the code a little. I'm trying to code my 
> own little
> software 3D "engine" and here's a snippet from the triangle scan 
> conversion:
>
> int leftOffset = (y * framebufferWidth + cast(int)(leftX + 0.5)) * 3;
> int rightOffset = (y * framebufferWidth + cast(int)(rightX + 0.5)) * 3;
>
> for(int i=leftOffset; i<=rightOffset; i+=3)
> {
>    framebuffer[i] = color.r;
>    framebuffer[i + 1] = color.g;
>    framebuffer[i + 2] = color.b;
> }
>
> It draws one scan line from a triangle (setting all pixels on a line from 
> leftOffset to
> rightOffset to given color). Now it seems that in C you can go about it 
> something like
> this (pseudo code):
>
> memset(framebuffer+left, color, (right-left+1));
>
> This assumes color is an int so framebuffer format is RGBA. Now I did a 
> test in D like
> this:
>
> ubyte[] framebuffer = window.getFramebuffer();
> memset(framebuffer.ptr, 0xffff, 1);
>
> I thought I would get a white and a red pixel (framebuffer format is RGB), 
> but just got
> one red pixel. I get one white, if I change last parameter to 3, so it 
> seems memset in this case only writes bytes. My question is: what is the 
> fastest way to set continuous block of bytes to some repeating sequence 
> (int this case rgb[a] values)?
>

So I assume your 'color' is a struct defined something like

struct Color { int r, g, b; }

How about something like..

(cast(Color[])frameBuffer[leftOffset .. rightOffset + 1])[] = color;

First we're slicing the array to get only the subset that we want.  The 
upper bound is noninclusive so we have to put +1 to include the byte at 
rightOffset.  Then we cast it to a Color[] -- this will perform a runtime 
check to ensure that the size of the data in the source array really is a 
multiple of Color.sizeof.  Lastly we just slice-fill as before.

You might want/need to make Color "packed" to ensure that the compiler 
doesn't insert any extra padding in for alignment:

align(1) struct Color { int r, g, b; } 




More information about the Digitalmars-d-learn mailing list