0
\$\begingroup\$

I have written code to import meshes from OBJs and textures from BMPs. My texture coordinates seem to be getting messed up somehow, or some other issue is occuring, because I am getting these effects with meshes and textures: enter image description here

enter image description here

I do not know what to call these issues and I do not know what might cause them (I am new to OpenGL) so it is hard for me to figure out how to fix them. Does this look familiar to anyone? If so, what's up and how might I fix it?

Relevant code for importing mesh data:

obj.vertBufferSize = sizeof(GLfloat) * vertCount * 3;
obj.uvBufferSize = sizeof(GLfloat) * vertCount * 2;
obj.vertBuffer = vertBuffer;
obj.uvBuffer = uvBuffer;

glGenBuffers(1, &obj.vertBufferID);
glBindBuffer(GL_ARRAY_BUFFER, obj.vertBufferID);
glBufferData(GL_ARRAY_BUFFER, obj.vertBufferSize, &(obj.vertBuffer[0]), GL_STATIC_DRAW);

glGenBuffers(1, &obj.uvBufferID);
glBindBuffer(GL_ARRAY_BUFFER, obj.uvBufferID);
glBufferData(GL_ARRAY_BUFFER, obj.uvBufferSize, &(obj.uvBuffer[0]), GL_STATIC_DRAW);

Relevant code for importing texture data:

glGenTextures(1, &(bmp.ID));
glBindTexture(GL_TEXTURE_2D, bmp.ID);

glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, bmp.width, bmp.height, 0, GL_BGR, GL_UNSIGNED_BYTE, bmp.array);

glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glGenerateMipmap(GL_TEXTURE_2D);

Before the main loop (for sending mesh data to shaders):

glBindBuffer(GL_ARRAY_BUFFER, mesh.vertBufferID);
glVertexAttribPointer
(
    0,
    3,
    GL_FLOAT,
    GL_FALSE,
    0,
    (void*)0
);

glBindBuffer(GL_ARRAY_BUFFER, mesh.uvBufferID);
glVertexAttribPointer
(
    1,
    2,
    GL_FLOAT,
    GL_FALSE,
    0,
    (void*)0
);

Samples from inside the main loop:

glEnableVertexAttribArray(0);
glEnableVertexAttribArray(1);
    
glBindTexture(GL_TEXTURE_2D, texture.ID);

glDrawArrays(GL_TRIANGLES, 0, mesh.vertCount);
    
glDisableVertexAttribArray(0);
glDisableVertexAttribArray(1);
\$\endgroup\$
3
  • \$\begingroup\$ I think you could try to debug your uv buffers with renderdoc (renderdoc.org). \$\endgroup\$ Oct 12 '20 at 22:48
  • \$\begingroup\$ Also, it looks like every one of every two of your triangles is having uvs like 0,0 0,1 and 0,0 again (instead of 1,0), like if you had the wrong stride in your uv buffer. \$\endgroup\$ Oct 12 '20 at 22:53
  • \$\begingroup\$ @PedroBoechat I will check RenderDoc out, thank you. Also, I thought I checked everything to make sure I was passing the right stride to the relevant functions but maybe I missed something. \$\endgroup\$
    – sunsigil
    Oct 13 '20 at 1:20
1
\$\begingroup\$

The issue turned out to be in my code for importing UVs. I was skipping indices in the UV buffer due to an arithmetic error.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.