OpenGL window is unresponsive, fully white, and closes itself after a few seconds

I’m running this on Visual Studio 2022, with the Win32 (x86) platform. The same bug occurs on x64, though, I’ve tested it.

I have found no errors in my code, but still I’ve tried adding compile error checks and printing errors to the console using std::cout yet nothing pops up on it (the console does work with cout).

When I remove EVERYTHING from // compile shaders to // create buffers n stuff and remove EVERYTHING relating to the VBOs/VAOs, it works perfectly fine! But this is clearly a problem, because I’m pretty sure I can’t render a triangle without them.

Why this is happening and how can it be solved?

main.cpp:

#include <windows.h>
#include <GL/glew.h>
#include <GLFW/glfw3.h>
#include <iostream>

using std::cout;


const char* vshSrc = 
"#version 330 coren"
"layout (location = 0) in vec3 aPos;n"
"void main()n"
"{n"
"   gl_Position = vec4(aPos.x, aPos.y, aPos.z, 1.0);n"
"}";

const char* fshSrc = 
"#version 330 coren"
"out vec4 FragColor;n"
"void main()n"
"{n"
"   FragColor = vec4(1.0f, 0.8f, 0.4f, 1.0f);n"
"}";


static void debugMode()
{
    AllocConsole();
    FILE* file;
    freopen_s(&file, "CONOUT$", "w", stdout);
    freopen_s(&file, "CONOUT$", "w", stderr);
}

static void processInput(GLFWwindow* window)
{
    if (glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS)
        glfwSetWindowShouldClose(window, GLFW_TRUE);
}

static void framebufferSize(GLFWwindow* window, int width, int height)
{
    glViewport(0, 0, width, height);
}

_Use_decl_annotations_ int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR lpCmdLine, int nShowCmd)
{
    if (!glfwInit())
        return -1;

    // window size and position
    const int vWidth = 1280;
    const int vHeight = 720;
    const int vX = (GetSystemMetrics(SM_CXSCREEN) / 2) - (vWidth / 2);
    const int vY = (GetSystemMetrics(SM_CYSCREEN) / 2) - (vHeight / 2);

    glfwWindowHint(GLFW_POSITION_X, vX);
    glfwWindowHint(GLFW_POSITION_Y, vY);

    // create window
    GLFWwindow* window = glfwCreateWindow(vWidth, vHeight, "Game", nullptr, nullptr);
    if (!window)
    {
        glfwTerminate();
        return -1;
    }

    // buncha settings
    glfwMakeContextCurrent(window);
    glfwSetFramebufferSizeCallback(window, framebufferSize);

    glfwSwapInterval(1);
    glEnable(GL_DEPTH_TEST);
    glDepthFunc(GL_LESS);

    // comment below to remove console obv
    debugMode();

    // compile shaders
    unsigned int vsh = glCreateShader(GL_VERTEX_SHADER);
    glShaderSource(vsh, 1, &vshSrc, NULL);
    glCompileShader(vsh);

    unsigned int fsh = glCreateShader(GL_FRAGMENT_SHADER);
    glShaderSource(fsh, 1, &fshSrc, NULL);
    glCompileShader(fsh);

    // create shader program
    unsigned int sProgram = glCreateProgram();
    glAttachShader(sProgram, vsh);
    glAttachShader(sProgram, fsh);
    glLinkProgram(sProgram);

    glDeleteShader(vsh);
    glDeleteShader(fsh);

    // get verts
    float verts[] = 
    {
         -0.5f, -0.5f, 0.0f,
         0.5f, -0.5f, 0.0f,
         0.0f,  0.5f, 0.0f
    };

    // create buffers n stuff
    unsigned int VBO, VAO;
    glGenVertexArrays(1, &VAO);
    glGenBuffers(1, &VBO);
    glBindVertexArray(VAO);

    glBindBuffer(GL_ARRAY_BUFFER, VBO);
    glBufferData(GL_ARRAY_BUFFER, sizeof(verts), verts, GL_STATIC_DRAW);

    glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), (void*)0);
    glEnableVertexAttribArray(0);

    glBindBuffer(GL_ARRAY_BUFFER, 0);
    glBindVertexArray(0);

    // the loop ig
    while (!glfwWindowShouldClose(window))
    {
        processInput(window);

        // render
        glClearColor(0.1f, 0.35f, 0.35f, 1.0f);
        glClear(GL_COLOR_BUFFER_BIT);

        glUseProgram(sProgram);
        glBindVertexArray(VAO);
        glDrawArrays(GL_TRIANGLES, 0, 3);

        // swap buffers and poll events
        glfwSwapBuffers(window);
        glfwPollEvents();
    }

    // kill everyone and leave without elaborating
    glDeleteVertexArrays(1, &VAO);
    glDeleteBuffers(1, &VBO);
    glDeleteProgram(sProgram);
    glfwTerminate();
    FreeConsole();

    return 0;
}

5

The problem is that GLEW is never initialized. Therefore, the function pointers to OpenGL >1.1 functions are not set. In this case, the program tries to call glCreateShader, which is an uninitialized function pointer, and crashes.

The solution is to call glewInit() after glfwMakeContextCurrent(window);. Preferably with error checking, as in this example from the GLEW documentation:

GLenum err = glewInit();
if (GLEW_OK != err)
{
  /* Problem: glewInit failed, something is seriously wrong. */
  fprintf(stderr, "Error: %sn", glewGetErrorString(err));
  ...
}

Some GPU drivers consider some OpenGL functions to be experimental and do not expose them by default. If this happens to be problem, add glewExperimental = GL_TRUE; before calling glewInit().

The program should now render the background color, but not the triangle. This is due to depth testing being enabled, while the depth buffer is never cleared. To clear the depth buffer as well as the color buffer, use this call:

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

Or in this case, you could also disable depth testing altogether. Now the triangle renders as well:

0

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật