Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Save more on your purchases now! discount-offer-chevron-icon
Savings automatically calculated. No voucher code required.
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
OpenGL Data Visualization Cookbook

You're reading from   OpenGL Data Visualization Cookbook Over 35 hands-on recipes to create impressive, stunning visuals for a wide range of real-time, interactive applications using OpenGL

Arrow left icon
Product type Paperback
Published in Aug 2015
Publisher Packt
ISBN-13 9781782169727
Length 298 pages
Edition 1st Edition
Languages
Tools
Arrow right icon
Toc

Table of Contents (11) Chapters Close

Preface 1. Getting Started with OpenGL FREE CHAPTER 2. OpenGL Primitives and 2D Data Visualization 3. Interactive 3D Data Visualization 4. Rendering 2D Images and Videos with Texture Mapping 5. Rendering of Point Cloud Data for 3D Range-sensing Cameras 6. Rendering Stereoscopic 3D Models using OpenGL 7. An Introduction to Real-time Graphics Rendering on a Mobile Platform using OpenGL ES 3.0 8. Interactive Real-time Data Visualization on Mobile Devices 9. Augmented Reality-based Visualization on Mobile or Wearable Platforms Index

Creating your first OpenGL application with GLFW

Now that you have successfully configured your development platform and installed the GLFW library, we will provide a tutorial on how to create your first OpenGL-based application.

Getting ready

At this point, you should already have all the pre requisite tools ready regardless of which operating system you may have, so we will immediately jump into building your first OpenGL application using these tools.

How to do it...

The following code outlines the basic steps to create a simple OpenGL program that utilizes the GLFW library and draws a rotating triangle:

  1. Create an empty file, and then include the header file for the GLFW library and standard C++ libraries:
    #include <GLFW/glfw3.h>
    #include <stdlib.h>
    #include <stdio.h>
  2. Initialize GLFW and create a GLFW window object (640 x 480):
    int main(void)
    {
      GLFWwindow* window;
      if (!glfwInit())
        exit(EXIT_FAILURE);
      window = glfwCreateWindow(640, 480, "Chapter 1: Simple GLFW Example", NULL, NULL);
      if (!window)
      {
        glfwTerminate();
        exit(EXIT_FAILURE);
      }
      glfwMakeContextCurrent(window);
  3. Define a loop that terminates when the window is closed:
      while (!glfwWindowShouldClose(window))
      {
  4. Set up the viewport (using the width and height of the window) and clear the screen color buffer:
        float ratio;
        int width, height;
    
        glfwGetFramebufferSize(window, &width, &height);
        ratio = (float) width / (float) height;
    
        glViewport(0, 0, width, height);
        glClear(GL_COLOR_BUFFER_BIT);
  5. Set up the camera matrix. Note that further details on the camera model will be discussed in Chapter 3, Interactive 3D Data Visualization:
        glMatrixMode(GL_PROJECTION);
        glLoadIdentity();
        glOrtho(-ratio, ratio, -1.f, 1.f, 1.f, -1.f);
        glMatrixMode(GL_MODELVIEW);
        glLoadIdentity();
  6. Draw a rotating triangle and set a different color (red, green, and blue channels) for each vertex (x, y, and z) of the triangle. The first line rotates the triangle over time:
        glRotatef((float)glfwGetTime() * 50.f, 0.f, 0.f, 1.f);
        glBegin(GL_TRIANGLES);
        glColor3f(1.f, 0.f, 0.f);
        glVertex3f(-0.6f, -0.4f, 0.f);
        glColor3f(0.f, 1.f, 0.f);
        glVertex3f(0.6f, -0.4f, 0.f);
        glColor3f(0.f, 0.f, 1.f);
        glVertex3f(0.f, 0.6f, 0.f);
        glEnd();
  7. Swap the front and back buffers (GLFW uses double buffering) to update the screen and process all pending events:
        glfwSwapBuffers(window);
        glfwPollEvents();
      }
  8. Release the memory and terminate the GLFW library. Then, exit the application:
      glfwDestroyWindow(window);
      glfwTerminate();
      exit(EXIT_SUCCESS);
    }
  9. Save the file as main.cpp using the text editor of your choice.

How it works...

By including the GLFW library header, glfw3.h, we automatically import all necessary files from the OpenGL library. Most importantly, GLFW automatically determines the platform and thus allows you to write portable source code seamlessly.

In the main function, we must first initialize the GLFW library with the glfwInit function in the main thread. This is required before any GLFW functions can be used. Before a program exits, GLFW should be terminated to release any allocated resources.

Then, the glfwCreateWindow function creates a window and its associated context, and it also returns a pointer to the GLFWwindow object. Here, we can define the width, height, title, and other properties for the window. After the window is created, we then call the glfwMakeContextCurrent function to switch the context and make sure that the context of the specified window is current on the calling thread.

At this point, we are ready to render our graphics element on the window. The while loop provides a mechanism to redraw our graphics as long as the window remains open. OpenGL requires an explicit setup on the camera parameters; further details will be discussed in the upcoming chapters. In the future, we can provide different parameters to simulate perspective and also handle more complicated issues (such as anti-aliasing). For now, we have set up a simple scene to render a basic primitive shape (namely a triangle) and fixed the color for the vertices. Users can modify the parameters in the glColor3f and glVertex3f functions to change the color as well as the position of the vertices.

This example demonstrates the basics required to create graphics using OpenGL. Despite the simplicity of the sample code, it provides a nice introductory framework on how you can create high-performance graphics rendering applications with graphics hardware using OpenGL and GLFW.

You have been reading a chapter from
OpenGL Data Visualization Cookbook
Published in: Aug 2015
Publisher: Packt
ISBN-13: 9781782169727
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime