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

simple OPENGL, worked in 1.51, doesn't work anymore....

$
0
0


hello all,

this is a simple OPENGL-code, it worked in 1.51, doesn't work anymore....

can anyone help?

Thank you!

Chrisir  



  1. // imports ---------------------------------------------------
  2. //import processing.opengl.PGraphicsOpenGL;
  3. import javax.media.opengl.GL;
  4. import javax.media.opengl.glu.GLU;
  5. import javax.media.opengl.glu.GLUquadric;
  6. import com.sun.opengl.util.texture.Texture;
  7. import com.sun.opengl.util.texture.TextureIO;
  8. import peasy.PeasyCam; //peasy camera control
  9. // opengl
  10. import processing.opengl.*;
  11. /** PeasyCam **/
  12. import peasy.*;
  13. // Audio
  14. //import ddf.minim.*;
  15. // for export
  16. import superCAD.*;
  17. // global vars -----------------------------------------------
  18. // control behaviour parameters --------------------
  19. boolean testingForDebugging      = false;
  20. int showingCubeData              = 0;
  21. //
  22. boolean boolGl_Control           = false;  // Marble with opengl?
  23. boolean boolGl_Control_for_Boxes = false; // Boxes with opengl?
  24. // Camera Types
  25. boolean CameraMoving          = false;    // camera always ahead of Marble
  26. boolean CameraMoving2         = false;    // good for movie (camera on a circle track)
  27. // Movie
  28. boolean boolMakeMovie         = false;  // saves a Movie
  29. boolean boolUseCheckeredFloor = true;   // Floor Type
  30. // other vars ---------------------------------------------------
  31. // cube
  32. final int cubeSize = 49; // width, height and depth
  33. final int undefinedCube = -11;
  34. // GL stuff
  35. PGraphicsOpenGL pgl;
  36. GL gl;
  37. GLU glu;
  38. GLUquadric mysphere;
  39. GLUquadric myBox;
  40. Texture textureMarble;
  41. float marbleX=200;
  42. float marbleY=200;
  43. float marbleZ=-100;
  44. float r = 19; // Sized to fit the texture
  45. float rotateMarble = 0.39;
  46. float rotateMarbleSpeed = 0.02; // .196
  47. float MarbleRotateX=0.2;
  48. float MarbleRotateY=0.2;
  49. float MarbleRotateZ=0.2;
  50. void setup() {
  51.   size(890, 660, OPENGL);
  52.   // size(screenWidth, screenHeight, OPENGL );
  53.   // noCursor();
  54.   //cam.setMinimumDistance(40);
  55.   //cam.setMaximumDistance(6550);
  56.   // cam.lookAt( 70, 390, 0 );
  57.   pgl = (PGraphicsOpenGL) g; 
  58.   gl = pgl.gl; 
  59.   glu = pgl.glu;
  60.   mysphere = glu.gluNewQuadric();
  61.   glu.gluQuadricDrawStyle(mysphere, glu.GLU_FILL);
  62.   glu.gluQuadricNormals(mysphere, glu.GLU_NONE);
  63.   glu.gluQuadricTexture(mysphere, true);
  64.   myBox = glu.gluNewQuadric();
  65.   glu.gluQuadricDrawStyle(myBox, glu.GLU_FILL);
  66.   glu.gluQuadricNormals(myBox, glu.GLU_NONE);
  67.   glu.gluQuadricTexture(myBox, true); 
  68.   try {   
  69.     textureMarble = TextureIO.newTexture(new
  70.       File(dataPath("textures2.jpg")), true);
  71.   } 
  72.   catch (IOException e) {       
  73.     println("failed to load picture - line 159.   ++++++++++++++++++++++");
  74.     javax.swing.JOptionPane.showMessageDialog(this, e);
  75.     exit();
  76.   }
  77. } // func
  78. void draw (  ) {
  79.   background (0);
  80.   PaintMarble () ;
  81. }
  82. void PaintMarble () {
  83.   pgl.beginGL();
  84.   gl.glPushMatrix();
  85.   gl.glTranslatef( marbleX, marbleY, marbleZ);
  86.   gl.glRotatef(degrees(rotateMarble), MarbleRotateX, MarbleRotateY, MarbleRotateZ);
  87.   gl.glColor3f(1, 1, 1);
  88.   textureMarble.enable();
  89.   textureMarble.bind();
  90.   //The Sphere
  91.   glu.gluSphere(mysphere, r, 40, 40);
  92.   textureMarble.disable();
  93.   gl.glPopMatrix();
  94.   pgl.endGL();
  95.   if (rotateMarble < 360) {
  96.     rotateMarble = rotateMarble + rotateMarbleSpeed;
  97.   }
  98.   else {
  99.     rotateMarble = 0;
  100.   }
  101. } // method


Re : simple OPENGL, worked in 1.51, doesn't work anymore....

$
0
0


or let me reframe the question:
how can I texture a sphere and let it look like it's rolling?

Thank you!


How to use a fragment shader on a PGraphics in Processing 2.0xx?

$
0
0
So far I've only gotten the filter(shader) method to work as a post-processing filter on the whole window. While the syntax pgraphics.filter(shader) is allowed, it doesn't seem to work. The method filter(shader) works from/to the main PGraphics, so it would make sense that pgraphics.filter(shader) would work from/to a user-defined PGraphics. At least that's what I'm hoping, because I'd like to use a GLSL fragment shader on an offscreen PGraphics.

Is this possible? And if so, how?

Re : How to reset a PGraphics ?

$
0
0
No need for begin/endShape, only begin/endDraw are necessary to draw on a PGraphics.

Calendar class related error in latest Processing 2.0b7

$
0
0
I'm testing the code "M_1_6_01_TOOL" from the book Generative Design, and the latest Processing 2.0b7 will give me error related to the Calendar class, but the code run smoothly in 2.0b6

