gluUnProject Rotation issues

Hello All,

I’m working on drawing a dynamic line for a sample JOGL application. In my application there are a number of boxes, once one is selected you are able to draw line from that box that follows the mouse point anywhere on screen. The problem I am having is that whenever I rotate the screen my line starts plotting odd coordinates. Im sure there is something im not taking into account once I rotate but I could use some help determining what that is. Sample code below. Im using gluUnProject to convert my coordinates

	int[] viewport = new int[4];   
	openGLContext.glGetIntegerv(GL.GL_VIEWPORT, viewport, 0);  
	double mvmatrix[] = new double[16];  
	openGLContext.glGetDoublev(GL.GL_MODELVIEW_MATRIX, mvmatrix, 0);  
	double projmatrix[] = new double[16];
	openGLContext.glGetDoublev(GL.GL_PROJECTION_MATRIX, projmatrix, 0);  
	double wcoord[] = new double[4];      

	glUtilities.gluUnProject((double) mousePoint.x, (double) mousePoint.y, 0.0, //take the current mouse points and convert them to opengl coordinates
	              mvmatrix, 0,
	              projmatrix, 0, 
	              viewport, 0, 
	              wcoord, 0);

drawConnection(wcoord[0]wcoord[2] ,wcoord[1] -wcoord[2],wcoord[2]); //pass in the converted mouse coordinates, multiplied by the depth, seems to only work if i multiply by the Z value

public void drawConnection (double x, double y, double z){
	 openGLAdapter.glPushMatrix();
	openGLAdapter.glColor3f(0.5f, 0.0f, 1.0f);     
	openGLAdapter.glLineWidth(2.0f);                 
	  openGLAdapter.glRotated( x, 1.0,0.0, 0.0 );
      openGLAdapter.glRotated( y, 0.0,1.0, 0.0 ); 
	openGLAdapter.glBegin(GL.GL_LINES);             //begin drawing connection line  starting at the x,y coordinates of the selected box     
	if(selectedNode!= null && (x != 0 && y != 0)){	
	openGLAdapter.glVertex3d( selectedNode.x, selectedNode.y, selectedNode.z); 
	System.out.println("targetNode x and y " + selectedNode.x + " " + selectedNode.y);
	openGLAdapter.glVertex3d(x,y, 0.0f);          //the ending point of the connection line is equal to the current mouse position
	System.out.println("line draw end point " + x + " " + y);
	}
	openGLAdapter.glEnd();
	openGLAdapter.glLineWidth(1.0f);  
	openGLAdapter.glPopMatrix();
	
}

u shouldnt use multiplication of values

gluUnProject will convert the mouse position by multiplying by the inverse of the matices of the position specified

You are setting the depth value to zero which which is z-near or z-far, in case u are aligned it will look like its right but actually wrong.

glUtilities.gluUnProject((double) mousePoint.x, (double) mousePoint.y, 0.0…

You have two options to solve this issue

1- take the depth value of the first point you picked on (using project) and use this value with unproject while draging then correct the value once u reach destination

2- use the 0 depth value while dragging and recorrect once you reached a drawn pixel then correct

but in both cases its visually better to use one line only if you want a free hand like tool its better to have lots of objects drawn.

Thanks for the help rsantina but what do you mean by “correct” in either case?