This is my code that i use to get my mouse position in the 3d scene:
void GetOGLPos(int x, int y, GLdouble &pX, GLdouble &pY, GLdouble &pZ){
GLint viewport[4];
GLdouble modelview[16];
GLdouble projection[16];
GLfloat winX, winY, winZ;
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, &pX, &pY, &pZ);
}
But i noticed bad thing... 1-2 calls to that function per frame makes CPU usage 100%, 3 or more calls 200% CPU usage (i have 4 cores, 1-2 calls = 25%, 3 or more calls = 50%, i cant make it higher than 50% i think..)
Is there any other way to do this efficiently? I'm using 4 calls to that function every frame so i know which areas i should render for my scene (i take them from each screen corner).
Also i use this to know which place i am pointing with mouse, so i need it in real time, but i would like to use less CPU, because even just 1 call makes it 100% usage for single core systems.
Edit: Ive tried glPushName() method but its even slower, more likely slower in my GPU than in CPU. Also my CPU usage is only like 0-1% when i dont use a single glReadPixels() call in my program. Weird thing is that i get high CPU usage, but it doesnt make the program lag as you would expect with 100% usage... only problem comes when i use other programs while my program is on, then its laggy to use them.