我无法让我的OpenGL程序渲染三角形

时间:2014-08-29 18:36:32

标签: c++ opengl shader

所以我在OpenGL中编写了一个模型加载器,实际上它确实加载了模型,但由于某种原因,我无法在屏幕上看到生成的三角形。看看你自己:

#define GLEW_STATIC

#include <GL\glew.h>    // Graphics Libraries
#include <GLFW\glfw3.h>

#include <iostream>
#include <fstream>
#include <vector>
#include <string>

int main()
{
    std::cout << "Please enter the name of the mesh file: ";
    std::string FileName;
    std::cin >> FileName;

    std::cout << "You entered: " << FileName << std::endl;

    if (!glfwInit())
    {
        std::cout << "Failed to initialize GLFW" << std::endl;
        system("Pause");
        return EXIT_FAILURE;
    }

    std::cout << "GLFW 3.0.4 Initialized" << std::endl;

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

    GLFWwindow* Window = glfwCreateWindow(800, 600, "Window", NULL, NULL);
    glfwMakeContextCurrent(Window);

    if (Window == NULL)
    {
        std::cout << "Failed to create an OpenGL 3.3 context" << std::endl;
        system("Pause");
        return EXIT_FAILURE;
    }

    std::cout << "Created an OpenGL 3.3 context" << std::endl;

    glewExperimental = GL_TRUE; 

    if (glewInit() != GLEW_OK)
    {
        std::cout << "Failed to initialize GLEW 1.11.0" << std::endl;
        system("Pause");
        return EXIT_FAILURE;
    }

    std::ifstream FileStream(FileName);
    std::vector <float> Vertices;

    if (!FileStream)
    {
        std::cout << "An error was encountered while opening "<< FileName << std::endl;
        system("Pause");
        return EXIT_FAILURE;
    }

    float coordinateX, coordinateY, coordinateZ;
    std::string Character;

    while (!FileStream.eof())
    {
        FileStream >> Character;

        if (Character == "v")
        {
            FileStream >> coordinateX >> coordinateY >> coordinateZ;
            std::cout << "Loading in " << coordinateX << " " << coordinateY << " " << coordinateZ << std::endl;
            Vertices.push_back(coordinateX);
            Vertices.push_back(coordinateY);
            Vertices.push_back(coordinateZ);
        }

        else
        {
            std::cout << "Skipping " << Character << std::endl;
            continue;
        }
    }

    std::cout << "Loaded " << FileName << std::endl;

    glEnable(GL_DEPTH_TEST);
    glDepthFunc(GL_LESS);

    unsigned int VBO;
    glGenBuffers(1, &VBO);
    glBindBuffer(GL_ARRAY_BUFFER, VBO);
    glBufferData(GL_ARRAY_BUFFER, sizeof(float) * Vertices.size(), &Vertices.front(), GL_STATIC_DRAW);

    unsigned int VAO;
    glGenVertexArrays(1, &VAO);
    glBindVertexArray(VAO);
    glEnableVertexAttribArray(0);
    glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, 0);

    const char* Vertex_Shader =
        "#version 330\n"
        "in vec3 vp;"
        "void main () {"
        "  gl_Position = vec4 (vp, 1.0);"
        "}";

    const char* Fragment_Shader =
        "#version 330\n"
        "out vec4 frag_colour;"
        "void main () {"
        "  frag_colour = vec4 (0.5, 0.0, 0.5, 1.0);"
        "}";

    unsigned int VertexShader = glCreateShader(GL_VERTEX_SHADER);
    glShaderSource(VertexShader, 1, &Vertex_Shader, NULL);
    glCompileShader(VertexShader);

    unsigned int FragmentShader = glCreateShader(GL_VERTEX_SHADER);
    glShaderSource(FragmentShader, 1, &Fragment_Shader, NULL);
    glCompileShader(FragmentShader);

    unsigned int Shader_Program = glCreateProgram();
    glAttachShader(Shader_Program, FragmentShader);
    glAttachShader(Shader_Program, VertexShader);
    glLinkProgram(Shader_Program);

    while (!glfwWindowShouldClose(Window))
    {

        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
        glUseProgram(Shader_Program);
        glBindVertexArray(VAO);
        glDrawArrays(GL_TRIANGLES, 0, Vertices.size());

        glfwPollEvents();
        glfwSwapBuffers(Window);
    }

    return EXIT_SUCCESS;
}

加载的文件如下:

v 0.0 0.5 0.0
v 0.5 -0.5 0.0
v -0.5 -0.5 0.0

3 个答案:

答案 0 :(得分:0)

glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, 0);

const char* Vertex_Shader =
    "#version 330\n"
    "in vec3 vp;"
    "void main () {"
    "  gl_Position = vec4 (vp, 1.0);"
    "}";

无法保证在声明顺序中为属性分配属性。

您需要告诉OpenGL(通过a location qualifierglBindAttribLocation() pre-link)放置或询问(glGetAttribLocation() post-link)放置位置。

答案 1 :(得分:0)

此代码中至少存在3个问题:

  1. 它创建了两个顶点着色器对象,而没有片段着色器对象。替换:

    unsigned int FragmentShader = glCreateShader(GL_VERTEX_SHADER);
    

    由:

    unsigned int FragmentShader = glCreateShader(GL_FRAGMENT_SHADER);
    
  2. glDrawArrays()的第三个参数必须是顶点数,而此代码则传递总坐标数。由于此处使用每个顶点3个坐标,因此将调用更改为:

    glDrawArrays(GL_TRIANGLES, 0, Vertices.size() / 3);
    
  3. 未指定顶点属性的位置。这可能通过“意外”起作用,特别是只要只有一个属性。但是指定位置会更安全。最简单的方法是将位置添加到vp顶点着色器的声明中:

    "layout(location = 0) in vec3 vp;"
    

答案 2 :(得分:0)

  1. 您绑定了GL_ARRAY_BUFFER错误的
  2. 您的着色器创建中存在错误
  3. 你的着色器错了
  4. 1)在GL_ARRAY_BUFFER来电之后,只需绑定(并创建)glBindVertexArray 即可。

    2)正如在另一个答案中已经说过的那样,你的片段着色器创建是错误的,应该是:unsigned int FragmentShader = glCreateShader(GL_FRAGMENT_SHADER);

    3)你理所当然地认为你将在着色器中获得正确的属性,这是不好的做法。您应该明确并且具有与此类似的布局layout(location = X) in vec3 myVec;


    作为一些提示:

    没有必要在循环中重新绑定您的VAO,只需在while(!glfwWindowShouldClose(Window))之前绑定它。您的着色器程序也是如此。

    您应该检查您的着色器是否已正确编译并且您的程序是否正确链接!分别查看glGetShaderivglGetProgramiv GL_COMPILE_STATUSGL_LINK_STATUS;如果您没有获得GL_TRUE这两种状态,请glGetShaderInfoLogglGetProgramInfoLog

    这将为您提供一条消息,告诉您编译/链接时遇到的确切问题,以便您可以轻松调试着色器代码而无需猜测。