Hello guys, basically I am trying to draw a background and a player in front of it. I don’t know whether I should create a new VBO or VAO for each different object. Here’s what I have so far:
In my Sprite Class: (This is currently only drawing the background, which is two triangles)
Sprite::Sprite(Vertex* vertices, unsigned int numVertices)
{
// Create Shaders
m_Program = m_Shader.CreateProgram("VShader.vs", "FShader.fs");
m_NumVertices = numVertices;
GLubyte m_Background[] = { 0, 1, 3, 1, 2, 3 };
glGenVertexArrays(1, &m_VertexArrayObject);
glBindVertexArray(m_VertexArrayObject);
glGenBuffers(1, &m_VertexBufferObject);
glBindBuffer(GL_ARRAY_BUFFER, m_VertexBufferObject);
glBufferData(GL_ARRAY_BUFFER, m_NumVertices * sizeof(vertices[0]), vertices, GL_STATIC_DRAW);
glGenBuffers(1, &m_ElementBufferObject);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_ElementBufferObject);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(m_Background), m_Background, GL_STATIC_DRAW);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 5 * sizeof(GLfloat), (GLvoid*)0);
glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 5 * sizeof(GLfloat), (GLvoid*)(3 * sizeof(GLfloat)));
glEnableVertexAttribArray(0);
glEnableVertexAttribArray(1);
}
In the main header file
// Background
Vertex Position[] =
{
Vertex(glm::vec3(1.0f, 1.0f, 0.0f), glm::vec2(0.0f, 0.0f)),
Vertex(glm::vec3(1.0f, -1.0f, 0.0f), glm::vec2(0.0f, 1.0f)),
Vertex(glm::vec3(-1.0f, -1.0f, 0.0f), glm::vec2(1.0f, 1.0f)),
Vertex(glm::vec3(-1.0f, 1.0f, 0.0f), glm::vec2(1.0f, 0.0f))
};
// Player
Vertex Position2[] =
{
Vertex(glm::vec3(0.5f, 0.5f, 0.0f), glm::vec2(0.0f, 0.0f)),
Vertex(glm::vec3(0.5f, -0.5f, 0.0f), glm::vec2(0.0f, 1.0f)),
Vertex(glm::vec3(-0.5f, -0.5f, 0.0f), glm::vec2(1.0f, 1.0f)),
Vertex(glm::vec3(-0.5f, 0.5f, 0.0f), glm::vec2(1.0f, 0.0f))
};
void Render()
{
CSprite Background(Position, sizeof(Position) / sizeof(Position[0]));
CSprite Player(Position2, sizeof(Position2) / sizeof(Position2[0]));
Background.Draw();
Player.Draw();
}
The result is just a player (that’s really just two triangles for now) but no background behind.
How can I make it so that I can draw more than one object on the screen?
Thank you.