如何使用glColor4f()和glDrawArrays(GL_LINES,0,2)绘制彩色线条

时间:2013-06-16 01:13:03

标签: c++ opengl 3d

我有以下程序,我无法弄清楚为什么glColor4f()调用没有从glDrawArrays()的输出中产生红线,任何人都可以告诉我我做错了什么?出于某种原因正在输出白线。现代版本的GL不再支持glColor4f()。如果是这样的话,那么用这种颜色线的正确方法是什么?

我所有的谷歌搜索让我发现我应该打电话:

glDisable(GL_LIGHTING); 
glDisable(GL_TEXTURE_2D);
glEnable(GL_COLOR_MATERIAL);
glColorMaterial(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE);

然而,在添加这些电话后,我看不出任何差异。

#include <vector>

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

int main(int argc, char *argv[]) {
    if(!glfwInit()) {
        return EXIT_FAILURE;
    }

    glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
    glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
    glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 2);
    glfwWindowHint(GLFW_RESIZABLE, GL_FALSE);
    GLFWwindow* window = glfwCreateWindow(800, 600, "Line Intersection", NULL, NULL);
    if(!window) {
        glfwTerminate();
        return EXIT_FAILURE;
    }
    glfwMakeContextCurrent(window);

    glewExperimental = GL_TRUE;
    if(glewInit() != GLEW_OK) {
        glfwTerminate();
        return EXIT_FAILURE;
    }

    if(!GLEW_VERSION_3_2) {
        glfwTerminate();
        return EXIT_FAILURE;
    }

    static const glm::vec3 arr[] = { 
        glm::vec3(0,0,0), 
        glm::vec3(0,0.8f,0) 
    };
    const glm::vec3* first = arr;
    const glm::vec3* last = arr + sizeof(arr) / sizeof(arr[0]);
    std::vector<glm::vec3> vertexBufferData (first, last);

    static GLuint vertexBufferId;
    glGenBuffers(1, &vertexBufferId);
    glBindBuffer(GL_ARRAY_BUFFER, vertexBufferId);
    glBufferData(GL_ARRAY_BUFFER, sizeof(vertexBufferData[0]) * vertexBufferData.size(), &vertexBufferData[0], GL_STATIC_DRAW);

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

    glDisable(GL_LIGHTING); 
    glDisable(GL_TEXTURE_2D);
    glEnable(GL_COLOR_MATERIAL);
    glColorMaterial(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE);

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

        glEnableVertexAttribArray(0);
            glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, NULL);
            glColor4f(1.0f,0.0f,0.0f,1.0f);
            glDrawArrays(GL_LINES, 0, vertexBufferData.size());
        glDisableVertexAttribArray(0);

        glfwSwapBuffers(window);
        glfwPollEvents();
    }

    glfwTerminate();
    return EXIT_SUCCESS;
}

1 个答案:

答案 0 :(得分:2)

您已选择了核心个人资料。

Core中没有免费赠品,你必须提供顶点和片段着色器。