0
\$\begingroup\$

I'm trying to apply the default badlogic.jpg image to a .obj I have. I'm currently loading the texture from AssetManager and creating a material with a texture attribute (as a diffuse).

In my fragment shader, I try to get the texture value and output it but it just results in a solid black. I outputted v_texCoord (as a vec4) and I could see different colors that resemble what they should be (which should mean that the texture coordinates are correct). But yet I still get a solid black color.

I don't understand why the texture is not coming through. Am I missing something?

Code:

/.../
model = assetManager.get("mesh/Sting-Sword-lowpoly.obj", Model::class.java)
texture = assetManager.get("badlogic.jpg", Texture::class.java)

val m = ModelInstance(model)

val mat = Material()
mat.set(TextureAttribute.createDiffuse(texture))
m.materials[0] = mat

/.../

Vertex Shader:

attribute vec3 a_position;
attribute vec3 a_normal;
attribute vec2 a_texCoord0;

uniform mat4 u_projViewWorldTrans;
uniform sampler2D u_diffuseTexture;

varying vec3 v_normal;
varying vec2 v_texCoord;

void main()
{
    v_texCoord = a_texCoord0;
    v_normal = normalize(a_normal);
    gl_Position = u_projViewWorldTrans * vec4(a_position, 1.0);
}

Fragment Shader:

uniform sampler2D u_diffuseTexture;
varying vec2 v_texCoord;

void main()
{
    //Get color
    vec4 color = texture(u_diffuseTexture, v_texCoord);

    gl_FragColor = color;
}
\$\endgroup\$
0

1 Answer 1

1
\$\begingroup\$

The OpenGL specification states:

If a fragment shader uses a sampler whose associated texture object is not complete ... the texture image unit will return(R,G,B,A)=(0,0,0,1).

The first thing to check is therefore that your texture loaded successfully and is complete. A common cause of textures failing to load, when attempting to load them by name from the file system, is the source image file not being found. Using an OO wrapper around the native texture loading functions can often mask errors such as this, e.g. by providing default values, or fallbacks for bad parameters or non-existent files.

texture = assetManager.get("badlogic.jpg", Texture::class.java)

Depending on the facilities offered by your language and/or programming environment, you should perform some error-checking around this line. Does assetManager.get throw an exception that you need to catch? Do you need to check the returned value of texture?

mat.set(TextureAttribute.createDiffuse(texture))

This also merits some error-checking. Again, does TextureAttribute.createDiffuse throw an exception or return a value that you need to check?

Hopefully this should enable you to hunt down the cause.

\$\endgroup\$
2
  • \$\begingroup\$ After reading this I realised that I never actually did check if the texture was fine when loading from AssetManager. Thanks, I'll check it out when I'm home. Hopefully this will lead me on the right path \$\endgroup\$ Mar 21, 2019 at 11:48
  • 1
    \$\begingroup\$ I checked for errors and return values for the two aforementioned lines of code;texture is getting a proper value, and mat is getting a diffuse texture. Neither are throwing exceptions. I'm at a lost now. \$\endgroup\$ Mar 22, 2019 at 20:38

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .