Is there a "pure" OpenGL replacement for Java2D javax.media.opengl .* .glu.gluUnProject method ?
in
Programming Questions
•
2 years ago
Hello,
In the sketch below, a 2D grid is drawn and inserted in a 3D space. I want to be able to move some grid's points. Using the screenX and screenY functions I know when my mouse pointer is over a point. However as there is no reciprocal ("screen to world") function in Processing, I am using some code found on the older forum. It seems that it relies on Java 2D so I get this incompatibility message :
Thank you in advance.
In the sketch below, a 2D grid is drawn and inserted in a 3D space. I want to be able to move some grid's points. Using the screenX and screenY functions I know when my mouse pointer is over a point. However as there is no reciprocal ("screen to world") function in Processing, I am using some code found on the older forum. It seems that it relies on Java 2D so I get this incompatibility message :
The message is caused by the code at lines 40 and 41:ClassCastException: processing.core.PGraphicsJava2D cannot be cast to processing.opengl.PGraphicsOpenGL
- gl=((PGraphicsOpenGL)g).gl; // for screen to world
glu=((PGraphicsOpenGL)g).glu; // same here
Thank you in advance.
- //
- // Grid creation with rows x cols points. Plane is set in an OpenGL 3D scene. Intersections can be moved.
- //
- import controlP5.*;
- import processing.opengl.*;
- import codeanticode.glgraphics.*;
- import javax.media.opengl.*;
- import javax.media.opengl.glu.*;
- import java.nio.*;
- ControlP5 controlP5;
- GL gl; // for mouse to world conversion
- GLU glu; // same here
- int rows = 15; // number of rows
- int cols = 8; // number of cols
- int points = rows * cols ; // number of points
- int x_size = 500; // width
- int y_size = 500; // height
- int margin = 50; // margin
- int button_height = 100; // additional height for button zone
- //int coords = 2; // number of coordinates per point (2D at the moment)
- int bgColor = color(255, 255, 255); // background color
- int squareColor = color(128, 0, 0); // square color upon mouse over point
- boolean bover = false;
- boolean locked = false;
- int movingPoint = -1 ; // number of selected point
- PVector[] mesPoints = new PVector[points];
- // this is only a note.
- // we will not use variable b in the code below.
- // we have to use controlP5.Button here since there
- // would be a conflict if we only use Button to declare button b.
- controlP5.Button b;
- // a button-controller with name buttonValue will change the
- // value of this variable when pressed.
- //int buttonValue = 0;
- //********** SETUP
- void setup()
- {
- gl=((PGraphicsOpenGL)g).gl; // for screen to world
- glu=((PGraphicsOpenGL)g).glu; // same here
- smooth();
- ellipseMode(CENTER);
- rectMode(CENTER);
- size(x_size, (y_size+button_height), OPENGL);
- background(bgColor);
- //Button
- controlP5 = new ControlP5(this);
- controlP5.addButton("Bouton1",0,50, (y_size+50),88,21);
- controlP5.setColorActive(color(255, 0, 0)); // color for mouse-over
- controlP5.setColorBackground(color(128, 0, 0)); // default background color
- controlP5.setColorLabel(color(255, 255, 255)); // text color
- controlP5.controller("Bouton1").setLabel("Regenerate points"); // button label
- //controlP5.setColorForeground(color(128, 0, 0)); // default color
- // Creates positions for points
- for (int i = 0; i < rows; i++) {
- for (int j = 0; j < cols; j++) {
- int current_point = (cols * i)+j;
- mesPoints[current_point] = new PVector( ((((x_size - (2*margin))/(cols-1)) * j)+margin), ((((y_size - (2*margin))/(rows-1)) * i)+margin), 0);
- }
- }
- }
- //********** DRAW
- void draw()
- {
- // Erases everything - redraws lines
- background(bgColor);
- //Open GL 3D stuff
- lights();
- //translate(width / 2, height / 2);
- rotateY(map(mouseX/10, 0, width, 0, -PI));
- rotateZ(map(mouseY/10, 0, height, 0, PI));
- // Draw lines
- strokeWeight(2);
- for (int i = 0; i < rows ; i++) {
- for (int j = 0; j < cols ; j++) {
- int current_point = (cols * i)+j;
- //Draw horizontal lines
- if (j < cols - 1) { // all but last column
- if ((i == 0) || (i == (rows - 1))) { // first and last rows
- stroke(0, 0, 0); // outer lines are black
- } else {
- stroke(0,192,255); // inner lines are blue
- }
- beginShape(LINES);
- vertex(mesPoints[current_point].x, mesPoints[current_point].y, 0);
- vertex(mesPoints[current_point+1].x, mesPoints[current_point+1].y, 0);
- endShape();
- }
- //Draw vertical lines
- if (i < rows - 1) { // all but last line
- if ((j == 0) || (j == (cols - 1))) { // first and last column
- stroke(0, 0, 0); // outer lines are black
- } else {
- stroke(0,192,255); // inner lines are blue
- }
- beginShape(LINES);
- vertex(mesPoints[current_point].x, mesPoints[current_point].y, 0);
- vertex(mesPoints[current_point+cols].x, mesPoints[current_point+cols].y, 0);
- endShape();
- }
- }
- }
- // Draws points
- strokeWeight(1);
- for (int i = 0; i < (rows * cols) ; i++) {
- if ( (i % cols == 0) || (i % cols == (cols - 1)) || (((i+1) / cols) == 0) || (((i+1) / cols) == rows - 1 ) ) { //outer points
- stroke(0,0, 0);
- fill(0,0,0);
- } else { // inner points
- stroke(0,192, 255);
- fill(255,255,0);
- }
- ellipse(mesPoints[i].x, mesPoints[i].y, 10, 10);
- }
- //Tests whether mouse rolls over points and draws red squares
- strokeWeight(1);
- if (locked) {
- squareColor = color(255, 0, 0);
- } else {
- squareColor = color(128, 0, 0);
- }
- stroke(squareColor);
- fill(squareColor);
- for (int i = 0; i < points; i++) {
- // distance between mouse pointer and projection of underneath vertice on screen.
- if(dist(mouseX, mouseY, screenX(mesPoints[i].x, mesPoints[i].y, mesPoints[i].z), screenY(mesPoints[i].x, mesPoints[i].y, mesPoints[i].z)) <= 5) {
- movingPoint = i;
- rect(mesPoints[i].x, mesPoints[i].y, 10, 10);
- i = points ; //not necessary to go on with loop
- } else {
- movingPoint = -1;
- }
- }
- if (movingPoint != -1) {
- bover = true ;
- } else {
- bover = false;
- }
- //
- }
- //********** Detects whether mouse button is pressed
- void mousePressed() {
- if(bover) {
- locked = true;
- } else {
- locked = false;
- }
- }
- //********** Moves selected point while button held
- void mouseDragged() {
- if(locked) {
- strokeWeight(1);
- stroke(255,0,0);
- fill(255,0,0);
- mesPoints[movingPoint].x = getMouse3D()[0] ;
- mesPoints[movingPoint].y = getMouse3D()[1] ;
- mesPoints[movingPoint].z = getMouse3D()[2] ; // mouse (screen) to world conversion
- //mesPoints[movingPoint].x = mouseX;
- //mesPoints[movingPoint].y = mouseY;
- //rect(mesPoints[movingPoint].x, mesPoints[movingPoint].y, 10, 10);
- }
- }
- //********** Unlocks upon button release
- void mouseReleased() {
- locked = false;
- bover = false;
- }
- //**********function Bouton1 will receive changes from
- //********** controller with name controlP5
- public void Bouton1(int theValue) {
- for (int i = 0; i < rows; i++) {
- int randomX;
- int randomY;
- for (int j = 0; j < cols; j++) {
- int current_point = (cols * i)+j;
- if ( (current_point % cols == 0) || (current_point % cols == (cols - 1)) || (((current_point+1) / cols) == 0) || (((current_point+1) / cols) == rows - 1 )) { //outer points
- randomX = 0;
- randomY = 0;
- } else { // inner points
- randomX = int(random(-10, 10));
- randomY = int(random(-10, 10));
- }
- mesPoints[current_point] = new PVector( ((((x_size - (2*margin))/(cols-1)) * j)+margin+randomX), ((((y_size - (2*margin))/(rows-1)) * i)+margin+randomY), 0);
- }
- }
- }
- //************* SCREEN TO WORLD FUNCTION - found on old Processing forum ********************
- float[] getMouse3D()
- {
- ((PGraphicsOpenGL)g).beginGL();
- int viewport[] = new int[4];
- double[] proj=new double[16];
- double[] model=new double[16];
- gl.glGetIntegerv(GL.GL_VIEWPORT, viewport, 0);
- gl.glGetDoublev(GL.GL_PROJECTION_MATRIX,proj,0);
- gl.glGetDoublev(GL.GL_MODELVIEW_MATRIX,model,0);
- // java.nio.FloatBuffer fb = java.nio.ByteBuffer.allocateDirect(4).order(java.nio.ByteOrder.nativeOrder()).asFloatBuffer(); // replaces line below if java.nio not available
- FloatBuffer fb = ByteBuffer.allocateDirect(4).order(ByteOrder.nativeOrder()).asFloatBuffer();
- gl.glReadPixels(mouseX, height-mouseY, 1, 1, GL.GL_DEPTH_COMPONENT, GL.GL_FLOAT, fb);
- fb.rewind();
- double[] mousePosArr=new double[4];
- glu.gluUnProject((double)mouseX,height-(double)mouseY,(double)fb.get(0),model,0,proj,0,viewport,0,mousePosArr,0);
- ((PGraphicsOpenGL)g).endGL();
- return new float[]{(float)mousePosArr[0],(float)mousePosArr[1],(float)mousePosArr[2]};
- }
- //*********
- // END
- //*********
1