Shop OBEX P1 Docs P2 Docs Learn Events
VGA - Demo: Beauty of fractals — Parallax Forums

VGA - Demo: Beauty of fractals

This demo is for vga, connect the A/V Board to pin 48.
Alternating 2 known fractals are calculated and drawn in real time.

Have fun!
Reinhard

Comments

  • That's a very cool demo!
  • Wish I could see it. In a motel without a monitor. :(
  • roglohrogloh Posts: 5,171
    edited 2020-07-15 05:24
    Nice fern Reinhard, though we need it in a suitable green colour. :wink:

    In monochrome you could go higher resolutions too. 1024x768 or up to 1920x1080 might look even more detailed...

    I think it would be good to get a graphics library working soon, ideally one that works with both HUB based and external memory frame buffers with HyperRAM which is now imminent too. It would be handy if it was done in SPIN2 and then could work for both FastSPIN and official PNUT based SPIN2.

    One benefit of using it in Fastspin would be the graphics library itself could grow to be quite substantial with heaps of useful drawing functions but it would only actually take up image space if the routines were called. Without dead code removal, using this library in Chip's SPIN2 version could mean it unfortunately grows rather large, unless @cgracey plans to ultimately support dead code removal. If he doesn't want that then it might then need to be broken into different smaller modules somehow, which can get a bit more tedious to manage and doesn't buy as much code savings unless every method is its own object.
  • roglohrogloh Posts: 5,171
    edited 2020-07-15 08:00
    Reinhard, here's how you can tweak your demo code to support larger dimensions and fit in HUB RAM. It's doing mono 1920x1080 1bpp with a green on black palette combo. I just changed the Setup_Video code, array dimensions and the pixel plot and screen clear routines to do 1bpp instead of 8bpp. The mono frame buffer now has more space for drawing much larger images but only takes up ~254 kbytes. The P2 will be clocked at 297MHz, up from 252MHz.
    /*
    +--------------------------------------------------------------------
    | TERMS OF USE: MIT License
    +--------------------------------------------------------------------
    Permission is hereby granted, free of charge, to any person obtaining
    a copy of this software and associated documentation files
    (the "Software"), to deal in the Software without restriction,
    including without limitation the rights to use, copy, modify, merge,
    publish, distribute, sublicense, and/or sell copies of the Software,
    and to permit persons to whom the Software is furnished to do so,
    subject to the following conditions:
    
    The above copyright notice and this permission notice shall be
    included in all copies or substantial portions of the Software.
    
    THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
    EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
    MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
    IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
    CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
    TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
    SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
    +--------------------------------------------------------------------
    */
    
    
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    #include <unistd.h>
    
    #include <propeller2.h>
    #define P2_TARGET_MHZ 160
    #include "sys/p2es_clock.h"
    
    #define BAUD 230400
    
    // screen size
    #define SCRN_WIDTH 1920
    #define SCRN_HEIGHT 1080
    
    // VGA definitions
    #define VGA_BASE_PIN 48
    #define VGA_VSYNC_PIN (VGA_BASE_PIN + 4)
    
    // enough room for a scanline at full colour bit depth
    #define LINEBUFSIZE (1920*4)
    
    // the video object
    struct __using("video/p2videodrv.spin2") vid;
    
    // space for video driver display and region info
    int display1[14];
    int first[14];
    
    // the actual frame and line buffer
    char frameBuffer[SCRN_WIDTH*SCRN_HEIGHT/8];
    char lineBuffer[LINEBUFSIZE*2];
    int palette[2] = {0,0x00ff0000};
    
    
    void Setup_Video()
    {
        int timing = vid.getTiming(vid.RES_1920x1080);
    
        vid.initDisplay(&display1, vid.VGA, VGA_BASE_PIN, VGA_VSYNC_PIN, vid.RGBHV, &lineBuffer, LINEBUFSIZE, timing);
    
        vid.initRegion(&first, vid.LUT1, SCRN_HEIGHT, 0, &palette[0], 0, 8, &frameBuffer[0], 0);
      
        // enable display list
        vid.setDisplayRegions(display1, first);
    }
    
    void line( int x0, int y0, int x1, int y1,int color)
    {
      int dx =  abs(x1-x0), sx = x0<x1 ? 1 : -1;
      int dy = -abs(y1-y0), sy = y0<y1 ? 1 : -1;
      int err = dx+dy, e2; /* error value e_xy */
    
      for(;;){  /* loop */
        plot(x0,y0,color);
        if (x0==x1 && y0==y1) break;
        e2 = 2*err;
        if (e2 > dy) { err += dy; x0 += sx; } /* e_xy+e_x > 0 */
        if (e2 < dx) { err += dx; y0 += sy; } /* e_xy+e_y < 0 */
      }
    }
    
    // routine to plot a point at screen coordinate (x,y) and color "color"
    void plot(int x, int y, int color)
    {
        if (color)
    	    frameBuffer[y*SCRN_WIDTH/8+x/8] |= 1<<(x&7);
        else
    	    frameBuffer[y*SCRN_WIDTH/8+x/8] &= ~(1<<(x&7));
    }
    
    void clearscreen ()
    {
    	// clear the screen
        memset(frameBuffer, 0, SCRN_WIDTH*SCRN_HEIGHT/8);	
    }
    
    void sierpinski ()
    {
        int x,y,z;
        
        for(y = 0 ; y <= 255; y++)
        {
    		for(x = 0; x <= y; x++)
    		{
    			z = x & (y-x);
    			if(z == 0)
    				plot(x + 320 - y/2, y + 30, 0b111_11111);
    		}
    	}	
    }
    
    void fern ()
    {
    	int imax = 5000;
    	float left = (30.0);
    	float w = (300.0);
    	float w1 = w + left;
    	float e1 = (0.5 * w);
    	float e2 = (0.57 * w);
    	float e3 = (0.408 * w);
    	float e4 = (0.1075 * w);
    	float f1 = (0 * w);
    	float f2 = (-0.036 * w);
    	float f3 = (0.0893 * w);
    	float f4 = (0.27 * w);
    	
    	float x = (e1);
    	float y = 0;
    	float xn,yn;
    	
    	for (int i = 1; i < imax; i++)
    	{
    		int z = (rand() & 0x7FFF);
    		
    		if (z > 164) goto _100;
    			xn = (0 * x +  0 * y    + e1);
    			yn = (0 * x +  0.27 * y + f1);
    			goto _400;
    		
    		_100: if(z > 5570) goto _200;
    			xn = (-0.139 * x +  0.263 * y + e2);
    			yn = (0.246 * x  + 0.224  * y + f2);
    			goto _400;		
    		
    		_200: if(z > 9830) goto _300;
    			xn = (0.17 * x -  0.215 * y + e3);
    			yn = (0.222 * x + 0.176 * y + f3);
    			goto _400;
    					
    		_300: 
    		xn = (0.781 * x +  0.034 * y + e4);
    		yn = (-0.032 * x + 0.739 * y + f4);
    		
    		_400:
    		plot((int)(xn+left),(int)(w1-yn),0b111_11111);
    		//printf("%x %x %f %f \n",(z),RAND_MAX,(xn),(yn));
    		x = xn;
    		y = yn;
    	}
    }
    
    void main()
    {
    	
        _clkset(_SETFREQ, _CLOCKFREQ);
        Setup_Video();
        _setbaud(BAUD);
    
    LOOP: 
        sleep(1);	
        clearscreen();
    	sierpinski();
    	
    	sleep(1);
    	clearscreen();
        fern();
        
        goto LOOP;
        	
    }
    
  • rogloh wrote: »
    Nice fern Reinhard, though we need it in a suitable green colour. :wink:
    In monochrome you could go higher resolutions too. 1024x768 or up to 1920x1080 might look even more detailed...
    :wink: this is true.
    I do a few cosmetic changes in sierpinsky.c
    Thank you for the modified version with high resolution, I named it fractals.c

    I hope to find time soon to do more experiments with higher resolution.

    best regards
    Reinhard

  • By the way, all examples are derived from BASIC programs (goto:wink:)
    From the books
    Fractals for the Classroom Part1 & 2 (1992) by Peitgen,Jürgens and Saupe
  • Yes I was wondering about all those gotos in that C code for the fern but didn't mention that.

    It would be nice to make the images fill the entire screen space too. I guess the code was probably designed for lower resolution initially but scaling it up could look nicer.
  • Reinhard wrote: »
    By the way, all examples are derived from BASIC programs (goto:wink:)
    From the books
    Fractals for the Classroom Part1 & 2 (1992) by Peitgen,Jürgens and Saupe

    You could have just compiled the BASIC programs with fastspin :).
Sign In or Register to comment.