<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
		>
<channel>
	<title>Comments on: Simple OpenGL Keyboard and Mouse FPS Controls</title>
	<atom:link href="http://r3dux.org/2011/05/simple-opengl-keyboard-and-mouse-fps-controls/feed/" rel="self" type="application/rss+xml" />
	<link>http://r3dux.org/2011/05/simple-opengl-keyboard-and-mouse-fps-controls/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=simple-opengl-keyboard-and-mouse-fps-controls</link>
	<description>A number-pimping side project from the valleys in *NEW* upside-down flavour.</description>
	<lastBuildDate>Tue, 15 May 2012 23:01:55 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=</generator>
	<item>
		<title>By: r3dux</title>
		<link>http://r3dux.org/2011/05/simple-opengl-keyboard-and-mouse-fps-controls/#comment-7678</link>
		<dc:creator>r3dux</dc:creator>
		<pubDate>Thu, 19 Apr 2012 21:55:51 +0000</pubDate>
		<guid isPermaLink="false">http://r3dux.org/?p=3884#comment-7678</guid>
		<description>When glfwSwapInterval is enabled (1), then drawing is locked to the vertical refresh rate of your screen, whether that&#039;s 50Hz, 60Hz, 75Hz, 85H or whatever. So if your program draws a frame, then it has time to spare before the next frame, it&#039;ll just wait.

When glfwSwapInterval is disabled (0), then the program will run as fast as it possibly can - once the program has drawn a frame, it moves right onto to the next frame without worrying about weather drawing at this point in the display&#039;s refresh might cause page-tearing or anything. 

These are your two built-in options.

The third option, which we need to implement ourselves, can be to lock our program to a specified refresh rate by working out how many milliseconds we want each frame to take (i.e. 60fps = 1000 / 60 = 16.6667ms), and then in our main loop we:
&lt;ol&gt;&lt;li&gt;Get the current system time (frameStart time)&lt;/li&gt;
&lt;li&gt;Do stuff and draw the frame,&lt;/li&gt;
&lt;li&gt;Get the current system time (frameEnd time),&lt;/li&gt;
&lt;li&gt;Calculate the frameDraw time (which is frameStart - frameEnd),&lt;/li&gt;
&lt;li&gt;If the frameDraw time is &gt; 16.667ms draw the next frame, otherwise, if the frameDraw time is &lt; 16.667ms, sleep for (16.667 - frameDraw time)ms&lt;/li&gt;&lt;/ol&gt;

To do this, you can use code like this:

&lt;pre lang=&quot;cpp&quot;&gt;
// Frame limiting vars
int    desiredFPS = 60;
double msPerFrame = 1000 / desiredFPS;
double frameStartTime, frameEndTime, frameDrawTime;


// ***** Everything below this line goes inside the main loop *****
frameStartTime = glfwGetTime();

// Do any calculations and draw the frame here

// Lock to ~60fps
frameEndTime = glfwGetTime();
frameDrawTime = frameEndTime - frameStartTime;
if (frameDrawTime &lt; msPerFrame)
{
	glfwSleep(msPerFrame - frameDrawTime);
}
&lt;/pre&gt;

Hope this helps.</description>
		<content:encoded><![CDATA[<p>When glfwSwapInterval is enabled (1), then drawing is locked to the vertical refresh rate of your screen, whether that&#8217;s 50Hz, 60Hz, 75Hz, 85H or whatever. So if your program draws a frame, then it has time to spare before the next frame, it&#8217;ll just wait.</p>
<p>When glfwSwapInterval is disabled (0), then the program will run as fast as it possibly can &#8211; once the program has drawn a frame, it moves right onto to the next frame without worrying about weather drawing at this point in the display&#8217;s refresh might cause page-tearing or anything. </p>
<p>These are your two built-in options.</p>
<p>The third option, which we need to implement ourselves, can be to lock our program to a specified refresh rate by working out how many milliseconds we want each frame to take (i.e. 60fps = 1000 / 60 = 16.6667ms), and then in our main loop we:</p>
<ol>
<li>Get the current system time (frameStart time)</li>
<li>Do stuff and draw the frame,</li>
<li>Get the current system time (frameEnd time),</li>
<li>Calculate the frameDraw time (which is frameStart &#8211; frameEnd),</li>
<li>If the frameDraw time is > 16.667ms draw the next frame, otherwise, if the frameDraw time is < 16.667ms, sleep for (16.667 - frameDraw time)ms</li>
</li>
</ol>
<p>To do this, you can use code like this:</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #666666;">// Frame limiting vars</span>
<span style="color: #0000ff;">int</span>    desiredFPS <span style="color: #000080;">=</span> <span style="color: #0000dd;">60</span><span style="color: #008080;">;</span>
<span style="color: #0000ff;">double</span> msPerFrame <span style="color: #000080;">=</span> <span style="color: #0000dd;">1000</span> <span style="color: #000040;">/</span> desiredFPS<span style="color: #008080;">;</span>
<span style="color: #0000ff;">double</span> frameStartTime, frameEndTime, frameDrawTime<span style="color: #008080;">;</span>
&nbsp;
&nbsp;
<span style="color: #666666;">// ***** Everything below this line goes inside the main loop *****</span>
frameStartTime <span style="color: #000080;">=</span> glfwGetTime<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
&nbsp;
<span style="color: #666666;">// Do any calculations and draw the frame here</span>
&nbsp;
<span style="color: #666666;">// Lock to ~60fps</span>
frameEndTime <span style="color: #000080;">=</span> glfwGetTime<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
frameDrawTime <span style="color: #000080;">=</span> frameEndTime <span style="color: #000040;">-</span> frameStartTime<span style="color: #008080;">;</span>
<span style="color: #0000ff;">if</span> <span style="color: #008000;">&#40;</span>frameDrawTime <span style="color: #000080;">&lt;</span> msPerFrame<span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
	glfwSleep<span style="color: #008000;">&#40;</span>msPerFrame <span style="color: #000040;">-</span> frameDrawTime<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p>Hope this helps.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: haxpor</title>
		<link>http://r3dux.org/2011/05/simple-opengl-keyboard-and-mouse-fps-controls/#comment-7663</link>
		<dc:creator>haxpor</dc:creator>
		<pubDate>Mon, 16 Apr 2012 07:36:34 +0000</pubDate>
		<guid isPermaLink="false">http://r3dux.org/?p=3884#comment-7663</guid>
		<description>@r3dux, I wonder about glfwSwapInterval() (apparently I just see that there&#039;re just two options to set 1, 0 not merely a number or some other values, Am i right about this ?) whether how can we&#039;re sure it will be lock down to 60Hz or 60 fps. I believe it&#039;s too much for the system with this restriction. If I want the game to run only at 30fps (i don&#039;t think it will relate with Hz no more, 30Hz no good for monitor), how can I do that ?

I found Glut really flexible on this as its underlying system won&#039;t strict automatically to some setting of fps.</description>
		<content:encoded><![CDATA[<p>@r3dux, I wonder about glfwSwapInterval() (apparently I just see that there&#8217;re just two options to set 1, 0 not merely a number or some other values, Am i right about this ?) whether how can we&#8217;re sure it will be lock down to 60Hz or 60 fps. I believe it&#8217;s too much for the system with this restriction. If I want the game to run only at 30fps (i don&#8217;t think it will relate with Hz no more, 30Hz no good for monitor), how can I do that ?</p>
<p>I found Glut really flexible on this as its underlying system won&#8217;t strict automatically to some setting of fps.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: r3dux</title>
		<link>http://r3dux.org/2011/05/simple-opengl-keyboard-and-mouse-fps-controls/#comment-7658</link>
		<dc:creator>r3dux</dc:creator>
		<pubDate>Sun, 15 Apr 2012 03:08:57 +0000</pubDate>
		<guid isPermaLink="false">http://r3dux.org/?p=3884#comment-7658</guid>
		<description>I can give you my Linux Code::Blocks project if that&#039;s what you mean, but the OpenGL library (libGL.so etc) comes as part of the graphics driver package.

If you really want the C::B project let me know and I&#039;ll upload it.</description>
		<content:encoded><![CDATA[<p>I can give you my Linux Code::Blocks project if that&#8217;s what you mean, but the OpenGL library (libGL.so etc) comes as part of the graphics driver package.</p>
<p>If you really want the C::B project let me know and I&#8217;ll upload it.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Kien Ha</title>
		<link>http://r3dux.org/2011/05/simple-opengl-keyboard-and-mouse-fps-controls/#comment-7656</link>
		<dc:creator>Kien Ha</dc:creator>
		<pubDate>Sat, 14 Apr 2012 16:24:48 +0000</pubDate>
		<guid isPermaLink="false">http://r3dux.org/?p=3884#comment-7656</guid>
		<description>Can you give me , your opengl library ?</description>
		<content:encoded><![CDATA[<p>Can you give me , your opengl library ?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: r3dux</title>
		<link>http://r3dux.org/2011/05/simple-opengl-keyboard-and-mouse-fps-controls/#comment-7507</link>
		<dc:creator>r3dux</dc:creator>
		<pubDate>Mon, 27 Feb 2012 10:19:29 +0000</pubDate>
		<guid isPermaLink="false">http://r3dux.org/?p=3884#comment-7507</guid>
		<description>I can&#039;t say I&#039;ve tried it, but you should be able to use GLUT keyboard &amp; mouse handlers without any issues - certainly sounds reasonable. Give it a whirl! =D</description>
		<content:encoded><![CDATA[<p>I can&#8217;t say I&#8217;ve tried it, but you should be able to use GLUT keyboard &amp; mouse handlers without any issues &#8211; certainly sounds reasonable. Give it a whirl! =D</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Annette</title>
		<link>http://r3dux.org/2011/05/simple-opengl-keyboard-and-mouse-fps-controls/#comment-7503</link>
		<dc:creator>Annette</dc:creator>
		<pubDate>Sun, 26 Feb 2012 21:45:00 +0000</pubDate>
		<guid isPermaLink="false">http://r3dux.org/?p=3884#comment-7503</guid>
		<description>Hi,
I managed to get the keyboard code working in my program. As it turns out, there were some changes I needed to make in my code which I over looked. I&#039;m working on the mouse code now but I have a question. I know you can use both glut and glfw functions in one program. That is how mine is. Is it possible for me to use glut functions for the keyboard and mouse interactions instead of glfw functions? Will my program work or I have to stick with glfw functions?</description>
		<content:encoded><![CDATA[<p>Hi,<br />
I managed to get the keyboard code working in my program. As it turns out, there were some changes I needed to make in my code which I over looked. I&#8217;m working on the mouse code now but I have a question. I know you can use both glut and glfw functions in one program. That is how mine is. Is it possible for me to use glut functions for the keyboard and mouse interactions instead of glfw functions? Will my program work or I have to stick with glfw functions?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Annette</title>
		<link>http://r3dux.org/2011/05/simple-opengl-keyboard-and-mouse-fps-controls/#comment-7494</link>
		<dc:creator>Annette</dc:creator>
		<pubDate>Fri, 24 Feb 2012 15:04:46 +0000</pubDate>
		<guid isPermaLink="false">http://r3dux.org/?p=3884#comment-7494</guid>
		<description>Thank you so much for your prompt response and detailed explanations. It helps me learn more. Thank you. I&#039;ll take a look at the sample codes and revisit this code again. Hopefully, I will be able to sort it out. Thanks again.</description>
		<content:encoded><![CDATA[<p>Thank you so much for your prompt response and detailed explanations. It helps me learn more. Thank you. I&#8217;ll take a look at the sample codes and revisit this code again. Hopefully, I will be able to sort it out. Thanks again.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: r3dux</title>
		<link>http://r3dux.org/2011/05/simple-opengl-keyboard-and-mouse-fps-controls/#comment-7493</link>
		<dc:creator>r3dux</dc:creator>
		<pubDate>Fri, 24 Feb 2012 06:39:54 +0000</pubDate>
		<guid isPermaLink="false">http://r3dux.org/?p=3884#comment-7493</guid>
		<description>It sounds like something&#039;s off with the mouse and keyboard callback registration, which should occur with these lines in the main:

&lt;pre lang=&quot;cpp&quot;&gt;// Specify the function which should execute when a key is pressed or released
glfwSetKeyCallback(handleKeypress);
 
// Specify the function which should execute when the mouse is moved
glfwSetMousePosCallback(handleMouseMove);&lt;/pre&gt;

The mouse cursor is supposed to be in the dead-centre of the screen and invisible, because the way we handle mouse movement is to calc how far from the centre of the screen the mouse has moved per frame (i.e. + or - horizontally (x-axis) and + or - vertically (y-axis) ), and then modify the ModelView matrix accordingly so you actually &quot;look&quot; in different directions. The mouse cursor then gets set back to the dead-centre of the window after each frame where we detect some cursor movement.

I&#039;ve dug out some example code from when I was learning keyboard and mouse handlers for you. Once you can get those to work, you should be fine for the FPS controls:
&lt;a href=&quot;http://r3dux.org/files/code/GLFW-Input-Handlers/GLFWKeyboardHandlerTest.zip&quot;&gt;GLFWKeyboardHandlerTest.zip&lt;/a&gt;
&lt;a href=&quot;http://r3dux.org/files/code/GLFW-Input-Handlers/GLFWMouseHandlerTest.zip&quot;&gt;GLFWMouseHandlerTest.zip&lt;/a&gt;

Hope this helps.

&lt;strong&gt;Update&lt;/strong&gt;: I should add that as far as I&#039;m concerned these mouse and keyboard handlers work perfectly. I just don&#039;t know if they&#039;ll work for you. They should, but who knows... Could be something specific to your setup causing issues.</description>
		<content:encoded><![CDATA[<p>It sounds like something&#8217;s off with the mouse and keyboard callback registration, which should occur with these lines in the main:</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #666666;">// Specify the function which should execute when a key is pressed or released</span>
glfwSetKeyCallback<span style="color: #008000;">&#40;</span>handleKeypress<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
&nbsp;
<span style="color: #666666;">// Specify the function which should execute when the mouse is moved</span>
glfwSetMousePosCallback<span style="color: #008000;">&#40;</span>handleMouseMove<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span></pre></div></div>

<p>The mouse cursor is supposed to be in the dead-centre of the screen and invisible, because the way we handle mouse movement is to calc how far from the centre of the screen the mouse has moved per frame (i.e. + or &#8211; horizontally (x-axis) and + or &#8211; vertically (y-axis) ), and then modify the ModelView matrix accordingly so you actually &#8220;look&#8221; in different directions. The mouse cursor then gets set back to the dead-centre of the window after each frame where we detect some cursor movement.</p>
<p>I&#8217;ve dug out some example code from when I was learning keyboard and mouse handlers for you. Once you can get those to work, you should be fine for the FPS controls:<br />
<a href="http://r3dux.org/files/code/GLFW-Input-Handlers/GLFWKeyboardHandlerTest.zip">GLFWKeyboardHandlerTest.zip</a><br />
<a href="http://r3dux.org/files/code/GLFW-Input-Handlers/GLFWMouseHandlerTest.zip">GLFWMouseHandlerTest.zip</a></p>
<p>Hope this helps.</p>
<p><strong>Update</strong>: I should add that as far as I&#8217;m concerned these mouse and keyboard handlers work perfectly. I just don&#8217;t know if they&#8217;ll work for you. They should, but who knows&#8230; Could be something specific to your setup causing issues.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Annette</title>
		<link>http://r3dux.org/2011/05/simple-opengl-keyboard-and-mouse-fps-controls/#comment-7488</link>
		<dc:creator>Annette</dc:creator>
		<pubDate>Thu, 23 Feb 2012 23:26:41 +0000</pubDate>
		<guid isPermaLink="false">http://r3dux.org/?p=3884#comment-7488</guid>
		<description>Hi,

Thanks for the code. I used your keyboard and mouse input code in my program but the mouse is stationary in the middle of the screen and won&#039;t move way up or down. Nothing happens when I press the keyboard too. Any pointers as to what I might be doing wrong and how I can tweak your code to work for me?</description>
		<content:encoded><![CDATA[<p>Hi,</p>
<p>Thanks for the code. I used your keyboard and mouse input code in my program but the mouse is stationary in the middle of the screen and won&#8217;t move way up or down. Nothing happens when I press the keyboard too. Any pointers as to what I might be doing wrong and how I can tweak your code to work for me?</p>
]]></content:encoded>
	</item>
</channel>
</rss>

