News:

MASM32 SDK Description, downloads and other helpful links
MASM32.com New Forum Link
masmforum WebSite

Fractal rendering

Started by AeroASM, February 25, 2005, 07:16:24 PM

Previous topic - Next topic

AeroASM

Has anyone made a fractal renderer? I made one which renders the Mandelbrot and Julia fractal sets. Please could you post the code if you have, so we could possibly help each other to optimise? I have attached mine.

[attachment deleted by admin]

Mark_Larson


I am pretty sure I remember Raymond doing one a while back.

BIOS programmers do it fastest, hehe.  ;)

My Optimization webpage
htttp://www.website.masmforum.com/mark/index.htm

raymond

Quote from: Mark_Larson on February 26, 2005, 05:47:40 PM

I am pretty sure I remember Raymond doing one a while back.


I did but using the FPU. AeroASM  is using xmm instructions. I tried assembling his posted asm file with MASM32 but without success. I mainly wanted to compare its speed with my version which displays the basic 640x480 Mandelbrot in less than 0.25 second on a P3-550.

I will be posting the executable on my site within the next week for anybody who may be interested. I do not intend to post the entire 9000-line source file but will be ready to discuss some of the code for the various features of the program.

Raymond
When you assume something, you risk being wrong half the time
http://www.ray.masmcode.com

AeroASM

How come it is 9000 lines?

One of the reasons you might not be able to assemble it is that you might be using ML 6.14, which doesn't support SSE2.

roticv

Raymond,

Why don't you use ml 7.0?

MichaelW

ML 6.15 can assemble it OK, but it will not run on my P3 :(

eschew obfuscation

raymond

Quote from: AeroASM on February 27, 2005, 07:50:12 AM
How come it is 9000 lines?

You will probably understand when you run the program.

Raymond
When you assume something, you risk being wrong half the time
http://www.ray.masmcode.com

Tedd

Here's one I made earlier - if it's any use. It's in java (someone shoot me now :bdg) but it should be understandable.


    // x = left co-ord
    // y = top co-ord
    // s = size (in co-ord space)
    // p = size (in pixels)
    // maxdepth = maximum depth :D (accuracy control)

    private byte[] mandel(double x, double y, double s, int p, int MAXDEPTH) {
        byte[] mouthful = new byte[p*p];
        double h = s/p; double v = s/p;
        for (int py=0; py<p; py++) {
            for (int px=0; px<p; px++) {
                int count = 0;
                boolean separate = false;
                double Cr = px*h+x; double Ci = py*v+y;
                double Zr = 0, Zi = 0;
                double rr = 0, ii = 0;
                while ((count<MAXDEPTH) && !separate) {
                    Zi = 2 * Zr * Zi + Ci;
                    Zr = rr - ii + Cr;
                    rr = Zr * Zr;
                    ii = Zi * Zi;
                    count++;
                    separate = (rr + ii > 4);
                }
                mouthful[py*p+px] = (byte)(count&255);
            }
        }
        return mouthful;
    }

No snowflake in an avalanche feels responsible.

raymond

I got the package ready a little sooner than expected. You can download my WMANJUL2 from the bottom of my complex number page at:

http://www.ray.masmcode.com/complex.html

Raymond
When you assume something, you risk being wrong half the time
http://www.ray.masmcode.com

AeroASM

Wow raymond, that is immense. Where did you find out about the different formula for the Julia set? (I only knew the z^2+k)
Also how do you write to bitmap? My maths beak wants some nice printouts.
Finally, how do you work out the colour for each point (I know that you base it on the number of iterations it takes for |z|>2 , but I would like to know how to work out the colour given the number of iterations.)

raymond

QuoteWhere did you find out about the different formula for the Julia set? (I only knew the z^2+k)

The only difference between a Julia set and a Mandelbrot set is that the "c" varies with each pixel in a Mandel fractal while the "c" is constant and user defined for the Julia fractals.

QuoteAlso how do you write to bitmap?
Finally, how do you work out the colour for each point

I create 8-bit DIBs and a color table of 256 32-bit colors.

      mov   bi.biSize,sizeof BITMAPINFOHEADER
      m2m   bi.biWidth,screenX
      mov   eax,screenY
      neg   eax
      mov   bi.biHeight,eax
      invoke CreateDIBSection,hDC,ADDR bi,DIB_RGB_COLORS,ADDR lpbmp,0,0
      mov   hDIB,eax
      invoke SelectObject,hCompDC,eax


The returned value at "lpbmp" is the address of the DIB array where I simply store the low byte of the number of iterations. I change any value of 0 to 1, retaining the value of 0 only for points which do not escape.  The color table is filled and then selected with:

invoke SetDIBColorTable,hCompDC,0,256,ADDR bmpcolors

Raymond
When you assume something, you risk being wrong half the time
http://www.ray.masmcode.com

AeroASM

#11
So you just store the number of iterations in the DIB, and Windows will look it up in the colour table automatically? Ingenious!

What I meant about the bitmap is how do you make a .bmp file out of a DIB?

Also, in your program there are many different functions for the Julia fractal, involving different powers of z and different combinations of sin and cos.

raymond

Iteration of the z^2+c formula is the very basic formula popularized by Mandelbrot. Any other similar formula is dubbed a Mandelbrot type formula. The same goes with the associated Julia type fractals where you would maintain the "c" portion constant.

As for preparing a .bmp file, you will have to become familiar with the layout of such files. First the BITMAPINFOHEADER, then an array of 32-bit colors (when needed as in this case), followed by the color "bits" which you have in the DIB. You write each section to an open file and then close the file.

Raymond
When you assume something, you risk being wrong half the time
http://www.ray.masmcode.com