Jump to content
  • Advertisement
Sign in to follow this  
mcdruga

OpenGL Mouse moving Camera C++

This topic is 3832 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

Need Help! Programming in C + + and using OpenGL, GLUT, I would like to move the camera with the mouse ... I know that there are some topics about It, I already read, but can not implement ... (noob) hehehehe... =) I can make the camera move with the keyboard keys... but not with the mouse ... Someone could help me? Thank you!!!

Share this post


Link to post
Share on other sites
Advertisement
Tks for the example, but:
I like to Detect Passive motion...
Like in FPS games...

Dunno how to do...
plz HELP!!!!

tks all

Share this post


Link to post
Share on other sites
Lets see if I explain better my problem:

I need to do a game in First Person, in an environment more like the real as possible ...


I've done somethings, but glutPassiveMotionFunc is Killing me...
I don´t know how to define it very well.


What I Like to do is:
The Mouse Moving the camera as in FPS games.
But I dont like to press a Button to Active that...


With keys is easy, because each time you press one, you increase or decrease in LookAt ...

With the Mouse is difficult, I do not know how to do it...
How to get such data from the Mouse.


Someone has done something similar?

Thank you ...
If anyone has any ideas, code, tutorial, or something that can help me ...

Tks =)

Share this post


Link to post
Share on other sites
don't know if that's what your asking but i'll try to explain, it's only useful for win32 api's though....
you can retrieve data from the mouse by reading the WM_MOUSEMOVE message, you have the x coordinate that is the LOWORD of the WM_MOUSEMOVE lparam message and the y coordinate that is the HIWORD of the WM_MOUSEMOVE lparam message.... by keeping track of the muose position you can determine where it's headed to in the screen...

here's the code of a program of mine....

case WM_MOUSEMOVE: // mouse movement

if(LOWORD(lParam) > 500)
{
g_glRender->y_world += 10.0f;
if(g_glRender->y_world > 360.0f)
{
g_glRender->y_world = 0;
}
}

if(LOWORD(lParam) < 300)
{
g_glRender->y_world -= 10.0f;
if(g_glRender->y_world < 0.0f)
{
g_glRender->y_world = 360.0f;
}
}

if(HIWORD(lParam) > 400)
{
g_glRender->x_world += 10.0f;
if(g_glRender->x_world > 90.0f)
{
g_glRender->x_world = 90;
}
}

if(HIWORD(lParam) < 200)
{
g_glRender->x_world -= 10.0f;
if(g_glRender->x_world < -90.0f)
{
g_glRender->x_world = -90.0f;
}
}

if(LOWORD(lParam) > 501 || LOWORD(lParam) < 298)
{
SetCursorPos(400, 300);
}

if(HIWORD(lParam) > 401 || HIWORD(lParam) < 198)
{
SetCursorPos(400, 300);
}

break;


Share this post


Link to post
Share on other sites
Here's some clean code, that I generally use with a dualshock gamepad and modified it to use the mouse:


//---[ camera data ]-----------------[
vec3 camera_pos=vec3(0,0,0);

float mouse_rotx=0; // X rotation
float mouse_roty=0.0; // Y rotation
//-----------------------------------/


//-----[ bounds and settings ]--------------[
float mouse_minY= -85.0,mouse_maxY= 85.0; // bounds of y-rotation
float mouse_sensitivity=3.3;
float movement_speed=0.1;
//------------------------------------------/

char keys[256]={0};



void MoveCamera(float lx,float ly){
//-----[ normalize movement ]------[
if(lx && ly){
lx*=0.7071;
ly*=0.7071;
}
//---------------------------------/

float moveforth = -ly*movement_speed;
float moveright = lx*movement_speed;

float rotx = mouse_rotx*M_PI/180.0; // convert to radians
float roty = mouse_roty*M_PI/180.0; // convert to radians

camera_pos.x += sinf(roty) * moveforth;
camera_pos.z -= cosf(roty) * moveforth;
camera_pos.y -= sinf(rotx) * moveforth;

camera_pos.x += cosf(roty) * moveright;
camera_pos.z += sinf(roty) * moveright;
}

void RotateCamera(float rx,float ry){
mouse_rotx+= rx * mouse_sensitivity;
mouse_roty+= ry * mouse_sensitivity;

//-------[ fixup Y rotation ]-----------------------[
if(mouse_roty<mouse_minY)mouse_roty=mouse_minY;
else if(mouse_roty>mouse_maxY)mouse_roty=mouse_maxY;
//--------------------------------------------------/
}

void ApplyCamera(){
//----[ first, check pressed keys and move ]---------[
float lx=0,ly=0;
if(keys['A'])lx= -1;
if(keys['D'])lx= 1;
if(keys['W'])ly= 1;
if(keys['S'])ly= -1;
MoveCamera(lx,ly);
//---------------------------------------------------/


//------[ now onto creating the camera matrix ]------------[
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();

glRotatef(mouse_rotx,1,0,0);
glRotatef(mouse_roty,0,1,0);
glTranslatef(-camera_pos.x,-camera_pos.y,-camera_pos.z);
//---------------------------------------------------------/

}




LRESULT CALLBACK WndProc(HWND hWnd,UINT uMsg,WPARAM wParam,LPARAM lParam){
static int WindowWidth=1280,WindowHeight=720; // set these in WM_CREATE or however you like
......
case WM_CREATE:
SetCursorPos(WindowWidth/2,WindowHeight/2);
SetCapture(hWnd); // optional
return 0;
case WM_KEYDOWN:
keys[wParam&255]=true;
return 0;
case WM_KEYUP:
keys[wParam&255]=true;
return 0;
case WM_MOUSEMOVE:
int dx=LOWORD(lParam) - WindowWidth/2;
int dy=HIWORD(lParam) - WindowHeight/2;
if(dx || dy){
RotateCamera(dx,dy);
SetCursorPos(WindowWidth/2,WindowHeight/2);
}
return 0;
......
}

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

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

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!