Quantcast
Channel: Processing Forum
Viewing all 1768 articles
Browse latest View live

Re : P2D and set() behave strange

$
0
0
I can reproduce your problem with 2.0b7 so it seems there is indeed something buggy with P2D/P3D and the set() method. However using the pixel array directly is better anyway when setting large amounts of pixels. When you adapt the code to use the pixel array, there are no problems with P2D/P3D. Note that line 13 in your original code does nothing because you declare another local color c, besides it's not needed because the id starts as 0. Calculating the ix, iy and total number of pixels isn't needed either. The first two not, because you can use the index directly in the pixel array. The last because this is already a calculated number that you can get with pixels.length. See the adapted code below.

Adapted Code
  1. int sz = 512;
  2. int n = 1000;
  3. int npoints;
  4. color c;
  5.  
  6. void setup() {
  7.   size(sz, sz, P2D);
  8.   background(255);
  9. }
  10.  
  11. void draw() {
  12.   loadPixels();
  13.   for (int i=0;i<n; i++) {
  14.     int id = npoints % pixels.length;
  15.     if (id==0) { c = color(random(255), random(255), random(255)); }
  16.     pixels[id] = c;
  17.     npoints++;
  18.   }
  19.   updatePixels();
  20. }

Re : P2D and set() behave strange

$
0
0
Hi, You are right, I have no special reason to use P2D in this case, and I can also use pixels[].

I just wanted to report bug (maybe I'm in wrong category, but I din't found any bug-report category)

I don't think this is some importaint issue, but maybe it points out some more deep problem (if there is any) concerning sychronization bewneen GPU  based buffers and processing. 

Anyway, thanks for answers.

Re : I need Help

$
0
0
Thank you so much. im new so i did not not know much. 

Re : P2D and set() behave strange

$
0
0
Bugs can be reported in the Code section, ie. now the GitHub account.
Now, part of the problem might come from the fact that P2D uses smooth() by default (I think), so set() might do anti-aliasing, unlike the usage of pixels[]. Putting lot of anti-aliased items close of each other might result in some artifacts.

Re : I need Help

$
0
0
Thx for the explanation, PhiLho!

So, Java has a secret way to pick 1 item from a returned array,
rather than getting it full as most programming languages would expect!

Although I fail to see why your 2nd example has anything to do w/ the previous exotic "element selection".
Method append() from class StringBuilder returns its whole reference. You take it all or leave it!
And it is a very logical one, as opposed to use an index to "fish" right way an item from an array reference!

Well, unless Java has another secret to select a specific field from a returned object reference!!!  

adapt temporal slide shader into processing; ouch !!

$
0
0
hello all,
I try to adapt a temporal slide shader into processing.
I'm pretty newbie to processing, and to the shader world.
The shader should do really cool effects of fading frames from camera for instance.
But still black screen...

here's the code pde, and fragment code:
  1. import processing.video.*;
    PShader slide;
    Capture cam;
    void setup() {
      size(320, 240, P2D);
      cam = new Capture(this, 320, 240);
      textureWrap(CLAMP);
      slide = loadShader( "slide-frag.glsl");
      cam.start(); slide.set("tex0", cam);
    }

    void draw() {
      println(frameRate);
      if (cam.available())
      {
        cam.read();
      }
      slide.set("slide_down", 20);
      shader(slide);
      image(cam, 0, 0,width,height);
     
    }
here's the slide-frag.glsl code:

  1. #ifdef GL_ES
    precision mediump float;
    precision mediump int;
    #endi

    uniform float slide_up;
    uniform float slide_down;

    varying vec2 texcoord0;
    varying vec2 texcoord1;

    uniform sampler2D tex0;
    uniform sampler2D tex1;

    void main(void)
    {
        vec4 su, sd, up, down, amount;

        // sample inputs at texcoords
        vec4 input0 = texture2DRect(tex0, texcoord0);
        vec4 input1 = texture2DRect(tex1, texcoord1);
       
        // get contribution
        amount.x = input0.x > input1.x ? 1.0 : 0.0;
        amount.y = input0.y > input1.y ? 1.0 : 0.0;
        amount.z = input0.z > input1.z ? 1.0 : 0.0;
        amount.w = input0.w > input1.w ? 1.0 : 0.0;
       
        // calculate slide down
        float d = max(1.0, abs(slide_down));
        sd = vec4(1.0 / d);
        down = input1 + ((input0 - input1) * sd);

        // calculate slide up
        float u = max(1.0, abs(slide_up));
        su = vec4(1.0 / u);
        up = input1 + ((input0 - input1) * su);

        // mix between down and up
        gl_FragColor = mix(down, up, amount);
    }

any help would be appreciated, thank you !!

Re : adapt temporal slide shader into processing; ouch !!

