   Seamus Mcnally's fire effect
  Hi, I've been working on this for a while and have run into a problem that I can't spot.I'm using OpenGL and C++ and I'm not looking for optimizations just yet, just a solution to make this work. I originally had a working implementation where I just stored a buffer of unsigned bytes and just used a standard putpixel type function to render my particles, blurred it as necessary, then used glDrawPixels to render.I'd like to have everything drawn using glBegin and glEnd calls now so I can have things like burning text. As a result, I've modified the way the code works and abstracted the blurring process and apply it to the whole screen using glReadPixels and glDrawPixels. This is my code as is. It works, but there's no blurring effect.unsigned char* buffer = new unsigned char[640*480*3]; // this is created elsewhere obviously.// main rendering loop.glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);glLoadIdentity();glReadPixels(0,0,640,480,GL_RGB,GL_UNSIGNED_BYTE,buffer);unsigned char pixel = 0;for (int i=1923;i<640*480*3-3;i++){pixel = ((buffer + buffer[i+3] + buffer[i-3] + buffer[i-1920]) >> 2) - 1;if (pixel >= 255)pixel = 0;if (pixel <= 0)pixel = 0;buffer = pixel;}glDrawPixels(640,480,GL_RGB,GL_UNSIGNED_BYTE,buffer);SwapBuffers(hDC);And of course, assume that before glReadPixels is called, I'm making calls to glBegin(GL_POINTS); setting the color and vertex data and then calling glEnd(); As I said, it renders the points just fine, but with no blur effect.   
  Any thoughts?   ;  Ok, either I'm making a huge obvious mistake that I can't pinpoint or nobody has any ideas on how to make this work. I'm really doubting the latter is true, so if anybody can help me out, PLEASE do!   ;  I suggest you use a texture instead of the frame buffer directly.Where are you calling glRasterPos before the call to glDrawPixels?   ;  I accidentally omitted it from the code I posted but its called immediately before glDrawPixels and it's just a basic glRasterPos2i(0, 0);I tried rendering to a texture but for some reason it just made the entire screen turn the color of the current pixel being modified. So basically just fullscreen flashes of color.   ;...if (pixel >= 255)pixel = 0;...The first thing that springs to mind... shouldn't the assignment be "pixel = 255;"?C++ isn't my forte so I am not certain of all the rules regarding integer size and typecasting and all, but what happens if you do arithmetic on two chars and the result overflows?  Is it truncated (e.g. 128 + 129 = 1) or returned as an int that can accommodate it?Personally, to be safe, I would define pixel as a signed int.   ;  The entire buffer is an array of unsigned chars. It limits the values to 0-255 which makes things simple to deal with. For some reason when I set the value back to 255 if it was over 255, it caused weird artifacts.   ;  What I mean is that 'pixel' will likely overflow in that calculation because you are adding a load of unsigned chars together and storing the result in an unsigned char - it won't cap at 255 if it goes over; it will wrap around back to zero (similarly, if it goes below zero, it will wrap to 255).  This is why I suggest defining 'pixel' as a signed int, because then these overflows won't occur and you can then clamp the result to between 0 and 255 without any trouble.   ;Quote:Original post by WaterCraneWhat I mean is that 'pixel' will likely overflow in that calculation because you are adding a load of unsigned chars together and storing the result in an unsigned char - it won't cap at 255 if it goes over; it will wrap around back to zero (similarly, if it goes below zero, it will wrap to 255).  This is why I suggest defining 'pixel' as a signed int, because then these overflows won't occur and you can then clamp the result to between 0 and 255 without any trouble.Changing the type of 'pixel' will not fix the problem, but it is a step in the right direction. Pixel is only used in the assignment - each addition would still be addition of unsigned chars.Change 'pixel' to int, then change the math like so:pixel = ((int(buffer) + int(buffer[i+3]) + int(buffer[i-3]) + int(buffer[i-1920])) / 4) - 1;pixel = std::min(255, std::max(pixel, 0));buffer = static_cast<unsigned char>(pixel & 0xFF);Note that only the first int() is required, but I like having all 4 for consistency.   