Visual Studio
The Swiftless Tutorials should function as a good basis. I'm going to point out the second paragraph in the Coding section in particular:
So open up Visual Studio and create a new Windows Console Application
in C++. Make sure it is empty to begin with, and then create 4 files.
I am calling them: “main.cpp, main.h, opengl_3.cpp, opengl_3.h”. I am
going to go through these files one by one, starting with the header
files, so that we don’t get mixed up shuffling between them.
And its pragmas:
#pragma comment(lib, "glew32.lib")
#pragma comment(lib, "opengl32.lib")
Though I'm not particularly familiar with visual studio, so I'm just going to leave you with that tutorial.
CodeBlocks
You can create an OpenGL project in the projects menu. However, that sets up an OpenGL 2.1 project, not an OpenGL 4.2 project. Once you have that project created, you can fallow along with most visual-studio tutorials to the point of copy/past. (only adjusting things here and there for the compiler differences.)
Feel free to stop reading. The following is how I prefer to do it (In codeblocks.) It might not be how you want to do it for whatever reason, it works regardless.
But, since I can: You can add in the code that upgrades it from 2.1 to 4.2 yourself with wglCreateContextAttribsARB(...)
Which you would use after the 2.1 context was created.
In the particular case of the CodeBlocks projects, that would be by modifying the EnableOpenGL()
function.
//#include <GL/gl.h> <- this is OpenGL 2.1 header. You don't want that.
#include <GL/wglext.h> // you might need to download these
#include <GL/gl3.h>
//...
void EnableOpenGL(HWND hwnd, HDC* hDC, HGLRC* hRC)
{
//...
PFNWGLCREATECONTEXTATTRIBSARBPROC wglCreateContextAttribsARB = NULL;
int attributes[] = {
WGL_CONTEXT_MAJOR_VERSION_ARB, major,
WGL_CONTEXT_MINOR_VERSION_ARB, minor,
WGL_CONTEXT_FLAGS_ARB, WGL_CONTEXT_FORWARD_COMPATIBLE_BIT_ARB,
0,
};
wglCreateContextAttribsARB = (PFNWGLCREATECONTEXTATTRIBSARBPROC)wglGetProcAddress("wglCreateContextAttribsARB");
if (wglCreateContextAttribsARB == NULL)
{
// HANDLE FAILURE
}
HGLRC sglcv = wglCreateContextAttribsARB(hDC, NULL, attributes);
if (sglcv != NULL)
{
wglMakeCurrent(NULL, NULL);
wglDeleteContext(hRC);
hRC = sglcv;
wglMakeCurrent(hDC, hRC);
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
}
else
{
// HANDLE FAILURE
}
}
Then (Only then. You need a context to be bound first.) you can get OpenGL functions as needed with:
PFN$(glFunctionCapitalized)PROC FunctionName;
FunctionName = (PFN$(glFunctionCapitalized)PROC)wglGetProcAddress("$(glFunctionName)");
if (FunctionName == NULL)
{
// You cannot use that function. Handle as needed.
}
For example:
PFNGLVERTEXATTRIBPOINTERPROC VertexAttribPointer;
VertexAttribPointer = (PFNGLVERTEXATTRIBPOINTERPROC)wglGetProcAddress("glVertexAttribPointer");
if (VertexAttribPointer == NULL)
{
cout << "glVertexAttribPointer could not be loaded. Exiting application." << endl;
system("pause");
return FATAL_ERROR;
}
Lastly, changing the 2.1 rendering code that's near the end of the main function, in the program loop. It should be fairly Identifiable. (it uses glBegin
, glVertex
, and glEnd
) The section should be something like this:
/* OpenGL animation code goes here */
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
glClear(GL_COLOR_BUFFER_BIT);
glPushMatrix();
glRotatef(theta, 0.0f, 0.0f, 1.0f);
glBegin(GL_TRIANGLES);
glColor3f(1.0f, 0.0f, 0.0f); glVertex2f(0.0f, 1.0f);
glColor3f(0.0f, 1.0f, 0.0f); glVertex2f(0.87f, -0.5f);
glColor3f(0.0f, 0.0f, 1.0f); glVertex2f(-0.87f, -0.5f);
glEnd();
glPopMatrix();
SwapBuffers(hDC);
theta += 1.0f;
Sleep (1);
Which you can change around to your liking. Any input, or window-stuff you might want to do would be in the WindowProc
function, just after the main
function.
opengl32.lib
. But in Windows, to have access to anything newer than OGL 1.1, you need to install the Windows SDK as far as I know. \$\endgroup\$ – Appleshell May 29 '14 at 20:58gcc ogltest.cpp -framework OpenGL
. On Windows you probably use Visual Studio, or another IDE? You have to add opengl32.lib in the linker options. After you've done that, I suggest looking into GLFW for context creation and GLEW for binding the latest OGL features. Also read open.gl/context \$\endgroup\$ – Appleshell May 29 '14 at 21:16opengl32.lib
. Windows uses a WGL API to manage the render context, whereas on OS X you probably used CGL, AGL or NSOpenGL. Likewise, on Windows anything newer than OpenGL 1.1 has to be resolved at run-time usingwglGetProcAddress (...)
because the platform ships with a 1.1 implementation and display drivers extend it using Installable Client Drivers. \$\endgroup\$ – Andon M. Coleman May 29 '14 at 23:20