source code:
http://www.generative-gestaltung.de/M_1_6_01_TOOL

Anybody encounter similar problem?

Re : Calendar class related error in latest Processing 2.0b7

$
0
0
See several similar threads on the forum (just search 2.0b7), and when you upgrade, it is often a good idea to read the changelist...

Re : 2.x PGraphics thread crash

Re : How to use a fragment shader on a PGraphics in Processing 2.0xx?

$
0
0
The filer(PShader) function should work with a PGraphics surface, as long as it is created as P2D or P3D:
  1. PShader edges;
  2. boolean applyFilter = true;
  3. PGraphics pg;

  4. void setup() {
  5.   size(640, 360, P3D);
  6.   edges = loadShader("edges.glsl");
  7.   pg = createGraphics(640, 360, P3D);  
  8.   noStroke(); 
  9. }

  10. void draw() {
  11.   pg.beginDraw();
  12.   pg.noStroke();
  13.   pg.background(0);
  14.   pg.lights();
  15.   
  16.   pg.translate(width/2, height/2);  
  17.   pg.pushMatrix();
  18.   pg.rotateX(frameCount * 0.01);  
  19.   pg.rotateY(frameCount * 0.01);
  20.   pg.box(120);
  21.   pg.popMatrix();
  22.     
  23.   if (applyFilter == true) {
  24.     pg.filter(edges);
  25.   }
  26.   
  27.   // The sphere doesn't have the edge detection applied 
  28.   // on it because it is drawn after filter() is called.
  29.   pg.rotateY(frameCount * 0.02);
  30.   pg.translate(150, 0);
  31.   pg.sphere(40);
  32.   pg.endDraw();
  33.   
  34.   image(pg, 0, 0, width, height);
  35. }

  36. void mousePressed() {
  37.   applyFilter = !applyFilter;
  38. }

Re : simple OPENGL, worked in 1.51, doesn't work anymore....

$
0
0


I know there are approaches with double for loops to redo the Sphere-Command and make y sphere from rectangles and texture them somehow....

but that 's too slow.

the approach above used to work in 1.51..............

All help appreciated


Re : How to use a fragment shader on a PGraphics in Processing 2.0xx?

$
0
0
Thank you andres. It works!

I also see now where the error was in my test code. I called pg.filter(shader) outside beginDraw-endDraw.

Now that you have shown how to apply it correctly, it works fine.

Re : simple OPENGL, worked in 1.51, doesn't work anymore....

Color Tracking with Multiple Cameras

$
0
0
I have trying to get two USB cameras to track a specific color. This is going to be used for hand tracking, tip of a finger colored to track. The two cameras is to get three dimensional output, to control a robot arm. Yes, i know their is easer ways to control the arm, but I would like to use this method. Here is some example code if found to track colors, how do I go about using two cameras? 

  1. // Learning Processing
  2. // Daniel Shiffman
  3. // http://www.learningprocessing.com

  4. // Example 16-11: Simple color tracking

  5. import processing.video.*;

  6. // Variable for capture device
  7. Capture video;

  8. // A variable for the color we are searching for.
  9. color trackColor; 

  10. void setup() {
  11.   size(640,480);
  12.   video = new Capture(this, width, height,15);
  13.   // Start off tracking for red
  14.   trackColor = color(255,0,0);
  15.   video.start();
  16.   smooth();
  17. }

  18. void draw() {
  19.   // Capture and display the video
  20.   if (video.available()) {
  21.     video.read();
  22.   }
  23.   video.loadPixels();
  24.   image(video,0,0);

  25.   // Before we begin searching, the "world record" for closest color is set to a high number that is easy for the first pixel to beat.
  26.   float worldRecord = 500; 

  27.   // XY coordinate of closest color
  28.   int closestX = 0;
  29.   int closestY = 0;

  30.   // Begin loop to walk through every pixel
  31.   for (int x = 0; x < video.width; x ++ ) {
  32.     for (int y = 0; y < video.height; y ++ ) {
  33.       int loc = x + y*video.width;
  34.       // What is current color
  35.       color currentColor = video.pixels[loc];
  36.       float r1 = red(currentColor);
  37.       float g1 = green(currentColor);
  38.       float b1 = blue(currentColor);
  39.       float r2 = red(trackColor);
  40.       float g2 = green(trackColor);
  41.       float b2 = blue(trackColor);

  42.       // Using euclidean distance to compare colors
  43.       float d = dist(r1,g1,b1,r2,g2,b2); // We are using the dist( ) function to compare the current color with the color we are tracking.

  44.       // If current color is more similar to tracked color than
  45.       // closest color, save current location and current difference
  46.       if (d < worldRecord) {
  47.         worldRecord = d;
  48.         closestX = x;
  49.         closestY = y;
  50.       }
  51.     }
  52.   }
  53.   
  54. println("x = " + closestX + " y = " + closestY);
  55.   // We only consider the color found if its color distance is less than 10. 
  56.   // This threshold of 10 is arbitrary and you can adjust this number depending on how accurate you require the tracking to be.
  57.   if (worldRecord < 10) { 
  58.     // Draw a circle at the tracked pixel
  59.     fill(trackColor);
  60.     strokeWeight(4.0);
  61.     stroke(0);
  62.     ellipse(closestX,closestY,16,16);
  63.    
  64.   }
  65. }

  66. void mousePressed() {
  67.   // Save color where the mouse is clicked in trackColor variable
  68.   int loc = mouseX + mouseY*video.width;
  69.   trackColor = video.pixels[loc]; }
