2

I'm writting opengl code on c++ and I create an own class of object. But for some reason it doesn't rendering. I think problem in vector, which I use as coordinates of vertex. And i 100% sure problem is not in shaders.

Here is a code:

source.cpp

    //other shader code

    GLobject obj(Points);
    obj.SetBufferData(0, GL_ARRAY_BUFFER, 2, GL_FALSE);

    glClearColor(0.3, 0.5, 0.7, 1);

    while (!glfwWindowShouldClose(window)) {
        glfwSwapBuffers(window);

        glClear(GL_COLOR_BUFFER_BIT);

        glUseProgram(ShaderProgram);

        obj.Draw(0, GL_ARRAY_BUFFER, GL_TRIANGLES, 2);
    }
}

GLobject.cpp

#include "GLobject.h"

GLobject::GLobject(float points[])
{
    glGenVertexArrays(1, &this->VAO);
    glGenBuffers(1, &this->VBO);

    for (int i = 0; i < sizeof(points); i++)
        this->points.push_back(points[i]);

    p = this->points.data();
}

GLobject::~GLobject()
{
}

void GLobject::SetBufferData(int index, GLenum btype, int size, GLenum normalize)
{
    glBindVertexArray(this->VAO);
    glBindBuffer(btype, this->VBO);
    glVertexAttribPointer(index, size, GL_FLOAT, normalize, size*sizeof(float), (void*)0);

    glBufferData(btype, sizeof(p), p, GL_STATIC_DRAW);

    glBindVertexArray(0);
    glBindBuffer(btype, 0);
}

void GLobject::Draw(int index, GLenum btype, GLenum dtype, int size) {
    glBindVertexArray(this->VAO);
    glBindBuffer(btype, this->VBO);

    glEnableVertexAttribArray(index);

    glDrawArrays(dtype, 0, sizeof(p) / size);
}

I'm be very happy if you help me.

P.S. I'm from Ukraine so my English may be bad.

  • `sizeof(p)` doesn't return the number of items in `p`, but does return the size of `float*` (this is the number of bytes a pointer needs). – Rabbid76 Nov 21 '21 at 17:19
  • I tried to change it and nothing changed. Result is the same. – Normal Name Nov 21 '21 at 17:22
  • Again. Nothing changed. – Normal Name Nov 21 '21 at 17:25
  • The size argument of [`glBufferData`](https://www.khronos.org/registry/OpenGL-Refpages/gl4/html/glBufferData.xhtml) (`this->points.size()*sizeof(float)`) must be the size of the buffer in bytes. The size argument of [`glDrawArrays`](https://www.khronos.org/registry/OpenGL-Refpages/gl4/html/glDrawArrays.xhtml) must be the number of vertices. – Rabbid76 Nov 21 '21 at 17:27
  • 1
    Actually, this is not an OpenGL problem. This is just a basic C/C++ question. Read about the [`sizeof` operator](https://en.cppreference.com/w/cpp/language/sizeof). – Rabbid76 Nov 21 '21 at 17:29
  • I don't use this->points.size()*sizeof(float), I use size*sizeof(float), where size is a parameter which I set in source.cpp – Normal Name Nov 21 '21 at 17:32
  • 1
    Please read your own code: `glBufferData(btype, sizeof(p), p, GL_STATIC_DRAW);` – Rabbid76 Nov 21 '21 at 17:32

0 Answers0