Eraser85’s Corner

Replace me with something cool already said by someone else ;)

  • Pages

  •  

    July 2009
    M T W T F S S
    « Jan    
     12345
    6789101112
    13141516171819
    20212223242526
    2728293031  
  • My Flickr

    First boot

    GTX285 with Core i7 920

    Intel Core i7 on Asus P6T

    Panoramic view of Frankfurt am Main

    More Photos

How to count total lines of code in VS.NET

Posted by eraser85 on January 19, 2009

I’ve found this little neat string that can be used in the VS.NET integrated Find and gives your current project / solution / file total line count:

^~(:Wh@//.+)~(:Wh@\{:Wh@)~(:Wh@\}:Wh@)~(:Wh@/#).+

To use it, just press CTRL+SHIFT+F, then select Use Regular Expressions and finally write the file extensions in which you want to count the lines of code. :)

The results will be displayed at the Find result window’s end.

Thanks go to Germán Schuager for the tip.

Posted in General Programming | Tagged: , , , | Leave a Comment »

Multiple OpenGL rendering contexts

Posted by eraser85 on July 16, 2008

Hi all. Currently I’m working on a software which “should” have different OpenGL areas, each drawn in a different window or a more generic drawable surface (a panel, a group box, etc.. anything that has a window handle in practice).

But what I’m facing is demotivating me :(

From what I’m reading, if you loose an OpenGL context, you also loose anything associated with it (textures, state changes, etc..):

This means that all the GL state is destroyed with it. This includes textures, among other things. More precisely, on Windows the textures are corrupted, whereas Linux handles it correctly.

Actually, in my application, I have different contexts and simply switch between them thanks to the wglMakeCurrent and everything works fine as long as I just render polygons with glColor.

But as I try to apply a texture (loaded with SDL_Image) nothing happens. So I tried to dump the texture raw data to a file and open it with PhotoShop but the resulting image is completely gray (205/255) :|

I thought it could be the loosing-context problem mentioned above, so I tried to dump the texture as soon as I upload the data in it with glTexImage2D but I got the same gray image as before. Just to be sure I checked that SDL_Image was loading the texture correctly and that was the case…

I still have to check a couple of things… If you have some tips, please share them with me!

Bye

Posted in OpenGL | Tagged: , , , | 4 Comments »

My brand new Nikon D60

Posted by eraser85 on May 17, 2008

Hi there,

a few days ago I finally decided to get a reflex digital SLR. At first, I was undecided between Canon 400D and Nikon D40x. After reading online reviews, I ended up choosing the latter.

One of the “features” I most appreciated from the D40x was the grain noise at high ISOs. While the D40x produced a slightly noisier image, this kind of noise is almost monochromatic, like using an high sensitive film on a classic SLR; on the other hand, the 400D produces a more “colorfNoise Comparisonul” noise.

One day I was passing by a shop nearby my house and saw the D40x at a very competitive price (compared to several online shops), so I asked if it was possible to not get the 18-55mm kit and instead getting just the body with the nice 18-135mm lens. The main reason I didn’t wanted the kit was the lack of image stabilization.

After a bit of talks, the shopkeeper asked me if I was interested in the newer Nikon D60, which was due to arrive in his shop in a couple of days. The D60 kit includes two stabilized lenses, an AF-S 18-55mm VR and an AF-S 55-200mm VR ED. Intrigued for both the price AND the lenses, I decided to wait for the D60 so I could take a closer look at it.

While I was waiting for the camera to arrive, I read a couple of online reviews, expecially the one from dpreview.com, which helped me a lot deciding for what I was going to buy.

Infact, as the guy from the shop told me the camera was arrived, I went there almost convinced to take it. Once there, he explained me that the camera body had three-years warranty, while each lens had an extraordinary 4-years warranty! Amazed by the build quality, the body’ size and weight.. I ended up buying it :) .

