Placing trees on Terrain with mouse?

I have created a height field editor, I now wish to select a tree button in the app then when I click the mouse button on the terrain it places a tree exactly on the x,y,z of that piece of terrain.


Hi IronDuke !

I think you could do it using the depth buffer : I assume you know the X,Y coordinates of the point that has been clicked !

  1. Use glReadPixels with GL_DEPTH_COMPONENT to retrieve the depth Z of the pixel that has been clicked. Be aware that the OpenGL Y Axis is reversed compared to the window Y axis.
  2. Use gluUnProject(X,Y,Z,modelMat,projMat,viewPort,&spaceX,&spaceY,&spaceZ);
    You obtain the matrices with glGetDoublev ang glGetIntegerv.

Then, spaceX,spaceY and spaceZ contain the coordinates of the point clicked on the landscape !

I use this method in most of my programs for picking and transforming objects (translations/rotations).

Hope it helps.



I understand what you mean, if you have the time perhaps you could go into a little more detail?

thanks !!! it works fine !!!

hey im try to make the same thing but im realy dont understend.