Jump to content
  • Advertisement

Archived

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

Explosive

Mouse coordinates to OpenGL coordinates?

This topic is 5279 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

How it can be done? I want to move my 3D cube with the mouse,but there is a difference between windows coordinates of the mouse (i draw in a window)and GL coordinates of my cube wich is at the origin of the scene.I use glRenderMode(GL_SELECT),I mean that I can determinate that my mouse is on the cube, and I can grab it,but when I move mouse the cube didn''t move equaly with mouse becouse of the coordinates....What I must done?

Share this post


Link to post
Share on other sites
Advertisement
I use this code to pick 2D objects but I think it may be useful.
It has to be written in the WindowProc.

case WM_MOUSEMOVE:
{
MousePos.x = (int)(((float)LOWORD(lParam))/APP_WIDTH);
MousePos.y = (int)(((float)HIWORD(lParam))/APP_HEIGHT);
}
break;

The max value it gives to mousePos.x and MousePos.y are 1.0
The top left corner of the screen is (0,0).
APP_WIDTH and APP_HEIGHT are the width and height of you app.
This code does not work properly if you resize your window.

BTW, does anyone know how to make it impossible to resize the window?

Share this post


Link to post
Share on other sites
I dont know how it is in API code, I use MFC, but saw that:
void CGLView::OnSize(UINT nType, int cx, int cy)
{
TRACE0("OnSize\r\n") ;
CView::OnSize(nType, cx, cy);

// TODO: Add your message handler code here


if((cx <= 0) || (cy <= 0) ) return ;

CClientDC dc(this) ;
//
//Make Rendering Context Current
//

BOOL bResult = wglMakeCurrent(dc.m_hDC, m_hRC) ;
if(!bResult)
{
TRACE("wglMakeCurrent failed - %d\r\n",
GetLastError()) ;
return ;
}
//
//Seting the world
//
m_gldAspect = (GLdouble) cx/ (GLdouble) cy ;
glMatrixMode(GL_PROJECTION) ;
glLoadIdentity() ;
gluPerspective(45.0, m_gldAspect, 0.1, 100000.0) ;
glViewport(0, 0, cx, cy) ;
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glTranslated(0.0f, 0.0f, -4.0f);


//No rendering contex will be current
wglMakeCurrent(NULL, NULL) ;

}

Share this post


Link to post
Share on other sites
I'm kinda having the same problem as you. I'm using gluProject to get the model's position in screen coordinates like so:

GLdouble modelMatrix[16] ;
GLdouble projMatrix[16] ;
GLint viewPort[4] ;

glGetDoublev ( GL_MODELVIEW_MATRIX, modelMatrix ) ;
glGetDoublev ( GL_PROJECTION_MATRIX, projMatrix ) ;
glGetIntegerv ( GL_VIEWPORT, viewPort ) ;

gluProject ( sprite_x,
sprite_y, sprite_z ,
modelMatrix, projMatrix, viewPort,
&screen_x, &screen_y, &screen_z ) ;

I'm not exactly sure what screen_z is all about. I guess its to mirror gluUnProject. Anyway, you can just ignore it

Now you've got the model's screen coordinates, I'll leave it up to you to decide if the mouse is over it and if it is clicked or whatever.

The next part is where I've been having trouble. You can you gluUnProject to convert the screen coordinates to your model's coordinate system...

gluUnProject ( (double) mouse_x,
(double) mouse_y, 0.0,
modelMatrix, projMatrix, viewPort,
&x, &y, &z );

Unfortunately this gives coordinates that are pretty mush useless. z will be the camera's z and x and y will be there, not where z=0 which is what you want. I've tried putting different values in where I have 0.0 above, but It didn't really do what I want.

So what you (and I) need is to be able to make a vector from the camera that passes through the x,y,z from gluUnproject above and find out where it intersects the z=0 plane.

I hope this gets you a little further, and if you do find the soution please post it here, since I need the answer too.

Peace.


