Archived

This topic is now archived and is closed to further replies.

guppy

width of the openGL screen

Recommended Posts

This may seem a fairly stupid question but bear with me... how do you find the range for wide og high of the openGL screen? I don''t mean the resolution (ie: 800x600) but how maney "glDots" there is (ie: 5.0f ... -5.0f) or even better is there an easy way to convert form a screen coordinate to the openGL coordinate.. (no witty sugestions about Ortho mode please...) /Please excuse my bad spelling - My native language is binary not english |Visit me \Take my advice - I don''''t use it...

Share this post


Link to post
Share on other sites
"No witty suggestions about Ortho mode please"?

But, with glOrtho(...), you can make the dimension to whatever
you like. If you use the same resolution as pixels, then
one "glDots" is one pixel. If you use half, then one "glDots"
is two pixels... etc.


~~~~
Kami no Itte ga ore ni zettai naru!

Share this post


Link to post
Share on other sites
quote:
Original post by tangentz
"No witty suggestions about Ortho mode please"?

But, with glOrtho(...), you can make the dimension to whatever
you like. If you use the same resolution as pixels, then
one "glDots" is one pixel. If you use half, then one "glDots"
is two pixels... etc.


~~~~
Kami no Itte ga ore ni zettai naru!


I need to convert a screen coordinate (from the mouse) to its equivalent on the OpenGL screen. simply switching to Ortho mode wont help with that....



/Please excuse my bad spelling - My native language is binary not english
|Visit me
\Take my advice - I don''''t use it...

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
have you tried gamedev''s search forum tool? it''s quite amazing actually. With it you can search through all of the replies already given to this very question.

(hint: search for "convert opengl screen coordinates" and its ilk)

Share this post


Link to post
Share on other sites
gluUnproject turned out to be the solution...

um or well...

The following code seems to be what everybody on the net uses:

  
int x = theWindow.xMouse;
int y = theWindow.yMouse;
GLint viewport[4];
GLdouble mvmatrix[16], projmatrix[16];
GLint realy; /* OpenGL y coordinate position */
GLdouble wx, wy, wz; /* returned world x, y, z coords */

glGetIntegerv (GL_VIEWPORT, viewport);
glGetDoublev (GL_MODELVIEW_MATRIX, mvmatrix);
glGetDoublev (GL_PROJECTION_MATRIX, projmatrix);
/* note viewport[3] is height of window in pixels */

realy = viewport[3] - (GLint) y - 1;
//at z=0:

gluUnProject ((GLdouble) x, (GLdouble) realy, 0.0,
mvmatrix, projmatrix, viewport, &wx, &wy, &wz);


xMouse = wx;
yMouse = wy;


xMouse,yMouse are floats that i use to draw a red triangel at the mouse position.

However the above code makes the red triangel jump about like crazy on the screen..

ofcourse I thougth somthing was wrong with my mouse code - but the behavoiur is the same if i substitute:

int x = theWindow.xMouse;
int y = theWindow.yMouse;

with

int x= 400; int y =200;

if I don''t rotate the triangel it just bobs between to spots on the screen...

Any clues?




/Please excuse my bad spelling - My native language is binary not english
|Visit me
\Take my advice - I don''''t use it...

Share this post


Link to post
Share on other sites
How i accomplished this was I put it into Ortho mode And then to get the 0,0 out of the middle of the screen, i translated the entire rendering by -halfwindowlength for the x and +halfwindowheight for the y. It puts 0,0 in the top left corner which is the same way GetCursorPos() is. Then to move my new cursor i simply did this:

    
RenderScene()
{
...More code up there ^

GetCursorPos(&cPos);
glTranslated(cPos.x , -cPos.y ,0);
RenderCursorSprite(mode);
}


EDIT: Source Tags

[edited by - PRod on July 15, 2002 11:28:19 PM]

[edited by - Prod on July 15, 2002 11:29:21 PM]

Share this post


Link to post
Share on other sites
I personally think ortho mode is a complete waste of time..

this is much easier:


  

glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glScalef(1.0f/windowWidth,-1.0f/windowHeight,1);




glVertex2i(50,50);
etc...

Share this post


Link to post
Share on other sites
the problem is not that I cant draw the triangel where I want..
it''s just there so I can see the difference between the window cursor (the actual mouse position) and what gluUnproject is returning...

the problem is that gluUnproject seemingly gives me more or less random values...


this is the entire "game loop"

  
while(!done)
{
dEH.windows_stuff();

if ( theWindow.mouseUpdated )
{
x = theWindow.xMouse ;
y = theWindow.yMouse;
theWindow.mouseUpdated = false;
}



GLint viewport[4];
GLdouble mvmatrix[16], projmatrix[16];
GLint realy; /* OpenGL y coordinate position */
GLdouble wx, wy, wz; /* returned world x, y, z coords */

glGetIntegerv (GL_VIEWPORT, viewport);
glGetDoublev (GL_MODELVIEW_MATRIX, mvmatrix);
glGetDoublev (GL_PROJECTION_MATRIX, projmatrix);
/* note viewport[3] is height of window in pixels */

realy = viewport[3] - (GLint) y - 1;
//at z=0:

gluUnProject ((GLdouble) x, (GLdouble) realy, 0.0,
mvmatrix, projmatrix, viewport, &wx, &wy, &wz);


xMouse = wx;
yMouse = wy;



//##########Execute game code here...

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

glLoadIdentity();

angle = angle + 0.1f;

if (angle >= 360.0f) angle = 0.0f;

glTranslatef(xMouse, yMouse, -10.0f);
glRotatef(angle, 0.0f, 0.0f, 1.0f);
glColor3f(1.0f, 0.0f, 0.0f);
glBegin(GL_TRIANGLES);
glVertex3f(0.0f, 0.0f, 0.0f);
glVertex3f(1.0f, 0.0f, 0.0f);
glVertex3f(1.0f, 1.0f, 0.0f);
glEnd();
//##########end Render...


SwapBuffers( theWindow.hDC );

}//end message loop


return 0;
}


Well im off to bed now - prehaps to morrow it will make more sense..

/Please excuse my bad spelling - My native language is binary not english
|Visit me
\Take my advice - I don''''t use it...

Share this post


Link to post
Share on other sites
you should know that in GL, the verticle values are reversed, ie, 0,0 is the bottom left, where as in windows it''s top left. Also, note that you need to take into account not only the position of the window but also the thinkness of the frame of the window.

Share this post


Link to post
Share on other sites
The solution (to the bopping problem any ways) turned out to be:

glPushMatrix();
glLoadIdentity();
<...Unproject code here....>
glPopMatrix();



/Please excuse my bad spelling - My native language is binary not english
|Visit me
\Take my advice - I don''''t use it...

Share this post


Link to post
Share on other sites