简体   繁体   English

OpenGL中什么都没有显示

[英]Nothing is showing up in OpenGL

well, I have being working in java and c++ for a while, but Im new to OpenGL, so I started using a library called GLFW, I have being following a book called "OpenGL Super Bible 6th Edition" but in GLFW mode. 好吧,我已经在Java和C ++中工作了一段时间,但是我是OpenGL的新手,所以我开始使用一个名为GLFW的库,我一直在遵循一本名为“ OpenGL Super Bible 6th Edition”的书,但是使用的是GLFW模式。 The problem here is that I have rechecked all and watched other tutorials and my code seams to be alright but nothing from the shaders renders. 这里的问题是我重新检查了所有内容,并观看了其他教程和代码缝,但着色器渲染中没有任何内容。 I don't know if the part where I declare the shader src is okay or even a valid form. 我不知道声明着色器src的部分是否正常,甚至是有效形式。

Thank you for even read this :) 谢谢你连读这篇文章:)

NOTE: I know it will render only a point but I resized it with "glPointSize(40.0f);". 注意:我知道它只会渲染一个点,但是我使用“ 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;
}

The two variables that contain shaders' sources are named incorrectly. 包含着色器源的两个变量的名称不正确。 You've misplaced vertex source into fragmentShader_src and fragment source into vertexShader_src . 您已将顶点源放到fragmentShader_src而片段源放到了vertexShader_src

You would easily found the error if you checked shader compilation and linking status. 如果检查了着色器的编译和链接状态,则很容易发现错误。 You should add appropriate if s and print logs if shader compilation or linking fails. 您应该添加适当的if S和打印日志,如果着色器编译或链接失败。

Also, you're missing an explicit OpenGL version selection. 另外,您缺少显式的OpenGL版本选择。 You should ask GLFW to give you 'OpenGL 4.3 compatibility profile' context. 您应该要求GLFW为您提供“ OpenGL 4.3兼容性配置文件”上下文。 ('core profile' works too if you don't need any deprecated features.) Check GLFW docs for information how to do it. (如果您不需要任何不推荐使用的功能,“核心配置文件”也可以使用。)请查看GLFW文档,以获取有关操作方法的信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM