Skip to main content
gamedev.net
🔒 Locked

gluProject efficient?

Started by xwskx 4 replies 5k views
Original Post
xwskx
xwskx
Hello, I am developing an application that requires me to get the screen coordinate of multiple objects in 3D environment. So i was wondering, how efficient gluProject is? As I recall from my 3D graphics course, that given the parameters used for the gluProject i can create a translation matrix and i can apply my 3D location to convert it to Screen coordinates. So does this mean that gluProject creates the translation matrix every time for each points? Is there a reason why i can't reuse the translation matrix? If i can reuse it, then will it be worth my time to create a algorithm to convert the coordinates; if i do gluproject 1000+ times a frame. I don't remember most of the lectors, because I had too much fun programming. So I can use any insight or advice. thanks in advance.
Cowboy Coder
Cowboy Coder
Here's the code from the sample implementation:
GLint GLAPIgluProject(GLdouble objx, GLdouble objy, GLdouble objz, 	      const GLdouble modelMatrix[16], 	      const GLdouble projMatrix[16],              const GLint viewport[4],	      GLdouble *winx, GLdouble *winy, GLdouble *winz){    double in[4];    double out[4];    in[0]=objx;    in[1]=objy;    in[2]=objz;    in[3]=1.0;    __gluMultMatrixVecd(modelMatrix, in, out);    __gluMultMatrixVecd(projMatrix, out, in);    if (in[3] == 0.0) return(GL_FALSE);    in[0] /= in[3];    in[1] /= in[3];    in[2] /= in[3];    /* Map x, y and z to range 0-1 */    in[0] = in[0] * 0.5 + 0.5;    in[1] = in[1] * 0.5 + 0.5;    in[2] = in[2] * 0.5 + 0.5;    /* Map x,y to viewport */    in[0] = in[0] * viewport[2] + viewport[0];    in[1] = in[1] * viewport[3] + viewport[1];    *winx=in[0];    *winy=in[1];    *winz=in[2];    return(GL_TRUE);}


But really the best test is to try calling it a million times, and see how long it takes.
Zipster
Zipster
With projection, you're just multiplying the input point by the modelview and projection matrices, as in PMx. So if P and M are constant, you can just cache PM and multiply that by x, which is something gluProject doesn't do. However, if your objects aren't already in world-space, the each one will require it's own copy of PMn for that object. And if the objects are moving, then you won't be able to cache Mn and it will need to be calculated each frame. So in terms of performance, the limiting factor is how often you need to recalculate these matrices.
xwskx
xwskx
Well my idea was something like this:
since gluproject request vertex, projection matrix, model matrix, and viewport, i'm speculating that gluproject does the following basic matrix calculation.

V = vertex
P = Project Matrix
M = Model Matrix

newV = P*M
finalV = M*newV

or

translationM = M*P
finalV = translationM*V

but what if i did some like this:
get the projection matrix and model matrix for the frame

P = Project Matrix
M = Model Matrix

cache this matrix
cacheM = M*P

So when i call myGLUproject(Vertex,Transaltion Matrix,Viewport)

I can just do one matrix multiplication
finalV = cacheM*V

So if gluProject requires model and projection matrix and not the product of the two, it means its doing it for me. But if i'm doing multiple translation for the one frame does that mean it does that (M*P) multiple times. My idea is, I should cache the product of model and projection matrix and reuse it.

does this make sense? I remember in 3D graphics class u multiple the various matrices to create one final translation matrix and u multiple the vertex to that matrix and convert the coordinates.
Zipster
Zipster
That absolutely makes sense, and was pretty much what I suggested :) gluProject does PMx every time you call it. But if you know that PM is a constant (because M is a constant) you could do PM yourself once (call it A) and do Ax in myGluProject. However, if PM isn't a constant (because M isn't a constant), then your function wouldn't be able to cache PM and it would end up doing PMx which is the same as gluProject.
xwskx
xwskx
Thanks for the help.

I just implemented it and it gives me about 15+ fps.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.