Here is what I tried to do.
  1. // Learning Processing
  2. // Daniel Shiffman
  3. // http://www.learningprocessing.com

  4. // Example 16-11: Simple color tracking

  5. import processing.video.*;

  6. // Variable for capture device
  7. Capture video;
  8. Capture video2;
  9. // A variable for the color we are searching for.
  10. color trackColor; 

  11. void setup() {
  12.   size(640,480);
  13.   video = new Capture(this, width, height,15);
  14.   // Start off tracking for red
  15.   trackColor = color(255,0,0);
  16.   video.start();
  17.   smooth();
  18.   
  19.   size(640,480);
  20.   video2 = new Capture(this, width, height,15);
  21.   // Start off tracking for red
  22.   trackColor = color(255,0,0);
  23.   video2.start();
  24.   smooth();
  25. }

  26. void draw() {
  27.   // Capture and display the video
  28.    if (video.available()) {
  29.     video.read();
  30.   }
  31.   video.loadPixels();
  32.   image(video,0,0);

  33.   // Before we begin searching, the "world record" for closest color is set to a high number that is easy for the first pixel to beat.
  34.   float worldRecord = 500; 

  35.   // XY coordinate of closest color
  36.   int closestX = 0;
  37.   int closestY = 0;

  38.   // Begin loop to walk through every pixel
  39.   for (int x = 0; x < video.width; x ++ ) {
  40.     for (int y = 0; y < video.height; y ++ ) {
  41.       int loc = x + y*video.width;
  42.       // What is current color
  43.       color currentColor = video.pixels[loc];
  44.       float r1 = red(currentColor);
  45.       float g1 = green(currentColor);
  46.       float b1 = blue(currentColor);
  47.       float r2 = red(trackColor);
  48.       float g2 = green(trackColor);
  49.       float b2 = blue(trackColor);

  50.       // Using euclidean distance to compare colors
  51.       float d = dist(r1,g1,b1,r2,g2,b2); // We are using the dist( ) function to compare the current color with the color we are tracking.
  52. println("x = " + closestX + " y = " + closestY);
  53.       // If current color is more similar to tracked color than
  54.       // closest color, save current location and current difference
  55.       if (d < worldRecord) {
  56.         worldRecord = d;
  57.         closestX = x;
  58.         closestY = y;
  59.       }
  60.     }
  61.     
  62.    //second camera below 
  63.   if (video2.available()) {
  64.     video2.read();
  65.   }
  66.   video2.loadPixels();
  67.   image(video2,0,0);

  68.   // Before we begin searching, the "world record" for closest color is set to a high number that is easy for the first pixel to beat.
  69.   float worldRecord = 500; 

  70.   // XY coordinate of closest color
  71.   int closestX2 = 0;
  72.   int closestY2 = 0;

  73.   // Begin loop to walk through every pixel
  74.   for (int x = 0; x < video2.width; x ++ ) {
  75.     for (int y = 0; y < video2.height; y ++ ) {
  76.       int loc = x + y*video2.width;
  77.       // What is current color
  78.       color currentColor2 = video2.pixels[loc];
  79.       float r1 = red(currentColor2);
  80.       float g1 = green(currentColor2);
  81.       float b1 = blue(currentColor2);
  82.       float r2 = red(trackColor);
  83.       float g2 = green(trackColor);
  84.       float b2 = blue(trackColor);

  85.       // Using euclidean distance to compare colors
  86.       float d = dist(r12,g12,b12,r22,g22,b22); // We are using the dist( ) function to compare the current color with the color we are tracking.
  87. println("x2 = " + closestX2 + " y2 = " + closestY2);
  88.       // If current color is more similar to tracked color than
  89.       // closest color, save current location and current difference
  90.       if (d < worldRecord2) {
  91.         worldRecord2 = d;
  92.         closestX2 = x;
  93.         closestY2 = y;
  94.       }
  95.     }
  96.   }
  97.   
  98.   // We only consider the color found if its color distance is less than 10. 
  99.   // This threshold of 10 is arbitrary and you can adjust this number depending on how accurate you require the tracking to be.
  100.   if (worldRecord < 10) { 
  101.     // Draw a circle at the tracked pixel
  102.     fill(trackColor);
  103.     strokeWeight(4.0);
  104.     stroke(0);
  105.     ellipse(closestX2,closestY2,16,16);
  106.    
  107.   }
  108.  }

  109. void mousePressed() {
  110.   // Save color where the mouse is clicked in trackColor variable
  111.   int loc = mouseX + mouseY*video.width;
  112.   trackColor = video.pixels[loc];
  113. }
Here is the error i am getting
  1. unexpected token: void
I am new to processing and java so this is most likely a stupid mistake, but any help would be appreciated.

~Thanks Idan   

Amplifying a certaing part of a visualization

$
0
0
Hi all (and merry christmas),

I need help. Ive built a heartbeat sensor in Arduino and am now trying to visualize it in Processing. Below is an image of the Arduino input being visualized in Processing:
 



Now my problem is that the heartbeats are only vaguely depicted, so I need to find a way to "amplify" the peaks, so the heartbeats can be more easily read. Like this guy has done. If theres a need for more info or if you have any questions just ask! For more info on the project: http://www.ddlab.dk/i-found-my-pulse/.

Below is the Processing Code I am using, its just a slight rewrite of the Basic Graphing Tutorial:
  1. /*
  2.  Graph
  3.  A simple example of communication from the Arduino board to the computer:
  4.  the value of analog input 0 is sent out the serial port.  We call this "serial"
  5.  communication because the connection appears to both the Arduino and the
  6.  computer as a serial port, even though it may actually use
  7.  a USB cable. Bytes are sent one after another (serially) from the Arduino
  8.  to the computer.
  9.  You can use the Arduino serial monitor to view the sent data, or it can
  10.  be read by Processing, PD, Max/MSP, or any other program capable of reading 
  11.  data from a serial port.  The Processing code below graphs the data received 
  12.  so you can see the value of the analog input changing over time.
  13.  
  14.  The circuit:
  15.  Any analog input sensor is attached to analog in pin 0.
  16.   
  17.  created 2006
  18.  by David A. Mellis
  19.  modified 9 Apr 2012
  20.  by Tom Igoe and Scott Fitzgerald
  21.  
  22.  This example code is in the public domain.

  23.  http://www.arduino.cc/en/Tutorial/Graph
  24.  */

  25. import processing.serial.*;
  26.  
  27.  Serial myPort;        // The serial port
  28.  int xPos = 1;         // horizontal position of the graph
  29.  
  30.  void setup () {
  31.  // set the window size:
  32.  size(1400, 500);      
  33.  
  34.  // List all the available serial ports
  35.  println(Serial.list());
  36.  // I know that the first port in the serial list on my mac
  37.  // is always my  Arduino, so I open Serial.list()[0].
  38.  // Open whatever port is the one you're using.
  39.  myPort = new Serial(this, Serial.list()[0], 9600);
  40.  // don't generate a serialEvent() unless you get a newline character:
  41.  myPort.bufferUntil('\n');
  42.  // set inital background:
  43.  background(0);
  44.  }
  45.  void draw () {
  46.  // everything happens in the serialEvent()
  47.  }
  48.  
  49.  void serialEvent (Serial myPort) {
  50.  // get the ASCII string:
  51.  String inString = myPort.readStringUntil('\n');
  52.  
  53.  if (inString != null) {
  54.  // trim off any whitespace:
  55.  inString = trim(inString);
  56.  // convert to an int and map to the screen height:
  57.  float inByte = float(inString); 
  58.  inByte = map(inByte, 0, 1023, 0, height);
  59.  
  60.  // draw the line:
  61.  stroke(216, 24, 24);
  62.  line(xPos, height, xPos, height - inByte);
  63.  
  64.  // at the edge of the screen, go back to the beginning:
  65.  if (xPos >= width) {
  66.  xPos = 0;
  67.  background(0); 
  68.  } 
  69.  else {
  70.  // increment the horizontal position:
  71.  xPos++;
  72.  }
  73.  }
  74.  }

Regards,
René 

Re : Processing 2.0 - 1,000,000 points in OpenGL using Vertex Arrays

$
0
0
What was missing in the code I posted before is the copy of the Processing projection and modelview matrices to the corresponding matrices in OpenGL:
  1. import javax.media.opengl.GL;
  2. import javax.media.opengl.GL2;
  3. import java.nio.*;
  4.  
  5. // let's try 1,000,000 points
  6. int numPoints = 1000000;
  7.  
  8. // pan, zoom and rotate
  9. float tx = 0, ty = 0;
  10. float sc = 1;
  11. float a = 0.0;
  12.  
  13. FloatBuffer vbuffer;
  14. FloatBuffer cbuffer;
  15. float[] projMatrix;
  16. float[] mvMatrix;
  17.  
  18. void setup() {
  19.   size(displayWidth/2, displayHeight/2, OPENGL); 
  20.   smooth();
  21.   
  22.   PGraphicsOpenGL pg = ((PGraphicsOpenGL)g);
  23.   PGL pgl = pg.beginPGL();
  24.   GL gl = pgl.gl;
  25.   GL2 gl2 = pgl.gl.getGL2();
  26.    
  27.   int vSize = (numPoints * 2);
  28.   int cSize = (numPoints * 3);
  29.   vSize = vSize << 2;
  30.   cSize = cSize << 2;

  31.   vbuffer = ByteBuffer.allocateDirect(vSize).order(ByteOrder.nativeOrder()).asFloatBuffer();
  32.   cbuffer = ByteBuffer.allocateDirect(cSize).order(ByteOrder.nativeOrder()).asFloatBuffer();
  33.   for (int i = 0; i < numPoints; i++) {
  34.     // random x,y
  35.     vbuffer.put(random(width));
  36.     vbuffer.put(random(height));
  37.     // random r,g,b
  38.     cbuffer.put(random(1.0));
  39.     cbuffer.put(random(1.0));
  40.     cbuffer.put(random(1.0));
  41.   }
  42.   vbuffer.rewind();
  43.   cbuffer.rewind();
  44.  
  45.   gl2.glEnableClientState(GL2.GL_VERTEX_ARRAY);
  46.   gl2.glVertexPointer(2, GL2.GL_FLOAT, 0, vbuffer);
  47.  
  48.   gl2.glEnableClientState(GL2.GL_COLOR_ARRAY);
  49.   gl2.glColorPointer(3, GL2.GL_FLOAT, 0, cbuffer);
  50.   
  51.   pg.endPGL();
  52.  
  53.   projMatrix = new float[16];
  54.   mvMatrix = new float[16]; 
  55. }
  56.  
  57. void draw() {
  58.   background(0);
  59.   
  60.   PGraphicsOpenGL pg = ((PGraphicsOpenGL)g);
  61.   PGL pgl = pg.beginPGL();
  62.   GL gl = pgl.gl;
  63.   GL2 gl2 = pgl.gl.getGL2();
  64.   
  65.   
  66.   gl2.glMatrixMode(GL2.GL_PROJECTION);
  67.   projMatrix[0] = pg.projection.m00;
  68.   projMatrix[1] = pg.projection.m10;
  69.   projMatrix[2] = pg.projection.m20;
  70.   projMatrix[3] = pg.projection.m30;

  71.   projMatrix[4] = pg.projection.m01;
  72.   projMatrix[5] = pg.projection.m11;
  73.   projMatrix[6] = pg.projection.m21;
  74.   projMatrix[7] = pg.projection.m31;

  75.   projMatrix[8] = pg.projection.m02;
  76.   projMatrix[9] = pg.projection.m12;
  77.   projMatrix[10] = pg.projection.m22;
  78.   projMatrix[11] = pg.projection.m32;

  79.   projMatrix[12] = pg.projection.m03;
  80.   projMatrix[13] = pg.projection.m13;
  81.   projMatrix[14] = pg.projection.m23;
  82.   projMatrix[15] = pg.projection.m33;
  83.   
  84.   gl2.glLoadMatrixf(projMatrix, 0);
  85.   
  86.   gl2.glMatrixMode(GL2.GL_MODELVIEW);
  87.   mvMatrix[0] = pg.modelview.m00;
  88.   mvMatrix[1] = pg.modelview.m10;
  89.   mvMatrix[2] = pg.modelview.m20;
  90.   mvMatrix[3] = pg.modelview.m30;

  91.   mvMatrix[4] = pg.modelview.m01;
  92.   mvMatrix[5] = pg.modelview.m11;
  93.   mvMatrix[6] = pg.modelview.m21;
  94.   mvMatrix[7] = pg.modelview.m31;

  95.   mvMatrix[8] = pg.modelview.m02;
  96.   mvMatrix[9] = pg.modelview.m12;
  97.   mvMatrix[10] = pg.modelview.m22;
  98.   mvMatrix[11] = pg.modelview.m32;

  99.   mvMatrix[12] = pg.modelview.m03;
  100.   mvMatrix[13] = pg.modelview.m13;
  101.   mvMatrix[14] = pg.modelview.m23;
  102.   mvMatrix[15] = pg.modelview.m33;
  103.   gl2.glLoadMatrixf(mvMatrix, 0);
  104.     
  105.   
  106.   gl2.glPushMatrix();
  107.   gl2.glTranslatef(width/2, height/2, 0);
  108.   gl2.glScalef(sc,sc,sc);
  109.   gl2.glRotatef(a, 0.0, 0.0, 1.0);
  110.   gl2.glTranslatef(-width/2, -height/2, 0);
  111.   gl2.glTranslatef(tx,ty, 0);
  112.  
  113.   gl2.glPointSize(2.0);
  114.  
  115.   gl2.glDrawArrays(GL2.GL_POINTS, 0, numPoints);
  116.  
  117.   gl2.glPopMatrix();

  118.   pg.endPGL();
  119. }
  120.  
  121. void mouseDragged() {
  122.   float dx = (mouseX - pmouseX) / sc;
  123.   float dy = (mouseY - pmouseY) / sc;
  124.   float angle = radians(-a);
  125.   float rx = cos(angle)*dx - sin(angle)*dy;
  126.   float ry = sin(angle)*dx + cos(angle)*dy;
  127.   tx += rx;
  128.   ty += ry;
  129. }
You have to do the glLoadMatrixf() call manually because the OpenGL renderer only uses functions available in the GLES2 API, and all the matrix-related functions are gone in GLES2 (and GL 3.2+). 

Re : Color Tracking with Multiple Cameras

$
0
0
Well I managed to fix the error and organize the code in to functions, but now when I run the program I just get a black screen. I had the USB cameras plugged in and on before running the code and I am still not sure what is wrong. I am also not sure how to make sure each of the camera variables is to a different camera, because I am using two cameras

  1. // Thanks too
  2. // Learning Processing
  3. // Daniel Shiffman
  4. // http://www.learningprocessing.com

  5. // Example 16-11: Simple color tracking
  6. import processing.video.*;

  7. // Variables for capture devices
  8. Capture video;

  9. Capture video2;

  10. // A variable for the color we are searching for.
  11. color trackColor; 

  12. void setup() {
  13.   
  14.   size(640,480);
  15.   video = new Capture(this, width, height,15);
  16.   video.start();
  17.   // Start off tracking for red
  18.   trackColor = color(255,0,0);
  19.   smooth();
  20.  
  21.   size(640,480);
  22.   video2 = new Capture(this, width, height,15);
  23.   video.start();
  24.   // Start off tracking for red
  25.   trackColor = color(255,0,0);
  26.   smooth();
  27.   }
  28. void draw()
  29. {
  30.   cam1();
  31.   cam2();
  32. }

  33. void cam1()
  34. {
  35.   // Capture and display the video
  36.   if (video.available()) {
  37.     video.read();
  38.   }
  39.   video.loadPixels();
  40.   image(video,0,0);

  41.   // Before we begin searching, the "world record" for closest color is set to a high number that is easy for the first pixel to beat.
  42.   float worldRecord = 500; 

  43.   // XY coordinate of closest color
  44.   int closestX = 0;
  45.   int closestY = 0;

  46.   // Begin loop to walk through every pixel
  47.   for (int x = 0; x < video.width; x ++ ) {
  48.     for (int y = 0; y < video.height; y ++ ) {
  49.       int loc = x + y*video.width;
  50.       // What is current color
  51.       color currentColor = video.pixels[loc];
  52.       float r1 = red(currentColor);
  53.       float g1 = green(currentColor);
  54.       float b1 = blue(currentColor);
  55.       float r2 = red(trackColor);
  56.       float g2 = green(trackColor);
  57.       float b2 = blue(trackColor);

  58.       // Using euclidean distance to compare colors
  59.       float d = dist(r1,g1,b1,r2,g2,b2); // We are using the dist( ) function to compare the current color with the color we are tracking.

  60.       // If current color is more similar to tracked color than
  61.       // closest color, save current location and current difference
  62.       if (d < worldRecord) {
  63.         worldRecord = d;
  64.         closestX = x;
  65.         closestY = y;
  66.       }
  67.     }
  68.   }

  69. if (worldRecord < 10) { 
  70.     // Draw a circle at the tracked pixel
  71.     fill(trackColor);
  72.     strokeWeight(4.0);
  73.     stroke(0);
  74.     ellipse(closestX,closestY,16,16);
  75. }
  76. }

  77. void cam2()
  78. {
  79.  // Capture and display the video
  80.   if (video2.available()) {
  81.     video2.read();
  82.   }
  83.   video2.loadPixels();
  84.   image(video2,0,0);

  85.   // Before we begin searching, the "world record" for closest color is set to a high number that is easy for the first pixel to beat.
  86.   float worldRecord = 500; 

  87.   // XY coordinate of closest color
  88.   int closestX2 = 0;
  89.   int closestY2 = 0;

  90.   // Begin loop to walk through every pixel
  91.   for (int x = 0; x < video2.width; x ++ ) {
  92.     for (int y = 0; y < video2.height; y ++ ) {
  93.       int loc = x + y*video2.width;
  94.       // What is current color
  95.       color currentColor2 = video2.pixels[loc];
  96.       float r12 = red(currentColor2);
  97.       float g12 = green(currentColor2);
  98.       float b12 = blue(currentColor2);
  99.       float r22 = red(trackColor);
  100.       float g22 = green(trackColor);
  101.       float b22 = blue(trackColor);

  102.       // Using euclidean distance to compare colors
  103.       float d2 = dist(r12,g12,b12,r22,g22,b22); // We are using the dist( ) function to compare the current color with the color we are tracking.

  104.       // If current color is more similar to tracked color than
  105.       // closest color, save current location and current difference
  106.       if (d2 < worldRecord) {
  107.         worldRecord = d2;
  108.         closestX2 = x;
  109.         closestY2 = y;
  110.       }
  111.     }
  112.   }
  113.  
  114.  if (worldRecord < 10) { 
  115.     // Draw a circle at the tracked pixel
  116.     fill(trackColor);
  117.     strokeWeight(4.0);
  118.     stroke(0);
  119.     ellipse(closestX2,closestY2,16,16); 
  120.     
  121.   }
  122. }

  123. void mousePressed() {
  124.   // Save color where the mouse is clicked in trackColor variable
  125.   int loc = mouseX + mouseY*video.width;
  126.   trackColor = video.pixels[loc];
  127. }
 ~Thanks Idan


2.0b7 Skybox - not working

$
0
0
Trying to create a skybox in the new 2.0x build.  Here is the code, translated from 1.5, but it's not working.  Am I doing something wrong or is this a bug? 

Here is zip of the sketch, which includes the image files.
  1. import processing.opengl.PGraphicsOpenGL;
  2. import javax.media.opengl.GL2;
  3. import com.jogamp.opengl.util.texture.Texture;
  4. import com.jogamp.opengl.util.texture.TextureIO;
  5. import com.jogamp.opengl.util.texture.awt.AWTTextureIO;
  6. import javax.media.opengl.GLProfile;
  7. import java.io.File;
  8. import javax.imageio.ImageIO;
  9. // set skybox filename without orientation part here...
  10. String skyboxName = "besiege";  
  11. PGraphicsOpenGL pgl;
  12. GL2 gl;
  13. int skybox;
  14. void setup()
  15. {
  16.   size(600, 500, OPENGL);
  17.   pgl = (PGraphicsOpenGL) g;
  18.   gl = g.beginPGL().gl.getGL2();
  19.   noStroke();
  20.   loadSkybox(skyboxName, ".png");
  21.   pgl.beginPGL();
  22.   skybox = gl.glGenLists(1);
  23.   gl.glNewList(skybox, GL2.GL_COMPILE);
  24.  
  25.   gl.glFrontFace(GL2.GL_CCW);
  26.   gl.glEnable(GL2.GL_CULL_FACE);
  27.   TexturedCube();
  28.   gl.glDisable(GL2.GL_CULL_FACE);
  29.   gl.glEndList();
  30.   pgl.endPGL();
  31.   float fov = PI/3.0;
  32.   float cameraZ = (height/2.0) / tan(fov/2.0);
  33.   perspective(fov, float(width)/float(height), cameraZ/10.0, cameraZ*200.0);
  34.   // gl.glEnable(GL.GL_CULL_FACE);
  35. }
  36. void draw()
  37. {
  38.   background(0);
  39.   pgl.beginPGL();
  40.   gl.glCallList( skybox );
  41.   pgl.endPGL();
  42.   fill(255);
  43.   pushMatrix();
  44.   translate(width/2,height/2,0);
  45.   sphere(20);
  46.   popMatrix();
  47. }
  48. float p = 40000;   // half skybox size
  49. float m = -p;
  50. // create cube edges
  51. PVector P000 = new PVector (m,m,m);
  52. PVector P010 = new PVector (m,p,m);
  53. PVector P110 = new PVector (p,p,m);
  54. PVector P100 = new PVector (p,m,m);
  55. PVector P001 = new PVector (m,m,p);
  56. PVector P011 = new PVector (m,p,p);
  57. PVector P111 = new PVector (p,p,p);
  58. PVector P101 = new PVector (p,m,p);
  59. Texture tex1,tex2,tex3,tex4,tex5,tex6;   // texture images
  60. // load six skybox images as cube texture
  61. void loadSkybox(String skyboxName, String fExt)
  62. {
  63.   try {
  64.     tex1 = AWTTextureIO.newTexture(GLProfile.getDefault(), ImageIO.read(new File(dataPath(skyboxName + "_front" + fExt))), true);
  65.     tex2 = AWTTextureIO.newTexture(GLProfile.getDefault(), ImageIO.read(new File(dataPath(skyboxName + "_back" + fExt))), true);
  66.     tex3 = AWTTextureIO.newTexture(GLProfile.getDefault(), ImageIO.read(new File(dataPath(skyboxName + "_left" + fExt))), true);
  67.     tex4 = AWTTextureIO.newTexture(GLProfile.getDefault(), ImageIO.read(new File(dataPath(skyboxName + "_right" + fExt))), true);
  68.     tex5 = AWTTextureIO.newTexture(GLProfile.getDefault(), ImageIO.read(new File(dataPath(skyboxName + "_bottom" + fExt))), true);
  69.     tex6 = AWTTextureIO.newTexture(GLProfile.getDefault(), ImageIO.read(new File(dataPath(skyboxName + "_top" + fExt))), true);
  70.   }
  71.   catch (IOException e) {   
  72.     println( e);
  73.   }
  74.   //textureMode(NORMALIZED);
  75. }
  76. // Assign six texture to the six cube faces
  77. void TexturedCube()
  78. {
  79.   TexturedCubeSide (P100, P000, P010, P110, tex1);   // -Z "front" face
  80.   TexturedCubeSide (P001, P101, P111, P011, tex2);   // +Z "back" face
  81.   TexturedCubeSide (P000, P001, P011, P010, tex3);   // -X "left" face
  82.   TexturedCubeSide (P101, P100, P110, P111, tex4);   // +X "right" face
  83.   TexturedCubeSide (P110, P010, P011, P111, tex5);   // +Y "base" face
  84.   TexturedCubeSide (P101, P001, P000, P100, tex6);   // -Y "top" face
  85. }
  86. // create a cube side given by 4 edge vertices and a texture
  87. void TexturedCubeSide(PVector P1, PVector P2, PVector P3, PVector P4, Texture tex)
  88. {
  89.   tex.enable(gl);
  90.   tex.bind(gl);
  91.   gl.glBegin(GL2.GL_QUADS);
  92.   gl.glTexCoord2f(1.0f, 0.0f);
  93.   gl.glVertex3f(P1.x, P1.y, P1.z);
  94.   gl.glTexCoord2f(0.0f, 0.0f);
  95.   gl.glVertex3f(P2.x, P2.y, P2.z);
  96.   gl.glTexCoord2f(0.0f, 1.0f);
  97.   gl.glVertex3f(P3.x, P3.y, P3.z);
  98.   gl.glTexCoord2f(1.0f, 1.0f);
  99.   gl.glVertex3f(P4.x, P4.y, P4.z);
  100.   gl.glEnd();
  101.   tex.disable(gl);
  102. }


Re : 2.0b7 Skybox - not working

$
0
0
Figured it out... wish this was somehow automatically included in Processing.  Have to load the matrix (not sure if it is in each draw or just once in the setup).  As described by Andres:
You have to do the glLoadMatrixf() call manually because the OpenGL renderer only uses functions available in the GLES2 API, and all the matrix-related functions are gone in GLES2 (and GL 3.2+).
  1. import processing.opengl.PGraphicsOpenGL;
  2. import javax.media.opengl.GL2;
  3. import com.jogamp.opengl.util.texture.Texture;
  4. import com.jogamp.opengl.util.texture.TextureIO;
  5. import com.jogamp.opengl.util.texture.awt.AWTTextureIO;
  6. import javax.media.opengl.GLProfile;
  7. import java.io.File;
  8. import javax.imageio.ImageIO;
  9. float[] projMatrix;
  10. float[] mvMatrix;
  11. // set skybox filename without orientation part here...
  12. String skyboxName = "besiege";   
  13. PGraphicsOpenGL pg;
  14. GL2 gl;
  15. int skybox;
  16. float a = 0.0;
  17. void setup()
  18. {
  19.   size(600, 500, OPENGL);
  20.   pg = (PGraphicsOpenGL) g;
  21.   gl = g.beginPGL().gl.getGL2();
  22.   noStroke();  // comment it to see cube edges
  23.   loadSkybox(skyboxName, ".png");
  24.   pg.beginPGL();
  25.   skybox = gl.glGenLists(1);
  26.   gl.glNewList(skybox, GL2.GL_COMPILE);
  27.  
  28.   gl.glFrontFace(GL2.GL_CCW);
  29.   gl.glEnable(GL2.GL_CULL_FACE);
  30.   TexturedCube();
  31.   gl.glDisable(GL2.GL_CULL_FACE);
  32.   gl.glEndList();
  33.   pg.endPGL();
  34.   float fov = PI/3.0;
  35.   float cameraZ = (height/2.0) / tan(fov/2.0);
  36.   perspective(fov, float(width)/float(height), cameraZ/10.0, cameraZ*200.0);
  37.   projMatrix = new float[16];
  38.   mvMatrix = new float[16];
  39.  
  40.   // gl.glEnable(GL.GL_CULL_FACE);
  41. }
  42. void draw()
  43. {
  44.   background(0);
  45.   loadMatrix();
  46.   pg.beginPGL();
  47.   gl.glCallList( skybox );
  48.   pg.endPGL();
  49.   fill(255);
  50.   pushMatrix();
  51.   translate(width/2,height/2,0);
  52.   sphere(20);
  53.   popMatrix();
  54.   //println("FPS: "+ frameRate);
  55. }
  56. float p = 40000;   // half skybox size
  57. float m = -p;
  58. // create cube edges
  59. PVector P000 = new PVector (m,m,m);
  60. PVector P010 = new PVector (m,p,m);
  61. PVector P110 = new PVector (p,p,m);
  62. PVector P100 = new PVector (p,m,m);
  63. PVector P001 = new PVector (m,m,p);
  64. PVector P011 = new PVector (m,p,p);
  65. PVector P111 = new PVector (p,p,p);
  66. PVector P101 = new PVector (p,m,p);
  67. Texture tex1,tex2,tex3,tex4,tex5,tex6;   // texture images
  68. // load six skybox images as cube texture
  69. void loadSkybox(String skyboxName, String fExt)
  70. {
  71.   try {
  72.     tex1 = AWTTextureIO.newTexture(GLProfile.getDefault(), ImageIO.read(new File(dataPath(skyboxName + "_front" + fExt))), true);
  73.     tex2 = AWTTextureIO.newTexture(GLProfile.getDefault(), ImageIO.read(new File(dataPath(skyboxName + "_back" + fExt))), true);
  74.     tex3 = AWTTextureIO.newTexture(GLProfile.getDefault(), ImageIO.read(new File(dataPath(skyboxName + "_left" + fExt))), true);
  75.     tex4 = AWTTextureIO.newTexture(GLProfile.getDefault(), ImageIO.read(new File(dataPath(skyboxName + "_right" + fExt))), true);
  76.     tex5 = AWTTextureIO.newTexture(GLProfile.getDefault(), ImageIO.read(new File(dataPath(skyboxName + "_bottom" + fExt))), true);
  77.     tex6 = AWTTextureIO.newTexture(GLProfile.getDefault(), ImageIO.read(new File(dataPath(skyboxName + "_top" + fExt))), true);
  78.   }
  79.   catch (IOException e) {    
  80.     println( e);
  81.   }
  82.   //textureMode(NORMALIZED);
  83. }
  84. // Assign six texture to the six cube faces
  85. void TexturedCube()
  86. {
  87.   TexturedCubeSide (P100, P000, P010, P110, tex1);   // -Z "front" face
  88.   TexturedCubeSide (P001, P101, P111, P011, tex2);   // +Z "back" face
  89.   TexturedCubeSide (P000, P001, P011, P010, tex3);   // -X "left" face
  90.   TexturedCubeSide (P101, P100, P110, P111, tex4);   // +X "right" face
  91.   TexturedCubeSide (P110, P010, P011, P111, tex5);   // +Y "base" face
  92.   TexturedCubeSide (P101, P001, P000, P100, tex6);   // -Y "top" face
  93. }
  94. // create a cube side given by 4 edge vertices and a texture
  95. void TexturedCubeSide(PVector P1, PVector P2, PVector P3, PVector P4, Texture tex)
  96. {
  97.   tex.enable(gl);
  98.   tex.bind(gl);
  99.   gl.glBegin(GL2.GL_QUADS);
  100.   gl.glTexCoord2f(1.0f, 0.0f);
  101.   gl.glVertex3f(P1.x, P1.y, P1.z);
  102.   gl.glTexCoord2f(0.0f, 0.0f);
  103.   gl.glVertex3f(P2.x, P2.y, P2.z);
  104.   gl.glTexCoord2f(0.0f, 1.0f);
  105.   gl.glVertex3f(P3.x, P3.y, P3.z);
  106.   gl.glTexCoord2f(1.0f, 1.0f);
  107.   gl.glVertex3f(P4.x, P4.y, P4.z);
  108.   gl.glEnd();
  109.   tex.disable(gl);
  110. }
  111. void loadMatrix() {
  112.   gl.glMatrixMode(GL2.GL_PROJECTION);
  113.   projMatrix[0] = pg.projection.m00;
  114.   projMatrix[1] = pg.projection.m10;
  115.   projMatrix[2] = pg.projection.m20;
  116.   projMatrix[3] = pg.projection.m30;
  117.  
  118.   projMatrix[4] = pg.projection.m01;
  119.   projMatrix[5] = pg.projection.m11;
  120.   projMatrix[6] = pg.projection.m21;
  121.   projMatrix[7] = pg.projection.m31;
  122.  
  123.   projMatrix[8] = pg.projection.m02;
  124.   projMatrix[9] = pg.projection.m12;
  125.   projMatrix[10] = pg.projection.m22;
  126.   projMatrix[11] = pg.projection.m32;
  127.  
  128.   projMatrix[12] = pg.projection.m03;
  129.   projMatrix[13] = pg.projection.m13;
  130.   projMatrix[14] = pg.projection.m23;
  131.   projMatrix[15] = pg.projection.m33;
  132.  
  133.   gl.glLoadMatrixf(projMatrix, 0);
  134.  
  135.   gl.glMatrixMode(GL2.GL_MODELVIEW);
  136.   mvMatrix[0] = pg.modelview.m00;
  137.   mvMatrix[1] = pg.modelview.m10;
  138.   mvMatrix[2] = pg.modelview.m20;
  139.   mvMatrix[3] = pg.modelview.m30;
  140.  
  141.   mvMatrix[4] = pg.modelview.m01;
  142.   mvMatrix[5] = pg.modelview.m11;
  143.   mvMatrix[6] = pg.modelview.m21;
  144.   mvMatrix[7] = pg.modelview.m31;
  145.  
  146.   mvMatrix[8] = pg.modelview.m02;
  147.   mvMatrix[9] = pg.modelview.m12;
  148.   mvMatrix[10] = pg.modelview.m22;
  149.   mvMatrix[11] = pg.modelview.m32;
  150.  
  151.   mvMatrix[12] = pg.modelview.m03;
  152.   mvMatrix[13] = pg.modelview.m13;
  153.   mvMatrix[14] = pg.modelview.m23;
  154.   mvMatrix[15] = pg.modelview.m33;
  155.   gl.glLoadMatrixf(mvMatrix, 0);
  156. }

Re : Processing 2.0 - 1,000,000 points in OpenGL using Vertex Arrays

$
0
0
Thanks Andres, this looks like it fixed a few sketches for me.  Is there anything we can do to simplify this - a new call to load this that is compatible? Does the matrix need to be loaded once, or is it in each draw cycle?  If the matrix-related functions are gone, what is the replacement in GLES2 and GL 3.2+?

Maybe the matrix could automatically be loaded for the GL if it is compatible... ?

Thanks, Jeff

Re : Processing 2.0 - 1,000,000 points in OpenGL using Vertex Arrays

$
0
0
You should load the matrices every time you change them: for example, if you call perspective() or ortho(), then you have to load the projection matrix. If you use camera() or any geometric transformation (translate, rotate, scale), then you need to load the modelview.

There is no replacement for those functions in GLES2, GL3+, you have to take care of the projection/modelview matrices all by yourself and then pass the corresponding arrays to the shaders. 


Re : Processing 2.0 - 1,000,000 points in OpenGL using Vertex Arrays

$
0
0
Ok, so has a shader already been written for Processing that is called when these are used?  If so, is it public where I could call it as oppose to doing my own.  If not, doesn't Processing need it?  Thanks :)
Viewing all 1768 articles
Browse latest View live