Hello, I am trying to draw a triangle in front of a background but for some reason I can't seem to be able to see the triangle. Here's my code below: (try not to cringe, I'm still just getting started with OpenGL :) ) I would greatly appreciate some tips regarding better performance/graphics code design, but for now I just want to keep it simple and draw a simple triangle in front of two triangles that are forming a quad (the background) and I want to be able to move the triangle around with a joystick later on.
// Background
Sprite::Sprite(Vertex* vertices, unsigned int numVertices)
{
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);
glGenTextures(1, &m_Textures);
glBindTexture(GL_TEXTURE_2D, m_Textures);
// Texture Wrapping
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
// Texture Filtering
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
unsigned char* image = SOIL_load_image("adventure.png", &m_iWidth, &m_iHeight, 0, SOIL_LOAD_RGBA);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, m_iWidth, m_iHeight, 0, GL_RGBA, GL_UNSIGNED_BYTE, image);
SOIL_free_image_data(image);
glBindTexture(GL_TEXTURE_2D, 0);
}
// Player
void Sprite::DrawPlayerMesh(Vertex* vertices, unsigned int numVertices)
{
m_NumVertices = numVertices;
GLubyte m_Player[] = { 0, 1, 3 };
glGenVertexArrays(1, &m_VertexArrayObject2);
glBindVertexArray(m_VertexArrayObject2);
glGenBuffers(1, &m_VertexBufferObject2);
glBindBuffer(GL_ARRAY_BUFFER, m_VertexBufferObject2);
glBufferData(GL_ARRAY_BUFFER, m_NumVertices * sizeof(vertices[0]), vertices, GL_STATIC_DRAW);
glGenBuffers(1, &m_ElementBufferObject2);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_ElementBufferObject2);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(m_Player), m_Player, 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);
}
// Render
void Sprite::Render()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glUseProgram(m_Program);
// Background
glBindVertexArray(m_VertexArrayObject);
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, m_Textures);
glUniform1i(glGetUniformLocation(m_Program, "Texture1"), 0);
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_BYTE, 0);
glBindVertexArray(0);
glBindTexture(GL_TEXTURE_2D, 0);
// Player
glBindVertexArray(m_VertexArrayObject2);
glDrawElements(GL_TRIANGLES, 3, GL_UNSIGNED_BYTE, 0);
glBindVertexArray(0);
glutSwapBuffers();
}
Result is just a background with no player. If I create a separate drawing function for the player, all I see is the player and not the background the moment glutSwapBuffers() is executed. Any simple solution to this please?
Thank you.
Oh and here's how I'm introducing data from main.cpp
Sprite Player;
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))
};
Vertex PlayerPosition[] =
{
Vertex(glm::vec3(-0.5f, 0.0f, 0.0f), glm::vec2(0.0f, 0.0f)),
Vertex(glm::vec3(0.0f, 0.5f, 0.0f), glm::vec2(0.0f, 1.0f)),
Vertex(glm::vec3(0.5f, 0.0f, 0.0f), glm::vec2(1.0f, 1.0f))
};
Call function:
void Render()
{
Sprite Background(Position, sizeof(Position) / sizeof(Position[0]));
Player.DrawPlayerMesh(PlayerPosition, sizeof(PlayerPosition) / sizeof(PlayerPosition[0]));
Background.Render();
}