user3358152
user3358152

Reputation: 127

Why isn't my Triangle object being drawn?

I am creating an OpenGL program which simply creates a triangle and draws it on the screen. I have created a class called Drawable which contains the draw function, as well as the VAO & VBO objects which are used. I have also created a Triangle class which inherits from the Drawable class. In the Triangle class I have a constructor which takes an array of points and a single array containing a color, and it initializes the VAO & VBO (which are inherited from Drawable) using this data.

In my main function I initialize the Triangle using point and color data and then call the draw function on the Triangle object (this draw function is in Drawable). Ultimately though, the window opens and the triangle is not drawn. I have a simple constructor in the Drawable class, and if I initialize the object using this constructor and call the draw function it will display the object so I don't think its my draw function. The only thing I could think of is that because of how I am accessing the VAO & VBO objects maybe they aren't actually getting initialized to anything, but I don't know how to check that.

Drawable.h

class Drawable
{
    public:
        Drawable();

        void draw();
        GLuint objBuffer, VAO, program;
        std::string vShaderName, fShaderName;
        bool isUniform = false;
        int numVertices = 0;
        vec4 uniformColorVec;
};

Drawable.cpp

Drawable::Drawable()
{}

void Drawable::draw()
{
        GLuint program = InitShader(vShaderName.c_str(), fShaderName.c_str());
        glUseProgram(program);
        GLuint color_loc = glGetUniformLocation(program, "color");

        glBindVertexArray(VAO);

        glUniform4fv(color_loc, 1, uniformColorVec);
        glDrawArrays(GL_TRIANGLE_FAN, 0, numVertices);

}

Triangle.cpp

Triangle::Triangle(vec4 triPoints[3], vec4 color)
{
    uniformColorVec = color;

    vShaderName = "vshader00_v150.glsl";
    fShaderName = "fshader00_v150.glsl";
    numVertices = 3;

    glGenVertexArrays(1, &VAO);
    glBindVertexArray(VAO);
    glGenBuffers(1, &objBuffer);

    glBindBuffer(GL_ARRAY_BUFFER, objBuffer);
    glBufferData(GL_ARRAY_BUFFER, sizeof(triPoints), triPoints, GL_STATIC_DRAW);

    program = InitShader(vShaderName.c_str(), fShaderName.c_str());
    glUseProgram(program);

    GLuint vPosition = glGetAttribLocation(program, "vPosition");
    GLuint color_loc = glGetUniformLocation(program, "color");

    glEnableVertexAttribArray(vPosition);
    glVertexAttribPointer(vPosition, 4, GL_FLOAT, GL_FALSE, 0, BUFFER_OFFSET(0));

    glUniform4fv(color_loc, 1, uniformColorVec);

}

main file

/* All the OpenGL windowing is intialized but I won't 
    include that since I know it's fine due to other objects being drawn
    correctly */


vec4 triPoints2[3] = {
    vec4(0.0, 0.0, 1.0, 1.0),
    vec4(-0.75, -0.75, 1.0, 1.0),
    vec4(0.0, -0.75, 1.0, 1.0)
};

vec4 blue_opaque = vec4(0.0, 0.0, 1.0, 1.0);

Triangle t;
t = Triangle(triPoints2, blue_opaque);

glClearColor(1.0, 1.0, 1.0, 1.0);

glClear(GL_COLOR_BUFFER_BIT);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glEnable(GL_BLEND);

t.draw();

glFlush();

Upvotes: 2

Views: 111

Answers (1)

Yakov Galka
Yakov Galka

Reputation: 72449

Your function gets a vec4 triPoints[3] argument, but by C++ rules it's equivalent to vec4 *triPoints as a function argument. As a result sizeof(triPoints) is not the size of the data but rather the size of the pointer itself (either 4 or 8). Instead, change it to:

glBufferData(GL_ARRAY_BUFFER, sizeof(*triPoints)*numVertices, triPoints, GL_STATIC_DRAW);

Upvotes: 3

Related Questions