Lockyer
Lockyer

Reputation: 1401

How do I draw a colored line with glColor4f() and glDrawArrays(GL_LINES, 0, 2)

I have the following program, and I cannot figure out why the glColor4f() call is not resulting in a red line from the output of glDrawArrays() can anyone tell me what I'm doing wrong? A white line is being output for some reason. Is glColor4f() no longer supported in modern versions of GL. If that's the case, what's the proper way to color lines like this?

All of my googling led me to find that I should be calling:

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

However, after adding those calls I don't see any difference.

#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;
}

Upvotes: 1

Views: 1643

Answers (1)

genpfault
genpfault

Reputation: 52166

You've selected a Core profile.

There are no freebies in Core, you must provide a vertex and fragment shader.

Upvotes: 2

Related Questions