July 18, 2009

Sprint 2: Plotting Pixels!

OK, 2nd sprint is basically done...the goal of this sprint is to have a very simple set of functions in which I could open a window and plot a pixel in any RGB colour. My requirements were very basic, as I do not want to "waste" time on a very fancy GUI. In fact, a ray tracer could write its result to an output file,and could do withhout any GUI at all. But that has a major drawback: There is something magical about looking at a screen and seeing your picture appeaing pixel by pixel...

It also allows you to stop a long-running trace when early in the picture you discover a mistake or bug, and you obviously want to correct it without waiting for the finalpixelto be finished.
Looking way into the future, I also wanted to allow for some multi-threading capabilities, and allow multiple pictures to be calculated in parallel. Hence the ray tracer should support multiple windows to be opened and painted to.

As an interface, I was looking for something very simple. Just opening a window, plot a poixel, and close the window. Obviously, any pixel needed to be stored in some kind of bitmap structure as well,since you want to save the picture at a later stage. I decided to use a separate bitmap for the calculated pixels and not use the one used by the window system. This allows me to stay away from any dependancy issues with the operating system, and avoids any requirements on the windows event handling and refreshing. As I said, I don't want to dwell on the GUI too much...

All in all, I decided to use a protected object type in my Ada 2005 sources. I would like to reference the book "Programming in Ada 2005" by John Barnes. I took all the stuff I used from there. For those interrested: This is the package specification of my Windows handler. It is based upon the X11ada library by Mitch Gart, with some modifications by Srini.

-- Package X11Windows
with X;
with X.Xlib;
with Interfaces.C;
with Ada.Text_IO;

package X11Windows is

MAX_XSIZE : constant Integer := 1280;
MAX_YSIZE : constant Integer := 1024;

type FrameBuffer is
array (Interfaces.C.unsigned range <>,
Interfaces.C.unsigned range <>)
of Interfaces.C.unsigned_long;

protected type X11Window (XS, YS : Interfaces.C.unsigned) is
procedure OpenWindow;
procedure CloseWindow;
procedure ProcessEvents;
procedure RedrawWindow;
procedure DrawPixel (Xc, Yc : in Integer; Colour : in Integer); private
XSize : Interfaces.C.unsigned := XS;
YSize : Interfaces.C.unsigned := YS;
PixelMap : FrameBuffer (1 .. XS, 1 .. YS);
GCon : X.Xlib.GC;
Screen : Interfaces.C.int;
Display : aliased X.Xlib.XDisplay_access;
Win : aliased X.Window;
GCon_Vals : aliased X.Xlib.XGCValues;
Event : aliased X.Xlib.XEvent;
end X11Window;

end X11Windows;


The next sprint will focus on the development of the basic mathematic functions around vectors and matrices. Of course, if you're interrested in the full code of the package above, let me know...I'll send you the sources...

C-Ya,Marinko