我有以下程序,但我不知道为什么glColor4f()调用未从glDrawArrays()的输出中引起红线,有人可以告诉我我在做什么错吗?由于某种原因,正在输出白线。 GL的现代版本中不再支持glColor4f()吗?如果是这样,给这样的颜色上色的正确方法是什么?

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

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

但是,添加这些 call 后,我看不出任何区别。
#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;
}

最佳答案

您选择了核心资料。

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

关于c++ - 如何使用glColor4f()和glDrawArrays(GL_LINES,0,2)绘制彩色线条,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/17129335/

10-11 23:23