Search code examples
c++openglopengl-3

Whenever I run my OpenGL Code I get a white screen and a crash


Whenever I run my code for OpenGL, the window that I create will pop-up for a few seconds as a blank, white, window and then immediately crashed. I get the error code: (process 15692) exited with code -1073741819.

I'm not sure what the problem is. I used some classes that I made such as, Shader which just creates a program given two shaders. Here is the code below:

#pragma once

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


#include <iostream>
#include <vector>
#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>
#include <glm/gtc/type_ptr.hpp>

#ifndef STBI_INCLUDE_STB_IMAGE_H
#define STB_IMAGE_IMPLEMENTATION
#include "stb_image.h"
#endif
#include "Shader.h"

void processInput(GLFWwindow* window, Shader Program) {
    static glm::mat4 BasicMatrix;
    static glm::vec3 BasicVector;

    if (glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS) {
        glfwSetWindowShouldClose(window, true);
    }
    if (glfwGetKey(window, GLFW_KEY_W) == GLFW_PRESS) {
        BasicMatrix = glm::mat4(1.0f);
        BasicMatrix = translate(BasicMatrix, glm::vec3(0.05f, 0.0f, 0.0f));
        glUniformMatrix4fv(glGetUniformLocation(Program.ID, "Matrices"), 1, GL_FALSE, glm::value_ptr(BasicMatrix));
    }
}

void runTest() {
    if (!glfwInit()) {
        throw(-1);
        std::cout << "Error: GLFW Init Failed" << std::endl;
    }

    glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
    glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
    glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);

    GLFWwindow* window = glfwCreateWindow(1920, 1080, "HAHAH BRRR", NULL, NULL);
    if (window == NULL) {
        std::cout << "Failed to create GLFW Window \n";
        glfwTerminate();
        throw (-1);
    }

    glfwMakeContextCurrent(window);

    glewExperimental = true;
    if (glewInit() != GLEW_OK) {
        std::cout << "GLEW INIT FAILED\n";
        exit(1);
    }

    //The first two parameters set the position of the lower left corner
    glViewport(0, 0, 1920, 1080);


    float vertex[] = {

        -0.15f, -0.15f, 0.0f,   1.0f, 0.0f, 0.0f,   0.0f, 0.0f,
        -0.15f, 0.15f, 0.0f,    0.0f, 1.0f, 0.0f,   1.0f, 0.0f,
         0.15f, 0.15f, 0.0f,    0.0f, 0.0f, 1.0f,   1.0f, 1.0f,
        -0.15f, 0.15f, 0.0f,    1.0f, 0.0f, 0.0f,   0.0f, 1.0f

    };

    unsigned int vertices[] = {
        0, 1, 3,
        3, 1, 2
    };

    const char* vertexTexShader =
        "#version 330 core\n"
        "\n"
        "layout (location = 0) in vec3 aPos;\n"
        "layout (location = 1) in vec3 aColor;\n"
        "layout (location = 2) in vec2 TexCoords;\n"
        "\n"
        "out vec3 Color;\n"
        "out vec2 TexCoord;\n"
        "\n"
        "uniform mat4 Matrices;\n"
        "\n"
        "void main(){\n"
        "\n"
        "gl_Position = Matrices * vec4(aPos, 1.0f);\n"
        "Color = aColor;\n"
        "TexCoord = TexCoords;\n"
        "\n"
        "}\n";

    const char* fragmentTexShader =
        "#version 330 core\n"
        "\n"
        "in vec3 Color;\n"
        "in vec2 TexCoord;\n"
        "out vec4 FragColor;\n"
        "uniform sampler2D texture1;\n"
        "\n"
        "void main(){\n"
        "\n"
        "\n"
        "FragColor = texture(texture1, TexCoord) * Color;\n"
        "\n"
        "\n"
        "}\n";

    Shader GameProgram(vertexTexShader, fragmentTexShader, 1);

    unsigned int VAO4, VBO4, EBO4;
    glGenVertexArrays(1, &VAO4);
    glBindVertexArray(VAO4);

    glGenBuffers(1, &VBO4);
    glBindBuffer(GL_ARRAY_BUFFER, VBO4);
    glBufferData(GL_ARRAY_BUFFER, sizeof(vertex), vertex, GL_STATIC_DRAW);

    glGenBuffers(GL_ELEMENT_ARRAY_BUFFER, &EBO4);
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO4);
    glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);

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

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

    glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void*)(6 * sizeof(float)));
    glEnableVertexAttribArray(2);


    unsigned int Texture3;
    glGenTextures(1, &Texture3);
    glBindTexture(GL_TEXTURE_2D, Texture3);

    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);

    int width, heigth, nrChannles;
    unsigned char* data = stbi_load("src/Images/awesomeface.png", &width, &heigth, &nrChannles, 4);
    if (data) {
        glTexImage2D(GL_TEXTURE_2D, 1, GL_RGB, width, heigth, 0, GL_RGBA, GL_UNSIGNED_BYTE, &data);
        glGenerateMipmap(GL_TEXTURE_2D);
    }
    else {
        std::cout << "ERROR: Could Not Generate Texture\n REASON:";
        std::cout << stbi_failure_reason();
    }

    GameProgram.use();
    glUniform1i(glGetUniformLocation(GameProgram.ID, "texture1"), 0);

    while (glfwWindowShouldClose(window)) {
        processInput(window, GameProgram);

        glClearColor(0.5f, 0.2f, 0.6f, 1.0f);
        glClear(GL_COLOR_BUFFER_BIT);

        glEnable(GL_TEXTURE0);
        glBindTexture(GL_TEXTURE_2D, Texture3);



        glBindVertexArray(VAO4);
        glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);

        glfwSwapBuffers(window);
        glfwPollEvents();
    }


    glfwTerminate();
}

Solution

  • All the errors in your application can be easily found while debugging your program. Hence I voted to close your question.

    Anyway here is a brief list of your mistakes:

    • The shader program fails to compile, because the type of the variable Color is vec3:

      "FragColor = texture(texture1, TexCoord) * Color;

      FragColor = texture(texture1, TexCoord) * vec4(Color, 1.0);
      
    • Furthermore there is a typo:

      glGenBuffers(GL_ELEMENT_ARRAY_BUFFER, &EBO4);

      glGenBuffers(1, &EBO4);
      
    • The 2nd argument of glTexImage2D must be 0:

      glTexImage2D(GL_TEXTURE_2D, 1, GL_RGB, width, heigth, 0, GL_RGBA, GL_UNSIGNED_BYTE, &data);

      glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, heigth, 0, GL_RGBA, GL_UNSIGNED_BYTE, &data);
      
    • Your application loop terminates immediately:

      while (glfwWindowShouldClose(window)) {

      while (!glfwWindowShouldClose(window)) {