2010年6月11日 星期五

Plasma Effect--Adobe Alchemy初體驗



本來看過Adobe Labs的Alchemy:Documentation:Getting Started"落落長"的內容有點猶豫不前,
想不到Adobe Alchemy在Ubuntu一下子就裝好了,接著搜尋到一些超炫Plasma Effect想說實驗看看

這裡有C語言實作的詳解,還有這篇Alchemy plasma experiment文章也有Alchemy實作的原始碼

以下是我修改後的C原始碼


#include <stdio.h>
#include <math.h>
#include "AS3.h"

int* palette;
int* plasma;
int* newPlasma;
int width;
int height;

AS3_Val generatePlasma(void* self, AS3_Val args)
{


AS3_ArrayValue(args, "IntType, IntType", &width, &height);

palette = malloc(256 * sizeof(int));
plasma = malloc(width * height * sizeof(int));
newPlasma = malloc(width * height * sizeof(int));

genPalette();
plasma_pattern();
return AS3_Ptr( plasma );
}


AS3_Val shiftPlasma(void* self, AS3_Val args)
{
int shift, x, y, index, paletteIndex;
AS3_ArrayValue(args, "IntType", &shift);

for(x=0; x<width; x++)
{
for(y=0; y<height; y++)
{
paletteIndex = (unsigned int)(plasma[index] + shift) % 256;
newPlasma[index] = palette[paletteIndex];
index++;
}
}

return AS3_Ptr(newPlasma);
}


void genPaletteHSV()
{
int x;
for(x=0; x<256; x++)
{
palette[x] = HSVtoRGB(x, 255, 255);
}
}

void plasma_pattern()
{
int x, y, index;
for(x=0; x<width; x++)
{
for(y=0; y<height; y++)
{
int color = (
128.0 + ( 128.0 * sin( x / 16.0 ) ) +
128.0 + ( 128.0 * sin( y / 8.0 ) ) +
128.0 + ( 128.0 * sin( ( x + y ) / 16.0 ) ) +
128.0 + ( 128.0 * sin( sqrt( x * x + y * y ) / 8.0 ) )
) / 4;
plasma[index++] = 0xff << 24 | color;
}
}
}

int main()
{
AS3_Val generatePlasmaMethod = AS3_Function( NULL, generatePlasma );
AS3_Val shiftPlasmaMethod = AS3_Function( NULL, shiftPlasma );
AS3_Val result = AS3_Object(
"generatePlasma: AS3ValType,shiftPlasma: AS3ValType",
generatePlasmaMethod, shiftPlasmaMethod
);
AS3_Release( generatePlasmaMethod );
AS3_Release( shiftPlasmaMethod );
AS3_LibInit( result );

return 0;
}




效果很炫
CPU也不怎麼操
看來書櫃裡塵封已久的C語言參考書將重見天日了

沒有留言:

張貼留言