Results 1 to 2 of 2

Thread: Why are some of my vertices not drawing?

  1. #1
    Newbie Newbie
    Join Date
    Jun 2015
    Posts
    2

    Why are some of my vertices not drawing?

    I'm attempting to make a voxel terrain generator in C++ with OpenGL, the code adds the elements correctly, but some aren't displayed. I checked if they are out of the screen bounds, as well as out of the znear or zfar, but that is not the case. The code for constructing a cube is as follows.

    Code :
    CubeData createCube(float x, float y, float z, float size) {
        float r = (float) (rand() % 255 + 1);
        float g = (float)(rand() % 255 + 1);
        float b = (float)(rand() % 255 + 1);
        float a = 1.0f;
        if (r > 1.0f)
            r = 100.0f / r;
        if (g > 1.0f)
            g = 100.0f / g;
        if (b > 1.0f)
            b = 100.0f / b;
        std::vector<GLfloat> _cube_verts = {
            -size + x, -size + y, size + z,
            size + x, -size + y, size + z,
            size + x, size + y, size + z,
            -size + x, size + y, size + z,
            -size + x, -size + y, -size + z,
            size + x, -size + y, -size + z,
            size + x, size + y, -size + z,
            -size + x, size + y, -size + z
        };
        std::vector<unsigned int> _indices = {
            // front
            last_verts_len, (last_verts_len + 1), (last_verts_len + 2),
            (last_verts_len + 2), (last_verts_len + 3), last_verts_len,
            // top
            (last_verts_len + 3), (last_verts_len + 2), (last_verts_len + 6),
            (last_verts_len + 6), (last_verts_len + 7), (last_verts_len + 3),
            // back
            (last_verts_len + 7), (last_verts_len + 6), (last_verts_len + 5),
            (last_verts_len + 5), (last_verts_len + 4), (last_verts_len + 7),
            // bottom
            (last_verts_len + 4), (last_verts_len + 5), (last_verts_len + 1),
            (last_verts_len + 1), last_verts_len, (last_verts_len + 4),
            // left
            (last_verts_len + 4), last_verts_len, (last_verts_len + 3),
            (last_verts_len + 3), (last_verts_len + 7), (last_verts_len + 4),
            // right
            (last_verts_len + 1), (last_verts_len + 5), (last_verts_len + 6),
            (last_verts_len + 6), (last_verts_len + 2), (last_verts_len + 1)
        };
        std::vector<GLfloat> _colors = {
            r, g, b, a,
            r, g, b, a,
            r, g, b, a,
            r, g, b, a,
            r, g, b, a,
            r, g, b, a,
            r, g, b, a,
            r, g, b, a
        };
        CubeData data;
        data.verts = _cube_verts;
        data.inds = _indices;
        data.colors = _colors;
        last_verts_len += _cube_verts.size();
        cubeIndex++;
        return data;
    }

    CubeData is just a struct containing the three vectors. And the two append* functions are just inserts to the end of the vector.

    The VAODrawer class is as follows.

    Code :
        #include "VAODrawer.h"
     
    void VAODrawer::SetVerticesWithIndicesAndColors(std::vector<GLfloat> verts, std::vector<GLuint> indices, std::vector<GLfloat> colors) {
        this->_VERTS = std::vector<GLfloat>(verts);
        this->_INDICES = std::vector<unsigned int>(indices);
        this->_COLORS = std::vector<GLfloat>(colors);
    }
     
    void VAODrawer::Init() {
        _MODEL_MATRIX = glm::mat4(1.0f);
        if (_VAO_ID == -1 && _VBO_VERT_ID == -1 && _VBO_COL_ID == -1 && _VAO_ID == -1 && _VERTS.size() != 0 && _INDICES.size() != 0 && _COLORS.size() != 0)
        {
            glGenVertexArrays(1, &_VAO_ID);
            glGenBuffers(1, &_VBO_VERT_ID);
            glGenBuffers(1, &_VBO_COL_ID);
            glGenBuffers(1, &_IND_ID);
            glBindVertexArray(_VAO_ID);
            glBindBuffer(GL_ARRAY_BUFFER, _VBO_VERT_ID);
            glBufferData(GL_ARRAY_BUFFER, _VERTS.size() * sizeof(GLfloat), &_VERTS[0], GL_STATIC_DRAW);
            glBindBuffer(GL_ARRAY_BUFFER, _VBO_COL_ID);
            glBufferData(GL_ARRAY_BUFFER, _COLORS.size() * sizeof(GLfloat), _COLORS.data(), GL_STATIC_DRAW);
            glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, _IND_ID);
            glBufferData(GL_ELEMENT_ARRAY_BUFFER, _INDICES.size() * sizeof(GLuint), &_INDICES[0], GL_STATIC_DRAW);
            glBindBuffer(GL_ARRAY_BUFFER, _VBO_VERT_ID);
            glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, (void*)0);
            glBindBuffer(GL_ARRAY_BUFFER, _VBO_COL_ID);
            glVertexAttribPointer(1, 4, GL_FLOAT, GL_FALSE, 0, (void*)0);
            glBindBuffer(GL_ARRAY_BUFFER, 0);
            glBindVertexArray(0);
            printf(std::string("Ind length" + std::to_string(_INDICES.size()) + "\n").c_str());
        }
    }
     
    const glm::mat4 VAODrawer::GetModelMatrix() {
        return _MODEL_MATRIX;
    }
     
    void VAODrawer::Draw(const Camera& camera, Shader* shader) {
        if (_VAO_ID != -1 && _VBO_VERT_ID != -1 && _VBO_COL_ID != -1) {
            glBindVertexArray(_VAO_ID);
            glBindBuffer(GL_ARRAY_BUFFER, _VBO_VERT_ID);
            glEnableVertexAttribArray(0);
            glBindBuffer(GL_ARRAY_BUFFER, _VBO_COL_ID);
            glEnableVertexAttribArray(1);
            glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, _IND_ID);
            shader->UniformMatrix4("MVPMatrix", (camera.GetProjectionMatrix() * camera.GetViewMatrix() * GetModelMatrix()));
            glDrawElements(GL_TRIANGLES, _INDICES.size(), GL_UNSIGNED_INT, (void*)0);
            glDisableVertexAttribArray(0);
            glDisableVertexAttribArray(1);
            glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
            glBindBuffer(GL_ARRAY_BUFFER, 0);
            glBindVertexArray(0);
        }
        else {
            this->Init();
        }
    }
     
    void VAODrawer::Rotate(float x, float y, float z) {
        _MODEL_MATRIX = glm::rotate(_MODEL_MATRIX, x, glm::vec3(1, 0, 0));
        _MODEL_MATRIX = glm::rotate(_MODEL_MATRIX, y, glm::vec3(0, 1, 0));
        _MODEL_MATRIX = glm::rotate(_MODEL_MATRIX, z, glm::vec3(0, 0, 1));
    }
     
    void VAODrawer::Dispose() {
        glDeleteBuffers(1, &_VBO_VERT_ID);
        glDeleteBuffers(1, &_VBO_COL_ID);
        glDeleteBuffers(1, &_IND_ID);
        glDeleteVertexArrays(1, &_VAO_ID);
    }

  2. #2
    Newbie Newbie
    Join Date
    Jun 2015
    Posts
    2
    I found out what the problem was. I thought the indexes were offset by the actual amount of vertex floats, not the amount of vertices.

Similar Threads

  1. Clipping of vertices in drawing wireframe cube to center of screen
    By Heisenberg in forum OpenGL: Basic Coding
    Replies: 1
    Last Post: 04-22-2018, 05:44 AM
  2. Problem drawing lines correctly in cube vertices
    By The_Notorious in forum OpenGL: Basic Coding
    Replies: 1
    Last Post: 01-17-2015, 01:43 AM
  3. Wrong vertices drawing with VBO with glDrawArrays
    By imported_tonyo_au in forum OpenGL: Basic Coding
    Replies: 3
    Last Post: 01-29-2012, 10:26 PM
  4. VBOs: Drawing vertices of same color
    By boozo in forum OpenGL: Basic Coding
    Replies: 6
    Last Post: 06-30-2010, 06:01 AM
  5. Drawing 3D heatmap surface based a set of vertices
    By oumind in forum OpenGL: Basic Coding
    Replies: 3
    Last Post: 10-28-2008, 06:28 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Proudly hosted by Digital Ocean