[edited by - JahToasted on January 17, 2004 2:11:22 AM]

Share this post


Link to post
Share on other sites
Ok I figured out the second half of the problem. I posted the solution here:

http://www.gamedev.net/community/forums/topic.asp?topic_id=201999

hope this helps.

Share this post


Link to post
Share on other sites
quote:
Original post by lazork357
I use this code to pick 2D objects but I think it may be useful.
It has to be written in the WindowProc.

case WM_MOUSEMOVE:
{
MousePos.x = (int)(((float)LOWORD(lParam))/APP_WIDTH);
MousePos.y = (int)(((float)HIWORD(lParam))/APP_HEIGHT);
}
break;



Here is how you change into window coords.

// Get the mouse''s current X, Y position
POINT mousePos;
GetCursorPos(&mousePos);
ScreenToClient(g_hWnd, &mousePos);

quote:

BTW, does anyone know how to make it impossible to resize the window?


Yes. To do this it''s how you initially create your window. This post is going to be messy but.


///////////////////////////////// CREATE WIN32 WINDOW \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\*

/////

///// This function creates a window, then maps calls to winproc

/////

///////////////////////////////// CREATE WIN32 WINDOW \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\*


HWND CreateWin32Window(LPSTR strWindowName, int width, int height, DWORD dwStyle, bool bFullScreen, HINSTANCE hInstance)
{
ShowCursor(TRUE); // Show the mouse


HWND hWnd;
char class_name[] = "72Hours";

if(!bFullScreen)
{
WNDCLASSEX wndclassex;
wndclassex.cbSize = sizeof(WNDCLASSEX);
wndclassex.style = CS_HREDRAW | CS_VREDRAW;
wndclassex.lpfnWndProc = WinProc;
wndclassex.cbClsExtra = 0;
wndclassex.cbWndExtra = 0;
wndclassex.hInstance = hInstance;
wndclassex.hIcon = NULL;
wndclassex.hCursor = NULL;
wndclassex.hCursor = (HCURSOR)LoadImage(hInstance, MAKEINTRESOURCE(IDC_POINTER),
IMAGE_CURSOR, 0, 0, LR_DEFAULTSIZE); // Window sets shape of mouse

wndclassex.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH);
wndclassex.lpszMenuName = NULL;
wndclassex.lpszClassName = class_name;
wndclassex.hIconSm = NULL;

//Support my window without the min/max stuff

RegisterClassEx(&wndclassex);

hWnd = CreateWindowEx(WS_EX_APPWINDOW, // This is a flag that can give our window different properties

class_name,
strWindowName,
WS_OVERLAPPED | WS_SYSMENU, // Window won''t be resizable

0,
0,
width,
height,
NULL,
NULL,
hInstance,
NULL);
}

if(!hWnd)
{
UnregisterClass(class_name, hInstance);
return NULL;
}

ShowWindow(hWnd, SW_SHOWNORMAL); // Show the window

UpdateWindow(hWnd); // Draw the window


SetFocus(hWnd); // Sets Keyboard Focus To The Window


return hWnd;
}

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Does anybody know a place where I can find an example/demo in C++ to move objects with the mouse? I''ve searched a long time but I wasn''t able to find one.

Fanny

Share this post


Link to post
Share on other sites
Since I get mouse input with direct X just multiply the mickey value by some arbitary number 0.001 works nice and translate, or rotate or do whatever with it.

If you want to place an object in an opengl window based on the cursor position then get the cursor position, relative to the client part of the window then do

x=x/Width*2-1
y=y/Height*2-1

And then translate your object, assuming of course that your object is centered on zero.

[edited by - RamboBones on February 29, 2004 10:53:04 PM]

Share this post


Link to post
Share on other sites
Hey Explosive.

You can use gluUnProject to accomplish this.

I have some Delphi source code on my site showing how to do this.

gluUnProject

I seem to remember there being a c++ tutorial on NeHe's site about this also.



[edited by - McCLaw on March 1, 2004 6:23:54 AM]

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!