OpenGL 中没有显示任何内容

Nothing is showing up in OpenGL

好吧,我已经在 java 和 c++ 中工作了一段时间,但我是 OpenGL 的新手,所以我开始使用一个名为 GLFW 的库,我一直在关注一本名为 "OpenGL Super Bible 6th Edition" 的书但在 GLFW 模式下。这里的问题是我已经重新检查了所有内容并观看了其他教程,并且我的代码接缝没有问题,但着色器渲染中没有任何内容。我不知道我声明着色器 src 的部分是否可以,甚至是有效的形式。

感谢您阅读本文:)

注意: 我知道它只会渲染一个点,但我用 "glPointSize(40.0f);".

调整了它的大小
#include <GL/glew.h>
#define GLFW_DLL
#include <GLFW/glfw3.h> 
#include <stdio.h>
#include <iostream> 
#include "jelly/lua_manager.h"
#include "jelly/keysManager.h"

jelly::keys_buttons::KeysManager km; 

GLuint vertex_array_obj;
GLuint program;

GLuint startRender(GLFWwindow* window)
{
    GLuint vertexShader = glCreateShader(GL_VERTEX_SHADER);
    GLuint fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);

    std::cout << "ASD" << std::endl;

    static const GLchar * fragmentShader_src[] =
    {
        "#version 430 core                                                      \n"
        "                                                                       \n"
        "void main(void)                                                        \n"
        "{                                                                      \n"
        "   gl_Position = vec4(0, 0.5, 0.0, 1);                                 \n"
        "}                                                                      \n"
    };

    static const GLchar * vertexShader_src[] =
    {
        "#version 430 core                          \n"
        "                                           \n"
        "out vec4 color;                            \n"
        "                                           \n"
        "void main(void)                            \n"
        "{                                          \n"
        "   color = vec4(0.0, 0.8, 1.0, 1.0);       \n"
        "}                                          \n"
    };

    glShaderSource(vertexShader, 1, vertexShader_src, NULL);
    glCompileShader(vertexShader);

    glShaderSource(fragmentShader, 1, fragmentShader_src, NULL);
    glCompileShader(fragmentShader);

    GLuint tprogram = glCreateProgram();
    glAttachShader(tprogram, vertexShader);
    glAttachShader(tprogram, fragmentShader);
    glLinkProgram(tprogram);
    glValidateProgram(tprogram);

    glDeleteShader(vertexShader);
    glDeleteShader(fragmentShader);

    glGenVertexArrays(1, &vertex_array_obj);
    glBindVertexArray(vertex_array_obj);

    return tprogram;
}

void render(GLFWwindow* window)
{
    glClearColor(1.0f, 0.0f, 0.0f, 0.0f);
    glClear(GL_COLOR_BUFFER_BIT);
    glDrawArrays(GL_POINTS, 0, 1);
    glPointSize(40.0f);
}

void mouseCallback(GLFWwindow* window, int button, int action, int mods)
{
    km.mouseClick(button, action, mods);
}

void keyCallback(GLFWwindow* window, int key, int scancode, int action, int mods)
{
    km.keyPressed(key, action, mods);
}

int main()
{
    jelly::lua::LuaManager lm;

    // 0 = Build | 1 = Release | 2 = Alpha | 3 = Beta
    int buildType = 0;

    std::string title = "Relieved";

    if (buildType != 1)
    {
        switch (buildType) {
            case 0 :
                title += " | Build Version";
            break;
            case 2 :
                title += " | Alpha Version";
            break;
            case 3 :
                title += " | Beta Version";
            break;
            default :
            break;
        }
    }

    GLFWwindow* window;

    if (!glfwInit()) {
        glfwTerminate();
        return -1;
    }

    window = glfwCreateWindow(640, 400, title.c_str(), NULL, NULL);

    if (!window) {
        glfwTerminate();  
        return -1;
    }

    glfwMakeContextCurrent(window);

    glewExperimental = GL_TRUE;
    glewInit ();

    program = startRender(window);
    glUseProgram(program);

    glfwSetKeyCallback(window, keyCallback);
    glfwSetMouseButtonCallback(window, mouseCallback);

    while(!glfwWindowShouldClose(window))
    {
        render(window);
        glfwSwapBuffers(window);
        glfwPollEvents();
    }

    glDeleteVertexArrays(1, &vertex_array_obj);
    glDeleteProgram(program);
    glDeleteVertexArrays(1, &vertex_array_obj);

    glfwTerminate();    

    return 0;
}

包含着色器源的两个变量命名不正确。您将顶点源放错了 fragmentShader_src 并将片段源放错了 vertexShader_src.

如果您检查着色器编译和链接状态,您很容易发现错误。如果着色器编译或链接失败,您应该添加适当的 if 并打印日志。

此外,您还缺少明确的 OpenGL 版本选择。你应该让 GLFW 给你 'OpenGL 4.3 compatibility profile' 上下文。 (如果您不需要任何已弃用的功能,'core profile' 也可以使用。)查看 GLFW 文档以获取有关如何操作的信息。