Page 1 of 1

gfxbrot.c and gfxjulia.c

Posted: 12 Sep 2021, 10:35
by veekoo
The idea of drawing fractals grew. First I wanted to do simple julia. Ended up doing it with ascii. Julia can't be done without mandelbrot, so it did ascbrot.c and then ascjulia.c. Now I was suggested to use graphics.
lb3361 wrote: 12 Sep 2021, 03:03 Now with graphics?

Code: Select all

#include <gigatron/console.h>

void drawPixel(int x, int y, int color)
{
    screenMemory[y][x] = color;
}
Note: the code above assumes the screen has not been scrolled -- default videoTable.
Otherwise you can include <gigatron/console.h> and call console_clear_screen(void) to reset it.
All I needed to do minor changes to asc-version to get gfx-version.

Code: Select all

#include <gigatron/console.h>
#include <gigatron/sys.h>

void drawPixel(int x, int y, int color)
{
  screenMemory[y][x] = color;
}
Warning! Because it takes hours to run the fractals, I haven't yet finished testing.
The colors are pseudo colors...color=iteration

Code: Select all

/*----------------------------------------------------------------------+
 |                                                                      |
 |     gfxbrot.c -- demonstrate fractal in gfx / quick and dirty        |
 |                                                                      |
 +----------------------------------------------------------------------*/
 
// Standard includes
#include <limits.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <string.h>
#include <math.h>
#include <gigatron/console.h>
#include <gigatron/sys.h>

#define HEIGHT 120
#define WIDTH 160
#define SCALE 1.4
#define YSTEP 1
#define XSTEP 1

int mandelbrot(float x, float y) {
 float zz;
 float a;
 float b;
 float a2;
 float b2;
 float atemp;
 int i;
  
  a = 0;
  b = 0;  
  i = 0;
  while (i < 63)
  {
    a2 = a * a;
    b2 = b * b;
    zz = a2 + b2;
    if(zz > 32) break;
    
    atemp = a2 - b2 + x;
    b = 2.0 * a * b + y;
    a = atemp;
    i++;
  }
  if(x+y < 4) return i;

  return 0;
}

void drawPixel(int x, int y, int color)
{
  screenMemory[y][x] = color;
}

void main(void) {
  int x, y, data;
  int offset = 0;
  float sx, sy;
   
  for(y = 0; y < HEIGHT; y = y + YSTEP ) {
    for(x = 0; x < WIDTH; x = x + XSTEP ) {
      sx = -0.7 + SCALE * (WIDTH/2.0 - x) / (WIDTH/2.0);
      sy = SCALE * (HEIGHT/2.0 - y) / (HEIGHT/2.0);
      data = mandelbrot(sx, sy);
      offset = ( x + y * WIDTH);
      drawPixel(x,y,data);
    }
  }
}

Code: Select all

/*----------------------------------------------------------------------+
 |                                                                      |
 |      gfxjulia.c -- demonstrate fractal in gfx / quick and dirty      |
 |                                                                      |
 +----------------------------------------------------------------------*/
 
// Standard includes
#include <limits.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <string.h>
#include <math.h>
#include <gigatron/console.h>
#include <gigatron/sys.h>

#define HEIGHT 120
#define WIDTH 160
#define SCALE 1.7
#define YSTEP 1
#define XSTEP 1
#define CX -0.8
#define CY 0.156

int julia(float x, float y) {
 float zz;
 float a;
 float b;
 float a2;
 float b2;
 float atemp;
 int i;
  
  a = x;
  b = y;  
  i = 0;
  while (i < 63)
  {
    a2 = a * a;
    b2 = b * b;
    zz = a2 + b2;
    if(zz > 32) break;
    
    atemp = a2 - b2 + CX;
    b = 2.0 * a * b + CY;
    a = atemp;
    i++;
  }
  if(x+y < 4) return i;
  
  return 0;
}

void drawPixel(int x, int y, int color)
{
  screenMemory[y][x] = color;
}

void main(void) {
  int x, y, data;
  int offset = 0;
  float sx, sy;
   
  for(y = 0; y < HEIGHT; y = y + YSTEP ) {
    for(x = 0; x < WIDTH; x = x + XSTEP ) {
      sx = SCALE * (WIDTH/2.0 - x) / (WIDTH/2.0);
      sy = SCALE * (HEIGHT/2.0 - y) / (HEIGHT/2.0);
      data = julia(sx, sy);
      offset = ( x + y * WIDTH);
      drawPixel(x,y,data);
    }
  }
}
I will add screenshots when I finish testing.
Development future is in github / veekooFIN

Re: gfxbrot.c and gfxjulia.c

Posted: 12 Sep 2021, 11:11
by lb3361
Cool.

I am afraid floating point on the Gigatron is not very fast. This is why official 'Mandelbrot' program uses fixed point. But that makes it very hard to follow. Anyway, thanks for giving such a good test to my floating point runtime.

Re: gfxbrot.c and gfxjulia.c

Posted: 12 Sep 2021, 14:15
by veekoo
Indeed slow stuff - gfxbrot - took 4,5 hours to calculate

Re: gfxbrot.c and gfxjulia.c

Posted: 12 Sep 2021, 14:33
by at67
veekoo wrote: 12 Sep 2021, 14:15 Indeed slow stuff - gfxbrot - took 4,5 hours to calculate
Looks good, try changing modes, (use Mode 3), to get about a 250% speedup compared to mode 0.

P.S. Mandlebrot in gtBASIC ROMvX0 in mode 3 using fixed point and symmetry to cut calculations in half takes just over 60seconds.
FastMandlebrot.JPG
FastMandlebrot.JPG (326.87 KiB) Viewed 220 times

Re: gfxbrot.c and gfxjulia.c

Posted: 12 Sep 2021, 17:16
by veekoo
gfxjulia - took 3 hours to calculate

I always with these apps use fast mode during calculations and afterwards when viewing the screen I switch to slow mode.
I think using less colors and iterations this could be made faster plus in code there might be something to optimize. Maybe a grey scale version? What color numbers those might be?

Re: gfxbrot.c and gfxjulia.c

Posted: 13 Sep 2021, 11:51
by lb3361
The graphics version has a much smaller gt1 because it doesn't need to link stdio or console support.

Re: gfxbrot.c and gfxjulia.c

Posted: 13 Sep 2021, 12:59
by at67
veekoo wrote: 12 Sep 2021, 17:16 I think using less colors and iterations this could be made faster plus in code there might be something to optimize. Maybe a grey scale version? What color numbers those might be?
Iterations will help with speed but will lose fidelity on the boundaries, (the interesting bits), I use 16 iterations and map those to a LUT filled with 16 interesting colours.

The Gigatron only has 2bits per colour component, (i.e. RGB), so that is a maximum of 4 grey scales, (including black and white). You could use dithering to sacrifice spatial resolution for colour/grey scale resolution and simulate more grey scales, but it would probably look much worse given how big the Gigatron's pixels already are, (only one way to find out though).

Re: gfxbrot.c and gfxjulia.c

Posted: 14 Sep 2021, 19:01
by veekoo
This is current state of coding. Pictures added.

Re: gfxbrot.c and gfxjulia.c

Posted: 17 Sep 2021, 19:00
by veekoo
Latest sources and executables can be found at:

https://github.com/veekooFIN/GigatronTTL/tree/master