Advertisement
JoshDreamland

Untitled

Sep 30th, 2012
47
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.64 KB | None | 0 0
  1. class framebuffer {
  2. GLUint texid;
  3. unsigned char **matrix;
  4. int width, height;
  5.  
  6. framebuffer::framebuffer(): texid(-1), matrix(0) {}
  7.  
  8. int framebuffer::getTexture() {
  9. if (texid == -1)
  10. glGenTextures(1, &texid);
  11. glBindTexture(GL_TEXTURE_2D, texid);
  12. glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_BYTE, matrix);
  13. return texid;
  14. }
  15.  
  16. int framebuffer::getPixel(int x, int y) {
  17. return ((int*)matrix[y])[x];
  18. }
  19.  
  20. void framebuffer::shadePixel(int x, int y, int r, int g, int b, double a) {
  21. unsigned char& red = matrix[yi][(xi<<2)+0];
  22. unsigned char& green = matrix[yi][(xi<<2)+1];
  23. unsigned char& blue = matrix[yi][(xi<<2)+2];
  24. unsigned char& alpha = matrix[yi][(xi<<2)+3];
  25. red = unsigned char((1-a)*red + a*r);
  26. green = unsigned char((1-a)*green + a*g);
  27. blue = unsigned char((1-a)*blue + a*b);
  28. a = unsigned char(0xFF*(1-(1-(alpha/double(0xFF)))*(1-a)));
  29. }
  30.  
  31. void shadeCircle(double x, double y, double rad, int r, int g, int b, double a) {
  32. int xr = int(x + rad), yb = int(y + rad);
  33. for (int xi = int(x - rad); xi < xr; ++xi)
  34. for (int yi = int(y - rad); yi < yb; ++yi)
  35. shadePixel(x, y, r, g, b, a * min(1,max(0,hypot(xi - x, yi - y) - rad)));
  36. }
  37.  
  38. void shadeLine(double x1, double y1, double x2, double y2,
  39. double w, int r, int g, int b, double a) {
  40. double rad = w / 2.0, at = a / w;
  41. double xx, yy, dir = atan2(y2-y1, x2-x1), xinc = cos(dir), yinc = sin(dir);
  42. for (xx = x1, yy = y1; hypot(xx - x2, yy - y2) >= 1; xx += xinc, yy += yinc)
  43. shadeCircle(xx, yy, w, r, h, b, at);
  44. }
  45. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement