VAO/VBO and classes

Started by
1 comment, last by Blackhardd 9 years, 6 months ago
Hey, everyone, need some help. I am really beginner with classes and programming at all so don't blame me biggrin.png
I have a class for creating and drawing VAO/VBO but it doesn't work. When i cut/paste all code from dat class into main.cpp it works fine but when i put it back to the class it draws nothing. What's amiss?
main.cpp

#include<iostream>

#include"sf_display.h"
#include"sf_mesh.h"
int main(int argc, char** argv)
{
SF_Display Display(1280, 720, "Viewport");
static const GLfloat triangle[] = { 0.0f, 0.5f, 0.0f, 0.5f, -0.5f, 0.0f, -0.5f, -0.5f, 0.0f };
SF_Mesh Mesh(triangle);
while (Display.SF_IsClosed())
{
Display.SF_Clear(0.0f, 0.1f, 0.3f, 1.0f);
Mesh.SF_Draw();
Display.SF_Update();
}
return 0;
}

sf_mesh.cpp

#include "sf_mesh.h"

SF_Mesh::SF_Mesh(static const GLfloat data[])
{
glGenVertexArrays(1, &sf_vertexArrayID);
glBindVertexArray(sf_vertexArrayID);
glGenBuffers(1, &sf_vertexBuffer);
glBindBuffer(GL_ARRAY_BUFFER, sf_vertexBuffer);
glBufferData(GL_ARRAY_BUFFER, sizeof(data), data, GL_STATIC_DRAW);
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, (void*)0);
glBindVertexArray(0);
}
SF_Mesh::~SF_Mesh()
{
glDeleteVertexArrays(1, &sf_vertexArrayID);
}
void SF_Mesh::SF_Draw()
{
glBindVertexArray(sf_vertexArrayID);
glDrawArrays(GL_TRIANGLES, 0, 3);
glBindVertexArray(0);
}

sf_mesh.h

#ifndef SF_MESH_H

#define SF_MESH_H
#include"sf_ogl_includes.h"
class SF_Mesh
{
public:
SF_Mesh(static const GLfloat data[]);
void SF_Draw();
~SF_Mesh();
private:
GLuint sf_vertexBuffer;
GLuint sf_vertexArrayID;
};
#endif
Advertisement

It isn't supported in standard C++ to declare a function parameter as static. I'm somewhat surprised that even compiles.

The bigger issue, however, occurs when you attempt to take sizeof(data). That works fine when you try it in the main() function, since the compiler has access to the array definition. When you try it inside the SF_Mesh constructor, you only have access to an array parameter of unknown size, and sizeof(data) is returning the native size of a pointer (i.e. 4 bytes, when compiled for a 32-bit architecture).

Try either passing in the size of the array as a separate parameter, or using a std::vector instead.

Tristam MacDonald. Ex-BigTech Software Engineer. Future farmer. [https://trist.am]

Thank you very much! I am using vector now and it's works.

This topic is closed to new replies.

Advertisement