OpenGL is used to render 2D and 3D vector graphics. We have briefly discussed OpenGL in our previous article “OpenGL – Open Graphics Library – An introduction“.
OpenGL is a cross-platform API; that means, it has generic functions which are NOT specific to any particular platform, which enables it to run on any platform. So, how does OpenGL know how to draw on MFC dialog? Here is the trick; it doesn’t need to know, how to draw on MFC dialog. What it does to know is, to draw on the canvas. That’s it, it is simple.
But, what we need to do is; integrate OpenGL into our MFC application to render the vector graphics on the MFC dialog. Through this article, we are going to discuss this and render a drawing on the MFC dialog window. Actually, this is not a single article; I would like to put this into a series of Articles for better readability and these will be linked.
How to link opengl32.lib
to our project?
Step 1. Create an MFC Dialog application using Visual Studio IDE.
Step 2. OpenGL functions start with ‘gl
‘; and we use glRectf
function to draw a rectangle on the MFC Dialog window. glRectf
function takes two vertexes to draw the rectangle, and the values are in float.
glRectf(0.0f, 0.0f, 1.0f, 1.0f);
Where do we have to add this statement? As you are aware, drawing or painting functions usually go into OnPaint()
or OnDraw()
overloaded functions. As it is an MFC dialog application, we add this in OnPaint()
function.
That’s it? Does it so simple? Let’s try to build the program.
Step 3. Now, observe the results of the build. Does the build succeed? No. You may see below errors as a result of the build.
1 error C3861: 'glRectf': identifier not found 2 IntelliSense: identifier "glRectf" is undefined
Now, we go through these errors and learn how to fix these.
Step 3.1. By looking at the second error, we can easily understand that; it is unable to identify the function declaration of glRectf
. That means we are missing using the header file where this function is declared.
All the OpenGL ‘gl‘ functions are declared in the header file ‘gl.h‘ which is under ‘gl‘ folder. So, you need to include this header file in your program. Here is the statement;
#include <gl/gl.h>
Step 4. Now again, build the program. Observe that, this time you will see different errors, and looks like below.
2 error LNK1120: 1 unresolved externals 1 error LNK2019: unresolved external symbol __imp__glRectf@16 referenced in function "protected: void __thiscall COpenGLDlg::OnPaint(void)" (?OnPaint@COpenGLDlg@@IAEXXZ)
Why these errors? Read the error, carefully. It is clearly mentioned as, “unresolved external symbol“. That means, we have provided the declaration of the glRectf
function; but we are not provided the implementation of it. Let’s do, to fix this.
Step 4.1. The implementation of OpenGL functions is available in the opengl32.dll
library. We need to load this DLL into our Application. How do we do this? It’s simple. We need to link the associated library file into our Project; so, the DLL automatically loads into our Application, when required.
The associated library file opengl32.dll
is opengl32.lib
file. We need to add this file to our Application.
Open Project properties, then add the opengl32.lib
file into the “Additional Dependencies” field of Input configurations of Linker; which is under Configuration properties.
Step 5. After adding opengl32.lib
file to the Project; re-build the Project. Now you will see, “Rebuild all succeeded” message. That means, we have successfully fixed the above errors.
Step 6. Now, run the application. Do you see, a rectangle is drawn on our MFC Dialog? Probably, NO. This is because we are NOT yet integrated OpenGL to work with the MFC Dialog window.
As we discussed, at the beginning of this article; OpenGL doesn’t need to know how to draw on the MFC dialog, but we need to integrate OpenGL with MFC Dialog; so, we can see what OpenGL is drawing.
Let’s prepare our MFC Dialog to draw OpenGL drawings. I put this into another Article, “OpenGL – How to draw on MFC dialog? – Prepare to draw the graphics“.
// Malin
One thought on “OpenGL – How to draw on MFC dialog? – Linking opengl32.lib”