In the days following I started enjoying the camera and, even beeing the very first time for me using a reflex, I was surprised by the results. You can see them too in my flickr page (I also bought the pro upgrade in flickr, so no more limitations with sets, image sizes, etc..).

I hope to rapidly increase my shooting expertise, I really like taking photos :)

Bye!

Posted in Personal | Tagged: , , , | Leave a Comment »

The Wild Italy Expo’ 2008

Posted by eraser85 on May 11, 2008

Hi there!

New photos have been added to my flickr.com account. This time are from the Wild Italy Expo’ 2008 edition. Hope you will enjoy them :) Bye!

PS: here’s the link to the set

Posted in Personal | Tagged: , , , , , , , , | Leave a Comment »

Input / Output with GPUs

Posted by eraser85 on May 1, 2008

It has been a while since my last blog post. Anyway, talking with Junskyman about the Translucent Shadow Map technique, a problem aroused: how do the GPU handles data within textures?

Before dealing with this, it might be better starting with the basics: let’s then talk about Input / Output with GPUs.

When you define a texture, you also specify it’s Format, Internal Format and expecially it’s type:

glTexImage2D( target, level, iFormat, w, h, border, format, type, *data );

with the InternalFormat you basically define how many channels are being used in that texture, while format specifies the format of the input data defined in *data.

Type, however, tells the OpenGL pipeline how the data associated with the texture is to be treated.

From now on one could decide to work exclusively on GPU thanks to shaders, or instead allowing the CPU to handle some of the operations.

As for the CPU/GPU approach, usually getting data from GPU is done by glReadPixels(). It copies the texture data located in the VRam into an array. Pay attention that if the array’s type differs from texture’s type, will result in artifacts and wrong data being read. Usually, with floating point textures, there are no restrictions in the output values (they can be both positive or negative).

Integer texture type, instead, forces data to be converted from floating point (if that’s the case with original data stored in VRam) to Unsigned Integer. Negative or greater than 8bit values are simply clamped in 0 … 2^8 range.

The same thing applies with data processed using the shaders-only approach, but in this case are also normalized in -1.0 … 1.0 range. But, let’s say we want to transfer negative data using integer textures. In this case, we simply have to map the original values according to the following equation:

vec4 convData = origData * 0.5 + 0.5;

A weird thing happened while using RenderMonkey, however. In RM, even if you set texture type to GL_RGBA32F, assigning negative data results in clamping. Therefore, compacting data in 0.0 … 1.0 range and then scale it back to -1.0 … 1.0 range is mandatory.

In my past tutorial I made some changes to the source code, removing this conversion since it should not be needed in a real world application.

At the end, my advice is simple though: always pay attention to the range of values your data have at any time, and use fewer conversions as possible, since it always introduces errors.

Posted in OpenGL | Tagged: , , , , | Leave a Comment »

Make it translucid! (Part Three)

Posted by eraser85 on April 4, 2008

Finally the third and final part of this tutorial.

In the first one we focused on the TSM creation, then in the second one we saw the translucency’s multiple scattering contribution. In this final part, we are going to see how to filter the TSM.

The main idea with TSM is that the more the object is “thick”, consequently the less the light will be able to pass through it. Another important idea with TSM is that since light (once penetrated the material) scatters in pseudo-random direction, can leave the object from a point that do not coincides with the point from where the light entered.

In order to simulate the first behaviour, one can simply read the Z coordinate from one vertex in light space (we can call it Xin) and then compare with the Z from another point (taken by the camera, but then projected in light space – called Xout). Obviously, Xout have to be on the same line that ideally connects the light source with Xin: this means Xout.XY must be equal to Xin.XY. Then, the only difference between those two points will lie in the Z value: by arbitrarily choosing Xout and projecting it in light space, Xin is simply taken by reading the content of the TSM in Xout.XY!

TSM thickness idea

That’s what basically has been discussed in the second part. The thickness is computed and used to modulate light intensity in the Rd function: the latter however does not take into account the scattering: it’s like the light enters one point, then it simply “fly” through the object in a straight line, and finally leaves it exactly in the same direction as when entered the material.

