Tuesday, November 4, 2008

Lord Alucard 2

Two objective for the next steps on developing:

  1. Understand more on shaders under directx. Until now i've used some simplex shaders from the Frank Luna's book. It's time to study, learn, and make more complex effect, understand how pixel shaders works, how to make it time-dependent for animation (i.e.: heat distorsion), and so on.
  2. Implement a new routine thats could write texts on screens using coloured font, aka bitmapped font. It's not easy.. with the previous version of directx there was the directDraw api that manage the 2D world. Now the DirectDraw is no more included on directx so i've to discover the easiest way to display a 2d text on screen using only 3d functions. There's some method that write some string on screen, and i've used to display the name of the planet, and the debug informations, but i need to write multicolored chars, and it seems that it's a hard work.

Anyway here you are my first experiment with pixel shaders.. a shader that give an (ugly) "cartoon style" to the displayed scene.
The behaviour is very simple: it takes the r,g and b components of the pixel, that are float in 0,1 range, multiply by 5, take only the int part, inserting so a "sampling error" due to low resolution of the sampler, then divide the result by 5 to obtain the result.



float4 SkyPS(float3 envTex : TEXCOORD0) : COLOR
{
float4 ret = texCUBE(EnvMapS, envTex);

ret.r = int(ret.r*5.0f)/5.0f;
ret.g = int(ret.g*5.0f)/5.0f;
ret.b = int(ret.b*5.0f)/5.0f;

return ret;
}





---

Ho due obiettivi per gli sviluppi futuri

  1. Capire bene come funzionano gli shader sotto directx. Per ora ho utilizzato alcuni semplici esempi presi dal libro di Frank Luna, ma e' tempo di studiare meglio la materia per cercare di creare qualcosa di piu' complesso, ad esempio come fare in modo che uno shader sia dipendente dal tempo che passa, per creare ad esempio gli effetti di "distorsione da calore".
  2. Implementare una nuova routine per mostrare delle stringhe sullo schermo che preveda l'utilizzo di font colorati e non solo monocromatici. Sembra sia un'impresa tutt'altro che facile: a quanto pare un tempo esisteva l'api DirectDraw che dava strumenti semplici per gestire il 2D (come le scritte) da mostrare. Ora pero' le DirectDraw sono state abbandonate e non sono nemmeno piu' presenti all'interno delle directx, col risultato che se si vuol mostrare qualcosa in 2D, come una stringa, occorre comunque passare attraverso il mondo 3D, e la faccenda sembra tutt'altro che banale. Fino ad oggi ho utilizzato alcune funzioni di sistema per mostrare del testo nello schermo (i nomi dei pianeti, le informazioni di debug..), ma purtroppo a quanto pare tali funzioni non prevedono la possibilita' di usare dei font bitmapped (multicolore per l'appunto), e quindi mi tocchera' far tutto da zero.

Ad ogni modo vi lascio col mio primo esperimento con un pixel shader. Uno che dorebbe dare una (brutta) rappresentazione dell'immagine come se si trattasse di un "cartone animato".
Il procedimento e' semplice: prende le componenti r,g,b di ciascun pixel, che sono numeri float nel range 0,1, le moltiplica per 5, ne prende solo la parte intera, introducendo cosi' una sorta di "errore di campionamento" dovuto a scarsa risoluzione del campionatore, e ridivide quanto ottenuto di nuovo per 5 per ottenere il risultato finale.



float4 SkyPS(float3 envTex : TEXCOORD0) : COLOR
{
float4 ret = texCUBE(EnvMapS, envTex);

ret.r = int(ret.r*5.0f)/5.0f;
ret.g = int(ret.g*5.0f)/5.0f;
ret.b = int(ret.b*5.0f)/5.0f;

return ret;
}




No comments: