Featured Posts

8. Bump Mapping in GLSL8. Bump Mapping in GLSL Introduction Bump mapping is essential in todays computer games, and computer graphics in general. Would you like to know the best thing about it? It is extremely simple to implement. Bump mapping works...

Read more

Swiftless GLSL Shader DeveloperSwiftless GLSL Shader Developer Swiftless GLSL Shader Developer   Version 0.1a Currently Swiftless GLSL Shader Developer is in it's first public release, and is currently in alpha status, meaning it is not complete and may contain...

Read more

Wordpress Optimization Wordpress Website Optimizations Introduction Wordpress itself is a fairly wonderful tool. Since switching to it, I find it is a lot quicker to make changes to my website and it is also quicker to get...

Read more

36. OpenGL Framebuffers36. OpenGL Framebuffers Introduction Frame buffers are one of those mythical things that we have all heard of, but many beginner OpenGL developers avoid because there is not much information about them, and they can be confusing...

Read more

1. Terrain Class1. Terrain Class Terrain is one of those things that so far, hasn't been perfectly recreated in computer graphics. But it is almost there! Looking over a beautiful landscape can be one of the most amazing feelings in the...

Read more

  • Prev
  • Next

1. OpenGL Window (Version 2.0)

Posted on : 23-03-2010 | By : Swiftless | In : OpenGL

Tags: , , , ,

39

Introduction

Welcome, to what is now your first OpenGL tutorial. Today, we are going to learn how to create a Window using OpenGL, GLUT (The OpenGL Utility) and GLEW.

But first, let me give you a brief rundown of GLUT and OpenGL, this is after all, your first time working with them both, and you must have some idea at least about OpenGL if you want to learn it.

OpenGL

OpenGL, also known as the Open Graphics Library, is a library designed for cross platform 3D graphics. It is directly, the only competitor to Direct3D in the DirectX library. OpenGL is also hardware accelerated, just like Direct3D, and both of these libraries are the main focus for graphics card performance.

GLUT

So what about GLUT? GLUT (The OpenGL Utility), is a tool which allows the creation of windows and handling of input on multiple systems. Making OpenGL cross-platform and extremely simple to set up. Unfortunately, the original GLUT is no longer being developed, but don’t worry, there is a remake of GLUT called FreeGLUT, which even works with the original GLUT dll files and is exactly the same to use.

GLEW

And finally GLEW, GLEW is the OpenGL Extension Wrangler, and gives us easy to use calls to OpenGL extensions, without us having to declare the dll entry points, which while are documented and are on the internet, is still a pain, and a waste of time, when you have a library designed for this.

Installation

Now that you know a little bit about them both, why don’t we actually get to using them? That is after all, why you are here.

Downloads

First off you are going to need to download:

FreeGLUT – http://freeglut.sourceforge.net/
GLEW – http://glew.sourceforge.net/

Once you have them both, follow their installation instructions, which are fairly simple. In short, find the “lib” and “include” folders for your current Visual Studio installation (for VC, not for VC#), and put the required files in each folder. Then find your System32 or SysWOW64 folder, and put the required dll files in them.

Visual Studio Project Configuration

Then you are going to want to open Visual Studio (all these tutorials have been written in the latest Visual Studio 2010, and they will work in everything prior) and start a New Project. From the project selection list, find and click on “Win32 Console Application”. You should then be prompted with a setup wizard, click on “Next” and tick “Empty Project”, and press “Finish”.

You should now have an empty Win32 console application, which we need to link to some library files. Go to your project properties, and under: “Configuration Properties” -> “Linker”, click on “Input” and add two additional dependencies. Add “glew32.lib” and “freeglut.lib” on separate lines.

Visual Studio should now be configured for us to start learning OpenGL.

Coding

And finally, let’s get on to some coding.

First, start a new C++ file in your project, you can call it whatever you like, but I always like to call my first file “main.cpp”, because I’m just so original :)

Inside your cpp file, you will want to first of all, include the header files for GLUT/FreeGLUT and OpenGL. Which after you have entered them, will look something like:

#include  // Include the GLEW header file
#include  // Include the GLUT header file

