glUnproject problem

Started by Jesse Aldridge, February 28, 2007, 19:34:58

Previous topic - Next topic

Jesse Aldridge


Can anyone tell me what I'm doing wrong here?
No matter which values I pass to getWorldCoordinates, the wCoord array always ends up full of zeroes.

void getWorldCoordinates(double x, double y) {
		IntBuffer viewport = BufferUtils.createIntBuffer(16);
		FloatBuffer mvmatrix = BufferUtils.createFloatBuffer(16);
		FloatBuffer projmatrix = BufferUtils.createFloatBuffer(16);
		int realy = 0;// GL y coord pos
		float wcoord[] = new float[4];

		GL11.glGetInteger(GL11.GL_VIEWPORT, viewport);
		GL11.glGetFloat(GL11.GL_MODELVIEW_MATRIX, mvmatrix);
		GL11.glGetFloat(GL11.GL_PROJECTION_MATRIX, projmatrix);

		int[] viewportArray = getBufferAsArray(viewport);

		//viewportArray[3] is height of window in pixels
		realy = viewportArray[3] - (int) y - 1;

		float[][] mvArray = getMatrixAsArray(mvmatrix), projArray = getMatrixAsArray(projmatrix);

		GLU.gluUnProject((float) x, realy, 0, mvArray, projArray,
				viewportArray, wcoord);

		for( int i = 0; i < wcoord.length; i++ ) {
			System.out.println("wcoord[i]: " + wcoord[i] );
		}
	}

	public static int[] getBufferAsArray(IntBuffer ib) {
		int[] buffer = new int[ib.limit()];
		for (int i = 0; i < ib.limit(); i++) {
			buffer[i] = ib.get();
		}
		return buffer;
	}

	public static float[][] getMatrixAsArray(FloatBuffer fb) {
		float[][] fa = new float[4][4];

		fa[0][0] = fb.get();
		fa[0][1] = fb.get();
		fa[0][2] = fb.get();
		fa[0][3] = fb.get();
		fa[1][0] = fb.get();
		fa[1][1] = fb.get();
		fa[1][2] = fb.get();
		fa[1][3] = fb.get();
		fa[2][0] = fb.get();
		fa[2][1] = fb.get();
		fa[2][2] = fb.get();
		fa[2][3] = fb.get();
		fa[3][0] = fb.get();
		fa[3][1] = fb.get();
		fa[3][2] = fb.get();
		fa[3][3] = fb.get();

		return fa;
	}