Hello again, thank you for your help. I think I am finally getting somewhere with this, thanks to you. As GClements mentioned, I should have been using the glTexParameteri function instead as I am identifying the textures by name. I removed all OpenGL code from constructors as John_Connor suggested and that way I am not longer re-creating meshes per frame as Silence mentioned.
Here is the result (so far): http://imgur.com/a/Ix32f (the forum wouldn’t let me use a full link, simply substitute the (dot) with an actual dot)
Pretty isn’t it? Well, as pretty as it is, that is not the result I was hoping to obtain. The mesh itself should have been a triangle, and this doesn’t look like a triangle, more like a tunnel. A fiery one, rather.
I’d like to ask for your assistance one last time, and help me identify the issue that’s causing this distortion.
Mesh.h
class Mesh
{
public:
Mesh();
~Mesh();
void InitGeometry(CVertex*, unsigned int);
void Draw();
private:
Mesh(const Mesh&) = delete;
void operator=(const Mesh&) = delete;
enum { POSITION_VB, TEXTURE_VB, NUM_BUFFERS };
GLuint m_VAO;
GLuint m_VBO[NUM_BUFFERS];
unsigned int m_DrawCount;
};
Mesh.cpp
void Mesh::InitGeometry(CVertex* vertices, unsigned int numVertices)
{
m_DrawCount = numVertices;
glGenVertexArrays(1, &m_VAO);
glBindVertexArray(m_VAO);
std::vector<glm::vec3> positions;
std::vector<glm::vec2> texCoords;
positions.reserve(numVertices);
texCoords.reserve(numVertices);
for (unsigned int i = 0; i < numVertices; ++i)
{
positions.push_back(vertices[i].GetPos());
texCoords.push_back(vertices[i].GetTex());
}
glGenBuffers(NUM_BUFFERS, m_VBO);
glBindBuffer(GL_ARRAY_BUFFER, m_VBO[POSITION_VB]);
glBufferData(GL_ARRAY_BUFFER, numVertices * sizeof(positions[0]), &positions[0], GL_STATIC_DRAW);
glVertexAttribPointer(0, 3, GL_FLOAT, false, 0, 0);
glBindBuffer(GL_ARRAY_BUFFER, m_VBO[TEXTURE_VB]);
glBufferData(GL_ARRAY_BUFFER, numVertices * sizeof(texCoords[0]), &texCoords[0], GL_STATIC_DRAW);
glVertexAttribPointer(1, 2, GL_FLOAT, false, 0, 0);
glEnableVertexAttribArray(0);
glEnableVertexAttribArray(1);
glBindVertexArray(0);
}
void Mesh::Draw()
{
glBindVertexArray(m_VAO);
glDrawArrays(GL_TRIANGLES, 0, m_DrawCount);
}
Texture.cpp
void Texture::InitTexture(const std::string& filename)
{
unsigned char* image = SOIL_load_image(filename.c_str(), &m_iWidth, &m_iHeight, 0, SOIL_LOAD_RGBA);
if (image == nullptr)
std::cerr << "Texture Image Loading Operation Failed.
";
glGenTextures(1, &m_texture);
glBindTexture(GL_TEXTURE_2D, m_texture);
// Texture Wrapping
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
// Texture Filtering
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, m_iWidth, m_iHeight, 0, GL_RGBA, GL_UNSIGNED_BYTE, image);
SOIL_free_image_data(image);
}
void Texture::Bind(unsigned int unit)
{
glActiveTexture(GL_TEXTURE0 + unit);
glBindTexture(GL_TEXTURE_2D, m_texture);
}
Vertices define as such:
CVertex vertices[] =
{
CVertex(glm::vec3(-0.5f, -0.5f, 0.0f), glm::vec2(0.0f, 0.0f)),
CVertex(glm::vec3(0.0f, 0.5f, 0.0f), glm::vec2(0.0f, 1.0f)),
CVertex(glm::vec3(0.5f, -0.5f, 0.0f), glm::vec2(1.0f, 1.0f))
};
source.cpp
void render()
{
glUseProgram(program);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
mesh.InitGeometry(vertices, sizeof(vertices) / sizeof(vertices[0]));
texture.InitTexture("Assets/Sprites/Fireball.png");
texture.Bind(0);
mesh.Draw();
glutSwapBuffers();
}
Thank you for your help so far. I am open to more tips if you can offer me, I’m trying to learn OpenGL the right way.