Jump to content

  • Log In with Google      Sign In   
  • Create Account

glReadPixels Z value


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
5 replies to this topic

#1 ilyazyilmaz   Members   -  Reputation: 103

Like
0Likes
Like

Posted 23 April 2013 - 12:49 PM

Hello I want to conver screen coordinate to OPENGL coordinate. but glReadPixels  function doesnt work .

I'm using c#. For two days I couldnt solve this problem. the code is below:

 

 

 

        void ikt_viewPort3d_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
        {
            unsafe
            {
                float winX, winY, winZ;
                isClicked= true;
                Int32[] viewPort = new Int32[4];
                GL.glGetIntegerv(GL.GL_VIEWPORT, viewPort);
                double[] modelView = new double[16];
                GL.glGetDoublev(GL.GL_MODELVIEW_MATRIX, modelView);
                double[] projection = new double[16];
                GL.glGetDoublev(GL.GL_PROJECTION_MATRIX, projection);
                winX = eXPre = (float)e.X;
                winY = eYPre = (float)e.Y;
                winY = viewPort[3] - winY;
                byte[] byt = new byte[4];
                uint[] pix=new uint[1];
                GL.glReadPixels((int)winX, (int)winY, 1, 1, GL.GL_DEPTH_COMPONENT, GL.GL_FLOAT, byt);
                winZ = System.BitConverter.ToSingle(byt, 0);
                double objX, objY, objZ;
                GL.gluUnProject(winX, winY, winZ, modelView, projection, viewPort, out objX, out objY, out objZ);
                MessageBox.Show("x : " + winX.ToString() + "y: " + winY.ToString() + "z: " + winZ.ToString());
                MessageBox.Show("x : " + objX.ToString() + "y: " + objY.ToString() + "z: " + objZ.ToString());
            }
 
        }

 

I'm waiting your answer . Thanks....The solution is very valuable for me.



Sponsor:

#2 WiredCat   Members   -  Reputation: 366

Like
0Likes
Like

Posted 23 April 2013 - 03:13 PM



t3dpoint __fastcall TModelGroupHandler::GetPositionFromSpaceDepth(int X, int Y, HWND hwnd)
{
glClear(GL_COLOR_BUFFER_BIT);
glClear(GL_DEPTH_BUFFER_BIT);
  glViewport(0, 0, int( DISPLAY_WIDTH), int(DISPLAY_HEIGHT));
  glMatrixMode(GL_PROJECTION);
  glLoadIdentity();
  gluPerspectiveA(90.0, DISPLAY_WIDTH/DISPLAY_HEIGHT,0.10f, 1000.0f * 1000.0f * 1000.0f);
  glMatrixMode(GL_MODELVIEW);

glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE);
glPushMatrix();
glLoadIdentity();

gluLookAt(base->eyepos.x,base->eyepos.y,base->eyepos.z,
base->eyepointingpos.x,base->eyepointingpos.y,base->eyepointingpos.z,base->upvector.x,
base->upvector.y,base->upvector.z);



base->DrawDepthBillboard(last_depth); //default 100 meters away


TPoint mouse;
GetCursorPos(&mouse);
ScreenToClient(hwnd, &mouse);
t3dpoint depth_result;
depth_result = base->Reproduce_Mouse_coordinates(mouse.x,mouse.y);


glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);

glPopMatrix();
return depth_result;
}

t3dpoint __fastcall ModelGroupEditor::Reproduce_Mouse_coordinates(int x, int y)
{
t3dpoint res;
glGetDoublev( GL_PROJECTION_MATRIX, mprojection );
glGetDoublev( GL_MODELVIEW_MATRIX, mmodelview );
glGetIntegerv( GL_VIEWPORT, mviewport );

				  int wy = mviewport[3] -y;
	  double nx,ny,nz;      //  double fx,fy,fz;
  double	winX = double(x);
   double	winY = double(mviewport[3]) - double(y);
double winZ;
			  // glReadBuffer(GL_DEPTH_BUFFER_BIT);
float * pdata = new float;
float pk;



	glReadPixels(x,wy,1,1,GL_DEPTH_COMPONENT,GL_FLOAT,pdata);
	pk = (*pdata);
delete pdata;	
winZ = double(pk);


gluUnProject(winX, winY, winZ, mmodelview, mprojection, mviewport, &nx, &ny, &nz);

res.x = float(nx);
res.y = float(ny);
res.z = float(nz);

 return res;
}


 

