Friday, April 10, 2009

opengl and demoscene

Demosceners are a small group of people who dedicate to making wonderful demos. Here is detailed information about them:

Recently, i began to learn how to write this kind of small demos. And i pick up OPENGL+GLSL to do the job.
The small demo i made is follow the idea of this website:
As the author said "it's lots of fun to invent as many formulas as you can"
i also felt the beauty of mathmatic from this demo.
Every thing is easy unless how to pass the float LUT table into the shader to calculate.
All the UV coordinate are floats, how can i save them into a texture.
After search on internet, i found an easy but may not be best way:
This article is about how to use FBO,VBO,PBO etc in opengl to do normal purpose calculation.
And i noticed i can use the 128bit opengl extend texture format. So the internal format is R32G32B32A32(X32Y32Z32W32,or S32T32Q32R32) 
I saved x,y coordinate into each pixel, but i only used R32G32,the others are waste.
uniform sampler2D tex;
uniform samplerRECT tex2;
uniform float     time;
void main()
vec4 uv =  texRECT( tex2, gl_TexCoord[0].xy );
vec4 co =  texture2D( tex, uv.xy+vec2(time));
gl_FragColor = co;
} ;
In this way i can implement nearly the same GLSL shader as the author mentioned
except i use samplerRECT,texRECT for the 128bit texture.
And you don't have to set texture coordinate into (0,0)-(1,1), keep the original size instead.

However i understand this is not the best way, the code is as easy as the author mentioned,
but the video memory is somehow waste a bit(I didn't use the Z,W at all).
A friend who is experienced with computer graphics write this two line's shader piece like this:
float x =  texture2D(tex1,float2(gl_TexCoord[0].x,0.0)); 
float y =  texture2D(tex1,float2(gl_TexCoord[0].x,1.0)); 
I realized i can create LUMINANCE format texture,use one pixel to save one float, the first half to
store X coordinates, the second half to store Y coordinates.
Some improvement may happen if i use PBO, i suppose.
However, either LUMINANCE or RECTANGLE_ARB is not supported by all the graphic cards.
NV3xNV4x, G7x, G8xATI
texture 2D, ATI/ARB_texture_float, LUMINANCEnonono
texture 2D, ATI/ARB_texture_float, RGB, RGBAnoyesyes
texture 2D, NV_float_buffer, LUMINANCEnonono
texture 2D, NV_float_buffer, RGB, RGBAnonono
texture RECT, ATI/ARB_texture_float, LUMINANCEnonono
texture RECT, ATI/ARB_texture_float, RGB, RGBAnoyesyes
texture RECT, NV_float_buffer, LUMINANCEyesyesno
texture RECT, NV_float_buffer, RGB, RGBAyesyesno

i attached two sources of small demo, one is use GLSL to implement simple multiple textures, one is LUT texture animation. The 1K/4Kframework is from
specially for compressed exe size and running efficient, so bear the coding style if you don't like it.
LUT example uses CRT malloc etc. functions so it doesn't compile in relese mode and is bigger than 4K. Anyway this is my first try on demoscene, i will do it better next time.



Post a Comment