Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
129 views
in Technique[技术] by (71.8m points)

c++ - OpenGL - Texture not rendering properly

I'm trying to render a cube with a wood texture (all faces of the cube with the same texture), but the texture is not being rendered as it should (looks like the texture is trasparent at some parts):

outside the cube

But if look inside the cube, it seems to be rendering properly:

:enter image description here

Here is the .obj file that I'm reading from (with the texture coords and vertices):

mtllib crate.mtl
o Cube_Cube.002
v -1.000000 -1.000000 1.000000
v -1.000000 -1.000000 -1.000000
v 1.000000 -1.000000 -1.000000
v 1.000000 -1.000000 1.000000
v -1.000000 1.000000 1.000000
v -1.000000 1.000000 -1.000000
v 1.000000 1.000000 -1.000000
v 1.000000 1.000000 1.000000
vt 1.000000 0.000000
vt 0.000000 1.000000
vt 0.000000 0.000000
vt 1.000000 0.000000
vt 0.000000 1.000000
vt 0.000000 0.000000
vt 1.000000 0.000000
vt 0.000000 1.000000
vt 0.000000 0.000000
vt 1.000000 0.000000
vt 0.000000 1.000000
vt 0.000000 0.000000
vt 1.000000 0.000000
vt 0.000000 0.000000
vt 0.000000 1.000000
vt 1.000000 1.000000
vt 1.000000 1.000000
vt 1.000000 1.000000
vt 1.000000 1.000000
vt 1.000000 1.000000
vn -1.0000 0.0000 0.0000
vn 0.0000 0.0000 -1.0000
vn 1.0000 0.0000 0.0000
vn 0.0000 0.0000 1.0000
vn 0.0000 -1.0000 0.0000
vn 0.0000 1.0000 0.0000
usemtl Material
s off
f 6/1/1 1/2/1 5/3/1
f 7/4/2 2/5/2 6/6/2
f 8/7/3 3/8/3 7/9/3
f 5/10/4 4/11/4 8/12/4
f 2/13/5 4/11/5 1/14/5
f 7/4/6 5/15/6 8/12/6
f 6/1/1 2/16/1 1/2/1
f 7/4/2 3/17/2 2/5/2
f 8/7/3 4/18/3 3/8/3
f 5/10/4 1/19/4 4/11/4
f 2/13/5 3/17/5 4/11/5
f 7/4/6 6/20/6 5/15/6

Here is the way I'm loading the texture to OpenGL:

unsigned char* localBuffer = stbi_load(filepath.c_str(), &this->width, &this->height, &this->bitsPerPixel, 4);

glGenTextures(1, &this->id);
glActiveTexture(GL_TEXTURE0 + this->slot);
glBindTexture(GL_TEXTURE_2D, this->id);

glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);

glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, this->width, this->height, 0, GL_RGBA, GL_UNSIGNED_BYTE, localBuffer);
glBindTexture(GL_TEXTURE_2D, 0);

Here is the the vertex shader:

layout (location = 0) in vec3 aPos;
layout (location = 1) in vec2 textureCoords;

out vec2 vTextureCoords;

uniform mat4 Mvp;

void main()
{
   vTextureCoords = textureCoords;
   gl_Position = Mvp * vec4(aPos, 1.0);
}

Here is the fragment shader:

out vec4 FragColor;

in vec2 vTextureCoords;

uniform sampler2D textureSlot;

void main()
{
    FragColor = texture(textureSlot, vTextureCoords);
} 

Can someone tell me what I'm doing wrong here?

question from:https://stackoverflow.com/questions/65878923/opengl-texture-not-rendering-properly

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Thanks to @Sync it I find out that the solution was to enable depth testing.

glEnable(GL_DEPTH_TEST);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...