OpenB3D ASCII Art

Post your FreeBASIC source, examples, tips and tricks here. Please don’t post code without including an explanation.
Post Reply
angros47
Posts: 2331
Joined: Jun 21, 2005 19:04

OpenB3D ASCII Art

Post by angros47 »

I found this shader, and I ported it to FreeBasic/OpenB3D:
https://www.shadertoy.com/view/lssGDj


The BASIC code (it requires the library OpenB3D)

Code: Select all

#include "openb3d.bi"

screenres 800,600,  32, , &h10002 

	
Graphics3d 800,600,32,1,1





var camera=createcamera(0)
cameraclscolor camera,0,0,255
'camerarange camera,3,10000

	var asciishader=LoadShader("ASCII", "ascii.vert", "ascii.frag")
	var asciiart=CreatePostFX(camera,1)
	AddRenderTarget(asciiart,0,1,0,8,1.0)
	PostFXBuffer(asciiart,0,0,1,0)
	PostFXShader(asciiart,0,asciishader)
	SetFloat2(asciishader, "screen", 640,480)

'var fx=camerassao(camera)
hideentity camera

'cameraclscolor camera,0,255,0
var cube=createcube()
'moveentity cube,0,0,7
entitycolor cube,255,0,0

var plane=createplane()'cube():scaleentity plane, 100,.1,100
moveentity plane,0,-1.4,15

var light=createlight()
moveentity light,10,10,-3
pointentity light,cube
moveentity camera,-2,0,-5

'createshadow cube

dim key as string
dim as integer bw, fullset
do

	key=inkey
        if key="a" then moveentity cube,0,0,1
        if key="z" then moveentity cube,0,0,-1

        if key="f" then fullset=1-fullset: setinteger asciishader, "fullset",fullset
        if key="b" then bw=1-bw: setinteger asciishader, "bw",bw


        if key=chr(255)+"H" then turnentity cube,1,0,0,0
        if key=chr(255)+"P" then turnentity cube,-1,0,0,0
        if key=chr(255)+"M" then turnentity cube,0,-1,0,0
        if key=chr(255)+"K" then turnentity cube,0,1,0,0
	updateworld 1
	renderworld
	flip
sleep 1
loop until key=chr(27)

ascii.vert:

Code: Select all

#version 130

varying vec3 Position;
varying vec2 TexCoord;


void main()
{
	TexCoord = gl_MultiTexCoord0.st;
	gl_Position = ftransform();
}
ascii.frag:

Code: Select all

// Port of Bitmap to ASCII (not really) fragment shader by movAX13h, September 2013

#version 130
uniform sampler2D tex0;

varying vec2 TexCoord;
uniform vec2 screen;
uniform int fullset;
uniform int bw;


float character(int n, vec2 p)
{
	p = floor(p*vec2(-4.0, 4.0) + 2.5);
    if (clamp(p.x, 0.0, 4.0) == p.x)
	{
        if (clamp(p.y, 0.0, 4.0) == p.y)	
		{
        	int a = int(round(p.x) + 5.0 * round(p.y));
			if (((n >> a) & 1) == 1) return 1.0;
		}	
    }
	return 0.0;
}


void main(){

	vec2 pix = TexCoord*screen;
	vec3 col = texture2D(tex0, floor(pix/8.0)*8.0/screen).rgb;
	float gray = 0.3 * col.r + 0.59 * col.g + 0.11 * col.b;

	int n =  4096;

	if (fullset==0){    
		// limited character set
		if (gray > 0.2) n = 65600;    // :
		if (gray > 0.3) n = 163153;   // *
		if (gray > 0.4) n = 15255086; // o 
		if (gray > 0.5) n = 13121101; // &
		if (gray > 0.6) n = 15252014; // 8
		if (gray > 0.7) n = 13195790; // @
		if (gray > 0.8) n = 11512810; // #
	}else{
		// full character set including A-Z and 0-9
		if (gray > 0.0233) n = 4096;
		if (gray > 0.0465) n = 131200;
		if (gray > 0.0698) n = 4329476;
		if (gray > 0.0930) n = 459200;
		if (gray > 0.1163) n = 4591748;
		if (gray > 0.1395) n = 12652620;
		if (gray > 0.1628) n = 14749828;
		if (gray > 0.1860) n = 18393220;
		if (gray > 0.2093) n = 15239300;
		if (gray > 0.2326) n = 17318431;
		if (gray > 0.2558) n = 32641156;
		if (gray > 0.2791) n = 18393412;
		if (gray > 0.3023) n = 18157905;
		if (gray > 0.3256) n = 17463428;
		if (gray > 0.3488) n = 14954572;
		if (gray > 0.3721) n = 13177118;
		if (gray > 0.3953) n = 6566222;
		if (gray > 0.4186) n = 16269839;
		if (gray > 0.4419) n = 18444881;
		if (gray > 0.4651) n = 18400814;
		if (gray > 0.4884) n = 33061392;
		if (gray > 0.5116) n = 15255086;
		if (gray > 0.5349) n = 32045584;
		if (gray > 0.5581) n = 18405034;
		if (gray > 0.5814) n = 15022158;
		if (gray > 0.6047) n = 15018318;
		if (gray > 0.6279) n = 16272942;
		if (gray > 0.6512) n = 18415153;
		if (gray > 0.6744) n = 32641183;
		if (gray > 0.6977) n = 32540207;
		if (gray > 0.7209) n = 18732593;
		if (gray > 0.7442) n = 18667121;
		if (gray > 0.7674) n = 16267326;
		if (gray > 0.7907) n = 32575775;
		if (gray > 0.8140) n = 15022414;
		if (gray > 0.8372) n = 15255537;
		if (gray > 0.8605) n = 32032318;
		if (gray > 0.8837) n = 32045617;
		if (gray > 0.9070) n = 33081316;
		if (gray > 0.9302) n = 32045630;
		if (gray > 0.9535) n = 33061407;
		if (gray > 0.9767) n = 11512810;
	}


	vec2 p = mod(pix/4.0, 2.0) - vec2(1.0);

	if (bw==1)
		col = vec3(character(n, p));
	else
		col = col*character(n, p);
	gl_FragColor = vec4(col,1.0);
}
hhr
Posts: 216
Joined: Nov 29, 2019 10:41

Re: OpenB3D ASCII Art

Post by hhr »

Where can I get OpenB3D?
A good library archive would be helpful.
Post Reply