Author Topic: TLU library for retro effects  (Read 760 times)

Marcus

  • Administrator
  • Hero Member
  • *****
  • Posts: 549
    • View Profile
TLU library for retro effects
« on: December 20, 2014 »
After looking at John's textured tunnel demo I got inspired to write an extension for speeding such effects up.

TLU (Texture LookUp) is very simple. It let's you setup a pair of texture coordinates for every pixel in an image (usually the primary image/window). When you call the render function, the texture coordinates plus an offset is used. This can be used for generating many of the cool effects from the Amiga days.

The zip contains the extension and two examples. The first example is very basic, the second one is a "hires" version of John's tunnel running darned much faster than the original :) 
.\\\opz

Marcus

  • Administrator
  • Hero Member
  • *****
  • Posts: 549
    • View Profile
Re: TLU library for retro effects
« Reply #1 on: December 20, 2014 »
I don't think anyone but me and John are into writing extensions, but here's the C (tlu.dll) sourcecode for this one:

Code: [Select]
#include "nl.h"
#include <stdlib.h>

static Color *dst = 0;
static int dstW = 0;
static int dstH = 0;
SurfaceInfo *srcInfo = 0;
static Color *src = 0;
static int srcW = 0;
static int srcH = 0;

typedef struct Map {
int u;
int v;
unsigned char alpha;
} Map;

static Map *map = 0;

int wrap(int value, int max) {
while (value < 0) value += max;
while (value >= max) value -= max;
return value;
}

NL_EXPORT(setup) {
if (map) free(map);
dst = (Color *)get_pointer(NL_TARGET_SURFACE_PTR, 0);
dstW = get_integer(NL_TARGET_WIDTH, 0);
dstH = get_integer(NL_TARGET_HEIGHT, 0);
map = (Map *)malloc(sizeof(Map)*dstW*dstH);
}

NL_EXPORT(set_tex) {
int img;

img = PARAM_GET_INT("img");

srcInfo = (SurfaceInfo *)get_pointer(NL_SURFACE_PTR, img);
src = srcInfo->colorBuffer;
srcW = srcInfo->width;
srcH = srcInfo->height;
}

NL_EXPORT(set_pix) {
int x, y, u, v, a, offset;

if (map == 0) return;

x = PARAM_GET_INT("x");
y = PARAM_GET_INT("y");
if (x < 0 || x >= dstW || y < 0 || y >= dstH) return;
offset = y*dstW + x;
u = wrap(PARAM_GET_INT("u"), srcW);
v = wrap(PARAM_GET_INT("v"), srcH);
a = PARAM_GET_INT("a");
if (a < 0) a = 0;
if (a > 255) a = 255;
map[offset].u = u;
map[offset].v = v;
if (a == 255) {
map[offset].alpha = 128;
}
else {
map[offset].alpha = (unsigned char)(a/2);
}
}

NL_EXPORT(render) {
int uoffs, voffs;
int size, i;
Map *m;
Color *s;
int u, v;
ColorComponent r, g, b, a;
Color c;

if (dst == 0 || src == 0) return;

uoffs = PARAM_GET_INT("u");
voffs = PARAM_GET_INT("v");

wrap(uoffs, srcW);
wrap(voffs, srcH);

size = dstW*dstH;

s = dst;
m = map;
for (i = 0; i < size; i++, m++, s++) {
u = (m->u + uoffs)%srcW;
v = (m->v + voffs)%srcH;
c = src[v*srcW + u];
a = m->alpha;
if (a == 128) {
*s = c;
}
else {
COLOR_TO_RGB(c, r, g, b);
r = (r*a) >> 7;
g = (g*a) >> 7;
b = (b*a) >> 7;
*s = TO_RGB(r, g, b);
}
}
}

__declspec(dllexport) void nlTerminate() {
if (map) {
free(map);
map = 0;
}
}

.\\\opz

John

  • Sr. Member
  • ****
  • Posts: 362
    • View Profile
Re: TLU library for retro effects
« Reply #2 on: December 20, 2014 »
This is some good shit  O0

Edit   But can you make it possible to setup more than one lookup table and for different images? I need to setup one for the primary and one for another image. I want to make a spinning, textured ball bounce around in a tunnel  >:D  May also need to set the real alpha (transparency)  OR colorkey through the mapping, possible?
« Last Edit: December 20, 2014 by John »

Marcus

  • Administrator
  • Hero Member
  • *****
  • Posts: 549
    • View Profile
Re: TLU library for retro effects
« Reply #3 on: December 20, 2014 »
Yes. Soon.
.\\\opz

johnno56

  • Guest
Re: TLU library for retro effects
« Reply #4 on: December 20, 2014 »
I have absolutely no idea how the TLU library works, but work it does, and I am impressed! Well done!!

J

Marcus

  • Administrator
  • Hero Member
  • *****
  • Posts: 549
    • View Profile
Re: TLU library for retro effects
« Reply #5 on: December 21, 2014 »
I have absolutely no idea how the TLU library works, but work it does, and I am impressed! Well done!!

J

The library does very little, it maps pixels from one image to another using a lookup table. The difficult part is to setup this table for cool effects. For 3D stuff like the tunnel you use some minimal raytracing.
.\\\opz

johnno56

  • Guest
Re: TLU library for retro effects
« Reply #6 on: December 21, 2014 »
I'm just glad at least one of us knows what we are doing... The rest of us will continue to be amazed. Great effect.

J