And then create your standard main method, something like:

int main (void) {
}

If this compiles, then you have your header files setup correctly. Congratulations!

GLUT

Now it’s time to actually start using GLUT and get a window appearing. The first thing we will want to do is redefine our main method, this is because all of our GLUT calls will be done in this method, and these parameters are required by GLUT. Change the declaration of your main method to:

int main (int argc, char **argv) {
}

The parameters argc and argv allow us to add command line arguments, and are required when we initialize GLUT.

So now we initialize GLUT, which is done like so:

glutInit(&argc, argv); // Initialize GLUT

After we have initialized GLUT, we need to tell it how we want to setup our window. Here we tell GLUT if we want one or two buffers, if we want an alpha channel, if we want depth and stencil buffers, etc. Let’s start off simple, and go with the most basic, GLUT_SINGLE which will give us a single buffered window.

glutInitDisplayMode (GLUT_SINGLE); // Set up a basic display buffer (only single buffered for now)

Next, we need to set the size and the position on the screen for our GLUT window:

glutInitWindowSize (500, 500); // Set the width and height of the window
glutInitWindowPosition (100, 100); // Set the position of the window

And finally, we create our window and give it a title/caption:

glutCreateWindow (“You’re first OpenGL Window“); // Set the title for the window

This is going good, we now have a window of the size and position we want, and this should compile and display if you would like to test it (I know I did!). Testing your code often is a great habit to get into. I have known a lot of people to write a lot of code, compile it, and then not know where to start debugging. Even if all you are doing is checking for compilation errors, it’s good to fix these up early.

Once you run this, you will have a window appear with a console window behind it, and it will then disappear as your application would have run out of code and have exited.

So now we have a couple more methods to set up before GLUT opens a window, and then keeps it open.

The first of these things, is we want to tell GLUT what method will store our code for drawing, so that GLUT can then call on it when needed. As per a lot of OpenGL tutorials, I am going to use a method called “display”, as I have picked up the habit. You can call this anything you like, but it has to be declared like such:

void display (void) {
}

Once we have a method we can use to do our drawing, we need to actually tell GLUT to use this method. To do so, we make a call to glutDisplayFunc() and pass in our display method, this is done inside of our main method after we create our window.

glutDisplayFunc(display);

Now that GLUT knows what method to use for drawing, all we have to do is tell GLUT to enter its Main loop. This is just a giant loop that continues forever, whilst calling the display, idle, keyboard, mouse and any other call back methods. So in code, it looks theoretically something like:

while (running) {
display();
}

And while this loop is going, so is your application. So to start it, call:

glutMainLoop();

If you choose to run this, your window will stay open, but your window doesn’t have any OpenGL calls in it yet. So to show that OpenGL is working, lets fill in a basic display method.

To do this, we will first set the colour we want the window background to be. We do this with the call to glClearColor, which takes in 4 float parameters, each one referring to a colour, and in the other (red, green, blue, alpha).

So add this line to your display function, and we should get a nice red background:

glClearColor(1.f, 0.f, 0.f, 1.f); // Clear the background of our window to red

Now that we have the background colour set, let’s get into the habit of some cleaning up in OpenGL. Every time this display method is called in our loop, it adds on to what is currently stored in OpenGL’s giant state machine. So we need to clean this out, and reset it. We will reset the colour buffer, so that we get our red background every time, and we will load the identity matrix, so all our drawing starts at the same location as the previous time this method was called:

glClear (GL_COLOR_BUFFER_BIT); //Clear the colour buffer (more buffers later on)
glLoadIdentity();  // Load the Identity Matrix to reset our drawing locations

And finally to top it all off, after we have drawn everything we want, we need a way to tell OpenGL to draw everything we have currently told it. In a single buffered window, we want to just do a straight flush from OpenGL to the window, and we can do this with:

glFlush(); // Flush the OpenGL buffers to the window

And that is it, if everything has worked along the way, you should now have a window that shows up with an OpenGL context, and displays a red background.

Well done!
If you have any questions, please email me at swiftless@gmail.com

#include <GL/glew.h> // Include the GLEW header file
#include <GL/glut.h> // Include the GLUT header file

