# Jumping Veiw

**URL:** <https://discourse.glfw.org/t/jumping-veiw/143>\
**Category:** support\
**Created:** [August 23, 2004, 2:39pm UTC](https://discourse.glfw.org/t/jumping-veiw/143 "2004-08-23T14:39:45Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex035/uploads/glfw/original/1X/b8cdefe6b61b8d5aee6c43b2c56e9cf5eabecfe4.svg) [@system](https://discourse.glfw.org/u/system)\
**Post date:** [August 23, 2004, 2:39pm UTC](https://discourse.glfw.org/t/jumping-veiw/143/1 "2004-08-23T14:39:45Z")

</div>

**anonymous** wrote on [Monday, August 23, 2004](https://sourceforge.net/p/glfw/discussion/247562/thread/a95bf10f/#b5af):

This problem I am having is probably my fault, but I can’t see what I’m doing wrong, so I’ll ask here.

I have my Mouse Pos Callback function:

void GLFWCALL Ortho::MousePosCallback(int PosX, int PosY)  
{  
This-\>Center.X+=5.0\*double(PosX)/double(This-\>Zoom);  
This-\>Center.Y+=5.0\*double(PosY)/double(This-\>Zoom);  
glfwSetMousePos(0,0);  
This-\>SetVeiw();  
}

In this function Center is the center of my veiwscreen, Zoom is how much I am zoomed in, and SetVeiw() resets my veiw to take into account all of my changes. When I set this function at the beginning of my program, everything worked fine. But I wanted to only move the camera when I right-clicked, so I made this function:

void GLFWCALL Ortho::MouseButtonCallback(int Button, int Action)  
{  
if(Button==GLFW\_MOUSE\_BUTTON\_RIGHT)  
{  
&nbsp;&nbsp;&nbsp; switch(Action)  
&nbsp;&nbsp;&nbsp; {  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; case GLFW\_PRESS:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; This-\>CameraMoveOn=true;  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; glfwDisable(GLFW\_MOUSE\_CURSOR);  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; glfwSetMousePosCallback(MousePosCallback);  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; break;  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; case GLFW\_RELEASE:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; This-\>CameraMoveOn=false;  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; glfwEnable(GLFW\_MOUSE\_CURSOR);  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; glfwSetMousePosCallback(NULL);  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }  
&nbsp;&nbsp;&nbsp; }  
}

Now, whenever I first right-click, the center of my screen jumps. I tried adding glfwSetMousePos(0,0); In the press case, but it didn’t help.

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex035/uploads/glfw/original/1X/b8cdefe6b61b8d5aee6c43b2c56e9cf5eabecfe4.svg) [@system](https://discourse.glfw.org/u/system)\
**Post date:** [August 23, 2004, 3:28pm UTC](https://discourse.glfw.org/t/jumping-veiw/143/2 "2004-08-23T15:28:26Z")

</div>

**anonymous** wrote on [Monday, August 23, 2004](https://sourceforge.net/p/glfw/discussion/247562/thread/a95bf10f/#b5af/d00a):

Just in case you want to know, here is my SetVeiw function:

void Ortho::SetVeiw()  
{  
if(WindowSize.Y==0)  
WindowSize.Y=1;

glViewport(0,0,WindowSize.X,WindowSize.Y);

glMatrixMode(GL\_PROJECTION);  
glLoadIdentity();

glOrtho(  
Center.X-(double(WindowSize.X)/Zoom),  
Center.X+(double(WindowSize.X)/Zoom),  
Center.Y-(double(WindowSize.Y)/Zoom),  
Center.Y+(double(WindowSize.Y)/Zoom),  
-1.0f,1.0f);

glMatrixMode(GL\_MODELVIEW);  
glLoadIdentity();  
}

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex035/uploads/glfw/original/1X/b8cdefe6b61b8d5aee6c43b2c56e9cf5eabecfe4.svg) [@system](https://discourse.glfw.org/u/system)\
**Post date:** [September 17, 2004, 4:53pm UTC](https://discourse.glfw.org/t/jumping-veiw/143/3 "2004-09-17T16:53:09Z")

</div>

**anonymous** wrote on [Friday, September 17, 2004](https://sourceforge.net/p/glfw/discussion/247562/thread/a95bf10f/#b5af/9e7c):

Anyone have anything? I stopped trying a while ago. Now I’ve come back to it, and I still can’t come up with anything. My solution so far has been to get rid of glfwDisable(GLFW\_MOUSE\_CURSOR);

case GLFW\_PRESS:  
{  
This-\>CameraMoveOn=true;  
glfwSetMousePos(This-\>WindowSize.X/2,This-\>WindowSize.Y/2);  
glfwSetMousePosCallback(MousePosCallback);  
break;  
}

void GLFWCALL Ortho::MousePosCallback(int PosX, int PosY)  
{  
This-\>Center.X+=5.0\*double(PosX-int(This-\>WindowSize.X/2))/double(This-\>Zoom);  
This-\>Center.Y-=5.0\*double(PosY-int(This-\>WindowSize.Y/2))/double(This-\>Zoom);  
glfwSetMousePos(This-\>WindowSize.X/2,This-\>WindowSize.Y/2);  
This-\>SetVeiw();  
}

This works, but the mouse isn’t disabled…

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex035/uploads/glfw/original/1X/b8cdefe6b61b8d5aee6c43b2c56e9cf5eabecfe4.svg) [@system](https://discourse.glfw.org/u/system)\
**Post date:** [September 20, 2004, 9:22am UTC](https://discourse.glfw.org/t/jumping-veiw/143/4 "2004-09-20T09:22:11Z")

</div>

**[marcus256](https://sourceforge.net/u/marcus256/)** wrote on [Monday, September 20, 2004](https://sourceforge.net/p/glfw/discussion/247562/thread/a95bf10f/#b5af/9e7c/5da0):

Sorry for not replying. I haven’t had the time to look at this. Here is a hint though: you shouldn’t call GLFW functions from within callback functions, except for glfwGetXYZ (e.g. glfwGetKey, glfwGetMousePos etc).

This is not mentioned in the GLFW 2.4 docs, but I have added a section about it in the GLFW 2.5 reference manual.

Calling glfwSetMousePos from the mouse callback may disorient the internal GLFW mouse handling. You should be able to find another solution for this (e.g. delay the GLFW call(s) to after the event polling call).

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex035/uploads/glfw/original/1X/b8cdefe6b61b8d5aee6c43b2c56e9cf5eabecfe4.svg) [@system](https://discourse.glfw.org/u/system)\
**Post date:** [September 20, 2004, 9:27am UTC](https://discourse.glfw.org/t/jumping-veiw/143/5 "2004-09-20T09:27:22Z")

</div>

**[marcus256](https://sourceforge.net/u/marcus256/)** wrote on [Monday, September 20, 2004](https://sourceforge.net/p/glfw/discussion/247562/thread/a95bf10f/#b5af/9e7c/5da0/a3ed):

Two more solutions (2nd solution is the preferred solution):

1) Always call glfwSetMousePos(0,0) after glfwPollEvents/glfwSwapBuffers

2) Store the old mouse position in a global variable, and form deltas manually, i.e.:

void GLFWCALL Ortho::MousePosCallback(int PosX, int PosY)  
{  
int dx,dy;  
dx=PosX-OldPosX;  
dy=PosY-OldPosY;  
OldPosX=PosX;  
OldPosY=PosY;  
This-\>Center.X+=5.0\*double(dx)/double(This-\>Zoom);  
This-\>Center.Y+=5.0\*double(dy)/double(This-\>Zoom);  
This-\>SetVeiw();  
}

Here, OldPosX and OldPosY are global variables.
