Archive for the ‘Programming’ tag
offsetof macro in C
I just discovered this macro available in C: offsetof. Here is an simple example:
struct { char a; int b; char c; } example; struct example s1; unsigned int offset; offset = (unsigned int)(&(((example *)(0))->b));
Thanks to offsetof (in the header stddef.h), the last line can be rewritten in:
unsigned int offset; offset = offsetof(example, b);
(GeeXLab) Bump Mapping et Self Shadow
Vertex Displacement Mapping in GLSL Now Available on Radeon!
As I said in this news, the release of Catalyst 8.10 BETA comes with a nice bugfix: vertex texture fetching is now operational on Radeon (at least on my Radeon HD 4850). From 2 or 3 months, Catalyst makes it possible to fetch texture from inside a vertex shader. You can see with GPU Caps Viewer how many texture units are exposed in a vertex shader for your Radeon:

But so far, vertex texture fetching in GLSL didn’t work due to a bug in the driver. But now this is an old story, since VTF works well. For more details about vertex displacement mapping, you can read this rather old (2 years!) tutorial: Vertex Displacement Mapping using GLSL.
This very cool news makes me want to create a new benchmark based on VTF!

I’ve only tested the XP version of Catalyst 8.10. If someone has tested the Vista version, feel free to post a comment…
Next step for ATI driver team: enable geometry texture fetching: allows texture fetching inside a geometry shader…
See you soon!
[English]Gamma Correction[/English][French]Correction Gamma[/French]
[English]
I’ve coded today a small gamma correction filter in Smode. I’ll talk about gamma correction a little bit more very soon with a Demoniak3D demo. Here is the result on a simple scene: a teapot lit with a Phong shader.
The following image shows the rendering of the scene done in the usual manner, I mean without gamma correction:

and now the same scene gamma-corrected (factor 2.2):

[/English]
[French]
Je suis amusé aujourd’hui à coder un petit filtre de correction gamma dans Smode. Je parlerai de la correction gamma un peu plus en détail très prochainement avec une petite demo Demoniak3D. Mais en attendant voilà le résultat sur une scène simple: une teapot éclairée avec un shader de Phong.
L’image suivante montre le rendu de la scène faite de manière classique c’est à dire sans correction gamma:

et voici le rendu avec la correction gamma (facteur gamma de 2.2):

[/French]
[English]Multithreaded Build with Visual Studio 2005[/English][French]Build Multithreadé avec Visual Studio 2005[/French]
[English]
Under Visual Studio 2005 (VC8), you can enable the multithreaded build of projects. This great feature makes it possible to use several CPUs to build your projects. This is a per-project setting and it’s done in command line: /MPx where x is the number of cores you want to use. Example: /MP2 to use 2 CPUs if you have (like me) a core2duo.
I’ve done some tests with Demoniak3D:
- Demoniak3D (default): Build Time 0:33
- Demoniak3D (/MP2): Build Time 0:15
Great boost in productivity of large projects!
[/English]
[French]
Sous Visual Studio 2005 (VC8) vous pouvez activer le build multithreadé des projets. Cette super fonctionnalité permet d’utiliser plusieurs CPUs pour builder vos projets. C’est un régalge par projet en ligne de commande: /MPx où x est le nombres de cores que vous voulez utiliser. Exemple: /MP2 pour utiliser 2 CPUs si vous avez (comme moi) un core2duo.
J’ai fait quelques tests avec Demoniak3D:
- Demoniak3D (default): Build Time 0:33
- Demoniak3D (/MP2): Build Time 0:15
Gros boost de productivité surtout pour les gros projets!
[/French]
[-source-]
Velvet Shader Preview
[French]
Un petit shader GLSL de velour (velvet en anglais) ça vous dit? Et bien en voilà un, tout du moins un aperçu de celui que je viens de coder pour les besoins d’une démo avec le logiciel Smode. Smode… Un pur soft pour produire de la démo. Et le truc cool c’est les démos que je fais avec Smode seront aussi disponible pour Demoniak3D. Ne cherchez pas Smode, il n’est pas disponible au public. Seules quelques rares personnes, très sévèrement sélectionnées ont la chance de s’amuser avec. Mais vous pouvez toujours m’envoyer un mail, on ne sait jamais…

Dès que la prochaine release de Demoniak3D, la 1.24.0 (le numéro de version sera peut être le 1.30.0 vu le nombre de modifs), je releaserai la démo du velour avec son beau shader GLSL. Et si je tarde un peu, n’hésitez pas à me poster un petit message pour me rappeler à l’ordre.
[/French]
[English]
Are you ready for a small velvet GLSL shader? Here’s one, at least a preview of the one I’ve just coded for a demo with Smode. Smode… a software dedicated to create… demos! And the cool thing, is that Smode demos will be also available for Demoniak3D. Don’t look for Smode, it’s not available for you, public… Only few people on this planet are enough lucky to play with. But if you really want to touch it, just drop me an email…

As soon as the next release of Demoniak3D, the 1.24.0 (or better the 1.30.0 because of the huge amount of changes), will be ok, I’ll put online the velvet demo with its nice GLSL shader. And if I’m late, don’t hesitate to post a small message to wake me up!
[/English]
GLSL float to RGBA8 encoder
Packing a [0-1] float value into a 4D vector where each component will be a 8-bits integer:
vec4 packFloatToVec4i(const float value)
{
const vec4 bitSh = vec4(256.0*256.0*256.0, 256.0*256.0, 256.0, 1.0);
const vec4 bitMsk = vec4(0.0, 1.0/256.0, 1.0/256.0, 1.0/256.0);
vec4 res = fract(value * bitSh);
res -= res.xxyz * bitMsk;
return res;
}
Unpacking a [0-1] float value from a 4D vector where each component was a 8-bits integer:
float unpackFloatFromVec4i(const vec4 value)
{
const vec4 bitSh = vec4(1.0/(256.0*256.0*256.0), 1.0/(256.0*256.0), 1.0/256.0, 1.0);
return(dot(value, bitSh));
}
Source of these codes: Gamedev forums
Better, smaller and faster random number generator
I found this cool random generator on rgba’s website. rgba is a wellknown demoscene group specialized in 4k prods. This random generator is used in their prods:
static unsigned int mirand = 1;
float sfrand( void )
{
unsigned int a;
mirand *= 16807;
a = (mirand&0x007fffff) | 0x40000000;
return( *((float*)&a) - 3.0f );
}
It produces values in the range [-1.0; 1.0].
You can find the making of this random gen HERE.
[English]Small Log System[/English][French]Petit Système de Log[/French]
[English]
Here is a small piece of code that can be useful if you need to quickly generate traces (or log) in your apps:
[/English]
[French]
Voilà un petit bout de code qui peut être utile si vous avez besoin de générer rapidement des traces (ou log) dans vos applications:
[/French]
class cLog
{
public:
cLog(char *logfile){};
static void trace(const char *s)
{ if(s) log << s << std::endl; };
static std::ofstream log;
};
std::ofstream cLog::log("c:\\app_log.txt");
[English]
Just use it as follows:
[/English]
[French]
Il suffit de l’utiliser de la manière suivante:
[/French]
cLog::trace("this is a log");
cLog::trace("this is a second trace");
Codage d’un Moteur de Ray-Tracing depuis Zéro en un Weekend
Voici un petit article sympa sur le codage d’un ray-traceur depuis zéro. Le code source du ray-traceur est fourni et est compréhensible. Donc si vous voulez vous plonger dans les entrailles d’un petit ray-traceur, c’est le moment.
L’article et le code source se trouvent ici: PixelMachine




