I have a problem in applying texture to a cube. I spent a lot of time on this problem and I don’t know what is wrong. Here is what I have made:
Fragment shader:
final String fragmentShader =
// Zmniejszenie domyślnego poziomu precyzji.
"precision mediump float;
"
+ "uniform vec3 lightPosition;
"
+ "uniform vec4 lightColour;
"
+ "uniform sampler2D s_texture;
"
+ "uniform float useTexturing;
"
// Zmienne przekazane z vertex shadera.
+ "varying vec2 interpolatedUV;
"
+ "varying vec4 interpolatedColour;
"
+ "varying vec3 interpolatedPosition;
"
+ "varying vec3 interpolatedNormal;
"
+ "void main()
"
+ "{
"
// Długość wektora światła padającego na model w danym fragmencie.
+ "float distance = length(lightPosition - interpolatedPosition);
"
// Znormalizowany wektor kierunku padania światła.
+ "vec3 lightVector = normalize(lightPosition - interpolatedPosition);
"
// Wyznaczenie siły oddziaływania światła za pomocą iloczynu skalarnego.
// Im mniejsza różnica pomiędzy kątem padania światła a normalną modeluw danym fragmencie,
// tym silniejsze oddziaływanie światła.
+ "float diffuse = max( dot(interpolatedNormal, lightVector), 0.0 );
"
// Wytłumienie siły oddziaływania światła wraz ze wzrostem odległości.
+ "diffuse = diffuse * (1.0 / (1.0 + (0.10 * distance)));
"
// Doświetlenie modelu światłem ogólnym ("ambient light").
+ "diffuse = diffuse + 0.2;
"
// Wyznaczenie koloru fragmentu poprzez przemnożenie zinterpolowanego koloru modelu w danym fragmencie"
// przez siłę oddziaływania światła.
+ "if(useTexturing==0.0)
"
+ "{
"
+ "gl_FragColor = interpolatedColour * diffuse * lightColour;
"
+ "}else{
"
+ " gl_FragColor = texture2D(s_texture, interpolatedUV) * diffuse * lightColour;
"
+ "}
"
+ "}
";
How I load the image
Bitmap bitmap = null;
try {
istr = getAssets().open(filePath);
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inScaled = false; // No pre-scaling
bitmap = BitmapFactory.decodeStream(istr,null,options);
} catch (IOException e) {
// handle exception
}
Setting the texture in onDrawFrame
GLES20.glUniform1f(useTextureHandle, 1.0f);
uvBuffer.position(0);
GLES20.glVertexAttribPointer(textUVHandle, 2, GLES20.GL_FLOAT, false, 0, uvBuffer);
GLES20.glEnableVertexAttribArray(textUVHandle);
GLES20.glActiveTexture(GLES20.GL_TEXTURE0);
GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, 0);
GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MIN_FILTER, GLES20.GL_NEAREST);
GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MAG_FILTER, GLES20.GL_NEAREST);
GLUtils.texImage2D(GLES20.GL_TEXTURE_2D, 0, bitmap, 0);
GLES20.glUniform1i(TextureHandle, 0);
Loaded model
v 0.0 0.0 0.0
v 0.0 0.0 1.0
v 0.0 1.0 0.0
v 0.0 1.0 1.0
v 1.0 0.0 0.0
v 1.0 0.0 1.0
v 1.0 1.0 0.0
v 1.0 1.0 1.0
vn 0.0 0.0 1.0
vn 0.0 0.0 -1.0
vn 0.0 1.0 0.0
vn 0.0 -1.0 0.0
vn 1.0 0.0 0.0
vn -1.0 0.0 0.0
vt 0.0 0.0
vt 1.0 0.0
vt 1.0 1.0
vt 0.0 1.0
f 1/1/2 7/3/2 5/2/2
f 1/1/2 3/4/2 7/3/2
f 1/1/6 4/3/6 3/4/6
f 1/1/6 2/2/6 4/3/6
f 3/1/3 8/3/3 7/4/3
f 3/1/3 4/2/3 8/3/3
f 5/2/5 7/3/5 8/4/5
f 5/2/5 8/4/5 6/1/5
f 1/1/4 5/2/4 6/3/4
f 1/1/4 6/3/4 2/4/4
f 2/1/1 6/2/1 8/3/1
f 2/1/1 8/3/1 4/4/1
Texture I’m loading
[ATTACH=CONFIG]1354[/ATTACH]
What I get after mapping
[ATTACH=CONFIG]1355[/ATTACH]