Click here to Skip to main content
Licence CPOL
First Posted 1 Feb 2008
Views 23,562
Downloads 433
Bookmarked 14 times

A Simple OpenGL Window with GLUT Library

By erjan123 | 1 Feb 2008
A simple C++ console application to create an OpenGL window using the GLUT library
2 votes, 28.6%
1

2
1 vote, 14.3%
3
3 votes, 42.9%
4
1 vote, 14.3%
5
2.91/5 - 7 votes
μ 2.91, σa 2.75 [?]
OpenGLBasicWindowImage.jpg

Introduction

This is a very simple C++ console application that creates a simple OpenGL window. The source code is from the book "OpenGL Programming Guide (Third Edition)." This article will explain how to download and include the GLUT library. Once you include the GLUT library, it's very simple to build and execute the code. It explains also the OpenGL code that is used in this program.

Background

You should be able to use Visual Studio 2005 and C++. You should also have some idea about OpenGL.

Using the Code

Steps:

  1. GLUT installation (download the GLUT library)
  2. Copy and paste the GLUT library files to the specific locations
  3. Create a C++ console application
  4. Type the code, build and execute

Step 1

Before you even start a new project, you have to download the GLUT library. You can get the latest version from this link: GLUT 3.7.6 for Windows. Click this (glut-3.7.6-bin.zip (117 KB) ) link and download the ZIP file into your computer.

Step 2

Unzip the folder, copy the glut32.dll file and paste it to C:\WINDOWS\system. Copy the glut32.lib file and paste it to this directory: C:\Program Files\Microsoft Visual Studio 8\VC\PlatformSDK\Lib. The last step is to copy the glut.h file and paste it to this directory: C:\Program Files\Microsoft Visual Studio 8\VC\PlatformSDK\Include\gl. The installation of the GLUT library files is done. However, if you using a different Windows or different Visual Studio version, the directories might be different. I'm assuming that you are using Windows Vista and Visual Studio 2005.

Step 3

Create a C++ console application. You don't need a Windows application. From Visual Studio 2005, select File -> New -> Projects. Select project types Visual C++ -> Win32 and create a Win32 console application.

Step 4

Type the code, build and execute. Make sure to include the <GL/glut.h >.

//Include files
#include "stdafx.h"
#include <GL/glut.h >

The Functions

Function display(void) displays a polygon:

void display(void)
{
    //Clear all pixels
    glClear(GL_COLOR_BUFFER_BIT);

    //draw white polygon (rectangle) with corners at
    // (0.25, 0.25, 0.0) and (0.75, 0.75, 0.0)
    glColor3f(1.0,1.0,1.0);
    glBegin(GL_POLYGON);
        glVertex3f(0.25, 0.25, 0.0);
        glVertex3f(0.75, 0.25, 0.0);
        glVertex3f(0.75, 0.75, 0.0);
        glVertex3f(0.25, 0.75, 0.0);
    glEnd();
    
    // Don't wait start processing buffered OpenGL routines
    glFlush();
}

Function init() initialises GLUT (sets the state):

void init(void)
{
    //select clearing (background) color
    glClearColor(0.0, 0.0, 0.0, 0.0);

    //initialize viewing values 
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    glOrtho(0.0, 1.0, 0.0, 1.0, -1.0, 1.0);

}

Main function: make sure to change the command-line parameter _TCHAR* to char**.

int _tmain(int argc, char** argv)
{
    //Initialise GLUT with command-line parameters. 
    glutInit(&argc, argv);
    
    //Set Display Mode
    glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
    
    //Set the window size
    glutInitWindowSize(250,250);
    
    //Set the window position
    glutInitWindowPosition(100,100);
    
    //Create the window
    glutCreateWindow("A Simple OpenGL Windows Application with GLUT");
    
    //Call init (initialise GLUT
    init();
    
    //Call "display" function
    glutDisplayFunc(display);
    
    //Enter the GLUT event loop
    glutMainLoop();

    return 0;
}

Variable or class names should be wrapped in <code> tags like this.

Points of Interest

Actually, this program is a very simple OpenGL program. However, if you are new and if you don't know how to initialise the GLUT library, it might take little bit more time to compile the code.

History

Uploaded on 01/29/2008

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

erjan123

Software Developer

United States United States

Member
None

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
Generalneed help Pinmembernarmeen khan4:56 25 Oct '09  
i need source code of 3D playmobil police station in visual c++ using console application.

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Mobile
Web03 | 2.5.120210.1 | Last Updated 1 Feb 2008
Article Copyright 2008 by erjan123
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid