How to get Z-value with mouseclick?

Started by
2 comments, last by Piranjah 17 years, 6 months ago
Hi, I'm a total newbie :-s I'm setting up my scene and I need to know how to get the z-value of the pixel I'm clicking on:s Is there any way to do this fast? Many thanks, Pascal
Advertisement
float output_z;glReadPixels(mouse_x, mouse_y, 1, 1, GL_DEPTH_COMPONENT, GL_FLOAT, &output_z);
glReadPixels is damn slow, because of framebuffer readbacks! I tried this with a simple landscape and it takes 10ms!
You should learn ray-picking. Use glunproject with the near and far clipplane and the mouse coordinates to get your picking ray. Then you have to calculate which of your objects the ray intersects. Here is an explanation on doing this (All calculation is done on the CPU with your vertex data):
http://www.cs.princeton.edu/courses/archive/fall00/cs426/lectures/raycast/sld016.htm
You can improve speed with spatial subdivision (Google will help you, it's very simple).
Many thanks for the help. I did use the glPixelRead, works perfectly, thx to both of you for the help.

Pascal

This topic is closed to new replies.

Advertisement