This problem is solved in this final part (thus implementing the second idea of TSM) by simulating pseudo-random scattering through filtering. Filtering an image basically consists in taking the whole image and then apply some sort of algorithm to produce the final result. In this case we need to simulate the scattering: simplifying this behaviour in order to make the algorithm real-time friendly is done by taking into account not only the point where the light enters or leaves, but also its neighbours. The more the neighbour are far from the original point, the less they will contribute to the final result.

One can implement filtering by following any schema, even though I’ve found the one from the Dachsbacher’s 2003 paper being of high quality (and a little heavy on the GPU :P ). A graphical representation can be seen in the following image, consisting in a 21-samples schema:

TSM Filtering Schema

The code used to implement the filtering is just too long to be posted here, so I decided to post the most important parts of it.

Filtering the TSM

vec4 filter21 (vec4 Xout)

{

   const float d = 1.0/1024.0;

   vec4 finalColor = vec4(0.0,0.0,0.0,0.0);

   vec4 Xin;

   //

   float v0 = tsm_smoothness*1.0;

   float v1 = tsm_smoothness*2.0;

   float v2 = tsm_smoothness*3.0;

   float k0 = 0.05556; // 1.0/18.0

   float k1 = 0.04167; // 1.0/24.0

   float k2 = 0.04167; // 1.0/24.0

   //

   Xin = Xout;

   finalColor = multipleScattering(Xin,Xout, v0)*k0;

   Xin.y = Xout.y + d;

   Xin.x = Xout.x;

   finalColor += multipleScattering(Xin,Xout, v0)*k0;

   Xin.y = Xout.y + d + d;

   Xin.x = Xout.x;

   //

   [...]

   //

   Xin.y = Xout.y + 1.5*d;

   Xin.x = Xout.x – 1.5*d;

   finalColor += multipleScattering(Xin,Xout, v1)*k1;

   Xin.y = Xout.y + 1.5*d;

   Xin.x = Xout.x + 1.5*d;

   finalColor += multipleScattering(Xin,Xout, v1)*k1;

   //

   [...]

   //

   Xin.y = Xout.y + 5.0*d;

   Xin.x = Xout.x;

   finalColor += multipleScattering(Xin,Xout, v2)*k2;

   Xin.y = Xout.y;

   Xin.x = Xout.x + 5.0*d;

   finalColor += multipleScattering(Xin,Xout, v2)*k2;

   Xin.y = Xout.y – 5.0*d;

   Xin.x = Xout.x;

   finalColor += multipleScattering(Xin,Xout, v2)*k2;

   Xin.y = Xout.y;

   Xin.x = Xout.x – 5.0*d;

   finalColor += multipleScattering(Xin,Xout, v2)*k2;

   //

   return finalColor*tsmGainFactor;
}

TSM and procedural Marble combined resultA little bit of gain is added to the final color in order to increase the TSM contribution in the final rendering.

The whole TSM stuff is then simply called in the main function of your GLSL code through filter21(..) . In order to correctly apply the translucency, the TSM’s result color must be added up to the base color. You can further increase the final look by taking into account the clamped-to-zero N dot L product, thus simulating dark zones not directly lit by the light source; or even ambient reflections on the main model..

Thank you for reading my very first tutorial. I know there are some unclear parts: some were not written by purpose, in order to give you greater flexibility. Lastly, excuse me if there are some grammar errors, but I’m Italian and I do not write long posts in English from long time…

Bye!

Posted in OpenGL | Tagged: , , , , , | 11 Comments »

Pictures of my new Macbook Pro

Posted by eraser85 on March 13, 2008

Hi all, I’ve added some pictures of my brand new Macbook Pro! Take a look at them, just click on my flickr pictures on the right..

Soon I’ll post a little review on it and the third and final part of TSM tecnique!

Posted in Personal | Tagged: , , , , | Leave a Comment »

Make it translucid! (Part Two)

Posted by eraser85 on March 3, 2008