void display (void) {
glClearColor(1.0f, 0.0f, 0.0f, 1.0f); // Clear the background of our window to red
glClear(GL_COLOR_BUFFER_BIT); //Clear the colour buffer (more buffers later on)
glLoadIdentity(); // Load the Identity Matrix to reset our drawing locations

glFlush(); // Flush the OpenGL buffers to the window
}

int main (int argc, char **argv) {
glutInit(&argc, argv); // Initialize GLUT
glutInitDisplayMode (GLUT_SINGLE); // Set up a basic display buffer (only single buffered for now)
glutInitWindowSize (500, 500); // Set the width and height of the window
glutInitWindowPosition (100, 100); // Set the position of the window
glutCreateWindow ("You’re first OpenGL Window"); // Set the title for the window

glutDisplayFunc(display); // Tell GLUT to use the method "display" for rendering

glutMainLoop(); // Enter GLUT's main loop
}
VN:F [1.9.3_1094]
Please rate so I know where to improve the site. 1 means needs a lot of improvement, 10 means perfect. If you leave a low rating, please state why. I don't want people just coming to bash the site.
Rating: 8.4/10 (48 votes cast)
VN:F [1.9.3_1094]
Rating: +18 (from 20 votes)
1. OpenGL Window (Version 2.0), 8.4 out of 10 based on 48 ratings

Related posts:

  1. 2. OpenGL Window Reshaping (Version 2.0)
  2. 5. OpenGL Color (Version 2.0)
  3. 6. OpenGL Cube (Version 2.0)
  4. 3. OpenGL Keyboard Interaction (Version 2.0)
  5. 4. OpenGL Primitives – Square (Version 2.0)

Comments (39)

I think you should also include to set subsystem or system of linker from window to console because i got this error : MSVCRTD.lib(crtexew.obj) : error LNK2019: unresolved external symbol _WinMain@16 referenced in function ___tmainCRTStartup

and I followed instructions of this page

http://social.msdn.microsoft.com/Forums/en-US/vclanguage/thread/14e85604-6929-4707-a22e-8cdf596926a6

the error happened at the point where you just put a window and tell to test it

VA:F [1.9.3_1094]
Rating: 0.0/5 (0 votes cast)
VA:F [1.9.3_1094]
Rating: 0 (from 0 votes)

Hi Lmrn,

You will only get WinMain errors if you created the wrong type of project. You need to create a console application, not a windows application.

Cheers,
Swiftless

VN:F [1.9.3_1094]
Rating: 0.0/5 (0 votes cast)
VN:F [1.9.3_1094]
Rating: 0 (from 0 votes)

