Jump to content
  • Advertisement
Sign in to follow this  
MaimaVanPersie

Texture problem (beginner)

This topic is 499 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello everyone, I am having trouble with my texture for some reason. :( Here is the result: http://imgur.com/a/Ix32f 

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 to help me identify the cause of the distortion. Here are all the relevant parts of the code:

Mesh.h
 

Code :
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
 

Code :
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.GetPos());
texCoords.push_back(vertices.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
 

Code :
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. \n";
 
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:

Code :
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
 

Code :
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 time. 

Share this post


Link to post
Share on other sites
Advertisement
  • How exactly are vertex coordinates transformed to screen coordinates? I don't see any matrix.
  • Can you draw your triangle with interpolated vertex colors (e.g. one red, one green and one blue) instead of a texture, to verify that the vertices are in the expected order, in the expected position, and not clipped?
  • What does the texture look like and how should it be drawn? The screenshot suggests a disk with a flame, of which only the upper half (presumably because of texture coordinates or clipping) is drawn slightly squeezed (possibly because of mismatched screen aspect ratio).

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!