OpenGL 代码在其他计算机上 window 中不显示任何内容

OpenGL code does not display anything in the window on other computers

我写了一些代码来在 window 中显示一个形状(在本例中是五边形)。五边形在我的电脑上显示得很好,但是当我创建一个 zip 文件并将其发送给其他人在他们的电脑上试用时,window 仍然打开,但 window 的内部是空白的,而不是显示五边形就像在我的电脑上一样。谁能告诉我为什么我在我的电脑上打开exe文件时在window中看到一个五边形,而其他人什么也看不到?

这里是“main.cpp”代码:

#include <iostream> 
#include <fstream> 
#include "program.h";

int main() 
{
    Program myProgram;
    myProgram.greetUser();

    return 0;
}

这是“program.h”代码:

#pragma once

class Program {
public:
    void greetUser();

private:
    char decision;
    void promptUser();
    void runOpenGL();
};

这里是“program.cpp”代码:

#include "program.h";
#include <glad/glad.h>
#include <GLFW/glfw3.h>
#include <iostream>

using namespace std;

void Program::greetUser() {
    cout << endl;
    cout << "Hello. Welcome to this program test." << endl;
    cout << endl;
    promptUser();
}

void Program::promptUser() {
    cout << "Would you like to open the program? (Y/N)" << endl;
    cout << endl;
    cin >> decision;
    cout << endl;

    if (decision == 'y' || decision == 'Y' || decision == 'Yes' || decision == 'yes') {
        cout << "Opening program...  ";
        runOpenGL();
    }
    else if (decision == 'n' || decision == 'N' || decision == 'No' || decision == 'no') {
        cout << "Okay. Exiting the program.";
    }
    else {
        cout << "Make up your mind already.";
    }
}

void Program::runOpenGL() {
    cout << "Program opened.";

    const char* vertexShaderSource = "#version 330 core\n" 
        "layout (location = 0 in vec3 Apos;\n"
        "void main()\n"
        "{\n"
        "    gl_Position = vec4(aPos.x, aPos.y, aPos.z, 1.0);\n"
        "}[=13=]";

    const char* fragmentShaderSource = "#version 330 core\n" 
        "out vec4 FragColor;\n"
        "void main()\n"
        "{\n"
        "    FragColor = vec4(0.8f, o.3f, 0.2f, 1.0f);\n"
        "}\n[=13=]";

    glfwInit(); 

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

    GLfloat vertices[] = 
    {
        -0.4f, -0.5f, 0.0f,
        0.4f, -0.5f, 0.0f,
        0.6f, 0.1f, 0.0f,
        0.0f, 0.6f, 0.0f,
        -0.6f, 0.1f, 0.0f
    };

    GLFWwindow* window = glfwCreateWindow(800, 800, "H6-Project", NULL, NULL); 

    if (window == NULL) 
    {
        std::cout << "Failed to create GLFW window" << std::endl;
        glfwTerminate();
        //return -1;
    }

    glfwMakeContextCurrent(window);  
    gladLoadGL(); 
    glViewport(0, 0, 800, 800); 

    GLuint vertexShader = glCreateShader(GL_VERTEX_SHADER); 

    glShaderSource(vertexShader, 1, &vertexShaderSource, NULL);
    glCompileShader(vertexShader);

    GLuint fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);

    glShaderSource(fragmentShader, 1, &fragmentShaderSource, NULL);
    glCompileShader(fragmentShader);

    GLuint shaderProgram = glCreateProgram();  
    glAttachShader(shaderProgram, vertexShader);
    glAttachShader(shaderProgram, fragmentShader);
    glLinkProgram(shaderProgram);

    glDeleteShader(vertexShader);
    glDeleteShader(fragmentShader);

    GLuint VAO, VBO; 

    glGenVertexArrays(1, &VAO);
    glGenBuffers(1, &VBO); 

    glBindVertexArray(VAO);

    glBindBuffer(GL_ARRAY_BUFFER, VBO); 
    glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW); 
    glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), (void*)0);
    glEnableVertexAttribArray(0);

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

    glClearColor(1.0f, 1.0f, 1.0f, 1.0f); 
    glClear(GL_COLOR_BUFFER_BIT); 
    glfwSwapBuffers(window); 

    while (!glfwWindowShouldClose(window)) 
    {
        glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
        glClear(GL_COLOR_BUFFER_BIT);
        glUseProgram(shaderProgram);
        glBindVertexArray(VAO);
        glDrawArrays(GL_LINE_LOOP, 0, 5); 
        glfwSwapBuffers(window);
        glfwPollEvents(); 
    }

    glDeleteVertexArrays(1, &VAO); 
    glDeleteBuffers(1, &VBO);
    glDeleteProgram(shaderProgram);

    glfwDestroyWindow(window);

    glfwTerminate(); 
}

由于语法错误,您的顶点和片段着色器将无法编译。

顶点着色器:

layout (location = 0 in vec3 Apos;

layout (location = 0) in vec3 aPos;

片段着色器:

FragColor = vec4(0.8f, o.3f, 0.2f, 1.0f);

FragColor = vec4(0.8f, 0.3f, 0.2f, 1.0f);

使用glGetShaderiv and glGetProgramInfoLog检查编译错误。例如:

bool CompileStatus(GLuint shader)
{
    GLint status = GL_TRUE;
    glGetShaderiv(shader, GL_COMPILE_STATUS, &status);
    if (status == GL_FALSE)
    {
        GLint logLen;
        glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &logLen);
        std::vector< char >log(logLen);
        GLsizei written;
        glGetShaderInfoLog(shader, logLen, &written, log.data());
        std::cout << "compile error:" << std::endl << log.data() << std::endl;
    }
    return status != GL_FALSE;
}
glCompileShader(vertexShader);
CompileStatus(vertexShader);
glCompileShader(fragmentShader);
CompileStatus(fragmentShader);