Finn Eggers
Finn Eggers

Reputation: 945

Getting GLFW together with Glad to work on Windows

I wanted to start a small project using OpenGL. I have done similar things in Java before and wanted to transfer some code to C++.

I started with installing glfw using msys2:

pacman -S mingw-w64-x86_64-glfw

Beside that, I had a glad.c and glad.h generated online here which I added to my project files

Furthermore, to verify my installation I downloaded a very small sample program:

#include <GLFW/glfw3.h>

int main(void)
{
    GLFWwindow* window;

    /* Initialize the library */
    if (!glfwInit())
        return -1;

    /* Create a windowed mode window and its OpenGL context */
    window = glfwCreateWindow(640, 480, "Hello World", NULL, NULL);
    if (!window)
    {
        glfwTerminate();
        return -1;
    }

    /* Make the window's context current */
    glfwMakeContextCurrent(window);

    /* Loop until the user closes the window */
    while (!glfwWindowShouldClose(window))
    {
        /* Render here */
        glClear(GL_COLOR_BUFFER_BIT);

        /* Swap front and back buffers */
        glfwSwapBuffers(window);

        /* Poll for and process events */
        glfwPollEvents();
    }

    glfwTerminate();
    return 0;
}

This did not compile so I manually changed the imports to

#define GLFW_INCLUDE_NONE
#include "glad.h"
#include <iostream>
#include <GLFW/glfw3.h>

I compile using CMake:

cmake_minimum_required(VERSION 3.19)
project(Engine3D)

find_package(glfw3 3.3 REQUIRED)
find_package(OpenGL REQUIRED)


set(CMAKE_CXX_STANDARD 17)


add_executable(Engine3D glad.h glad.c main.cpp)


target_link_libraries(Engine3D glfw)
target_link_libraries(Engine3D OpenGL::GL)

The Problem

So the code did compile using the code above, yet it crashes immediately. I tracked the issue down to this line:

/* Render here */
glClear(GL_COLOR_BUFFER_BIT);

After commenting out that line, it does not hang or crash and simply displays a black background.

I was wondering for which reason the sample code I downloaded is not working the way it should.

Upvotes: 0

Views: 187

Answers (0)

Related Questions