The last time we discussed about the approximations needed to compute translucency in real time using TSM. We also created a translucent shadow map and now we are going to use it…

4 – Light diffuses through the material

The light entering the material diffuses in it according to the Rd equation Rd Equation(take a look at the figure on the right).

Thanks to the TSM computed in the last article, we only need two more informations: Xin and Xout. Xin represents the fragment coordinate (in light space) were light enters the material, while Xout is the point from which the light leaves the object.

In practice, Xout is the fragment coordinate as seen by the camera moving around the object PROJECTED in light space. Once you get the fragment coordinate from the vertex shade, you have it automatically projected in camera (or view) space: since we need to compare this with the data stored in the TSM, we must have them both in the same space (light space). The projection in light space is done by multiplying the view-space-projected coordinate by the camera’s model-view-projection matrix inverse: by doing so we get the object space coordinates, ready to be multiplied by the light’s model-view-projection matrix.

OCS = Object Coordinate System

WCS = World Coordinate System

VCS = View Coordinate System

NDSC = Normalized Device Coordinate System

DCS = Device Coordinate System

Xin Xout in TSM Once we have projected Xout in light space, calculating Xin is just a matter of shifting Xout’s (x, y) coordinates by a delta (usually the size of a pixel – 1.0/resX and 1.0/res if you want resolution/ratio independency). In this way we can compute translucency simply by filtering the TSM previously calculated. An elegant an also pretty fast solution, if you ask! Of course, this method have some drawbacks: it assumes the object being completely convex so some errors might occur, even though them not being visually important in the majority of cases…

Rd function in GLSL:

vec4 multipleScattering (vec4 Xin, vec4 Xout, float lvl)

{

   vec4 finalColor = vec4(0.0,0.0,0.0,1.0);

   float e = 2.718281828459;

   /***************************/

   //irradiance, depth and normals must account for coordinate shifting!

   vec4 irradIN = texture2D(Irradiance, Xin.xy,lvl);

   vec4 depthIN = texture2D(DepthBuff, Xin.xy,lvl);

   vec4 sNormIN = texture2D(SNormals, Xin.xy,lvl);

   //

   vec4 sigma_a = lightFreqAbsorbed * tsm_freqAbsorption;

   vec4 sigma_s = lightFreqAbsorbed * (1.5-tsm_freqAbsorption);

   //

   vec4 extinction_coeff = (sigma_a + sigma_s);

   vec4 reduced_albedo = sigma_s / extinction_coeff;

   vec4 effective_extinction_coeff = sqrt(3.0 * sigma_a * extinction_coeff);

   vec4 D = 1.0/(3.0*extinction_coeff);

   //

   float fresnel_diff = -(1.440/(refr_index*refr_index))+(0.710/refr_index)+0.668+(0.0636*refr_index);

   float A = (1.0+fresnel_diff)/(1.0-fresnel_diff);

   //

   vec4 zr = 1.0/extinction_coeff;

   vec4 zv = zr + 4.0*A*D;

   //

   vec4 xr = Xin – zr * sNormIN;

   vec4 xv = Xin + zv * sNormIN;

   //

   float dr = length(xr – Xout);

   float dv = length(xv – Xout);

   //

   vec4 f1 = reduced_albedo/(4.0*3.1415296);

   vec4 f2 = zr * (effective_extinction_coeff * dr + 1.0);

   vec4 f3 = pow(vec4(e) , -effective_extinction_coeff * dr) / (extinction_coeff * pow(dr,3.0));

   vec4 f4 = zv * (effective_extinction_coeff * dv + 1.0);

   vec4 f5 = pow(vec4(e), -effective_extinction_coeff * dv) / (extinction_coeff * pow(dv,3.0));

   //

   finalColor = f1 * ( f2 * f3 + f4 * f5);

   //

   return irradIN*finalColor;

}

Posted in OpenGL | Tagged: , , , , , | 2 Comments »

My first notebook

Posted by eraser85 on March 2, 2008