if your code looks like this and you still have no results check your drawing code / or you just have float / double problem anyway this code works in C++


Edited by ___, 23 April 2013 - 03:17 PM.


#3 ilyazyilmaz   Members   -  Reputation: 103

Like
0Likes
Like

Posted 24 April 2013 - 04:18 PM

thanks for your reply.

 

Also I found this code on the web. it seems works. But maybe reason could be my dll files.

I use very old dll files. http://sourceforge.net/projects/csgl/files/

 

may be dll files has a problem.

 

 

I prepared simple interface to testing the  codes.
There is source code and exe files.
Project name DXFTOOPENGL.EXE.

If you have time Could you look for help? If you dont have a time no 
problem.
The project link:

https://dl.dropboxusercontent.com/u/51690534/TEST.rar




#4 WiredCat   Members   -  Reputation: 366

Like
0Likes
Like

Posted 24 April 2013 - 04:44 PM

i gave you working code just port it to c#, these exe files don't work



#5 ilyazyilmaz   Members   -  Reputation: 103

Like
0Likes
Like

Posted 25 April 2013 - 07:16 AM

Thanks for your interest...

Actually your code and my code is nearly same except :  glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);

 

But I use in my project OPENGL C# wrapper. And C# wrapper is very old :http://csgl.sourceforge.net/

 

Maybe this wrapper is old and it has bug. I dont know. I decided to use OPENTK SDK for c#.

 

if you have any advice OPENGL SDK for c# . Or which OPENGL sdk is good for CAD .



#6 WiredCat   Members   -  Reputation: 366

Like
0Likes
Like

Posted 26 April 2013 - 03:29 AM

lets see your code:

 





  void ikt_viewPort3d_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
        {
            unsafe
            {
                
                float winX, winY, winZ;
                ilkTikYapildi = true;
                Int32[] viewPort = new Int32[4];
                GL.glGetIntegerv(GL.GL_VIEWPORT, viewPort);
                double[] modelView = new double[16];
                GL.glGetDoublev(GL.GL_MODELVIEW_MATRIX, modelView);
                double[] projection = new double[16];
                GL.glGetDoublev(GL.GL_PROJECTION_MATRIX, projection);
                winX = eXPre = (float)e.X;
                winY = eYPre = (float)e.Y;
                winY = viewPort[3] - winY;
                byte[] byt = new byte[4];
                float[] flt=new float[4];
                uint[] pix=new uint[1];
                GL.glEnable(GL.GL_DEPTH_TEST);
                GL.glPixelStorei(GL.GL_UNPACK_ALIGNMENT, 1);// and why are you using it
                GL.glPixelStorei(GL.GL_PACK_ALIGNMENT, 1);// and why are you using it
                GL.glFinish();// and why are you using it
                GL.glReadPixels((int)winX, (int)winY, 1, 1, GL.GL_DEPTH_COMPONENT, GL.GL_FLOAT, byt);//wtf man
                winZ = System.BitConverter.ToSingle(byt, 0);//wtf man
   
             uint[] den1 = new uint[100];<-- since when float result is uint[100]

                GL.glReadPixels((int)winX, (int)winY, 1, 1, GL.GL_DEPTH_COMPONENT, GL.GL_FLOAT, den1);//here its okay but should be integers without cast
                winZ = den1[0];//no comments
          
     
                //now see this code below and compare it  with mine
                double objX, objY, objZ;
                GL.gluUnProject(winX, winY, aktifZoomDegeri, modelView, projection, viewPort, out objX, out objY, out objZ);
              why the f*** are you using here floats????????? and wtf is aktifzoomdegeri dont even tell me i dont want to know
            }

        }
    }



i told you to make it like mine but you use floats instead integers, floats instead doubles and aktifzoomdegeri is what? should be winZ (in your code it should be den1 cast to double since den1 should be float (make new variable and cast to it), i am tired of telling you this over and over and over again.


Edited by ___, 26 April 2013 - 03:35 AM.





Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS