<liclass="level1"><ahref="#quick_example">Putting it together</a></li>
</ul>
</div>
<divclass="textblock"><p>This guide takes you through writing a simple application using GLFW 3. The application will create a window and OpenGL context, render a rotating triangle and exit when the user closes the window or presses <em>Escape</em>. This guide will introduce a few of the most commonly used functions, but there are many more.</p>
<p>This guide assumes no experience with earlier versions of GLFW. If you have used GLFW 2 in the past, read <aclass="el"href="moving_guide.html">Moving from GLFW 2 to 3</a>, as some functions behave differently in GLFW 3.</p>
<h1><aclass="anchor"id="quick_steps"></a>
Step by step</h1>
<h2><aclass="anchor"id="quick_include"></a>
Including the GLFW header</h2>
<p>In the source files of your application where you use OpenGL or GLFW, you need to include the GLFW 3 header file.</p>
<divclass="fragment"><divclass="line"><spanclass="preprocessor">#include <<aclass="code"href="glfw3_8h.html">GLFW/glfw3.h</a>></span></div></div><!-- fragment --><p>This defines all the constants, types and function prototypes of the GLFW API. It also includes the OpenGL header from your development environment and defines all the constants and types necessary for it to work on your platform without including any platform-specific headers.</p>
<p>In other words:</p>
<ul>
<li>Do <em>not</em> include the OpenGL header yourself, as GLFW does this for you in a platform-independent way</li>
<li>Do <em>not</em> include <code>windows.h</code> or other platform-specific headers unless you plan on using those APIs yourself</li>
<li>If you <em>do</em> need to include such headers, include them <em>before</em> the GLFW header and it will detect this</li>
</ul>
<p>On some platforms supported by GLFW the OpenGL header and link library only expose older versions of OpenGL. The most extreme case is Windows, which only exposes OpenGL 1.2. The easiest way to work around this is to use an <aclass="el"href="context_guide.html#context_glext_auto">extension loader library</a>.</p>
<p>If you are using such a library then you should include its header <em>before</em> the GLFW header. This lets it replace the OpenGL header included by GLFW without conflicts. This example uses <ahref="https://github.com/Dav1dde/glad">glad</a>, but the same rule applies to all such libraries.</p>
<divclass="fragment"><divclass="line"><spanclass="preprocessor">#include <glad/glad.h></span></div><divclass="line"><spanclass="preprocessor">#include <<aclass="code"href="glfw3_8h.html">GLFW/glfw3.h</a>></span></div></div><!-- fragment --><h2><aclass="anchor"id="quick_init_term"></a>
Initializing and terminating GLFW</h2>
<p>Before you can use most GLFW functions, the library must be initialized. On successful initialization, <code>GLFW_TRUE</code> is returned. If an error occurred, <code>GLFW_FALSE</code> is returned.</p>
<divclass="fragment"><divclass="line"><spanclass="keywordflow">if</span> (!<aclass="code"href="group__init.html#ga317aac130a235ab08c6db0834907d85e">glfwInit</a>())</div><divclass="line">{</div><divclass="line"><spanclass="comment">// Initialization failed</span></div><divclass="line">}</div></div><!-- fragment --><p>Note that <code>GLFW_TRUE</code> and <code>GLFW_FALSE</code> are and will always be just one and zero.</p>
<p>When you are done using GLFW, typically just before the application exits, you need to terminate GLFW.</p>
<divclass="fragment"><divclass="line"><aclass="code"href="group__init.html#gaaae48c0a18607ea4a4ba951d939f0901">glfwTerminate</a>();</div></div><!-- fragment --><p>This destroys any remaining windows and releases any other resources allocated by GLFW. After this call, you must initialize GLFW again before using any GLFW functions that require it.</p>
<h2><aclass="anchor"id="quick_capture_error"></a>
Setting an error callback</h2>
<p>Most events are reported through callbacks, whether it's a key being pressed, a GLFW window being moved, or an error occurring. Callbacks are simply C functions (or C++ static methods) that are called by GLFW with arguments describing the event.</p>
<p>In case a GLFW function fails, an error is reported to the GLFW error callback. You can receive these reports with an error callback. This function must have the signature below. This simple error callback just prints the error description to <code>stderr</code>.</p>
<divclass="fragment"><divclass="line"><spanclass="keywordtype">void</span> error_callback(<spanclass="keywordtype">int</span> error, <spanclass="keyword">const</span><spanclass="keywordtype">char</span>* description)</div><divclass="line">{</div><divclass="line"> fprintf(stderr, <spanclass="stringliteral">"Error: %s\n"</span>, description);</div><divclass="line">}</div></div><!-- fragment --><p>Callback functions must be set, so GLFW knows to call them. The function to set the error callback is one of the few GLFW functions that may be called before initialization, which lets you be notified of errors both during and after initialization.</p>
<divclass="fragment"><divclass="line"><aclass="code"href="group__init.html#gaa5d796c3cf7c1a7f02f845486333fb5f">glfwSetErrorCallback</a>(error_callback);</div></div><!-- fragment --><h2><aclass="anchor"id="quick_create_window"></a>
Creating a window and context</h2>
<p>The window and its OpenGL context are created with a single call to <aclass="el"href="group__window.html#ga5c336fddf2cbb5b92f65f10fb6043344">glfwCreateWindow</a>, which returns a handle to the created combined window and context object</p>
<divclass="fragment"><divclass="line"><aclass="code"href="group__window.html#ga3c96d80d363e67d13a41b5d1821f3242">GLFWwindow</a>* window = <aclass="code"href="group__window.html#ga5c336fddf2cbb5b92f65f10fb6043344">glfwCreateWindow</a>(640, 480, <spanclass="stringliteral">"My Title"</span>, NULL, NULL);</div><divclass="line"><spanclass="keywordflow">if</span> (!window)</div><divclass="line">{</div><divclass="line"><spanclass="comment">// Window or OpenGL context creation failed</span></div><divclass="line">}</div></div><!-- fragment --><p>This creates a 640 by 480 windowed mode window with an OpenGL context. If window or OpenGL context creation fails, <code>NULL</code> will be returned. You should always check the return value. While window creation rarely fails, context creation depends on properly installed drivers and may fail even on machines with the necessary hardware.</p>
<p>By default, the OpenGL context GLFW creates may have any version. You can require a minimum OpenGL version by setting the <code>GLFW_CONTEXT_VERSION_MAJOR</code> and <code>GLFW_CONTEXT_VERSION_MINOR</code> hints <em>before</em> creation. If the required minimum version is not supported on the machine, context (and window) creation fails.</p>
<divclass="fragment"><divclass="line"><aclass="code"href="group__window.html#ga7d9c8c62384b1e2821c4dc48952d2033">glfwWindowHint</a>(<aclass="code"href="glfw3_8h.html#afe5e4922de1f9932d7e9849bb053b0c0">GLFW_CONTEXT_VERSION_MAJOR</a>, 2);</div><divclass="line"><aclass="code"href="group__window.html#ga7d9c8c62384b1e2821c4dc48952d2033">glfwWindowHint</a>(<aclass="code"href="glfw3_8h.html#a31aca791e4b538c4e4a771eb95cc2d07">GLFW_CONTEXT_VERSION_MINOR</a>, 0);</div><divclass="line"><aclass="code"href="group__window.html#ga3c96d80d363e67d13a41b5d1821f3242">GLFWwindow</a>* window = <aclass="code"href="group__window.html#ga5c336fddf2cbb5b92f65f10fb6043344">glfwCreateWindow</a>(640, 480, <spanclass="stringliteral">"My Title"</span>, NULL, NULL);</div><divclass="line"><spanclass="keywordflow">if</span> (!window)</div><divclass="line">{</div><divclass="line"><spanclass="comment">// Window or context creation failed</span></div><divclass="line">}</div></div><!-- fragment --><p>The window handle is passed to all window related functions and is provided to along to all window related callbacks, so they can tell which window received the event.</p>
<p>When a window and context is no longer needed, destroy it.</p>
<divclass="fragment"><divclass="line"><aclass="code"href="group__window.html#gacdf43e51376051d2c091662e9fe3d7b2">glfwDestroyWindow</a>(window);</div></div><!-- fragment --><p>Once this function is called, no more events will be delivered for that window and its handle becomes invalid.</p>
<p>Before you can use the OpenGL API, you must have a current OpenGL context.</p>
<divclass="fragment"><divclass="line"><aclass="code"href="group__context.html#ga1c04dc242268f827290fe40aa1c91157">glfwMakeContextCurrent</a>(window);</div></div><!-- fragment --><p>The context will remain current until you make another context current or until the window owning the current context is destroyed.</p>
<p>If you are using an <aclass="el"href="context_guide.html#context_glext_auto">extension loader library</a> to access modern OpenGL then this is when to initialize it, as the loader needs a current context to load from. This example uses <ahref="https://github.com/Dav1dde/glad">glad</a>, but the same rule applies to all such libraries.</p>
<divclass="fragment"><divclass="line">gladLoadGLLoader((GLADloadproc) <aclass="code"href="group__context.html#ga35f1837e6f666781842483937612f163">glfwGetProcAddress</a>);</div></div><!-- fragment --><h2><aclass="anchor"id="quick_window_close"></a>
Checking the window close flag</h2>
<p>Each window has a flag indicating whether the window should be closed.</p>
<p>When the user attempts to close the window, either by pressing the close widget in the title bar or using a key combination like Alt+F4, this flag is set to 1. Note that <b>the window isn't actually closed</b>, so you are expected to monitor this flag and either destroy the window or give some kind of feedback to the user.</p>
<divclass="fragment"><divclass="line"><spanclass="keywordflow">while</span> (!<aclass="code"href="group__window.html#ga24e02fbfefbb81fc45320989f8140ab5">glfwWindowShouldClose</a>(window))</div><divclass="line">{</div><divclass="line"><spanclass="comment">// Keep running</span></div><divclass="line">}</div></div><!-- fragment --><p>You can be notified when the user is attempting to close the window by setting a close callback with <aclass="el"href="group__window.html#gaade9264e79fae52bdb78e2df11ee8d6a">glfwSetWindowCloseCallback</a>. The callback will be called immediately after the close flag has been set.</p>
<p>You can also set it yourself with <aclass="el"href="group__window.html#ga49c449dde2a6f87d996f4daaa09d6708">glfwSetWindowShouldClose</a>. This can be useful if you want to interpret other kinds of input as closing the window, like for example pressing the <em>Escape</em> key.</p>
<h2><aclass="anchor"id="quick_key_input"></a>
Receiving input events</h2>
<p>Each window has a large number of callbacks that can be set to receive all the various kinds of events. To receive key press and release events, create a key callback function.</p>
<divclass="fragment"><divclass="line"><spanclass="keyword">static</span><spanclass="keywordtype">void</span> key_callback(<aclass="code"href="group__window.html#ga3c96d80d363e67d13a41b5d1821f3242">GLFWwindow</a>* window, <spanclass="keywordtype">int</span> key, <spanclass="keywordtype">int</span> scancode, <spanclass="keywordtype">int</span> action, <spanclass="keywordtype">int</span> mods)</div><divclass="line">{</div><divclass="line"><spanclass="keywordflow">if</span> (key == <aclass="code"href="group__keys.html#gaac6596c350b635c245113b81c2123b93">GLFW_KEY_ESCAPE</a>&& action == <aclass="code"href="group__input.html#ga2485743d0b59df3791c45951c4195265">GLFW_PRESS</a>)</div><divclass="line"><aclass="code"href="group__window.html#ga49c449dde2a6f87d996f4daaa09d6708">glfwSetWindowShouldClose</a>(window, <aclass="code"href="glfw3_8h.html#a2744fbb29b5631bb28802dbe0cf36eba">GLFW_TRUE</a>);</div><divclass="line">}</div></div><!-- fragment --><p>The key callback, like other window related callbacks, are set per-window.</p>
<divclass="fragment"><divclass="line"><aclass="code"href="group__input.html#ga7e496507126f35ea72f01b2e6ef6d155">glfwSetKeyCallback</a>(window, key_callback);</div></div><!-- fragment --><p>In order for event callbacks to be called when events occur, you need to process events as described below.</p>
<h2><aclass="anchor"id="quick_render"></a>
Rendering with OpenGL</h2>
<p>Once you have a current OpenGL context, you can use OpenGL normally. In this tutorial, a multi-colored rotating triangle will be rendered. The framebuffer size needs to be retrieved for <code>glViewport</code>.</p>
<divclass="fragment"><divclass="line"><spanclass="keywordtype">int</span> width, height;</div><divclass="line"><aclass="code"href="group__window.html#ga0e2637a4161afb283f5300c7f94785c9">glfwGetFramebufferSize</a>(window, &width, &height);</div><divclass="line">glViewport(0, 0, width, height);</div></div><!-- fragment --><p>You can also set a framebuffer size callback using <aclass="el"href="group__window.html#ga3203461a5303bf289f2e05f854b2f7cf">glfwSetFramebufferSizeCallback</a> and be notified when the size changes.</p>
<p>Actual rendering with OpenGL is outside the scope of this tutorial, but there are <ahref="https://open.gl/">many</a><ahref="http://learnopengl.com/">excellent</a><ahref="http://openglbook.com/">tutorial</a><ahref="http://ogldev.atspace.co.uk/">sites</a> that teach modern OpenGL. Some of them use GLFW to create the context and window while others use GLUT or SDL, but remember that OpenGL itself always works the same.</p>
<h2><aclass="anchor"id="quick_timer"></a>
Reading the timer</h2>
<p>To create smooth animation, a time source is needed. GLFW provides a timer that returns the number of seconds since initialization. The time source used is the most accurate on each platform and generally has micro- or nanosecond resolution.</p>
<divclass="fragment"><divclass="line"><spanclass="keywordtype">double</span> time = <aclass="code"href="group__input.html#gaa6cf4e7a77158a3b8fd00328b1720a4a">glfwGetTime</a>();</div></div><!-- fragment --><h2><aclass="anchor"id="quick_swap_buffers"></a>
Swapping buffers</h2>
<p>GLFW windows by default use double buffering. That means that each window has two rendering buffers; a front buffer and a back buffer. The front buffer is the one being displayed and the back buffer the one you render to.</p>
<p>When the entire frame has been rendered, the buffers need to be swapped with one another, so the back buffer becomes the front buffer and vice versa.</p>
<divclass="fragment"><divclass="line"><aclass="code"href="group__window.html#ga15a5a1ee5b3c2ca6b15ca209a12efd14">glfwSwapBuffers</a>(window);</div></div><!-- fragment --><p>The swap interval indicates how many frames to wait until swapping the buffers, commonly known as <em>vsync</em>. By default, the swap interval is zero, meaning buffer swapping will occur immediately. On fast machines, many of those frames will never be seen, as the screen is still only updated typically 60-75 times per second, so this wastes a lot of CPU and GPU cycles.</p>
<p>Also, because the buffers will be swapped in the middle the screen update, leading to <ahref="https://en.wikipedia.org/wiki/Screen_tearing">screen tearing</a>.</p>
<p>For these reasons, applications will typically want to set the swap interval to one. It can be set to higher values, but this is usually not recommended, because of the input latency it leads to.</p>
<divclass="fragment"><divclass="line"><aclass="code"href="group__context.html#ga6d4e0cdf151b5e579bd67f13202994ed">glfwSwapInterval</a>(1);</div></div><!-- fragment --><p>This function acts on the current context and will fail unless a context is current.</p>
<p>GLFW needs to communicate regularly with the window system both in order to receive events and to show that the application hasn't locked up. Event processing must be done regularly while you have visible windows and is normally done each frame after buffer swapping.</p>
<p>There are two methods for processing pending events; polling and waiting. This example will use event polling, which processes only those events that have already been received and then returns immediately.</p>
<divclass="fragment"><divclass="line"><aclass="code"href="group__window.html#ga37bd57223967b4211d60ca1a0bf3c832">glfwPollEvents</a>();</div></div><!-- fragment --><p>This is the best choice when rendering continually, like most games do. If instead you only need to update your rendering once you have received new input, <aclass="el"href="group__window.html#ga554e37d781f0a997656c26b2c56c835e">glfwWaitEvents</a> is a better choice. It waits until at least one event has been received, putting the thread to sleep in the meantime, and then processes all received events. This saves a great deal of CPU cycles and is useful for, for example, many kinds of editing tools.</p>
<h1><aclass="anchor"id="quick_example"></a>
Putting it together</h1>
<p>Now that you know how to initialize GLFW, create a window and poll for keyboard input, it's possible to create a simple program.</p>
<p>This program creates a 640 by 480 windowed mode window and starts a loop that clears the screen, renders a triangle and processes events until the user either presses <em>Escape</em> or closes the window.</p>
<divclass="fragment"><divclass="line"></div><divclass="line"><spanclass="preprocessor">#include <glad/glad.h></span></div><divclass="line"><spanclass="preprocessor">#include <<aclass="code"href="glfw3_8h.html">GLFW/glfw3.h</a>></span></div><divclass="line"></div><divclass="line"><spanclass="preprocessor">#include "linmath.h"</span></div><divclass="line"></div><divclass="line"><spanclass="preprocessor">#include <stdlib.h></span></div><divclass="line"><spanclass="preprocessor">#include <stdio.h></span></div><divclass="line"></div><divclass="line"><spanclass="keyword">static</span><spanclass="keyword">const</span><spanclass="keyword">struct</span></div><divclass="line">{</div><divclass="line"><spanclass="keywordtype">float</span> x, y;</div><divclass="line"><spanclass="keywordtype">float</span> r, g, b;</div><divclass="line">} vertices[3] =</div><divclass="line">{</div><divclass="line"> { -0.6f, -0.4f, 1.f, 0.f, 0.f },</div><divclass="line"> { 0.6f, -0.4f, 0.f, 1.f, 0.f },</div><divclass="line"> { 0.f, 0.6f, 0.f, 0.f, 1.f }</div><divclass="line">};</div><divclass="line"></div><divclass="line"><spanclass="keyword">static</span><spanclass="keyword">const</span><spanclass="keywordtype">char</span>* vertex_shader_text =</div><divclass="line"><spanclass="stringliteral">"uniform mat4 MVP;\n"</span></div><divclass="line"><spanclass="stringliteral">"attribute vec3 vCol;\n"</span></div><divclass="line"><spanclass="stringliteral">"attribute vec2 vPos;\n"</span></div><divclass="line"><spanclass="stringliteral">"varying vec3 color;\n"</span></div><divclass="line"><spanclass="stringliteral">"void main()\n"</span></div><divclass="line"><spanclass="stringliteral">"{\n"</span></div><divclass="line"><spanclass="stringliteral">" gl_Position = MVP * vec4(vPos, 0.0, 1.0);\n"</span></div><divclass="line"><spanclass="stringliteral">" color = vCol;\n"</span></div><divclass="line"><spanclass="stringliteral">"}\n"</span>;</div><divclass="line"></div><divclass="line"><spanclass="keyword">static</span><spanclass="keyword">const</span><spanclass="keywordtype">char</span>* fragment_shader_text =</div><divclass="line"><spanclass="stringliteral">"varying vec3 color;\n"</span></div><divclass="line"><spanclass="stringliteral">"void main()\n"</span></div><divclass="line"><spanclass="stringliteral">"{\n"</span></div><divclass="line"><spanclass="stringliteral">" gl_FragColor = vec4(color, 1.0);\n"</span></div><divclass="line"><spanclass="stringliteral">"}\n"</span>;</div><divclass="line"></div><divclass="line"><spanclass="keyword">static</span><spanclass="keywordtype">void</span> error_callback(<spanclass="keywordtype">int</span> error, <spanclass="keyword">const</span><spanclass="keywordtype">char</span>* description)</div><divclass="line">{</div><divclass="line"> fprintf(stderr, <spanclass="stringliteral">"Error: %s\n"</span>, description);</div><divclass="line">}</div><divclass="line"></div><divclass="line"><spanclass="keyword">static</span><spanclass="keywordtype">void</span> key_callback(<aclass="code"href="group__window.html#ga3c96d80d363e67d13a41b5d1821f3242">GLFWwindow</a>* window, <spanclass="keywordtype">int</span> key, <spanclass="keywordtype">int</span> scancode, <spanclass="keywordtype">int</span> action, <spanclass="keywordtype">int</span> mods)</div><divclass="line">{</div><divclass="line"><spanclass="keywordflow">if</span> (key == <aclass="code"href="group__keys.html#gaac6596c350b635c245113b81c2123b93">GLFW_KEY_ESCAPE</a>&& action == <aclass="code"href="group__input.html#ga2485743d0b59df3791c45951c4195265">GLFW_PRESS</a>)</div><divclass="line"><aclass="code"href="group__window.html#ga49c449dde2a6f87d996f4daaa09d6708">glfwSetWindowShouldClose</a>(window, <aclass="code"href="glfw3_8h.html#a2744fbb29b5631bb28802dbe0cf36eba">GLFW_TRUE</a>);</div><divclass="line">}</div><divclass="line"></div><divclass="line"><spanclass="keywordtype">int</span> main(<spanclass="keywordtype">void</span>)</div><divclass="line">{</div><divclass="line"><aclass="code"href="group__window.html#ga3c96d80d363e67d13a41b5d1821f3242">GLFWwindow</a>* window;</div><divclass="line"> GLuint vertex_buffer, vertex_shader, fragment_shader, program;</div><divclass="line"> GLint mvp_location, vpos_location, vcol_location;</div><divclass="line"></div><divclass="line"><aclass="code"href="group__init.html#gaa5d796c3cf7c1a7f02f845486333fb5f">glfwSetErrorCallback</a>(error_callback);</div><divclass="line"></div><divclass="line"><spanclass="keywordflow">if</span> (!<aclass="code"href="group__init.html#ga317aac130a235ab08c6db0834907d85e">glfwInit</a>())</div><divclass="line"> exit(EXIT_FAILURE);</div><divclass="line"></div><divclass="line"><aclass="code"href="group__window.html#ga7d9c8c62384b1e2821c4dc48952d2033">glfwWindowHint</a>(<aclass="code"href="glfw3_8h.html#afe5e4922de1f9932d7e9849bb053b0c0">GLFW_CONTEXT_VERSION_MAJOR</a>, 2);</div><divclass="line"><aclass="code"href="group__window.html#ga7d9c8c62384b1e2821c4dc48952d2033">glfwWindowHint</a>(<aclass="code"href="glfw3_8h.html#a31aca791e4b538c4e4a771eb95cc2d07">GLFW_CONTEXT_VERSION_MINOR</a>, 0);</div><divclass="line"></div><divclass="line"> window = <aclass="code"href="group__window.html#ga5c336fddf2cbb5b92f65f10fb6043344">glfwCreateWindow</a>(640, 480, <spanclass="stringliteral">"Simple example"</span>, NULL, NULL);</div><divclass="line"><spanclass="keywordflow">if</span> (!window)</div><divclass="line"> {</div><divclass="line"><aclass="code"href="group__init.html#gaaae48c0a18607ea4a4ba951d939f0901">glfwTerminate</a>();</div><divclass="line"> exit(EXIT_FAILURE);</div><divclass="line"> }</div><divclass="line"></div><divclass="line"><aclass="code"href="group__input.html#ga7e496507126f35ea72f01b2e6ef6d155">glfwSetKeyCallback</a>(window, key_callback);</div><divclass="line"></div><divclass="line"><aclass="code"href="group__context.html#ga1c04dc242268f827290fe40aa1c91157">glfwMakeContextCurrent</a>(window);</div><divclass="line"> gladLoadGLLoader((GLADloadproc) <aclass="code"href="group__context.html#ga35f1837e6f666781842483937612f163">glfwGetProcAddress</a>);</div><divclass="line"><aclass="code"href="group__context.html#ga6d4e0cdf151b5e579bd67f13202994ed">glfwSwapInterval</a>(1);</div><divclass="line"></div><divclass="line"><spanclass="comment">// NOTE: OpenGL error checks have been omitted for brevity</span></div><divclass="line"></div><divclass="line"> glGenBuffers(1, &vertex_buffer);</div><divclass="line"> glBindBuffer(GL_ARRAY_BUFFER, vertex_buffer);</div><divclass="line"> glBufferData(GL_ARRAY_BUFFER, <spanclass="keyword">sizeof</span>(vertices), vertices, GL_STATIC_DRAW);</div><divclass="line"></div><divclass="line"> vertex_shader = glCreateShader(GL_VERTEX_SHADER);</div><divclass="line"> glShaderSource(vertex_shader, 1, &vertex_shader_text, NULL);</div><divclass="line"> glCompileShader(vertex_shader);</div><divclass="line"></div><divclass="line"> fragment_shader = glCreateShader(GL_FRAGMENT_SHADER);</div><divclass="line"> glShaderSource(fragment_shader, 1, &fragment_shader_text, NULL);</div><divclass="line"> glCompileShader(fragment_shader);</div><divclass="line"></div><divclass="line"> program = glCreateProgram();</div><divclass="line"> glAttachShader(program, vertex_shader);</div><divclass="line"> glAttachShader(program, fragment_shader);</div><divclass="line"> glLinkProgram(program);</div><divclass="line"></div><divclass="line"> mvp_location = glGetUniformLocation(program, <spanclass="stringliteral">"MVP"</span>);</div><divclass="line"> vpos_location = glGetAttribLocation(program, <spanclass="stringliteral">"vPos"</span>);</div><divclass="line"> vcol_location = glGetAttribLocation(program, <spanclass="stringliteral">"vCol"</span>);</div><divclass="line"></div><divclass="line"> glEnableVertexAttribArray(vpos_location);</div><divclass="line"> glVertexAttribPointer(vpos_location, 2, GL_FLOAT, GL_FALSE,</div><divclass="line"><spanclass="keyword">sizeof</span>(<spanclass="keywordtype">float</span>) * 5, (<spanclass="keywordtype">void</span>*) 0);</div><divclass="line"> glEnableVertexAttribArray(vcol_location);</div><divclass="line"> glVertexAttribPointer(vcol_location, 3, GL_FLOAT, GL_FALSE,</div><divclass="line"><spanclass="keyword">sizeof</span>(<spanclass="keywordtype">float</span>) * 5, (<spanclass="keywordtype">void</span>*) (<spanclass="keyword">sizeof</span>(<spanclass="keywordtype">float</span>) * 2));</div><divclass="line"></div><divclass="line"><spanclass="keywordflow">while</span> (!<aclass="code"href="group__window.html#ga24e02fbfefbb81fc45320989f8140ab5">glfwWindowShouldClose</a>(window))</div><divclass="line"> {</div><divclass="line"><spanclass="keywordtype">float</span> ratio;</div><divclass="line"><spanclass="keywordtype">int</span> width, height;</div><divclass="line"> mat4x4 m, p, mvp;</div><divclass="line"></div><divclass="line"><aclass="code"href="group__window.html#ga0e2637a4161afb283f5300c7f94785c9">glfwGetFramebufferSize</a>(window, &width, &height);</div><divclass="line"> ratio = width / (float) height;</div><divclass="line"></div><divclass="line"> glViewport(0, 0, width, height);</div><divclass="line"> glClear(GL_COLOR_BUFFER_BIT);</div><divclass="line"></div><divclass="line"> mat4x4_identity(m);</div><divclass="line"> mat4x4_rotate_Z(m, m, (<spanclass="keywordtype">float</span>) <aclass="code"href="group__input.html#gaa6cf4e7a77158a3b8fd00328b1720a4a">glfwGetTime</a>());</div><divclass="line"> mat4x4_ortho(p, -ratio, ratio, -1.f, 1.f, 1.f, -1.f);</div><divclass="line"> mat4x4_mul(mvp, p, m);</div><divclass="line"></div><divclass="line"> glUseProgram(program);</div><divclass="line"> glUniformMatrix4fv(mvp_location, 1, GL_FALSE, (<spanclass="keyword">const</span> GLfloat*) mvp);</div><divclass="line"> glDrawArrays(GL_TRIANGLES, 0, 3);</div><divclass="line"></div><divclass="line"><aclass="code"href="group__window.html#ga15a5a1ee5b3c2ca6b15ca209a12efd14">glfwSwapBuffers</a>(window);</div><divclass="line"><aclass="code"href="group__window.html#ga37bd57223967b4211d60ca1a0bf3c832">glfwPollEvents</a>();</div><divclass="line"> }</div><divclass="line"></div><divclass="line"><aclass="code"href="group__window.html#gacdf43e51376051d2c091662e9fe3d7b2">glfwDestroyWindow</a>(window);</div><divclass="line"></div><divclass="line"><aclass="code"href="group__init.html#gaaae48c0a18607ea4a4ba951d939f0901">glfwTerminate</a>();</div><divclass="line"> exit(EXIT_SUCCESS);</div><divclass="line">}</div><divclass="line"></div></div><!-- fragment --><p> The program above can be found in the <ahref="http://www.glfw.org/download.html">source package</a> as <code>examples/simple.c</code> and is compiled along with all other examples when you build GLFW. If you built GLFW from the source package then already have this as <code>simple.exe</code> on Windows, <code>simple</code> on Linux or <code>simple.app</code> on OS X.</p>
<p>This tutorial used only a few of the many functions GLFW provides. There are guides for each of the areas covered by GLFW. Each guide will introduce all the functions for that category.</p>
<ul>
<li><aclass="el"href="intro_guide.html">Introduction to the API</a></li>
<p>You can access reference documentation for any GLFW function by clicking it and the reference for each function links to related functions and guide sections.</p>
<p>The tutorial ends here. Once you have written a program that uses GLFW, you will need to compile and link it. How to do that depends on the development environment you are using and is best explained by the documentation for that environment. To learn about the details that are specific to GLFW, see <aclass="el"href="build_guide.html">Building applications</a>. </p>