December 16, 2012, 2:12 pm
hello all,
this is a simple OPENGL-code, it worked in 1.51, doesn't work anymore....
can anyone help?
Thank you!
Chrisir
- // imports ---------------------------------------------------
- //import processing.opengl.PGraphicsOpenGL;
- import javax.media.opengl.GL;
- import javax.media.opengl.glu.GLU;
- import javax.media.opengl.glu.GLUquadric;
- import com.sun.opengl.util.texture.Texture;
- import com.sun.opengl.util.texture.TextureIO;
- import peasy.PeasyCam; //peasy camera control
- // opengl
- import processing.opengl.*;
- /** PeasyCam **/
- import peasy.*;
- // Audio
- //import ddf.minim.*;
- // for export
- import superCAD.*;
- // global vars -----------------------------------------------
- // control behaviour parameters --------------------
- boolean testingForDebugging = false;
- int showingCubeData = 0;
- //
- boolean boolGl_Control = false; // Marble with opengl?
- boolean boolGl_Control_for_Boxes = false; // Boxes with opengl?
- // Camera Types
- boolean CameraMoving = false; // camera always ahead of Marble
- boolean CameraMoving2 = false; // good for movie (camera on a circle track)
- // Movie
- boolean boolMakeMovie = false; // saves a Movie
- boolean boolUseCheckeredFloor = true; // Floor Type
- // other vars ---------------------------------------------------
- // cube
- final int cubeSize = 49; // width, height and depth
- final int undefinedCube = -11;
- // GL stuff
- PGraphicsOpenGL pgl;
- GL gl;
- GLU glu;
- GLUquadric mysphere;
- GLUquadric myBox;
- Texture textureMarble;
- float marbleX=200;
- float marbleY=200;
- float marbleZ=-100;
- float r = 19; // Sized to fit the texture
- float rotateMarble = 0.39;
- float rotateMarbleSpeed = 0.02; // .196
- float MarbleRotateX=0.2;
- float MarbleRotateY=0.2;
- float MarbleRotateZ=0.2;
- void setup() {
- size(890, 660, OPENGL);
- // size(screenWidth, screenHeight, OPENGL );
- // noCursor();
- //cam.setMinimumDistance(40);
- //cam.setMaximumDistance(6550);
- // cam.lookAt( 70, 390, 0 );
- pgl = (PGraphicsOpenGL) g;
- gl = pgl.gl;
- glu = pgl.glu;
- mysphere = glu.gluNewQuadric();
- glu.gluQuadricDrawStyle(mysphere, glu.GLU_FILL);
- glu.gluQuadricNormals(mysphere, glu.GLU_NONE);
- glu.gluQuadricTexture(mysphere, true);
- myBox = glu.gluNewQuadric();
- glu.gluQuadricDrawStyle(myBox, glu.GLU_FILL);
- glu.gluQuadricNormals(myBox, glu.GLU_NONE);
- glu.gluQuadricTexture(myBox, true);
- try {
- textureMarble = TextureIO.newTexture(new
- File(dataPath("textures2.jpg")), true);
- }
- catch (IOException e) {
- println("failed to load picture - line 159. ++++++++++++++++++++++");
- javax.swing.JOptionPane.showMessageDialog(this, e);
- exit();
- }
- } // func
- void draw ( ) {
- background (0);
- PaintMarble () ;
- }
- void PaintMarble () {
- pgl.beginGL();
- gl.glPushMatrix();
- gl.glTranslatef( marbleX, marbleY, marbleZ);
- gl.glRotatef(degrees(rotateMarble), MarbleRotateX, MarbleRotateY, MarbleRotateZ);
- gl.glColor3f(1, 1, 1);
- textureMarble.enable();
- textureMarble.bind();
- //The Sphere
- glu.gluSphere(mysphere, r, 40, 40);
- textureMarble.disable();
- gl.glPopMatrix();
- pgl.endGL();
- if (rotateMarble < 360) {
- rotateMarble = rotateMarble + rotateMarbleSpeed;
- }
- else {
- rotateMarble = 0;
- }
- } // method
↧
December 17, 2012, 9:52 am
or let me reframe the question:
how can I texture a sphere and let it look like it's rolling?
Thank you!
↧
↧
December 17, 2012, 2:39 pm
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?
↧
December 17, 2012, 2:53 pm
No need for begin/endShape, only begin/endDraw are necessary to draw on a PGraphics.
↧
December 18, 2012, 12:48 am
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_TOOLAnybody encounter similar problem?
↧
↧
December 18, 2012, 1:11 am
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...
↧
December 18, 2012, 8:37 am
↧
December 18, 2012, 8:57 am
The filer(PShader) function should work with a PGraphics surface, as long as it is created as P2D or P3D:
- PShader edges;
- boolean applyFilter = true;
- PGraphics pg;
- void setup() {
- size(640, 360, P3D);
- edges = loadShader("edges.glsl");
- pg = createGraphics(640, 360, P3D);
- noStroke();
- }
- void draw() {
- pg.beginDraw();
- pg.noStroke();
- pg.background(0);
- pg.lights();
-
- pg.translate(width/2, height/2);
- pg.pushMatrix();
- pg.rotateX(frameCount * 0.01);
- pg.rotateY(frameCount * 0.01);
- pg.box(120);
- pg.popMatrix();
-
- if (applyFilter == true) {
- pg.filter(edges);
- }
-
- // The sphere doesn't have the edge detection applied
- // on it because it is drawn after filter() is called.
- pg.rotateY(frameCount * 0.02);
- pg.translate(150, 0);
- pg.sphere(40);
- pg.endDraw();
-
- image(pg, 0, 0, width, height);
- }
- void mousePressed() {
- applyFilter = !applyFilter;
- }
↧
December 18, 2012, 10:05 am
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
↧
↧
December 18, 2012, 10:15 am
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.
↧
December 18, 2012, 11:51 am
↧
December 18, 2012, 9:30 pm
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?
- // Learning Processing
- // Daniel Shiffman
- // http://www.learningprocessing.com
- // Example 16-11: Simple color tracking
- import processing.video.*;
- // Variable for capture device
- Capture video;
- // A variable for the color we are searching for.
- color trackColor;
- void setup() {
- size(640,480);
- video = new Capture(this, width, height,15);
- // Start off tracking for red
- trackColor = color(255,0,0);
- video.start();
- smooth();
- }
- void draw() {
- // Capture and display the video
- if (video.available()) {
- video.read();
- }
- video.loadPixels();
- image(video,0,0);
- // 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.
- float worldRecord = 500;
- // XY coordinate of closest color
- int closestX = 0;
- int closestY = 0;
- // Begin loop to walk through every pixel
- for (int x = 0; x < video.width; x ++ ) {
- for (int y = 0; y < video.height; y ++ ) {
- int loc = x + y*video.width;
- // What is current color
- color currentColor = video.pixels[loc];
- float r1 = red(currentColor);
- float g1 = green(currentColor);
- float b1 = blue(currentColor);
- float r2 = red(trackColor);
- float g2 = green(trackColor);
- float b2 = blue(trackColor);
- // Using euclidean distance to compare colors
- 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.
- // If current color is more similar to tracked color than
- // closest color, save current location and current difference
- if (d < worldRecord) {
- worldRecord = d;
- closestX = x;
- closestY = y;
- }
- }
- }
-
- println("x = " + closestX + " y = " + closestY);
- // We only consider the color found if its color distance is less than 10.
- // This threshold of 10 is arbitrary and you can adjust this number depending on how accurate you require the tracking to be.
- if (worldRecord < 10) {
- // Draw a circle at the tracked pixel
- fill(trackColor);
- strokeWeight(4.0);
- stroke(0);
- ellipse(closestX,closestY,16,16);
-
- }
- }
- void mousePressed() {
- // Save color where the mouse is clicked in trackColor variable
- int loc = mouseX + mouseY*video.width;
- trackColor = video.pixels[loc]; }
Here is what I tried to do.
- // Learning Processing
- // Daniel Shiffman
- // http://www.learningprocessing.com
- // Example 16-11: Simple color tracking
- import processing.video.*;
- // Variable for capture device
- Capture video;
- Capture video2;
- // A variable for the color we are searching for.
- color trackColor;
- void setup() {
- size(640,480);
- video = new Capture(this, width, height,15);
- // Start off tracking for red
- trackColor = color(255,0,0);
- video.start();
- smooth();
-
- size(640,480);
- video2 = new Capture(this, width, height,15);
- // Start off tracking for red
- trackColor = color(255,0,0);
- video2.start();
- smooth();
- }
- void draw() {
- // Capture and display the video
- if (video.available()) {
- video.read();
- }
- video.loadPixels();
- image(video,0,0);
- // 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.
- float worldRecord = 500;
- // XY coordinate of closest color
- int closestX = 0;
- int closestY = 0;
- // Begin loop to walk through every pixel
- for (int x = 0; x < video.width; x ++ ) {
- for (int y = 0; y < video.height; y ++ ) {
- int loc = x + y*video.width;
- // What is current color
- color currentColor = video.pixels[loc];
- float r1 = red(currentColor);
- float g1 = green(currentColor);
- float b1 = blue(currentColor);
- float r2 = red(trackColor);
- float g2 = green(trackColor);
- float b2 = blue(trackColor);
- // Using euclidean distance to compare colors
- 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.
- println("x = " + closestX + " y = " + closestY);
- // If current color is more similar to tracked color than
- // closest color, save current location and current difference
- if (d < worldRecord) {
- worldRecord = d;
- closestX = x;
- closestY = y;
- }
- }
-
- //second camera below
- if (video2.available()) {
- video2.read();
- }
- video2.loadPixels();
- image(video2,0,0);
- // 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.
- float worldRecord = 500;
- // XY coordinate of closest color
- int closestX2 = 0;
- int closestY2 = 0;
- // Begin loop to walk through every pixel
- for (int x = 0; x < video2.width; x ++ ) {
- for (int y = 0; y < video2.height; y ++ ) {
- int loc = x + y*video2.width;
- // What is current color
- color currentColor2 = video2.pixels[loc];
- float r1 = red(currentColor2);
- float g1 = green(currentColor2);
- float b1 = blue(currentColor2);
- float r2 = red(trackColor);
- float g2 = green(trackColor);
- float b2 = blue(trackColor);
- // Using euclidean distance to compare colors
- 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.
- println("x2 = " + closestX2 + " y2 = " + closestY2);
- // If current color is more similar to tracked color than
- // closest color, save current location and current difference
- if (d < worldRecord2) {
- worldRecord2 = d;
- closestX2 = x;
- closestY2 = y;
- }
- }
- }
-
- // We only consider the color found if its color distance is less than 10.
- // This threshold of 10 is arbitrary and you can adjust this number depending on how accurate you require the tracking to be.
- if (worldRecord < 10) {
- // Draw a circle at the tracked pixel
- fill(trackColor);
- strokeWeight(4.0);
- stroke(0);
- ellipse(closestX2,closestY2,16,16);
-
- }
- }
- void mousePressed() {
- // Save color where the mouse is clicked in trackColor variable
- int loc = mouseX + mouseY*video.width;
- trackColor = video.pixels[loc];
- }
Here is the error i am getting
- 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
↧
December 19, 2012, 2:42 am
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:
- /*
- Graph
- A simple example of communication from the Arduino board to the computer:
- the value of analog input 0 is sent out the serial port. We call this "serial"
- communication because the connection appears to both the Arduino and the
- computer as a serial port, even though it may actually use
- a USB cable. Bytes are sent one after another (serially) from the Arduino
- to the computer.
- You can use the Arduino serial monitor to view the sent data, or it can
- be read by Processing, PD, Max/MSP, or any other program capable of reading
- data from a serial port. The Processing code below graphs the data received
- so you can see the value of the analog input changing over time.
-
- The circuit:
- Any analog input sensor is attached to analog in pin 0.
-
- created 2006
- by David A. Mellis
- modified 9 Apr 2012
- by Tom Igoe and Scott Fitzgerald
-
- This example code is in the public domain.
- http://www.arduino.cc/en/Tutorial/Graph
- */
- import processing.serial.*;
-
- Serial myPort; // The serial port
- int xPos = 1; // horizontal position of the graph
-
- void setup () {
- // set the window size:
- size(1400, 500);
-
- // List all the available serial ports
- println(Serial.list());
- // I know that the first port in the serial list on my mac
- // is always my Arduino, so I open Serial.list()[0].
- // Open whatever port is the one you're using.
- myPort = new Serial(this, Serial.list()[0], 9600);
- // don't generate a serialEvent() unless you get a newline character:
- myPort.bufferUntil('\n');
- // set inital background:
- background(0);
- }
- void draw () {
- // everything happens in the serialEvent()
- }
-
- void serialEvent (Serial myPort) {
- // get the ASCII string:
- String inString = myPort.readStringUntil('\n');
-
- if (inString != null) {
- // trim off any whitespace:
- inString = trim(inString);
- // convert to an int and map to the screen height:
- float inByte = float(inString);
- inByte = map(inByte, 0, 1023, 0, height);
-
- // draw the line:
- stroke(216, 24, 24);
- line(xPos, height, xPos, height - inByte);
-
- // at the edge of the screen, go back to the beginning:
- if (xPos >= width) {
- xPos = 0;
- background(0);
- }
- else {
- // increment the horizontal position:
- xPos++;
- }
- }
- }
Regards,
René
↧
↧
December 19, 2012, 7:41 am
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:
- import javax.media.opengl.GL;
- import javax.media.opengl.GL2;
- import java.nio.*;
-
- // let's try 1,000,000 points
- int numPoints = 1000000;
-
- // pan, zoom and rotate
- float tx = 0, ty = 0;
- float sc = 1;
- float a = 0.0;
-
- FloatBuffer vbuffer;
- FloatBuffer cbuffer;
- float[] projMatrix;
- float[] mvMatrix;
-
- void setup() {
- size(displayWidth/2, displayHeight/2, OPENGL);
- smooth();
-
- PGraphicsOpenGL pg = ((PGraphicsOpenGL)g);
- PGL pgl = pg.beginPGL();
- GL gl = pgl.gl;
- GL2 gl2 = pgl.gl.getGL2();
-
- int vSize = (numPoints * 2);
- int cSize = (numPoints * 3);
- vSize = vSize << 2;
- cSize = cSize << 2;
- vbuffer = ByteBuffer.allocateDirect(vSize).order(ByteOrder.nativeOrder()).asFloatBuffer();
- cbuffer = ByteBuffer.allocateDirect(cSize).order(ByteOrder.nativeOrder()).asFloatBuffer();
- for (int i = 0; i < numPoints; i++) {
- // random x,y
- vbuffer.put(random(width));
- vbuffer.put(random(height));
- // random r,g,b
- cbuffer.put(random(1.0));
- cbuffer.put(random(1.0));
- cbuffer.put(random(1.0));
- }
- vbuffer.rewind();
- cbuffer.rewind();
-
- gl2.glEnableClientState(GL2.GL_VERTEX_ARRAY);
- gl2.glVertexPointer(2, GL2.GL_FLOAT, 0, vbuffer);
-
- gl2.glEnableClientState(GL2.GL_COLOR_ARRAY);
- gl2.glColorPointer(3, GL2.GL_FLOAT, 0, cbuffer);
-
- pg.endPGL();
-
- projMatrix = new float[16];
- mvMatrix = new float[16];
- }
-
- void draw() {
- background(0);
-
- PGraphicsOpenGL pg = ((PGraphicsOpenGL)g);
- PGL pgl = pg.beginPGL();
- GL gl = pgl.gl;
- GL2 gl2 = pgl.gl.getGL2();
-
-
- gl2.glMatrixMode(GL2.GL_PROJECTION);
- projMatrix[0] = pg.projection.m00;
- projMatrix[1] = pg.projection.m10;
- projMatrix[2] = pg.projection.m20;
- projMatrix[3] = pg.projection.m30;
- projMatrix[4] = pg.projection.m01;
- projMatrix[5] = pg.projection.m11;
- projMatrix[6] = pg.projection.m21;
- projMatrix[7] = pg.projection.m31;
- projMatrix[8] = pg.projection.m02;
- projMatrix[9] = pg.projection.m12;
- projMatrix[10] = pg.projection.m22;
- projMatrix[11] = pg.projection.m32;
- projMatrix[12] = pg.projection.m03;
- projMatrix[13] = pg.projection.m13;
- projMatrix[14] = pg.projection.m23;
- projMatrix[15] = pg.projection.m33;
-
- gl2.glLoadMatrixf(projMatrix, 0);
-
- gl2.glMatrixMode(GL2.GL_MODELVIEW);
- mvMatrix[0] = pg.modelview.m00;
- mvMatrix[1] = pg.modelview.m10;
- mvMatrix[2] = pg.modelview.m20;
- mvMatrix[3] = pg.modelview.m30;
- mvMatrix[4] = pg.modelview.m01;
- mvMatrix[5] = pg.modelview.m11;
- mvMatrix[6] = pg.modelview.m21;
- mvMatrix[7] = pg.modelview.m31;
- mvMatrix[8] = pg.modelview.m02;
- mvMatrix[9] = pg.modelview.m12;
- mvMatrix[10] = pg.modelview.m22;
- mvMatrix[11] = pg.modelview.m32;
- mvMatrix[12] = pg.modelview.m03;
- mvMatrix[13] = pg.modelview.m13;
- mvMatrix[14] = pg.modelview.m23;
- mvMatrix[15] = pg.modelview.m33;
- gl2.glLoadMatrixf(mvMatrix, 0);
-
-
- gl2.glPushMatrix();
- gl2.glTranslatef(width/2, height/2, 0);
- gl2.glScalef(sc,sc,sc);
- gl2.glRotatef(a, 0.0, 0.0, 1.0);
- gl2.glTranslatef(-width/2, -height/2, 0);
- gl2.glTranslatef(tx,ty, 0);
-
- gl2.glPointSize(2.0);
-
- gl2.glDrawArrays(GL2.GL_POINTS, 0, numPoints);
-
- gl2.glPopMatrix();
- pg.endPGL();
- }
-
- void mouseDragged() {
- float dx = (mouseX - pmouseX) / sc;
- float dy = (mouseY - pmouseY) / sc;
- float angle = radians(-a);
- float rx = cos(angle)*dx - sin(angle)*dy;
- float ry = sin(angle)*dx + cos(angle)*dy;
- tx += rx;
- ty += ry;
- }
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+).
↧
December 19, 2012, 12:07 pm
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
- // Thanks too
- // Learning Processing
- // Daniel Shiffman
- // http://www.learningprocessing.com
- // Example 16-11: Simple color tracking
- import processing.video.*;
- // Variables for capture devices
- Capture video;
- Capture video2;
- // A variable for the color we are searching for.
- color trackColor;
- void setup() {
-
- size(640,480);
- video = new Capture(this, width, height,15);
- video.start();
- // Start off tracking for red
- trackColor = color(255,0,0);
- smooth();
-
- size(640,480);
- video2 = new Capture(this, width, height,15);
- video.start();
- // Start off tracking for red
- trackColor = color(255,0,0);
- smooth();
- }
- void draw()
- {
- cam1();
- cam2();
- }
- void cam1()
- {
- // Capture and display the video
- if (video.available()) {
- video.read();
- }
- video.loadPixels();
- image(video,0,0);
- // 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.
- float worldRecord = 500;
- // XY coordinate of closest color
- int closestX = 0;
- int closestY = 0;
- // Begin loop to walk through every pixel
- for (int x = 0; x < video.width; x ++ ) {
- for (int y = 0; y < video.height; y ++ ) {
- int loc = x + y*video.width;
- // What is current color
- color currentColor = video.pixels[loc];
- float r1 = red(currentColor);
- float g1 = green(currentColor);
- float b1 = blue(currentColor);
- float r2 = red(trackColor);
- float g2 = green(trackColor);
- float b2 = blue(trackColor);
- // Using euclidean distance to compare colors
- 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.
- // If current color is more similar to tracked color than
- // closest color, save current location and current difference
- if (d < worldRecord) {
- worldRecord = d;
- closestX = x;
- closestY = y;
- }
- }
- }
- if (worldRecord < 10) {
- // Draw a circle at the tracked pixel
- fill(trackColor);
- strokeWeight(4.0);
- stroke(0);
- ellipse(closestX,closestY,16,16);
- }
- }
- void cam2()
- {
- // Capture and display the video
- if (video2.available()) {
- video2.read();
- }
- video2.loadPixels();
- image(video2,0,0);
- // 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.
- float worldRecord = 500;
- // XY coordinate of closest color
- int closestX2 = 0;
- int closestY2 = 0;
- // Begin loop to walk through every pixel
- for (int x = 0; x < video2.width; x ++ ) {
- for (int y = 0; y < video2.height; y ++ ) {
- int loc = x + y*video2.width;
- // What is current color
- color currentColor2 = video2.pixels[loc];
- float r12 = red(currentColor2);
- float g12 = green(currentColor2);
- float b12 = blue(currentColor2);
- float r22 = red(trackColor);
- float g22 = green(trackColor);
- float b22 = blue(trackColor);
- // Using euclidean distance to compare colors
- 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.
- // If current color is more similar to tracked color than
- // closest color, save current location and current difference
- if (d2 < worldRecord) {
- worldRecord = d2;
- closestX2 = x;
- closestY2 = y;
- }
- }
- }
-
- if (worldRecord < 10) {
- // Draw a circle at the tracked pixel
- fill(trackColor);
- strokeWeight(4.0);
- stroke(0);
- ellipse(closestX2,closestY2,16,16);
-
- }
- }
- void mousePressed() {
- // Save color where the mouse is clicked in trackColor variable
- int loc = mouseX + mouseY*video.width;
- trackColor = video.pixels[loc];
- }
↧
December 19, 2012, 1:19 pm
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.
- import processing.opengl.PGraphicsOpenGL;
- import javax.media.opengl.GL2;
- import com.jogamp.opengl.util.texture.Texture;
- import com.jogamp.opengl.util.texture.TextureIO;
- import com.jogamp.opengl.util.texture.awt.AWTTextureIO;
- import javax.media.opengl.GLProfile;
- import java.io.File;
- import javax.imageio.ImageIO;
- // set skybox filename without orientation part here...
- String skyboxName = "besiege";
- PGraphicsOpenGL pgl;
- GL2 gl;
- int skybox;
- void setup()
- {
- size(600, 500, OPENGL);
- pgl = (PGraphicsOpenGL) g;
- gl = g.beginPGL().gl.getGL2();
- noStroke();
- loadSkybox(skyboxName, ".png");
- pgl.beginPGL();
- skybox = gl.glGenLists(1);
- gl.glNewList(skybox, GL2.GL_COMPILE);
-
- gl.glFrontFace(GL2.GL_CCW);
- gl.glEnable(GL2.GL_CULL_FACE);
- TexturedCube();
- gl.glDisable(GL2.GL_CULL_FACE);
- gl.glEndList();
- pgl.endPGL();
- float fov = PI/3.0;
- float cameraZ = (height/2.0) / tan(fov/2.0);
- perspective(fov, float(width)/float(height), cameraZ/10.0, cameraZ*200.0);
- // gl.glEnable(GL.GL_CULL_FACE);
- }
- void draw()
- {
- background(0);
- pgl.beginPGL();
- gl.glCallList( skybox );
- pgl.endPGL();
- fill(255);
- pushMatrix();
- translate(width/2,height/2,0);
- sphere(20);
- popMatrix();
- }
- float p = 40000; // half skybox size
- float m = -p;
- // create cube edges
- PVector P000 = new PVector (m,m,m);
- PVector P010 = new PVector (m,p,m);
- PVector P110 = new PVector (p,p,m);
- PVector P100 = new PVector (p,m,m);
- PVector P001 = new PVector (m,m,p);
- PVector P011 = new PVector (m,p,p);
- PVector P111 = new PVector (p,p,p);
- PVector P101 = new PVector (p,m,p);
- Texture tex1,tex2,tex3,tex4,tex5,tex6; // texture images
- // load six skybox images as cube texture
- void loadSkybox(String skyboxName, String fExt)
- {
- try {
- tex1 = AWTTextureIO.newTexture(GLProfile.getDefault(), ImageIO.read(new File(dataPath(skyboxName + "_front" + fExt))), true);
- tex2 = AWTTextureIO.newTexture(GLProfile.getDefault(), ImageIO.read(new File(dataPath(skyboxName + "_back" + fExt))), true);
- tex3 = AWTTextureIO.newTexture(GLProfile.getDefault(), ImageIO.read(new File(dataPath(skyboxName + "_left" + fExt))), true);
- tex4 = AWTTextureIO.newTexture(GLProfile.getDefault(), ImageIO.read(new File(dataPath(skyboxName + "_right" + fExt))), true);
- tex5 = AWTTextureIO.newTexture(GLProfile.getDefault(), ImageIO.read(new File(dataPath(skyboxName + "_bottom" + fExt))), true);
- tex6 = AWTTextureIO.newTexture(GLProfile.getDefault(), ImageIO.read(new File(dataPath(skyboxName + "_top" + fExt))), true);
- }
- catch (IOException e) {
- println( e);
- }
- //textureMode(NORMALIZED);
- }
- // Assign six texture to the six cube faces
- void TexturedCube()
- {
- TexturedCubeSide (P100, P000, P010, P110, tex1); // -Z "front" face
- TexturedCubeSide (P001, P101, P111, P011, tex2); // +Z "back" face
- TexturedCubeSide (P000, P001, P011, P010, tex3); // -X "left" face
- TexturedCubeSide (P101, P100, P110, P111, tex4); // +X "right" face
- TexturedCubeSide (P110, P010, P011, P111, tex5); // +Y "base" face
- TexturedCubeSide (P101, P001, P000, P100, tex6); // -Y "top" face
- }
- // create a cube side given by 4 edge vertices and a texture
- void TexturedCubeSide(PVector P1, PVector P2, PVector P3, PVector P4, Texture tex)
- {
- tex.enable(gl);
- tex.bind(gl);
- gl.glBegin(GL2.GL_QUADS);
- gl.glTexCoord2f(1.0f, 0.0f);
- gl.glVertex3f(P1.x, P1.y, P1.z);
- gl.glTexCoord2f(0.0f, 0.0f);
- gl.glVertex3f(P2.x, P2.y, P2.z);
- gl.glTexCoord2f(0.0f, 1.0f);
- gl.glVertex3f(P3.x, P3.y, P3.z);
- gl.glTexCoord2f(1.0f, 1.0f);
- gl.glVertex3f(P4.x, P4.y, P4.z);
- gl.glEnd();
- tex.disable(gl);
- }
↧
December 19, 2012, 2:07 pm
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+).
- import processing.opengl.PGraphicsOpenGL;
- import javax.media.opengl.GL2;
- import com.jogamp.opengl.util.texture.Texture;
- import com.jogamp.opengl.util.texture.TextureIO;
- import com.jogamp.opengl.util.texture.awt.AWTTextureIO;
- import javax.media.opengl.GLProfile;
- import java.io.File;
- import javax.imageio.ImageIO;
- float[] projMatrix;
- float[] mvMatrix;
- // set skybox filename without orientation part here...
- String skyboxName = "besiege";
- PGraphicsOpenGL pg;
- GL2 gl;
- int skybox;
- float a = 0.0;
- void setup()
- {
- size(600, 500, OPENGL);
- pg = (PGraphicsOpenGL) g;
- gl = g.beginPGL().gl.getGL2();
- noStroke(); // comment it to see cube edges
- loadSkybox(skyboxName, ".png");
- pg.beginPGL();
- skybox = gl.glGenLists(1);
- gl.glNewList(skybox, GL2.GL_COMPILE);
-
- gl.glFrontFace(GL2.GL_CCW);
- gl.glEnable(GL2.GL_CULL_FACE);
- TexturedCube();
- gl.glDisable(GL2.GL_CULL_FACE);
- gl.glEndList();
- pg.endPGL();
- float fov = PI/3.0;
- float cameraZ = (height/2.0) / tan(fov/2.0);
- perspective(fov, float(width)/float(height), cameraZ/10.0, cameraZ*200.0);
- projMatrix = new float[16];
- mvMatrix = new float[16];
-
- // gl.glEnable(GL.GL_CULL_FACE);
- }
- void draw()
- {
- background(0);
- loadMatrix();
- pg.beginPGL();
- gl.glCallList( skybox );
- pg.endPGL();
- fill(255);
- pushMatrix();
- translate(width/2,height/2,0);
- sphere(20);
- popMatrix();
- //println("FPS: "+ frameRate);
- }
- float p = 40000; // half skybox size
- float m = -p;
- // create cube edges
- PVector P000 = new PVector (m,m,m);
- PVector P010 = new PVector (m,p,m);
- PVector P110 = new PVector (p,p,m);
- PVector P100 = new PVector (p,m,m);
- PVector P001 = new PVector (m,m,p);
- PVector P011 = new PVector (m,p,p);
- PVector P111 = new PVector (p,p,p);
- PVector P101 = new PVector (p,m,p);
- Texture tex1,tex2,tex3,tex4,tex5,tex6; // texture images
- // load six skybox images as cube texture
- void loadSkybox(String skyboxName, String fExt)
- {
- try {
- tex1 = AWTTextureIO.newTexture(GLProfile.getDefault(), ImageIO.read(new File(dataPath(skyboxName + "_front" + fExt))), true);
- tex2 = AWTTextureIO.newTexture(GLProfile.getDefault(), ImageIO.read(new File(dataPath(skyboxName + "_back" + fExt))), true);
- tex3 = AWTTextureIO.newTexture(GLProfile.getDefault(), ImageIO.read(new File(dataPath(skyboxName + "_left" + fExt))), true);
- tex4 = AWTTextureIO.newTexture(GLProfile.getDefault(), ImageIO.read(new File(dataPath(skyboxName + "_right" + fExt))), true);
- tex5 = AWTTextureIO.newTexture(GLProfile.getDefault(), ImageIO.read(new File(dataPath(skyboxName + "_bottom" + fExt))), true);
- tex6 = AWTTextureIO.newTexture(GLProfile.getDefault(), ImageIO.read(new File(dataPath(skyboxName + "_top" + fExt))), true);
- }
- catch (IOException e) {
- println( e);
- }
- //textureMode(NORMALIZED);
- }
- // Assign six texture to the six cube faces
- void TexturedCube()
- {
- TexturedCubeSide (P100, P000, P010, P110, tex1); // -Z "front" face
- TexturedCubeSide (P001, P101, P111, P011, tex2); // +Z "back" face
- TexturedCubeSide (P000, P001, P011, P010, tex3); // -X "left" face
- TexturedCubeSide (P101, P100, P110, P111, tex4); // +X "right" face
- TexturedCubeSide (P110, P010, P011, P111, tex5); // +Y "base" face
- TexturedCubeSide (P101, P001, P000, P100, tex6); // -Y "top" face
- }
- // create a cube side given by 4 edge vertices and a texture
- void TexturedCubeSide(PVector P1, PVector P2, PVector P3, PVector P4, Texture tex)
- {
- tex.enable(gl);
- tex.bind(gl);
- gl.glBegin(GL2.GL_QUADS);
- gl.glTexCoord2f(1.0f, 0.0f);
- gl.glVertex3f(P1.x, P1.y, P1.z);
- gl.glTexCoord2f(0.0f, 0.0f);
- gl.glVertex3f(P2.x, P2.y, P2.z);
- gl.glTexCoord2f(0.0f, 1.0f);
- gl.glVertex3f(P3.x, P3.y, P3.z);
- gl.glTexCoord2f(1.0f, 1.0f);
- gl.glVertex3f(P4.x, P4.y, P4.z);
- gl.glEnd();
- tex.disable(gl);
- }
- void loadMatrix() {
- gl.glMatrixMode(GL2.GL_PROJECTION);
- projMatrix[0] = pg.projection.m00;
- projMatrix[1] = pg.projection.m10;
- projMatrix[2] = pg.projection.m20;
- projMatrix[3] = pg.projection.m30;
-
- projMatrix[4] = pg.projection.m01;
- projMatrix[5] = pg.projection.m11;
- projMatrix[6] = pg.projection.m21;
- projMatrix[7] = pg.projection.m31;
-
- projMatrix[8] = pg.projection.m02;
- projMatrix[9] = pg.projection.m12;
- projMatrix[10] = pg.projection.m22;
- projMatrix[11] = pg.projection.m32;
-
- projMatrix[12] = pg.projection.m03;
- projMatrix[13] = pg.projection.m13;
- projMatrix[14] = pg.projection.m23;
- projMatrix[15] = pg.projection.m33;
-
- gl.glLoadMatrixf(projMatrix, 0);
-
- gl.glMatrixMode(GL2.GL_MODELVIEW);
- mvMatrix[0] = pg.modelview.m00;
- mvMatrix[1] = pg.modelview.m10;
- mvMatrix[2] = pg.modelview.m20;
- mvMatrix[3] = pg.modelview.m30;
-
- mvMatrix[4] = pg.modelview.m01;
- mvMatrix[5] = pg.modelview.m11;
- mvMatrix[6] = pg.modelview.m21;
- mvMatrix[7] = pg.modelview.m31;
-
- mvMatrix[8] = pg.modelview.m02;
- mvMatrix[9] = pg.modelview.m12;
- mvMatrix[10] = pg.modelview.m22;
- mvMatrix[11] = pg.modelview.m32;
-
- mvMatrix[12] = pg.modelview.m03;
- mvMatrix[13] = pg.modelview.m13;
- mvMatrix[14] = pg.modelview.m23;
- mvMatrix[15] = pg.modelview.m33;
- gl.glLoadMatrixf(mvMatrix, 0);
- }
↧
↧
December 19, 2012, 2:26 pm
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
↧
December 19, 2012, 3:38 pm
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.
↧
December 19, 2012, 6:30 pm
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 :)
↧