OpenGL 2d mouse in a 3d world

Started by
7 comments, last by Neandrake 21 years, 2 months ago
I''ve looked for quite a while and haven''t found how to load and display a mouse in opengl. I''ve done it in Allegro. Is there no feature for it in opengl? My projects are in full screen view. My project now consists of a bunch of 3d pyramids and a large grid. Is there anyway to display a mouse?
Advertisement
what you have to do, is make yourself a fine looking mouse pointer, bmp tga, whatever, get GL to load that text, then make a quad, and make it place the enter of that quad at the mouse position... i aint no programmer, but i know thats how AdmiralBinary''s been doin it since he started GL.
Where have I been ?
but how do you get the mouse position?
GetCursorPos() if working in Win32. You can alternately use any of the lovely extant input APIs, or Win32 messages such as WM_MOUSEMOVE.

[twitter]warrenm[/twitter]

ok, so I''ve done all that. What I have now is a jpg file loaded where the mouse is. At first, the picture moved in opposite direction as the mouse. I fixed that, but even before I fixed it, I noticed that the picture had restrictions. Right now (following the mouse), it seems that the mouse can''t move past 0,0 going to the upper left or bottom left. I think the problem has to do with my directions. I had to create a 3d quad to put the mouse pic in. And when using the glVertex3f() function, I had to switch the Y and Z inputs so the picture faces the user (the camera is fixed to look from the top downwards at the ground. Anyone know how to fix this?
to get the mouse to openGL 3d coordinates.

something like this:

void MouseTo3d2()
{
glFlush();
POINT pt;
GetCursorPos(&pt);

GLint viewport[4];
GLdouble mvmatrix[16], projmatrix[16];
GLint realy; // OpenGL y coordinate position
GLdouble wx2, wy2, wz2;
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) mousey - 1;

//glReadPixels(mousex,realy,1,1,GL_DEPTH_COMPONENT, GL_FLOAT, &depth);
//gluUnProject ((GLdouble) mousex, (GLdouble) realy, 0.5f,
// mvmatrix, projmatrix, viewport, &wx, &wy, &wz);

gluUnProject ((GLdouble) mousex, (GLdouble) realy, depth,
mvmatrix, projmatrix, viewport, &wx2, &wy2, &wz2);


space_x = wx2;// + (factor_t * x_AB);
space_y = wy2;// + (factor_t * y_AB);
space_z = wz2;// + (factor_t * z_AB);

}
Game Core
ok, this is what I have, and now no mouse shows up. What do the functions glGetDoublev and glGetIntegerv do? I kinda skipped the space_x,y,z part, not sure what it did. mCurrent is declared as POINT mCurrent.

void DrawMouse()
{
glFlush();
GLint viewport[4];
GLdouble mvmatrix[16], projmatrix[16];
GLint realy;
GLdouble wx2, wy2, wz2;
glGetIntegerv (GL_VIEWPORT, viewport);
glGetDoublev (GL_MODELVIEW_MATRIX, mvmatrix);
glGetDoublev (GL_PROJECTION_MATRIX, projmatrix);

if ((GetCursorPos(&mCurrent)) != 0)
{
realy = viewport[3] - (GLint) mCurrent.y - 1;
glColor3ub(255, 255, 255);
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, g_Texture[0]);

glBegin(GL_QUADS);
glTexCoord2f(0.0f, 0.0f); //top left
glVertex2f((float)-mCurrent.x, (float)-realy);

glTexCoord2f(0.0f, 1.0f); //bottom left
glVertex2f((float)-mCurrent.x, (float)-realy - 3.0f);

glTexCoord2f(1.0f, 1.0f); //bottom right
glVertex2f((float)-mCurrent.x - 2.0f, (float)-realy - 3.0f);

glTexCoord2f(1.0f, 0.0f); //top right
glVertex2f((float)-mCurrent.x - 2.0f, (float)-realy);
glEnd();
gluUnProject ((GLdouble) mCurrent.x, (GLdouble) realy, 32/*depth*/, mvmatrix, projmatrix, viewport, &wx2, &wy2, &wz2);

glDisable(GL_TEXTURE_2D);
}
}
Maybe I''m just too new to know better,

but I''ve had good luck using Glut. Esp since it''s more portable, unlike using win32 stuff.
I don''t know what GLUT is but I managed to hack away and get what I wanted ( think). At least it looks that way for now

thanks for all the help

-chris

This topic is closed to new replies.

Advertisement