I`m having trouble finding how to setup freeglut, there are no instructions to set it up on windows

VA:F [1.9.3_1094]
Rating: 0.0/5 (0 votes cast)
VA:F [1.9.3_1094]
Rating: 0 (from 0 votes)

Nice tutorial, but why don’t you indent your code? :s

VA:F [1.9.3_1094]
Rating: 0.0/5 (0 votes cast)
VA:F [1.9.3_1094]
Rating: 0 (from 0 votes)

Hey Alcuadrado,

I do comment my code, but putting it into wordpress removes the indenting, and it takes too long to put it back.

Cheers,
Swiftless

VN:F [1.9.3_1094]
Rating: 0.0/5 (0 votes cast)
VN:F [1.9.3_1094]
Rating: 0 (from 0 votes)

Oh yes, wp sucks in that aspect, what a pitty..

VA:F [1.9.3_1094]
Rating: 0.0/5 (0 votes cast)
VA:F [1.9.3_1094]
Rating: 0 (from 0 votes)

I’ve just finished a year at university studying programming. I did a little openGL so I knew a little of this when I started. I got cofused about exactly where the function calls and code pieces were meant to go, and you seemed to miss the part where you set the glutDisplayFunc(), which threw me. Other than that, this is well set out and goes at a good pace to keep me always interested. Well worth looking at – even if you think you know the stuff.

VA:F [1.9.3_1094]
Rating: 0.0/5 (0 votes cast)
VA:F [1.9.3_1094]
Rating: 0 (from 0 votes)

Hey Chris,

I’m glad to hear you like the tutorial. I have since added the call to glutDisplayFunc into the tutorial for other readers.

Cheers,
Swiftless

VN:F [1.9.3_1094]
Rating: 0.0/5 (0 votes cast)
VN:F [1.9.3_1094]
Rating: 0 (from 0 votes)

Great blog mate, keep it up. I am currently following your tutorials and I got to say that they are way better and shorter than NeHe OpenGl tuts out there. :)

VA:F [1.9.3_1094]
Rating: 0.0/5 (0 votes cast)
VA:F [1.9.3_1094]
Rating: 0 (from 0 votes)

Nice tutorials man. I went lot deeper in to your samples. It was really good. I have one query here.
Can we do OpenGL coding without using glut? ie with out the inbuilt APIs for drawing shapes.

Keep posting tutorials, I would like to know start learning OpenGLES.

-anoop

VA:F [1.9.3_1094]
Rating: 0.0/5 (0 votes cast)
VA:F [1.9.3_1094]
Rating: 0 (from 0 votes)

Hey Anoop,

GLUT is only a cross-platforum windowing toolkit, therefore all of the OpenGL calls we make are independent of GLUT. Those inbuilt APIs for drawing shapes make calls to glEnable and glVertex calls like regular drawing in OpenGL. For creating a cube, you can plot the points out on paper and enter them in, or there are plenty of examples on the web. For the sphere, I have a tutorial on drawing them without GLUT. But for more advanced shapes, you will generally want to use a model loader of some sort.

OpenGL ES is a lot like OpenGL 3.0 and 4.0, going for a shader based approach, and is used in mobile phones and consoles such as the Playstation 3. I won’t be making OpenGL ES tutorials, but I recommend you check out the new ones when I put them up shortly.

Cheers,
Swiftless

VN:F [1.9.3_1094]
Rating: 0.0/5 (0 votes cast)
VN:F [1.9.3_1094]
Rating: 0 (from 0 votes)

Hmm!!
Thanks for the info…..actually I am working with iPhone…so was trying to dwell into OpenGLES….I have bought some books but it is quite different from OpenGL. Let me see how far I can go…….anyways keep up the good work..

-anoop

VA:F [1.9.3_1094]
Rating: 0.0/5 (0 votes cast)
VA:F [1.9.3_1094]
Rating: 0 (from 0 votes)

Absolutely fantastic! After a bit of initial headscratching I got it to work. The problems that arose were just my lack of experience with VB, now I know what the hell is going on it makes a lot of sense.

Cheers for the good work. :)

VA:F [1.9.3_1094]
Rating: 0.0/5 (0 votes cast)
VA:F [1.9.3_1094]
Rating: 0 (from 0 votes)

i read this first lesson on many other sites.seems like this one is always the simplest to learn.

Anyway, i really like this web-site for its easy and hospitable-to-new-ideas design,hopefully,we can learn lots of stuff from it.

Best regards!

VA:F [1.9.3_1094]
Rating: 0.0/5 (0 votes cast)
VA:F [1.9.3_1094]
Rating: 0 (from 0 votes)

“You are first OpenGL Window”
No, no I am not.

Grammar aside, good guide. First out of about 10 tutorials that I got the first lesson to compile and run.

VA:F [1.9.3_1094]
Rating: 0.0/5 (0 votes cast)
VA:F [1.9.3_1094]
Rating: 0 (from 0 votes)

To compile on Visual Studio you need to set the subsystem to windows with:

#pragma comment(linker,”/SUBSYSTEM:WINDOWS”);

and reset the entrypoint to main again with:

#pragma comment(linker,”/ENTRY:mainCRTStartup”);

(cause setting subsystem to windows will cause entrypoint to require WinMain, wich is undesireble).

VA:F [1.9.3_1094]
Rating: 5.0/5 (1 vote cast)
VA:F [1.9.3_1094]
Rating: +1 (from 1 vote)

Hi BrainStorm,

I am not sure of your setup, but with Visual Studio, from version 6.0 to 2010 (including 2005 and 2008), I can guarantee that if your headers and dlls are in the correct folder, and your libraries are linked correctly, and you are using a console application (a WinMain application has problems with GLUT) then the above code will copy and paste to compile.

Cheers,
Swiftless

VN:F [1.9.3_1094]
Rating: 5.0/5 (1 vote cast)
VN:F [1.9.3_1094]
Rating: 0 (from 0 votes)

Hey people having issues compiling color commands:

I had the same issue and also linked libopengl32.a and libglu32.a using MinGW

now everything works (I linked these after glew and freeglut)

VA:F [1.9.3_1094]
Rating: 0.0/5 (0 votes cast)
VA:F [1.9.3_1094]
Rating: 0 (from 0 votes)

Hi Garet,

I am not too familiar with MinGW. Glad you got it to work though.

Cheers,
Swiftless

VN:F [1.9.3_1094]
Rating: 0.0/5 (0 votes cast)
VN:F [1.9.3_1094]
Rating: 0 (from 0 votes)

Ok, it didn’t display the way it was supposed to, but I hope you know what I mean.

VA:F [1.9.3_1094]
Rating: 0.0/5 (0 votes cast)
VA:F [1.9.3_1094]
Rating: 0 (from 0 votes)

Hey Exxon,

Yes, you need to use standard quote characters for strings and character arrays, not those dodgy things HTML use :P

Cheers,
Swiftless

VN:F [1.9.3_1094]
Rating: 0.0/5 (0 votes cast)
VN:F [1.9.3_1094]
Rating: 0 (from 0 votes)

glutCreateWindow (“You’re first OpenGL Window“); // Set the title for the window
won’t compile, because it needs “…” instead of “…“ . It’s fixed in complete code at the end, however:)

VA:F [1.9.3_1094]
Rating: 0.0/5 (0 votes cast)
VA:F [1.9.3_1094]
Rating: 0 (from 0 votes)

lol am yet to check this
I will be back later

VA:F [1.9.3_1094]
Rating: 0.0/5 (0 votes cast)
VA:F [1.9.3_1094]
Rating: 0 (from 0 votes)

Hey,

That sounds weird, try changing your glutInitDisplayMode line to glutInitDisplayMode(GLUT_SINGLE | GLUT_RGBA);

If that doesn’t work, and all your code is identical to the code above, then it sounds like a driver issue.

Cheers,
Swiftless

VN:F [1.9.3_1094]
Rating: 0.0/5 (0 votes cast)
VN:F [1.9.3_1094]
Rating: 0 (from 0 votes)

my code is :
glClearColor(1.0f, 0.0f, 0.0f, 1.0f);
it doesnt mather if i palce a 1 or a 0 it still stays blanck

VA:F [1.9.3_1094]
Rating: 0.0/5 (0 votes cast)
VA:F [1.9.3_1094]
Rating: -1 (from 1 vote)

I have a problem with ure code, i have a white background and not a red one, did everything from ure fantastic tutorial, but only the beackground is not good

VA:F [1.9.3_1094]
Rating: 0.0/5 (0 votes cast)
VA:F [1.9.3_1094]
Rating: 0 (from 0 votes)

Hi Lonelobo,

Does your glClearColor line contain all 1′s? Or is it glClearColor(1.0f, 0.0f, 0.0f, 1.0f).

It should look like the second one to give the background the colour red. All 1′s will make it look white.

Cheers,
Swiftless

VN:F [1.9.3_1094]
Rating: 0.0/5 (0 votes cast)
VN:F [1.9.3_1094]
Rating: 0 (from 0 votes)

Hello there! I figure it out. It was just I was leaving the glutInit part out of the int brackets and the compiler was telling me something was wrong and I thought maybe i had them installed wrong. But now it runs smoothly and moving on to the second tutorial :D

Thanks,
Expora

VA:F [1.9.3_1094]
Rating: 0.0/5 (0 votes cast)
VA:F [1.9.3_1094]
Rating: 0 (from 0 votes)

Hello there!! The past 2 days I’ve been trying to start studying and learning about openGL but i stumble across multiple problems finally I found your tutorials but id like to know how you install glew and freeglut so i dont get into any problems.

Thanks,
Expora

VA:F [1.9.3_1094]
Rating: 0.0/5 (0 votes cast)
VA:F [1.9.3_1094]
Rating: 0 (from 0 votes)

Hi Expora,

I give a quick rundown of how to install both GLEW and FreeGLUT in the tutorial, towards the top.

If you get any specific problems, let me know.

Cheers,
Swiftless

VN:F [1.9.3_1094]
Rating: 0.0/5 (0 votes cast)
VN:F [1.9.3_1094]
Rating: 0 (from 0 votes)

Thanks for the well explained tut.
I found a little thingy. In Line 19 of the full code you use glutDisplayFunc(display); , but you never said anything about it beforehand.

Nice tut, will continue reading!

VA:F [1.9.3_1094]
Rating: 0.0/5 (0 votes cast)
VA:F [1.9.3_1094]
Rating: 0 (from 0 votes)

Very good tutorial for opengl.
I am working on GPU-accelerated 3D ultrasound imaging and i have to start with opengl and glsl.
Your tutorial really saves me a lot of trouble digging in textbooks.
Really appreciate your effort!

Good luck!

Prince Ma from China

VA:F [1.9.3_1094]
Rating: 0.0/5 (0 votes cast)
VA:F [1.9.3_1094]
Rating: +1 (from 1 vote)

Great tutorials! I’ve been looking all over for opengl tutorials, or more specifically glut tutorials and I’ve been hard-pressed to find any made even within the last 5 years. The fact that yours were made so recently is awesome. Keep em comming!

VA:F [1.9.3_1094]
Rating: 0.0/5 (0 votes cast)
VA:F [1.9.3_1094]
Rating: +1 (from 1 vote)

Thanks! :)
Seems like, that here is no forum ? Maybe you should make one, where other people could post their projects, ask for help etc. :)

VA:F [1.9.3_1094]
Rating: 0.0/5 (0 votes cast)
VA:F [1.9.3_1094]
Rating: +2 (from 2 votes)

I tried having a forum once, but no one (literally) used it. Since re-doing the site though, I will try adding one again and see how it goes.

Cheers,
Swiftless

VN:F [1.9.3_1094]
Rating: 0.0/5 (0 votes cast)
VN:F [1.9.3_1094]
Rating: +1 (from 1 vote)

Greeting!!
Thanks a lot for those perfect samples and explanations! I’m just trying to learn openGL and i think,that this site will be very ( very very) useful for me. Maybe u can post some books, from what you learned ? Or sites? Seems like that GL library’s contains a million of functions, would be gr8, if could understand, what each function means :)

Anyway, GL ( good luck this time :D )and keep up the good work!

VA:F [1.9.3_1094]
Rating: 5.0/5 (1 vote cast)
VA:F [1.9.3_1094]
Rating: +1 (from 1 vote)

Hi Wish,

Thanks for the comments! It’s always great to hear people benefitting from the site.

I mainly learnt about OpenGL through the online API once I got past the basics.
I would however recommend the following books for starting OpenGL and GLSL:
The Red Book – For OpenGL
The Orange Book – For GLSL

And the following sites for getting started with OpenGL:
NeHe Tutorials
LightHouse3D
Apron Tutorials OpenGL
Game Tutorials

Hope this helps,
Swiftless

VN:F [1.9.3_1094]
Rating: 0.0/5 (0 votes cast)
VN:F [1.9.3_1094]
Rating: +1 (from 1 vote)

Hi Cody,

Sure thing, I will post the completed source code at the bottom of this page. It should be on the bottom of all the other pages, but since rewriting this one, that is one thing I forgot.

Thanks,
Donald

VN:F [1.9.3_1094]
Rating: 5.0/5 (1 vote cast)
VN:F [1.9.3_1094]
Rating: 0 (from 2 votes)

I really like your tutorials… I’m already learning alot.. The only thing I wish you would do is to add the completed source code at the bottom of each… that is all… but other than that thanks

VA:F [1.9.3_1094]
Rating: 0.0/5 (0 votes cast)
VA:F [1.9.3_1094]
Rating: +1 (from 1 vote)

Write a comment

Improve the web with Nofollow Reciprocity.