OpenGL FreeGlut in Visual Studio 2015


Starting an OpenGL project in VS 2015 is really easy, thanks to the NupenGL.Core nuget package.

Here are really quick steps to start your first OpenGL project using the FreeGlut library to provide a Window toolkit.

Open Visual Studio 2015 Community Edition and create a new Visual Studio C++ Windows Console Application.

OpenGLConsole

Keep it simple. Choose an empty project template.

EmptyProject

Install the NupenGL.Core nuget package. From Visual Studio, Go to Tools \ NuGet Package Manager \ Manage Nuget Packages for this solution. Search for “NupenGL” and install the package.

NupenGL-0.1.0.1

Add a new C or C++ file. Here is some sample code


#include <gl/freeglut.h>

void init();
void display(void);
void centerOnScreen();
void drawObject();

//  define the window position on screen
int window_x;
int window_y;

//  variables representing the window size
int window_width = 480;
int window_height = 480;

//  variable representing the window title
char *window_title = "Sample OpenGL FreeGlut App";

//-------------------------------------------------------------------------
//  Program Main method.
//-------------------------------------------------------------------------
void main(int argc, char **argv)
{
	//  Connect to the windowing system + create a window
	//  with the specified dimensions and position
	//  + set the display mode + specify the window title.
	glutInit(&amp;argc, argv);
	centerOnScreen();
	glutInitWindowSize(window_width, window_height);
	glutInitWindowPosition(window_x, window_y);
	glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE);
	glutCreateWindow(window_title);

	//  Set OpenGL program initial state.
	init();

	// Set the callback functions
	glutDisplayFunc(display);

	//  Start GLUT event processing loop
	glutMainLoop();
}

//-------------------------------------------------------------------------
//  Set OpenGL program initial state.
//-------------------------------------------------------------------------
void init()
{
	//  Set the frame buffer clear color to black. 
	glClearColor(0.0, 0.0, 0.0, 0.0);
}

//-------------------------------------------------------------------------
//  This function is passed to glutDisplayFunc in order to display 
//	OpenGL contents on the window.
//-------------------------------------------------------------------------
void display(void)
{
	//  Clear the window or more specifically the frame buffer...
	//  This happens by replacing all the contents of the frame
	//  buffer by the clear color (black in our case)
	glClear(GL_COLOR_BUFFER_BIT);

	//  Draw object
	drawObject();

	//  Swap contents of backward and forward frame buffers
	glutSwapBuffers();
}

//-------------------------------------------------------------------------
//  Draws our object.
//-------------------------------------------------------------------------
void drawObject()
{
	//  Draw Icosahedron
	glutWireIcosahedron();
}

//-------------------------------------------------------------------------
//  This function sets the window x and y coordinates
//  such that the window becomes centered
//-------------------------------------------------------------------------
void centerOnScreen()
{
	window_x = (glutGet(GLUT_SCREEN_WIDTH) - window_width) / 2;
	window_y = (glutGet(GLUT_SCREEN_HEIGHT) - window_height) / 2;
}

After this, you should be able to build and run without any issues. Your application should look something like this:

SampleOpenGLFreeGLUT

You can find a Visual Studio 2015 project that uses many of the FreeGlut features on this Github repository.

OpenGL Geometric Primitives


The purpose of this interactive program is to make it easier to learn OpenGL Geometric Primitives. There are ten geometric primitives in OpenGL, from a point to a polygon, and all are represented by vertices. This program will give you the flexibility to add and remove vertices in a certain order, and shows you how the choice of the primitive determines how the vertices are to be combined. It can be used in the following ways:

  • Learn OpenGL Geometric Primitives through
    • Interactive Program
    • Source Code
    • Documentation
  • Learn some OpenGL functions designed to work with geometric primitives
  • Draw primitives manually and generate their corresponding OpenGL C code

Here is a view showing Monalisa drawn with lines only… Click on the image below to see a video demo.

Learn OpenGL Geometric Primitives

The source code can be downloaded from my GitHub page.

Enjoy!
Ali B

GLUI Subwindow Template


I have written an article on CodeProject that shows you how to create GUI controls for your OpenGL application and organize them into GLUI subwindows. In this article, we will take the source code from the previous article GLUI Window Template, and modify it so that our GLUI controls will be laid out inside two subwindows rather than in a single window. The reason is that having a separate window to contain our GLUI controls could be sometimes annoying for the user, as she or he will have to lose the focus on the OpenGL context window every time she or he wants to do an action. A nice way to avoid this is to place the GLUI controls directly into the GLUT OpenGL window by embedding them into a GLUI subwindow.

This article can be used in the following ways:

  • Learn how to use GLUI subwindows
  • Understand the Viewport concept in OpenGl
  • Use the program as a template for your OpenGL applications that require GUI controls

The image below shows how our controls were laid out into a single GLUI window in the previous article:

GLUI Window

The image below shows how our controls will be laid out into two separate subwindows placed on the left and the bottom of our main GLUT window:

GLUI Subwindow

You can download the code from my GitHub page. Check it out, and let me know what you think.

Enjoy!
Ali B

GLUI Window Template


I have written an article on CodeProject that describes in detail how to create your first GLUI window with some basic controls inside it, and provides you with a template for your OpenGL applications.

When OpenGL applications get more complex, we need something more than a GLUT mouse, keyboard, and\or popup menus to interact with our OpenGL objects drawn on the window. GLUI gives us more flexibility by allowing us to add GUI components to interact with our OpenGL objects, such as buttons, check boxes, radio buttons, spinners, list boxes, lists, trees, file browsers, text fields, text areas, and the special controls: rotation and translation.

This article can be used in the following ways:

  • Learn how to add GUI components to your OpenGL application in a very straight-forward and simple manner, through
    • Documentation
    • Interactive Program that displays to the user how every event is handled and classifies these events into GLUT and GLUI events.
    • Neat and commented Code that reflects the simplicity of the GLUI library
  • Learn some totally new controls in the GLUI library created specifically for graphical manipulation, such as the rotation and translation controls.
  • Use the code as a template for your OpenGL applications.

Click below for a screen shot of what the program would look like when its run.

GLUI Window Template

Make sure you read the GLUT Window Template article as a prerequisite to this article. One important thing to note is that GLUI is a C++ library, which means that your code must be written in files with .cpp extension rather than .C, or otherwise the linker will complain.

You can download the code from my GitHub page. Check it out, and let me know what you think.

Enjoy!
Ali B

GLUT Subwindow Template


I have written an article on CodeProject that describes in detail how to set up your first OpenGL GLUT window with multiple sub-windows, and provides you with a template for your OpenGL applications. It can be used in the following ways:

  • Understand GLUT sub-windows
  • Learn new GLUT event handlers
  • Use the code as template if you ever needed a Graphics program with multiple sub-windows

glut_subwindow_template

Make sure you read the Visual C++ 2015 section in the article to avoid any issues in running the program.

Check it out, and let me know if you find it useful.

Enjoy!
Ali B

GLUT Window Template


I have written an article on CodeProject that describes in detail how to set up your first OpenGL GLUT window and provides you with a template for your OpenGL applications. It can be used in the following ways:

  • Learn GLUT and OpenGL through
    • Interactive Program
    • Documentation
    • Source Code
  • Start your first OpenGL program
  • Use the code as a template for your OpenGL applications

Click below for a screen shot of what the program would look like when its run.

GLUT Window Template

Make sure you read the Visual C++ 2015 section in the article to avoid any issues in running the program.

Check it out, and let me know if you find it useful.

Enjoy!
Ali B