Mittwoch, 28. Oktober 2015

OpenGL with Codeblocks + MinGW + GLEW + GLFW3

This is an installation manual for modern OpenGL tools. Modern refers espacially to GLFW3.

Codeblocks 13.12 (with MinGW compiler) GLEW 1.13.0 and GLFW 3.1.1. configuration running and compiling to an error free executeable on Windows 8 (32 bit)


Downloads


- Codeblocks 13.12 (with MinGW compiler) http://www.codeblocks.org/
- GLEW glew-1.13.0-win32.zip http://glew.sourceforge.net/ > Binaries Windows 32-bit and 64-bit
- GLFW glfw-3.1.1.bin.WIN32.zip http://glfw.org > Download > 32-bit Windows binaries

Codeblocks is installed with the wizard into the predefined directory.

Prepare


For GLEW and GLFW, two directories are created by hand. C:\include, C:\lib.

From glew-1.13.0-win32.zip glew-1.13.0\include\GL extract the GL directory to C:\include\GL.
Three files should be there.
- C:\include\GL\glew.h
- C:\include\GL\glxew.h
- C:\include\GL\wglew.h

Next extract C:\Users\Daniel\OneDrive\OpenGL\libraries\glew-1.13.0-win32.zip\glew-1.13.0\lib\Release\Win32 to C:\lib.
- C:\lib\glew32.lib
- C:\lib\glew32s.lib

GLEW libraries are available for forther use.

The similar steps are done with GLFW, glfw-3.1.1.bin.WIN32.zip\glfw-3.1.1.bin.WIN32\include\GLFW is gonna be extracted to
- C:\include\GLFW\glfw3.h
- C:\include\GLFW\glfw3native.h.

All files from glfw-3.1.1.bin.WIN32.zip\glfw-3.1.1.bin.WIN32\lib-mingw has to be extracted to
- C:\lib\glfw3.dll
- C:\lib\glfw3dll.a
- C:\lib\libglfw3.a

Additionaly the DLL files should be copied in the C:\Windows\System32 directory.
Maybe a restart of Windows might be necassary to ensure the files are loaded.
At least the DLLs should be in the same directory as the exeecutable files (*.exe).

Config


I assume the libraries should only be configured for each Codeblocks project and not for the whole Codeblocks environment.

Open Codeblocks and create a new console project with File > New > Project > Console projet > Go > Next > C++ > Next > Project title: OpenGLTest > Next > Finish.


Overwrite main.cpp to the below sourcecode [(C) http://www.learnopengl.com/code_viewer.php?code=getting-started/hellowindow2].

#include

// GLEW
#define GLEW_STATIC
#include

// GLFW
#include


// Function prototypes
void key_callback(GLFWwindow* window, int key, int scancode, int action, int mode);

// Window dimensions
const GLuint WIDTH = 800, HEIGHT = 600;

// The MAIN function, from here we start the application and run the game loop
int main()
{
    std::cout << "Starting GLFW context, OpenGL 3.3" << std::endl;
    // Init GLFW
    glfwInit();
    // Set all the required options for GLFW
    glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
    glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
    glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
    glfwWindowHint(GLFW_RESIZABLE, GL_FALSE);
    
    // Create a GLFWwindow object that we can use for GLFW's functions
    GLFWwindow* window = glfwCreateWindow(WIDTH, HEIGHT, "LearnOpenGL", nullptr, nullptr);
    
    if (window == nullptr)
    {
        std::cout << "Failed to create GLFW window" << std::endl;
        glfwTerminate();
        return -1;
    }
    
    glfwMakeContextCurrent(window);
    // Set the required callback functions
    glfwSetKeyCallback(window, key_callback);
    
    // Set this to true so GLEW knows to use a modern approach to retrieving function pointers and extensions
    glewExperimental = GL_TRUE;
    // Initialize GLEW to setup the OpenGL Function pointers
    if (glewInit() != GLEW_OK)
    {
        std::cout << "Failed to initialize GLEW" << std::endl;
        return -1;
    }
    
    // Define the viewport dimensions
    glViewport(0, 0, WIDTH, HEIGHT);

    // Game loop
    while (!glfwWindowShouldClose(window))
    {
        // Check if any events have been activiated (key pressed, mouse moved etc.) and call corresponding response functions
        glfwPollEvents();

        // Render
        // Clear the colorbuffer
        glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
        glClear(GL_COLOR_BUFFER_BIT);

        // Swap the screen buffers
        glfwSwapBuffers(window);
    }
    
    // Terminate GLFW, clearing any resources allocated by GLFW.
    glfwTerminate();
    return 0;
    }
    
// Is called whenever a key is pressed/released via GLFW
void key_callback(GLFWwindow* window, int key, int scancode, int action, int mode)
{
    std::cout << key << std::endl;
    if (key == GLFW_KEY_ESCAPE && action == GLFW_PRESS)
        glfwSetWindowShouldClose(window, GL_TRUE);
}

Go to Project > Build options... and point to the treenode OpenGL and the register Linker settings.

The order of the defined library files are important. Compiling might not work in a different order.

C:\lib\glew32s.lib
C:\lib\glew32.lib
C:\lib\glfw3dll.a
glfw3
opengl32
glu32
gdi32

In the register Search directory > Compiler > Add the C:\include directory must be added here.
In the register Search directory > Linker > Add the C:\lib directory must be added here.

Build and run demo application


Building with Build > Build should create the Build log below.

-------------- Build: Release in OpenGLTest_2 (compiler: GNU GCC Compiler)---------------

mingw32-g++.exe -Wall -fexceptions -O2 -std=c++0x -IC:\include -c C:\workspace\OpenGLTest\main.cpp -o obj\Release\main.o
mingw32-g++.exe -LC:\lib -o bin\Release\OpenGLTest.exe obj\Release\main.o -s C:\lib\glew32s.lib C:\lib\glew32.lib C:\lib\glfw3dll.a -lglfw3 -lopengl32 -lglu32 -lgdi32
Warning: .drectve `/DEFAULTLIB:"LIBCMT" /DEFAULTLIB:"OLDNAMES" ' unrecognized
Output file is bin\Release\OpenGLTest.exe with size 655.50 KB
Process terminated with status 0 (0 minute(s), 6 second(s))
0 error(s), 1 warning(s) (0 minute(s), 6 second(s))

Starting the demo application with Build > Run should show up a black and emoty window "LearnOpenGL". If so, everything went well so far. :)

I can recommend the ressource http://www.learnopengl.com/ for further material about OpenGL.

BR Daniel

Keine Kommentare:

Kommentar veröffentlichen