-1

I'm attempting to render a triangle using VBOs in OpenGL via C++.

First, I identify my variables:

CBuint _vao;
CBuint _vbo;
CBuint _ebo;

struct Vertex
{
    CBfloat position[3];
};

Then, I set the positions for each vertex to form a geometrical triangle:

Vertex v[3];
v[0].position[0] = 0.5f;
v[0].position[1] = 0.5f;
v[0].position[2] = 0.0f;

v[1].position[0] = 0.5f;
v[1].position[1] = -0.5f;
v[1].position[2] = 0.0f;

v[2].position[0] = -0.5f;
v[2].position[1] = -0.5f;
v[2].position[2] = 0.0f;

Simple enough, right? Then, I declare my indices for the EBO/IBO:

unsigned short i[] =
{
    0, 1, 2
};

Now that I have all the attribute data needed for buffering, I bind the VAO as well as the VBOs:

// Generate vertex elements
glGenVertexArrays(1, &_vao);
glGenBuffers(1, &_vbo);
glGenBuffers(1, &_ebo);

// VAO
glBindVertexArray(_vao);

// VBO
glBindBuffer(GL_ARRAY_BUFFER, _vbo);    
glBufferData(GL_ARRAY_BUFFER, sizeof(Vertex) * 9, &v, GL_STATIC_DRAW);

// EBO
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, _ebo);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(unsigned short) * 3, &i, GL_STATIC_DRAW);

// Location 0 - Positions
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), BUFFER_OFFSET(0));

glBindVertexArray(0);

Next, I render them:

glBindVertexArray(_vao);
glDrawElements(GL_TRIANGLES, 3, GL_UNSIGNED_SHORT, 0);
glBindVertexArray(0);

I then use the vertex shader:

#version 330 core

// Vertex attributes
layout(location = 0) in vec3 position;

// Node parses
out vec3 Fragpos;

// Uniforms
uniform mat4 model;
uniform mat4 projection;
uniform mat4 view;

// Vertex loop
void main()
{
    gl_Position = projection * view * model * vec4(position, 1.0f);   
    Fragpos = vec3(model * vec4(position, 1.0f));
}

To simply calculate the model position as well as the camera view. The fragment shader is again, quite simplistic:

#version 330 core

// Node parses
out vec4 color;
in vec3 Fragpos;

// Camera data
uniform vec3    view_loc;

// Global uniforms
uniform struct GLOBAL
{
    float   ambient_coefficient;
    vec3    ambient_colour;
} _global;

// Main loop
void main(void)
{
    color = vec4(1.0, 1.0, 1.0, 1.0);
}

The uniforms work perfectly fine as I've been using this shader code for previous projects. So what could the problem be? The triangle simply does not render. I can't think of anything that's causing this, any ideas?

Click here to view the end result. Edit: Just to narrow things down, I also use these variables to handle the model matrix that are being parsed to and from the vertex shader:

CBuint  _u_model;

mat4    _model;
vec3    _position;
vec4    _rotation;
vec3    _scale;

Then inside the constructor, I initialize the variables like so:

_model = mat4::identity();
_position = vec3(0.0f, 0.0f, 0.0f);
_rotation = vec4(0.0f, 1.0f, 0.0f, 0.0f);
_scale = vec3(1.0f, 1.0f, 1.0f);

_u_model = glGetUniformLocation(shader->_program, "model");

And finally, I update the model matrix using this formula:

_model =            translate(_position) *
                    rotate(_rotation.data[0], _rotation.data[1], _rotation.data[2], _rotation.data[3]) *
                    scale(_scale);

Edit 2: This is the camera class I use for the MVP:

class Camera : public object
{
private:
    CBbool      _director;
    CBfloat     _fov;
    CBfloat     _near;
    CBfloat     _far;
    CBfloat     _speed;
    Math::vec3  _front;
    Math::vec3  _up;
    Math::mat4  _projection;
    Math::mat4  _view;

    CBuint      _u_projection;
    CBuint      _u_view;

public:
    Camera(Shader* shader, Math::vec3 pos, float fov, float n, float f, bool dir) : _speed(5.0f)
    {
        _model = Math::mat4::identity();
        _projection = Math::mat4::identity();
        _view = Math::mat4::identity();

        _position = pos;
        _fov = fov;
        _near = n;
        _far = f;
        _director = dir;
        _front = vec3(0.0f, 0.0f, -1.0f);
        _up = vec3(0.0f, 1.0f, 0.0f);

        _u_projection = glGetUniformLocation(shader->_program, "projection");
        _u_view = glGetUniformLocation(shader->_program, "view");
        _u_model = glGetUniformLocation(shader->_program, "view_loc");
    }

    ~Camera() {}


    inline CBbool isDirector()          { return _director; }

    inline void forward(double delta)   { _position.data[2] -= _speed * (float)delta; }
    inline void back(double delta)      { _position.data[2] += _speed * (float)delta; }
    inline void left(double delta)      { _position.data[0] -= _speed * (float)delta; }
    inline void right(double delta)     { _position.data[0] += _speed * (float)delta; }
    inline void up(double delta)        { _position.data[1] += _speed * (float)delta; }
    inline void down(double delta)      { _position.data[1] -= _speed * (float)delta; }

    virtual void update(double delta)
    {
        _view = Math::lookat(_position, _position + _front, _up);
        _projection = Math::perspective(_fov, 900.0f / 600.0f, _near, _far);
    }

    virtual void render()
    {
        glUniformMatrix4fv(_u_view, 1, GL_FALSE, _view);
        glUniformMatrix4fv(_u_projection, 1, GL_FALSE, _projection);
        glUniform3f(_u_model, _position.data[0], _position.data[1], _position.data[2]);
    }
};
genpfault
  • 51,148
  • 11
  • 85
  • 139
DrStrange
  • 94
  • 1
  • 9
  • Ok, without seen the complete program is difficult to say, but, have you construct correctly your program (shader)? How have you initialized your uniforms (projection, view and model)? Just to narrow down the possibilities, in your vertex shader, do: `gl_Position = vec4(position, 1.0f);` – Amadeus Sep 26 '16 at 17:05
  • Thank you for your response, I've edited the question above ^. I'll test gl_position = vec4(position, 1.0f); to see if that's the problem, thanks! – DrStrange Sep 26 '16 at 17:17
  • Oh wow! It works! Thank you ever so much! :D You've saved me so much time, thanks!!! – DrStrange Sep 26 '16 at 17:18

1 Answers1

0

As Amadeus mentioned, I simply had to use gl_Position = vec4(position, 1.0f); for it to render. No idea why, but now's the time to find out! Thanks for your time.

DrStrange
  • 94
  • 1
  • 9
  • because you haven't initilized your uniform matrixes correctly (projection, view and model). In other words, just make them equal to identity – Amadeus Sep 26 '16 at 17:24
  • Yes well I actually use these three uniforms in a camera class, I'll make a second edit and show you what I have – DrStrange Sep 26 '16 at 17:33
  • It seems to just ignore the values set for the MVP uniforms :S – DrStrange Sep 26 '16 at 17:39
  • I wasn't getting the model uniform before rendering EBO! It works now, thanks for all your help! – DrStrange Sep 26 '16 at 18:11