Opengl - 三角形不会渲染?

时间:2016-09-26 16:38:06

标签: c++ opengl graphics vbo vao

我试图通过C ++在OpenGL中使用VBO渲染三角形。

首先,我确定了我的变量:

CBuint _vao;
CBuint _vbo;
CBuint _ebo;

struct Vertex
{
    CBfloat position[3];
};

然后,我为每个顶点设置位置以形成几何三角形:

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;

够简单吧? 然后,我宣布EBO / IBO的指数:

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

现在我已经拥有了缓冲所需的所有属性数据,我绑定了VAO以及VBO:

// 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);

接下来,我渲染它们:

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

然后我使用顶点着色器:

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

简单地计算模型位置以及摄像机视图。片段着色器再次非常简单:

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

制服工作完全正常,因为我以前的项目使用此着色器代码。那么问题是什么呢?三角形根本不呈现。我想不出任何导致这种情况的事情,任何想法?

Click here to view the end result. 编辑:为了缩小范围,我还使用这些变量来处理正在从顶点着色器解析的模型矩阵:

CBuint  _u_model;

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

然后在构造函数中,我像这样初始化变量:

_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");

最后,我使用以下公式更新模型矩阵:

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

编辑2:这是我用于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]);
    }
};

1 个答案:

答案 0 :(得分:0)

正如 Amadeus 所提到的,我只需使用gl_Position = vec4(position, 1.0f);进行渲染。不知道为什么,但现在是时候找出来了!谢谢你的时间。