PDA

View Full Version : Texture lost, why?



pplanza2
02-26-2017, 06:58 AM
Hello,

I want to display a cube and a plane. Texture is lost when I apply two different shaders.

In the code above, when 'doit' takes 1 the folllowing is displayed:

https://s28.postimg.org/jkmnngz6h/option1.jpg (https://postimg.org/image/jkmnngz6h/)

Its ok. No lights, only textured objects.

doIt=2 disply this:

https://s11.postimg.org/yfavt6gmn/option2.jpg (https://postimg.org/image/yfavt6gmn/)

doIt=3, texture in the cube is lost:

https://s15.postimg.org/4lpmw2eo7/option3.jpg (https://postimg.org/image/4lpmw2eo7/)



....
....
int doit=3;

if(doit==1) {
int program1 = worldGl.useProgram("two");
prepareMeshFbo(program1, mesh1, proyection);
mesh1.draw();

int program2 = worldGl.useProgram("two");
prepareMeshFbo(program2, mesh2, proyection);
mesh2.draw();
}else if(doit==2){
int program1 = worldGl.useProgram("main");
prepareMesh(program1, mesh1, proyection);
mesh1.draw();

int program2 = worldGl.useProgram("main");
prepareMesh(program2, mesh2, proyection);
mesh2.draw();
}else if(doit==3){
int program1 = worldGl.useProgram("main");
prepareMesh(program1, mesh1, proyection);
mesh1.draw();

int program2 = worldGl.useProgram("two");
prepareMeshFbo(program2, mesh2, proyection);
mesh2.draw();
}
...
...



public int useProgram(String theprogram) {
int program = getProgram(theprogram);
glUseProgram(program);
return program;
}



private void prepareMeshFbo(int program, MeshGl mesh, ProyectionGl proyection) {
//vertex
mesh.vertices2shader(program, "aPosition");
//texture
mesh.texture2shader(program, "displayTexture", "aTextureCoord");
//normal2
mesh.normals2shader(program,"a_Normal");
//matrix
float[] rotatedViewMatrix = mesh.getRotatedViewMatrix(proyection.getCamera());
float[] mvpMatrix = worldGl.multiplyMatrixMatrix(proyection.getProject ionMatrix(), rotatedViewMatrix);
mesh.prepareMatrixForShader(program, mvpMatrix, "uMVPMatrix");
}



public void vertices2shader(int program, String vertexAttribute) {
getVertexBuffer().position(0);
int mPositionHandle = glGetAttribLocation(program, vertexAttribute);
glEnableVertexAttribArray(mPositionHandle);


glVertexAttribPointer(mPositionHandle, GlCtes.COORDS_PER_VERTEX, GL_FLOAT, false, GlCtes.STRIDE * GlCtes.BYTES_PER_FLOAT, getVertexBuffer());

GlUtils.checkGlError(TAG, "vertices2shader");
}


public void texture2shader(int program, String samplerUniform, String texAttribute) {
if (!GLES20.glIsTexture(textureHandle)) {
throw new RuntimeException(textureHandle + " no es una textura");
}

getVertexBuffer().position(3);


GLES20.glActiveTexture(GLES20.GL_TEXTURE0);
GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, textureHandle);
int mSamplerLoc = GLES20.glGetUniformLocation(program, samplerUniform);//u_texture
GLES20.glUniform1i(mSamplerLoc, 0);

GlUtils.checkGlError(TAG, "texture2shader 1");

int mTexHandle = GLES20.glGetAttribLocation(program, texAttribute);//a_texCoord
glEnableVertexAttribArray(mTexHandle);


glVertexAttribPointer(mTexHandle, GlCtes.COORDS_PER_TEXTURE, GL_FLOAT, false, GlCtes.STRIDE * GlCtes.BYTES_PER_FLOAT, getVertexBuffer());

GlUtils.checkGlError(TAG, "texture2shader 2");
}



public void normals2shader(int program, String normalAttribute) {
getVertexBuffer().position(5);
int mNormalHandle = glGetAttribLocation(program, normalAttribute);
glEnableVertexAttribArray(mNormalHandle);
glVertexAttribPointer(mNormalHandle, GlCtes.COORDS_PER_NORMAL, GL_FLOAT, false, GlCtes.STRIDE * GlCtes.BYTES_PER_FLOAT, getVertexBuffer());
GlUtils.checkGlError(TAG, "normals2shader");
}


public int useProgram(String theprogram) {
int program = getProgram(theprogram);
glUseProgram(program);
return program;
}



uniform mat4 uMVPMatrix;
attribute vec4 aPosition;
attribute vec2 aTextureCoord;
attribute vec3 a_Normal;

varying vec2 vTextureCoord;
varying vec3 v_Normal;

void main(){
vTextureCoord=aTextureCoord;
v_Normal=a_Normal;
gl_Position = uMVPMatrix * aPosition;
}



precision mediump float;

varying vec2 vTextureCoord;
varying vec3 v_Normal;
uniform sampler2D displayTexture;

void main() {
vec3 t;
t=v_Normal;
gl_FragColor = texture2D(displayTexture, vTextureCoord);
}


uniform mat4 u_MVPMatrix;
uniform mat4 u_MVMatrix;
uniform vec3 u_LightPos;

attribute float a_LightIntensity;
attribute vec4 a_Position;
attribute vec2 a_texCoord;
attribute vec3 a_Normal;
attribute vec4 a_Color;

varying vec2 v_texCoord;
varying vec3 v_Normal;
varying vec4 v_Color;
varying float v_LightIntensity;

void main(){
vec3 modelViewVertex = vec3(u_MVMatrix * a_Position);
vec3 modelViewNormal = vec3(u_MVMatrix * vec4(a_Normal, 0.0));
vec3 lightVector = normalize(u_LightPos - modelViewVertex);
float diffuse = max(dot(modelViewNormal, lightVector), 0.1);


//float distance = length(u_LightPos - modelViewVertex);
//float attenuant=(1.0 / (1.0 + (0.25 * distance * distance)));
//v_Color=a_Color*diffuse*attenuant;

v_Color=a_Color*diffuse;
gl_Position = u_MVPMatrix * a_Position;
v_texCoord = a_texCoord;
v_Normal = a_Normal;
v_LightIntensity = a_LightIntensity;
}


precision mediump float;

varying vec2 v_texCoord;
varying vec3 v_Normal;
varying vec4 v_Color;
varying float v_LightIntensity;

uniform sampler2D u_texture;

void main() {
gl_FragColor = texture2D( u_texture, v_texCoord ) * v_Color;
gl_FragColor = gl_FragColor * v_LightIntensity;
//gl_FragColor = gl_FragColor * 1.2
//gl_FragColor = vec4(1,0,0,1);
}

Thanks for your help

pplanza2
02-27-2017, 03:09 PM
Hello,

Disabling position, texture and normal handlers solves the problem.

glDisableVertexAttribArray(mPositionHandle);
glDisableVertexAttribArray(mTexHandle);
glDisableVertexAttribArray(mNormalHandle);}

Thanks.