This exercise requires Visual Studio 2012 Professional Edition.
Before you begin this exercise, be sure you have installed freeGLUT and GLEW.
Compiler: After you have installed VSCode, the first thing to do is install the GCC compiler and GDB. CMake is a tool that can generate project/solution files of the user's choice (e.g. Visual Studio, Code::Blocks, Eclipse) from a collection of source code files using pre-defined CMake scripts. This allows us to generate a Visual Studio 2019 project file from GLFW's source package which we can use to compile the library.
Building a Core Profile OpenGL View Class with MFC Framework
When Visual C++ has started, you will see a Start Screen similar to this one:
2. Create a New Project
The following show you how to create a framework for theOpenGL viewing class. You will need to perform the following steps.
- Create a new project:
- Select the 'File' menu
- Select 'New | Project'. A dialogue box will appear as shown below.
- Under the project types section on the left, expand 'Installed | Templates' and locate the 'Visual C++' language templates. It might be under 'Other Languages' if you did not select C++ as your development environment when you first installed Visual Studio.
- Select the MFC template category on the left, and choose 'MFC Application' to the right.
- In the 'Name:' field, type OpenGL.
- Leave the 'Location:' field at the default. Note the path for future reference.
(*note, it is not recommended to work off of network drives; youmay get strange messages and inconsistencies when you build yourproject over a network). - Click the 'OK' button.
- In 'Application Type'
- Select 'Single document'
- Select 'Document/View architecture support'
- Un-select 'Security Development Lifecycle (SDL) checks' - the security checks deprecate some insecure C routines that Dr. Angel's library uses.
- Un-select 'Use Unicode libraries
- Select 'MFC standard' - this will adjust some settings for you.
- Select 'Use MFC in a shared DLL'
- In 'Compound Document Support'
- Make sure 'None' is selected
- In 'Document Template Properties'
- You do not have to worry about any of these settings
- In 'Database Support'
- Make sure 'None' is selected
- In 'User Interface Features', the following setting are needed
- 'Thick frame'
- 'Minimize box'
- 'Maximize box'
- Under 'Command bars (menu/toolbar/ribbon):', select 'Use a classic menu' and uncheck the toolbar options
- You can ignore all other settings
- In 'Advanced Features'
- Clear all the options (keep things simple for now)
- In 'Generated Classes'
- Should see four: COpenGLView, COpenGLApp, COpenGLDoc, and CMainFrame
- Click the 'Finish' button
You should now see a dialogue box as below:
You will notice that on the left hand side are seven project setting options.The three that you will modify are circled in red.For thoroughness, we will go through all seven options.
OpenGL Game Engine Development in Visual Studio Code (Windows Only) Updated version of Kbrawley's OpenGL Game Engine that works on Windows in 2019 Links to download dependencies for C Extension Configuration Microsoft Visual Studio 2019 C Desktop Development. VC would be installed in 'C:Program FilesMicrosoft Visual Studio 10.0VC', with headers in sub-directory 'include' and libraries in 'lib'. Windows SDK which includes OpenGL and GLU (OpenGL Utility). The Visual C 2010 Express bundles the Microsoft Windows SDK, which would be installed in 'C:Program FilesMicrosoft SDKsWindowsv7.0A.
At this point, we have created a framework for the OpenGL program we areto develop. Your main window has changed. It now has three main regions.- Right Top: Code Editor where you can write and edit your programs.When you first start Visual Studio, this is the 'Start Page'.
- Right Bottom: Feedback Area. Depending on your current activity this area might show code definitions, compiler output, or debug information.
- Left: Project Management Panel: contains Solution Explorer (default), Class View, Property Manager, and Team Explorer. Choose the one you want with tabs at the bottom.
- Solution Explorer shows you the files in the project or solution that you are working on.
- Class View enables you to see the class hierarchies in your project.
- Property Manager can be ignored for now.
- Team Explorer can be ignored for now.
If your window differs from the one pictured, you can still probably find the same elements. Most likely the Solution Explorer and Property manager will be on the left. The exact layout does not matter much.
In the next section we will use the Class View tab in the Project Management Panel show the project's class hierarchy.There should be five classes (CAboutDlg, CMainFrame, COpenGLApp, COpenGLDoc, and COpenGLView) automatically created for the OpenGL project.One of them, called 'COpenGLView', will be particularly interesting toyou because the main task of this lab is to add message handling functions into this class to allow OpenGL to set up and render the view at the appropriate times.At this point, you might want to build and run the application (which ofcourse does nothing yet) just to make sure your the wizard gave you a working project.
It should look something like this:
3. Add Necessary Member Functions to the COpenGLViewClass
The MFC Project Wizard creates the minimum number of memberfunctions for each class being generated. We may take a look at themby looking at the Class View:
- Click on the 'Class View' tab underneath the 'Solution Explorer' on the right-hand side.
OR - Choose 'View | Class View' from the menu
- Expand the 'OpenGL' project by clicking on the arrow to the left of 'OpenGL'.
You will now see the five classes that have been automatically created. - Click on 'COpenGLView'. There is no need to expand it.
- If you examine the bottom panel of the Class view, you will see some member functions that have been automatically created including OnDraw and PreCreateWindow.
- WM_CREATE (for OnCreate)
- WM_DESTROY (for OnDestroy)
- WM_SIZE (for OnSize)
To add functions for these messages, first ensure that you have selected 'COpenGLView' in the 'Class View', then press Alt + Enter to open the Properties window. It will open on the right of the screen. You will notice that in the Properties window, some properties of 'COpenGLView' are listed. The icons along the top allow you to select other property types. We want to see and edit the message mapping list. Follow the example below to see how this is done, then add message handler functions for all the messages listed above.
- Click on the messages icon (circled in red in the diagram to the left)
You will see a listing of messages. - Scroll down in the 'Messages' window
- Click on message 'WM_CREATE' Notice that a drop-down arrow appears.
- Click on the arrow and select ' OnCreate'.
- You may notice that the OnCreate is added into the 'Class View' window. Visual Studio has created the member function for you. Remember that the system can only create a skeletonfor the member function. It is your responsibility to insert computationsinto the relevent member functions.
Click on COpenGLView again to add the next function and repeat as needed.
4. Compile Your Application
- Choose 'Build' from the menu options
- Select 'Build OpenGL'
It will take a little while to compile and link theprogram. You may look at the output from the compiling process being displayedin the Output region at the bottom.If the project has compiled successfully,you will see a message like this:It is very exciting to run this little window's programif it is your first Windows program.
- Choose 'Debug' from the menu options
- Select 'Start Without Debugging'
A new window should be displayed in a few seconds.There are three menus: 'File', 'Edit', and 'Help'.These are the minimum options automatically created by your VC++ system.Don't be surprised; the inside window is totally blank, because we haven'twritten any functions yet.
NOTE: On Alex Clarke's VS2012 Professional install the OnCreate has several lines of garbage code that did not appear in previous versions. If your code does not compile try using this version of OnCreate instead:
5. Linking OpenGL Libraries
So far what we have created is all standard MFC stuff.It has nothing to do with OpenGL yet. To connect to OpenGL, we have doa few things first. Then we may use functions from OpenGL library.
First, we need to insert the following lines intoOpenGLView.h header file:
To add the above contents, you have to firstfind the desired header file. This is a good time to learn how to navigatearound the files of the project.
- Click the 'Solution Explorer' tab (in the lower left), the 'root' folder should be displayed.
- Click the '+' node, and subfolders should be displayed. Two of them are of interest to you at the moment. They are 'Source Files' and 'Header Files'.
- Click the '+' node to the left of the 'Header Files' folder, you will see a number of header files are listed. One of them is 'OpenGLView.h'
- Double click on this filename. The contents of the 'Editing Window' change to the contents in this file.
Now, you may add the above contents atthe very beginning of this header file. Remember to save the document beforeyou close this Editing window.
The next step is to add the OpenGL libraries to thelink line:
- Choose the 'Project' menu option
- Select 'OpenGL Properties'
A dialogue box will appear similar to below. - Click on the 'Linker' folder to the left side of the dialogue
- In the sub sections of 'Linker', click 'Input'.
You should now see 'Additional Dependencies' on the top right-hand side. - Select the 'Additional Dependencies', click the arrow that appears to the right and select '
' - att the following content to the top box:
- opengl32.lib;glu32.lib;freeglut.lib;glew32.lib
- Click 'OK', then 'OK' again.
Last, but not least, add support code from Lab 1 to your project by:
- Downloading Angel.zip, uofrGraphics.zip
- Extracting the files into your project folder next to the .cpp files that were generated by the Visual Studio wizard.
- Downloading the shaders vshader.glsl and fshader.glsl to the project folder.
- Adding all the new files (Angel.h, CheckErrors.h, fshader.glsl, InitShaders.cpp, mat.h, uofrGraphics.cpp uofrGraphics.h, vec.h, and vshader.glsli) to your project with the 'Project | Add Existing Item...' menu item.
- Adding the line
#include 'stdafx.h'as the very first line in InitShader.cpp and uofrGraphics.h - Adding the line
#include <wglew.h>immediately after the line#include <glew.h>.
At this point, you might want compile your applicationagain to make sure that everything is right so far.
D. Editing Member Functions
Now that the OpenGL libraries have been added to our project, weare ready add some real OpenGL code. For this lab, you are concernedwith the COpenGLView class only. Therefore, you will workwith two files:
- OpenGLView.h
- OpenGLView.cpp
The code you will add will:
- Create and set up a Device Context
- Establish a Rendering Context
- Handle window events
- Draw a scene
- Clean up after itself
For now here are thechanges you need to make to the two files. The parts you will workwith are highlighted in red.
Visual Studio Opengl Examples
Once you have modified the above two files, you can compile and run the program.
Opengl In Visual Studio Code
If you got errors, you might have forgotten to add the OpenGL code from the lab notes to the OpenGLView.h and OpenGLView.cpp! You will also need the correct shaders, or the program will appear not to start. If you get a warning that says 'GL 3.2 Context not possible', you have a graphics card that doesn't support OpenGL 3.2 Core Profile and you will have to either update your drivers, or try again with a newer card. I suggest any nVidia or AMD card produced in the last couple years.
E. Clean Your Project Before You Submit It
Opengl In Visual Studio
To clean up your project for submission delete all Debug folders in the projectand delete all sdf files. They are rather large (in some cases > 30 MB) andthe .exe file in the Debug folder will be rejected by many spam filteringsystems. Besides, you will get them back the next time you compile yourprogram.