How to initialize OpenGL program in C

1 Answer

0 votes
#include <GL/glew.h>
#include <GLFW/glfw3.h>

#include <stdio.h>

const GLint WIDTH = 800, HEIGHT = 600;

int main(void)
{
    if (!glfwInit()) {
        printf("glfwInit() Error");
        return -1;
    }

    glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4);
    glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 6);

    glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
    glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);

    GLFWwindow* window = glfwCreateWindow(WIDTH, HEIGHT, "window title", NULL, NULL);
    if (!window) {
        printf("glfwCreateWindow() Error");
        glfwTerminate();
        return -1;
    }

    int bufferWidth, bufferHeight;
    glfwGetFramebufferSize(window, &bufferWidth, &bufferHeight);

    glfwMakeContextCurrent(window);

    glewExperimental = GL_TRUE;

    if (glewInit() != GLEW_OK) {
        printf("glewInit() Error");
        glfwDestroyWindow(window);
        glfwTerminate();
    }

    glViewport(0, 0, bufferWidth, bufferWidth);

    while (!glfwWindowShouldClose(window)) {
        glfwPollEvents();

        glClearColor(1.0f, 0.5f, 0.25f, 1.0f);
        glClear(GL_COLOR_BUFFER_BIT);

        glfwSwapBuffers(window);
    }

    glfwDestroyWindow(window);
    glfwTerminate();

    return 0;
}



/*
run:


*/

 



answered Mar 14, 2022 by avibootz

Related questions

1 answer 230 views
1 answer 168 views
1 answer 192 views
192 views asked Mar 19, 2022 by avibootz
1 answer 201 views
1 answer 313 views
...