Loading...
Logo
Processing Forum
Hi

Somebody can help me? I don´t understand way is vertically flipping the image , I find the problem is createGrapics, if comment pg.begindraw() and pg.endDraw(), no flip, but I need use it.

Thanks!!!!


Copy code
  1. // Off-screen rendering example using GLGraphics.
  2. // By Andres Colubri. Multisampling support in GLGraphicsOffScreen
  3. // implemented by Ilias Bergstrom (http://www.onar3d.com/) 
  4. //
  5. // Drag the mouse accross the horizontal direction to change
  6. // the mixing of the two off-screen textures.

  7. import processing.opengl.*;
  8. import codeanticode.glgraphics.*;

  9. GLGraphicsOffScreen glg1, glg2;

  10. int mixFactor = 127;


  11. // This will contain the pixels used to calculate the fire effect
  12. int[][] fire;

  13. // Flame colors
  14. color[] palette;
  15. float angle;
  16. int[] calc1,calc2,calc3,calc4,calc5;

  17. PGraphics pg;

  18. void setup() {
  19.   size(640, 480, GLConstants.GLGRAPHICS);
  20.     
  21.   // Creating an off-screen drawing surfaces. The first one has
  22.   // 4x multi-sampling enabled.
  23.   glg1 = new GLGraphicsOffScreen(this, 320, 240, true, 4);
  24.   glg2 = new GLGraphicsOffScreen(this, 640, 360, true, 4);

  25.   // Disabling stroke lines in the first off-screen surface.
  26.   glg1.beginDraw();
  27.   glg1.noStroke();
  28.   glg1.endDraw();
  29.   
  30.   //CUBE SETUP
  31.   pg = createGraphics(640, 360, P3D);

  32.   calc1 = new int[width];
  33.   calc3 = new int[width];
  34.   calc4 = new int[width];
  35.   calc2 = new int[height];
  36.   calc5 = new int[height];

  37.   colorMode(HSB);

  38.   fire = new int[width][height];
  39.   palette = new color[255];

  40.   // Generate the palette
  41.   for(int x = 0; x < palette.length; x++) {
  42.     //Hue goes from 0 to 85: red to yellow
  43.     //Saturation is always the maximum: 255
  44.     //Lightness is 0..255 for x=0..128, and 255 for x=128..255
  45.     palette[x] = color(x/3, 255, constrain(x*3, 0, 255));
  46.   }

  47.   // Precalculate which pixel values to add during animation loop
  48.   // this speeds up the effect by 10fps
  49.   for (int x = 0; x < width; x++) {
  50.     calc1[x] = x % width;
  51.     calc3[x] = (x - 1 + width) % width;
  52.     calc4[x] = (x + 1) % width;
  53.   }
  54.   
  55.   for(int y = 0; y < height; y++) {
  56.     calc2[y] = (y + 1) % height;
  57.     calc5[y] = (y + 2) % height;
  58.   }
  59. }

  60. void draw() {
  61.   background(0);
  62.   
  63.   // In the off-screen surface 1, we draw random ellipses.
  64.   glg1.beginDraw();
  65.   glg1.fill(230, 50, 20, random(50, 200));
  66.   glg1.ellipse(random(0, glg1.width), random(0, glg1.height), random(10, 50), random(10, 50));
  67.   glg1.endDraw();   

  68.   // In the off-screen surface 2, we draw random rectangles.  
  69.   glg2.beginDraw();
  70.   angle = angle + 0.05;

  71.   // Rotating wireframe cube
  72.   pg.beginDraw();
  73.   pg.translate(width >> 1, height >> 1);
  74.   pg.rotateX(sin(angle/2));
  75.   pg.rotateY(cos(angle/2));
  76.   pg.background(0);
  77.   pg.stroke(128);
  78.   pg.scale(25);
  79.   pg.noFill();
  80.   pg.box(4);
  81.   pg.endDraw();

  82.   // Randomize the bottom row of the fire buffer
  83.   for(int x = 0; x < width; x++)
  84.   {
  85.     fire[x][height-1] = int(random(0,190)) ;
  86.   }

  87.   glg2.loadPixels();

  88.   int counter = 0;
  89.   // Do the fire calculations for every pixel, from top to bottom
  90.   for (int y = 0; y < 360; y++) {
  91.     for(int x = 0; x < 640; x++) {
  92.       // Add pixel values around current pixel

  93.       fire[x][y] =
  94.           ((fire[calc3[x]][calc2[y]]
  95.           + fire[calc1[x]][calc2[y]]
  96.           + fire[calc4[x]][calc2[y]]
  97.           + fire[calc1[x]][calc5[y]]) << 5) / 129;

  98.       // Output everything to screen using our palette colors
  99.      glg2.pixels[counter] = palette[fire[x][y]];

  100.       // Extract the red value using right shift and bit mask 
  101.       // equivalent of red(pg.pixels[x+y*w])
  102.       if ((pg.pixels[counter++] >> 16 & 0xFF) == 128) {
  103.         // Only map 3D cube 'lit' pixels onto fire array needed for next frame
  104.         fire[x][y] = 128;
  105.       }
  106.     }
  107.   }
  108.   glg2.updatePixels();
  109.   glg2.endDraw();

  110.   // We mix the images together and scale them so the occupy the entire screen.
  111.   tint(255, 255 - mixFactor);
  112.   image(glg1.getTexture(), 10, 10, width - 20, height - 20); 

  113.   tint(255, mixFactor);
  114.   image(glg2.getTexture(), 10, 10, width - 20, height - 20);

  115.   // Image border for reference.
  116.   noFill();
  117.   stroke(255);
  118.   rect(10, 10, width - 20, height - 20);  
  119.   fill(0); // 1.0.2/1.0.3 need this, because noFill() affects the tint used in image().
  120. }

  121. void mouseDragged() {
  122.   mixFactor = int(255 * float(mouseX) / width);
  123. }