Some days ago I’ve ordered my first notebook ever. I’m quite excited since I’ve chosen a nice beast, without sacrificing mobility: an Apple MacBook Pro. Actually, I don’t really care about MacOS even though I’m in the need for that OS to test applications I write, with different OSes and hardware configs. One of the first things I’m gonna do is install WinXP since I’ve got already Vista on my desktop.

For all the people that might wonder what made me choose an Apple product… I just reply saying that an Apple product is just like any other product. Yes, they are quite a bit more expensive, but that’s because in commerce you ALWAYS pay more for better looking products. Always.

Having that said, what made me take that notebook instead of, say, a Dell or Asus product? Well, the specs. Pure and simple. What I was in the need was a notebook with:

  • Penryn Cpu, which is both fast and energy efficient (45nm, even better if the 3MB-chached version which consumes even less)
  • nVidia 8600 with at least 256MB of dedicated VRam, or if that was not available, an 8800 (actually the 8700 is being assembled with the old productive process, wich is way less efficient)
  • A good quality screen (possibly not with TN panel), with LED back-light in order to have better contrast and be less hungry on the battery
  • As a plus, my ideal notebook would have had a BTO option for a 7200RPM hard drive (even though capacity isn’t something I’m interested in as long as it’s greater than 60GB, an SSD option whould just be too much expensive)

I’ve studied all the possibile notebooks that could have met my standards, and actually there weren’t any at all. :o

The old base MacBook Pro had led back-lit display, 8600 (but with just 128MB vram) and no penryn.. The dell XPSes had no led back-lit displays, nor the penryn; Toshiba alternatives were even worse. The only two possible candidates were the Acer 5920 (the top version) and the Asus M50v.

While the latter was a good solution all around (a very nice thing was the nVidia 9500.. DX10.1 with per-MRT blending, and such..) didn’t had a very good display (just 1280 without led back-lighting); the former was similar, even though with “just” the nVidia 8600 and similarly to the M50v, no LED display (with a nice resolution of 1440 though). Actually I was more interested in the ASUS solution just because Acer products seems “cheap” or “not-so-well-made”.

I sweared to myself: if Apple is not going to update their pro notebooks in a couple of days (I could hold on up to Tuesday-Wednesday), I was going to take the Asus’ one.

Fortunately Apple did the long awaited update and I took the base model, with a 7200RPM hard drive. Seeing the online reviews, I was also impressed with the display quality: it doesn’t seem to be a TN panel thanks to the high viewing angles and overall quality.

Now my shipment is in transit and I hope to put my hands on it on Tuesday or Wednesday at most. As it arrives, I’ll take some shoots and will discuss here the positive and negative aspects.

W00t!

Posted in Personal | Tagged: , , , , , , | Leave a Comment »

Make it translucid! (Part One)

Posted by eraser85 on February 28, 2008

My first post will be about OpenGL, specifically about an advanced shading tecnique proposed by C. Dachsbacher e M. Stamminger in 2003, named Translucet Shadow Maps.

As the name suggests, this tecnique is about translucency in materials. Look at your hand when is between you and a strong light source: light seems to pass through you. Or a glass with milk, you can actually see that even though milk is opaque, light still soften its look. So light through objects: that’s what translucency is all about!

Simulating this effect in real time can be an heavy task. Up to now, illumination models used in games or generic 3D software used for displaying meshes in real time, have been using BRDF models. BRDF stands for Bidirectional Reflectance Distribution Function and it just defines illumination on a material by the amount of light reflected by the surface. There are simpler types of BRDFs, like the Phong model used by OpenGL, and more complex ones such as Ward or Cook-Torrance which are phisically based.

In both cases, those models are useful for describing in real time the majority of materials, expect the translucent ones. In those materials, light isn’t just reflected back in a diffusive and/or specular manner, it is also absorbed by the material itself, scattered inside, and then left out by a different position. This phenomenon is called Subsurface Scattering and in order to simulate it in real time, we utilize the BSSRDF (Bidirectional Subsurface Scattering Reflectance Distribution Function). Even though the BSSRDF is an approximation of the generic Rendering Equation proposed by James T. Kajiya in 1986, is still too heavy to be computed in real time.

