GL_LINE_STRIP始终终止于原点

时间:2016-05-12 00:29:17

标签: opengl graphics glfw

我的意思是以下代码在屏幕上绘制一条水平线。相反,它绘制线条,然后绘制一条向原点逐渐消失的线条。图片张贴在下面。

我认为关键的代码片段是

float vertices[] =
{
    -0.5, 0.7, 1, 1, 1,
     0.5, 0.7, 1, 1, 1
};

glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);

while (not glfwWindowShouldClose(window))
{
    glClearColor(0,0,0,1);
    glClear(GL_COLOR_BUFFER_BIT);

    glDrawArrays(GL_LINE_STRIP, 0, sizeof(vertices)/sizeof(float));

    glfwSwapBuffers(window);
    glfwPollEvents();
}

完整的代码是

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

const GLchar * vertex_shader_source =
"\
    # version 150 core\n\
    in vec2 position;\
    in vec3 color;\
    out vec3 Color;\
    void main() { Color = color; gl_Position = vec4(position, 0, 1); }\
";

const GLchar * fragment_shader_source =
"\
    # version 150 core\n\
    in vec3 Color;\
    out vec4 outColor;\
    void main() { outColor = vec4(Color, 1.0); }\
";

float vertices[] =
{
    -0.5, 0.7, 1, 1, 1,
     0.5, 0.7, 1, 1, 1
};

int main (int argc, char ** argv)
{
        // ---- INITIALIZE STUFF ---- //

    glfwInit();

    glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
    glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 2);
    glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
    glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
    glfwWindowHint(GLFW_RESIZABLE, GL_FALSE);

    GLFWwindow * window = glfwCreateWindow(800, 600, "open-gl", nullptr, nullptr);
    glfwMakeContextCurrent(window);

    glewExperimental = GL_TRUE;
    glewInit();

        // ---- MAKE SHADERS ---- //

    GLuint vertex_shader = glCreateShader(GL_VERTEX_SHADER);
    glShaderSource(vertex_shader, 1, &vertex_shader_source, NULL);
    glCompileShader(vertex_shader);

    GLuint fragment_shader = glCreateShader(GL_FRAGMENT_SHADER);
    glShaderSource(fragment_shader, 1, &fragment_shader_source, NULL);
    glCompileShader(fragment_shader);

    GLuint shader_program = glCreateProgram();
    glAttachShader(shader_program, vertex_shader);
    glAttachShader(shader_program, fragment_shader);
    glBindFragDataLocation(shader_program, 0, "outColor");
    glLinkProgram(shader_program);

    glUseProgram(shader_program);

        // ---- MAKE VERTEX BUFFER OBJECT ---- //

    GLuint vbo;
    glGenBuffers(1, &vbo);
    glBindBuffer(GL_ARRAY_BUFFER, vbo);

        // ---- MAKE VERTEX ARRAY OBJECT ---- //

    GLuint vao;
    glGenVertexArrays(1, &vao);
    glBindVertexArray(vao);

    GLint posAttrib = glGetAttribLocation(shader_program, "position");
    GLint colAttrib = glGetAttribLocation(shader_program, "color");

    glEnableVertexAttribArray(posAttrib);
    glEnableVertexAttribArray(colAttrib);

    glVertexAttribPointer(posAttrib, 2, GL_FLOAT, GL_FALSE, 5*sizeof(float), 0);
    glVertexAttribPointer(colAttrib, 3, GL_FLOAT, GL_FALSE, 5*sizeof(float), (void*)(2*sizeof(float)));

        // ---- DO OTHER THINGS ---- //

    glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);

    while (not glfwWindowShouldClose(window))
    {
        glClearColor(0,0,0,1);
        glClear(GL_COLOR_BUFFER_BIT);

        glDrawArrays(GL_LINE_STRIP, 0, sizeof(vertices)/sizeof(float));

        glfwSwapBuffers(window);
        glfwPollEvents();
    }

        // ---- CLEAN UP ---- //

    glDeleteProgram(shader_program);
    glDeleteShader(vertex_shader);
    glDeleteShader(fragment_shader);
    glDeleteVertexArrays(1, &vao);
    glDeleteBuffers(1, &vbo);

    glfwTerminate();

    return 0;
}

输出为output

我不知道问题是什么;我在网上搜索过,但找不到有类似问题的人。我发现的最好的是有人说OpenGL实现并不倾向于做得非常好。但是,GL_LINES不会发生这种情况。

我正在使用带有GLFW和GLEW的OpenGL 3.2。我有一台宏碁Aspire v5-571P-6648;我不知道具体是什么型号的显卡,但我可以寻找它。

1 个答案:

答案 0 :(得分:1)

的最后一个论点
glDrawArrays(GL_LINE_STRIP, 0, sizeof(vertices)/sizeof(float));

错了,它应该是2(见https://www.opengl.org/sdk/docs/man/html/glDrawArrays.xhtml)。