gfxbrot.c and gfxjulia.c

Using, learning, programming and modding the Gigatron and anything related.
Forum rules
Be nice. No drama.
Post Reply
veekoo
Posts: 26
Joined: 07 Jun 2021, 07:07

gfxbrot.c and gfxjulia.c

Post 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
Attachments
gfxjulia.gt1
(3 KiB) Downloaded 48 times
gfxbrot.gt1
(3.03 KiB) Downloaded 45 times
Last edited by veekoo on 13 Sep 2021, 06:46, edited 1 time in total.
lb3361
Posts: 107
Joined: 17 Feb 2021, 23:07

Re: gfxbrot.c and gfxjulia.c

Post 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.
veekoo
Posts: 26
Joined: 07 Jun 2021, 07:07

Re: gfxbrot.c and gfxjulia.c

Post by veekoo »

Indeed slow stuff - gfxbrot - took 4,5 hours to calculate
Attachments
IMG_0921.jpg
IMG_0921.jpg (3.02 MiB) Viewed 1442 times
at67
Posts: 431
Joined: 14 May 2018, 08:29

Re: gfxbrot.c and gfxjulia.c

Post 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 1439 times
veekoo
Posts: 26
Joined: 07 Jun 2021, 07:07

Re: gfxbrot.c and gfxjulia.c

Post 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?
Attachments
IMG_0923.JPG
IMG_0923.JPG (2.74 MiB) Viewed 1427 times
lb3361
Posts: 107
Joined: 17 Feb 2021, 23:07

Re: gfxbrot.c and gfxjulia.c

Post by lb3361 »

The graphics version has a much smaller gt1 because it doesn't need to link stdio or console support.
at67
Posts: 431
Joined: 14 May 2018, 08:29

Re: gfxbrot.c and gfxjulia.c

Post 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).
veekoo
Posts: 26
Joined: 07 Jun 2021, 07:07

Re: gfxbrot.c and gfxjulia.c

Post by veekoo »

This is current state of coding. Pictures added.
Attachments
gfxjulia.jpg
gfxjulia.jpg (35.54 KiB) Viewed 1302 times
gfxbrot.jpg
gfxbrot.jpg (39.55 KiB) Viewed 1302 times
veekoo
Posts: 26
Joined: 07 Jun 2021, 07:07

Re: gfxbrot.c and gfxjulia.c

Post by veekoo »

Latest sources and executables can be found at:

https://github.com/veekooFIN/
Post Reply