$
0
0
I suggest to first read andres' blog posts about shaders in Processing. And then check out the examples that come with Processing. One problem is that you have 4 uniforms in the shader but you're not setting two of them in your sketch (slide_up, tex1). Another problem is related to the different type of shaders that are recognised by their variables in Processing, see the blog posts. Another problem is the typo in your endif. Another problem is that this shader expects two texture coordinates coming from the vertex shader and you're not setting any in your program or even have a vertex shader. So all in all you're a long way from home. Which comes back to my first suggestions to read up and start with the examples.

Can't loop video backwards

$
0
0
If I set the loop speed for a video to zero or a negative number, I get  this error:

      GStreamer-CRITICAL **: gst_segment_set_seek: assertion `start <= stop' failed

Sometimes, when looping a video at a speed greater than zero, the loop just stops without an error message.

Here's my sketch (2.0b7):



Re : I need Help

$
0
0
I might have not been clear. it = getItems()[2] does exactly the same thing than lst = getItems(); it = lst[2];
It is not a way to return a single element by a special magic!
It is only a syntactical shortcut.
The second example was another example of shortcut, similar in the sense that the return value is used immediately, without intermediary variable.

Re : I need Help

$
0
0

It is not a way to return a single element by a special magic!
I guess there's some sorta miscommunication!
The way I understood is that this exotic syntax filters out a specific element from a returned array.
Now you just say that it's not a way to return a single element?  

I did an example below, and it doesn't compile b/c
it cannot convert an int (a single element) into an int[] (a whole array)!

And your 2nd example doesn't filter out anything. So, nothing exotic there! 
    void setup() {
      int[] a;
    
      // regular expected access:
      a = getArray();
      println(a);
    
      // cannot convert int to int[]:
      a = getArray()[1];  
      println(a[1]);
    
      exit();
    }
    
    int[] getArray() {
      return new int[] {-2, 34, 5};
    }
    

Re : Creating a "choose and zoom image" program

$
0
0


like this with arrays

(tested)


  1. //
  2. int indexBigImage = -1;
  3. //
  4. PImage big [] = new PImage [7];
  5. //
  6. PImage small [] = new PImage [7];
  7. //
  8. void setup()
  9. {
  10.   size(800, 600);
  11.   //
  12.   big[1] = loadImage("0big.jpg");
  13.   big[2] = loadImage("1big.jpg");
  14.   big[3] = loadImage("2big.jpg");
  15.   big[4] = loadImage("3big.jpg");
  16.   big[5] = loadImage("4big.jpg");
  17.   big[6] = loadImage("5big.jpg");
  18.   //
  19.   small[1] = loadImage("0small.jpg");
  20.   small[2] = loadImage("1small.jpg");
  21.   small[3] = loadImage("2small.jpg");
  22.   small[4] = loadImage("3small.jpg");
  23.   small[5] = loadImage("4small.jpg");
  24.   small[6] = loadImage("5small.jpg");
  25.   //
  26. }
  27. void draw()
  28. {
  29.   background (255);
  30.   // use for loop here
  31.   image (small[1], 100, 20);
  32.   image (small[2], 200, 20);
  33.   image (small[3], 300, 20);
  34.   image (small[4], 400, 20);
  35.   image (small[5], 500, 20);
  36.   image (small[6], 600, 20);
  37.   //
  38.   if (indexBigImage != -1)
  39.   {
  40.     image (big[indexBigImage], 60, 120);
  41.   }
  42.   //
  43.   if (mouseX>100&&mouseX<180&&mouseY>10&&mouseY<80)
  44.   {
  45.     indexBigImage=1;
  46.   }
  47.   else if (mouseX>200&&mouseX<280&&mouseY>10&&mouseY<80)
  48.   {
  49.     indexBigImage = 2;
  50.   }
  51.   else
  52.   {
  53.     indexBigImage = -1;
  54.   } // else
  55.   //
  56. } // func
  57. //

Re : Wait for Arduino Before Sending Data Over Serial

$
0
0
While not needing to do exactly what you are doing I am using the Arduino-SerialCommand library that was originally devised by Steven Cogswell and modified by Stefan Rado to send commands with arguments to an Arduino from Processing. I see that SerialCommand was initially created for stepper motor control so perhaps it is suited for what you are doing. On the Adruino side you can control the SerialCommand input buffer size. So I would think you could make sure your Processing side data sender sends data within that buffer size. This SerialCommand library fills its input buffer with incoming data while waiting for an incoming \n to signal the end of the command stream. It then looks at the first word in that command stream to assoicate with callback routines that you create to process whatever is in the input buffer. Maybe that communication scheme will work for you.    

Re : Creating a "choose and zoom image" program

$
0
0
Thanks again.
This procedure is interesting, but I think perhaps is a bit more complicated than the other, although it's quicker..

However, now it's time to relax.
I'm undecided about opening a new thread tomorrow, because I had in mind to create a jigsaw puzzle of four pieces with processing, but I don't know if it would be diffucult or easy to do.

Re : Processing 2.0b7 Serial.bufferUntil() not working?

$
0
0
I am having no problems with 2.0b7 using .bufferUntil('\n') and I am developing back and forth between OS X and Windows 7 machines. The problem could be that the until signal ("\n" for my case) is not being sent. You might need to create a diagnostic that does not use the bufferUntil that reveals exactly what is coming in so you can determine exactly what is going on. Remember that the computer is very most likely doing exactly what you are telling it to do. So if bufferUntil() never triggers serialEvent then bufferUntil() never actually buffers until. The until marker must not be coming in.

Different shaders for geometry, lines and text?

$
0
0
Hi!

I'm trying to add some basic fog effect through the new PShader object. Found this fairly simple glsl shader and adapted to my purposes. I would like to replace only the fragment shader and keep using the "default" vertex shader, but I couldn't find how to do it, so I made a basic vertex shader using the minimum attributes, which fall in the FLAT shader type.

Here is my test code:
Copy code

  1. PShader fog;

    void setup() {
      size(640, 360, P3D);
      fog = loadShader("fogF.glsl", "fogV.glsl");
  2.   fog.set("fogNear", 0.0);
      fog.set("fogFar", 500.0);
      hint(DISABLE_DEPTH_TEST);
    }

    void draw() {
        shader(fog);
      background(0);
      noStroke();
      translate(mouseX, mouseY, -100);
      box(200);
      fill(255,0,0);
      box(100);
      stroke(255);
      strokeWeight(10);
      line(0,0,0, width/2, height/2, 100);
      line(0,0,0, -width/2, height/2, 100);
      text("shader", 100,0,100);
    }
The vertex shader:
Copy code
  1. uniform mat4 projmodelviewMatrix;

    attribute vec4 inVertex;
    attribute vec4 inColor;

    varying vec4 vColor;

    void main() {
      // Vertex in clip coordinates
      gl_Position = projmodelviewMatrix * inVertex;
      vColor = inColor;
    }

The fragment shader:
Copy code
  1. #ifdef GL_ES
    precision highp float;
    #endif

    uniform float fogNear;
    uniform float fogFar;
    varying vec4 vColor;

    void main(){
        vec3 fogColor = vec3(1.0,1.0,1.0);
        gl_FragColor = vColor;
        float depth = gl_FragCoord.z / gl_FragCoord.w;
        float fogFactor = smoothstep(fogNear, fogFar, depth);
        gl_FragColor = mix(gl_FragColor, vec4(fogColor, gl_FragColor.w), fogFactor);

    }

The FLAT type shader only works for filled untextured geometry, so, do I need to write another one using the LINES type attributes  to apply the fog effect to lines and another one for text? It feels very convoluted for a simple effect, is there a way to simplify it?

Cheers!

Re : Different shaders for geometry, lines and text?

Re : Creating a "choose and zoom image" program

Re : Initializing PApplet with arguments

$
0
0
I don't quite understand what you meant. But thanks for your answer. Can you, please, be more specific? thanks.


Thsi is my problem:

I have a GUI interface with jFrame and two different processing sketches. My GUI should be able to choose the size of the two sketches. I need to pass information from that GUI to the choosen skecht. Is there a way to do that? or do I have to write to a file and load it? 


EDIT:

nevermind, I got it. Thanks!!!!

Re : Wait for Arduino Before Sending Data Over Serial

$
0
0
I ended up solving it with the following:

In my Processing program, I added a section that paused in a while loop every 32 iterations until it receives feedback from the arduino (and also sends a 'Y' character to let the arduino know that it's paused), then continues with the main void draw()

  1. void draw() {  

  2. ++waiting;
  3. if(waiting==32) {
  4.   arduinoPort.write('Y');
  5.   while (1==1) {
  6.     int inByte = arduinoPort.read();
  7.     if(inByte == 78) {
  8.       break;
  9.     }
  10.   }
  11.   waiting = 1;
  12. }
and in my arduino code, I just added  a section specifying that if the Processing program is paused, send an 'N' lo let it know to continue.

  1.     else if (hit == 89) {
  2.       Serial.print('N');
  3.     }

Re : Wait for Arduino Before Sending Data Over Serial

$
0
0
OK. Are these motor control commands executed in realtime or are they first stored and then executed? I am wondering if the pauses cause any issues. By the way, does your Processing application ride through a temporary serial disconnection from the Adruino and if so how is that accomplished? 
Viewing all 1768 articles
Browse latest View live