getting gluUnproject to work

Started by Eternity, March 10, 2009, 10:08:48

Previous topic - Next topic

Eternity

hey.

im currently trying to plot my mouse cursor to 3D space and i just cant seem to get it working
i've tried to port some C code from NEHE atricle #13 but i get an nullpointerException and i have no idea why. i'm also not sure if my method would work in the first place. please look at the following (first the origanel c method then my java method

CVector3 GetOGLPos(int x, int y)
{
   GLint viewport[4];
   GLdouble modelview[16];
   GLdouble projection[16];
   GLfloat winX, winY, winZ;
   GLdouble posX, posY, posZ;

   glGetDoublev( GL_MODELVIEW_MATRIX, modelview );
   glGetDoublev( GL_PROJECTION_MATRIX, projection );
   glGetIntegerv( GL_VIEWPORT, viewport );

   winX = (float)x;
   winY = (float)viewport[3] - (float)y;
   glReadPixels( x, int(winY), 1, 1, GL_DEPTH_COMPONENT, GL_FLOAT, &winZ );

   gluUnProject( winX, winY, winZ, modelview, projection, viewport, &posX, &posY, &posZ);

   return CVector3(posX, posY, posZ);

and then my java version of it

      public static Vector3 GetCursor3D(int x, int y)
      {
            int[] v = new int[4];
            IntBuffer viewport = IntBuffer.wrap(v);     
            float[] m = new float[16];
            FloatBuffer modelview = FloatBuffer.wrap(m);
            float[] p = new float[16];
            FloatBuffer projection = FloatBuffer.wrap(p);
            float winX, winY;
            float[] z = new float[1];
            FloatBuffer winZ = FloatBuffer.wrap(z);
            FloatBuffer pos = FloatBuffer.allocate(3);
            GL11.glGetFloat( GL11.GL_MODELVIEW_MATRIX, modelview);
            GL11.glGetFloat( GL11.GL_PROJECTION_MATRIX, projection);
            GL11.glGetInteger( GL11.GL_VIEWPORT, viewport);
            winX = (float)x;
            winY = (float)viewport.get(3) - (float)y;
            GL11.glReadPixels( x, (int)winY, 1, 1, GL11.GL_DEPTH_COMPONENT,  GL11.GL_FLOAT, winZ);
            GLU.gluUnProject( winX, winY, winZ.get(0), modelview, projection, viewport, pos);
            return new Vector3(pos.get(0),pos.get(1),pos.get(2));
      }

i get a nullpointerexception on GL11.glGetFloat( GL11.GL_MODELVIEW_MATRIX, modelview);

apart from that i would like to know if im going about this the right way at all?

sorry im not sure how to do source tags on this forum..



Kai

Getting a NullPointerException in such a LWJGL-Method almost always means that there is no OpenGL context current in the invoking Thread.
Make sure that you are calling any OpenGL-methods only in a Thread for which a context is current.

When does a Thread have a current context?:
1. If it was the one calling Display.create(...)
2. If it was the one calling AWTGLCanvas.paint() which itself called the overridable method paintGL()

Eternity

haha thnx cant believe i looked past the obvious... my engine does gamelogic and redering on separeate threads and ive allready writen a solution to all of that :p

anyway apart from that. do you see any other problems (logical/performance) i might encounter with my method? or does it look feasible

Eternity

hmm.. ive got rid of the error but the coords generated is far from right...

Eternity

nevermind after doing a fair amount of googling i found a proper solution that works perfectly

thnx for reading anyway