During my traineeship at Visual Computing Laboratory in the Italian National Research Council, we discussed a couple of possible tecniques for implementing the subsurface scattering in real time and we ended up using the Translucent Shadow Maps, which is both fast and pretty.

What this tecnique actually does is extending the concept of a classic shadow map, adding other useful informations in it, like the incoming irradiance and surface normals from the light point of view. Actually, to store those kind of informations we need at least two textures: one used to store the incoming irradiance (it’s colored, so we need three channels) AND the depth map, while the second is needed to store the surface normals (x, y, z, so three channels). On Shader Model 3.0 or newer we can actually do all of this in just one pass with Multiple Render Targets.

1 – Creating the Depth Map

That’s a simple task. Just render the geometry in light space, then save the z coordinate of each projected vertices on the texture.

Depth Map of an elephant

Vertex Shader:

varying vec4 vPosLS;

void main(void)
{

   //vertex position in light space

   vPosLS = gl_ProjectionMatrix * WorldToLight * gl_Vertex;

   vPosLS /= vPosLS.wwww;

   gl_Position = vPosLS;

}

Pixel Shader:

void main(void)

{

   gl_FragColor = vec4(vec3(vPosLS.z*.5+.5), -sign(abs(vPosLS.x)) );

}

2 – Compute the Irradiance Map

That’s a bit tricky. The irradiance incident a surface point Xin is scattered in the material according to the Fresnel term Ft:

math formulae

In order to compute this, we need basically:

  1. Per-pixel surface normals
  2. Light incident direction (vertexPosition – lightPosition)
  3. A copy of the texture applied to the model

The Fresnel term Ftcan be computed by the following approximation:

fresnel term img

Since we are observing the scene from the light point of view, V = L. Be sure to normalize both the normals and the light position!

The last term in the first equation is the irradiance impluse, which basically describes the intensity of the light source. Since the irradiance is wavelength dependent, multiply it by the texture image applied on the model. Varying the irradiance impulse you can control the intensity of the translucency.

irradiance.jpg

Vertex Shader:

varying vec4 vPosLS;

varying vec3 vNormLS;

void main(void)
{

   //vertex position in light space

   vPosLS = gl_ProjectionMatrix * WorldToLight * gl_Vertex;

   vPosLS /= vPosLS.wwww;

   //normal direction in light space

   vNormLS = (WorldToLight * vec4(gl_Normal,0.0)).xyz;

   gl_Position = vPosLS;

}

Pixel Shader:

void main(void)

{

   vec3 N = normalize(vNormLS);

   vec3 L = normalize(vPosLS-lightpos).xyz;

   vec3 V = L; //from the light pov V==L

   // Fresnel Refraction (F)

   float k = pow(1.0 – max(0.0, dot(N,V)), 5.0);

   float Ft = 1.0 – k + refr_index * k;

   // Incident Light at surface point

   vec3 E = vec3( Ft * max(0.0 , dot(N,L))) * irradiance_intensity;

   //Multiply E by the color texture here

   gl_FragColor = vec4(E, -sign(abs(N.x)));

}

3 – Surface Normals

Computing surface normals is also trivial: just project each vertex in light space, along with the normals, then write the latter’s coordinates as an RGB output.
normals.jpg

Vertex Shader:

varying vec4 vNormLS ;

void main(void)

{

   vNormLS = (WorldToLight * vec4(gl_Normal,0.0)).xyz;

   gl_Position = gl_ProjectionMatrix * WorldToLight * gl_Vertex;

}

Pixel Shader:

void main(void)

{

   gl_FragColor = vec4(vNormLS*.5+.5, 1.0);

}

To be continued.. stay tuned!

Posted in OpenGL | Tagged: , , , , , | 2 Comments »