Replies(5)

Actually, the  updatePixels() and loadPixels were not correctly implemented in GLGraphicsOffScreen. I prepared an updated package of GLGraphics which fixes this issue (download link).

Also, you don't need to call glg2.beginDraw()/glg2.endDraw() because you are directly accessing the pixels array.  beginDraw()/ endDraw() are needed when you are drawing on the offscreen surface using any of the standard methods (line, box, beginShape, etc), which is not the case here.
Fantastic!!! 

It works with 1.5, thanks for the quick answer, my proyect was blocked :D

Un saludo!!




I'm back,  still have the same error in another example. I´m trying to mix napplet and keystone libraries, and glgraphics is the glue. I suspect the error is when create an Offscreen stack, because the way I find to mix the libraries, is substitute the renderer by the offscreen, and the error comes when another Offscreen in the same window is created. I share my work, I hope somebody can help me because I´m lost. I´ve updated the napplet library to work with 1.5, but everything is alpha version.

so much thanks!!


Hello, I missed your last post before releasing the new GLGraphics package (0.99). Let me know if the problem still happens with it.

If it does, could you make a somewhat simpler code example that reproduces the error? Thanks!
Hello,

I tested the new version but the problem is not fixed 

The example in the last post is a simply example of my sw  , but if I explain maybe is more easy, but I will try to simplify the example.

Well, I want run papplets into an papplet, for this, NApplet seems to work correctly but, I need a little more, mix with keystone. The way had been to override paint() and size() methods,in size()  I use an offscreen substituting the graphics variable "this.g = offscreen;"  ( you can see at napplet.easymapping.EMNapplet) 

But the problem comes when in this "OffscreenFrankenNapplet" ( napplet.easymapping.EMNapplet :), is created another offscreen, everything work correctly, but, is vertically flipped.

The problem maybe is with offscreen-textures-stack when another offscreen is created inside the OffscreenFrankenNapplet, puuffff!! 

If I can help you in any way, please, tell me.

thanks!! 

miles de gracias!!