Search code examples
androidopengl-es3dscenegraph

How to a clear Opengl ES material properties to avoid incorrect materials being reused?


I am working on an Android opengl lib and have hit a small issue. I know it is something easy to fix but I am having a lot of trouble tracking it down. In a nut shell, I am working on a 3d scene graph library which is based on the really light weight and awesome Scenario 2D scene graph library. It would take too long to explain the structure in depth, but essentially every node in the tree has a load(GL10 gl) method in which any texture and/or material initialization happens. There are then draw() methods for shape nodes, and also killDraw() for materials.

My material classes are intended to be user friendly for people like me who come from a 3d modeling app program standpoint (3ds max for me). Here is the issue:

I create a few 3d shapes in a scene:

FXShape shape4 = new FXShape();
shape4.setShape(new Model("cup.obj"));

Material material = new Material();
material.setAmientAndDiffuse(1,0,0,1);          
shape5.addMaterial(material);

add(shape4);

FXShape shape5 = new FXShape();
shape5.setShape(new Cube());
shape5.addMaterial(new TextureMaterial(shape5.getShape(),R.drawable.crate));
add(shape5);

Everything works great except that the red material which is create for the first shape is applying not only to that one, but the second shape even though it has it's own texture.

here are the relevant Material methods

@Override
public void loadMaterial(GL10 gl) { 
}

@Override
public void draw(GL10 gl){
    gl.glMaterialfv(GL10.GL_FRONT_AND_BACK, GL10.GL_AMBIENT,his.ambientBuffer);
    gl.glMaterialfv(GL10.GL_FRONT_AND_BACK, GL10.GL_DIFFUSE, this.diffuseBuffer);
    gl.glMaterialfv(GL10.GL_FRONT_AND_BACK, GL10.GL_SPECULAR, this.specularBuffer);
    gl.glMaterialf(GL10.GL_FRONT_AND_BACK, GL10.GL_SHININESS, Math.min(shininess, 128));    
}

@Override
public void killDraw(GL10 gl) {
    gl.glDisableClientState(GL10.GL_COLOR_ARRAY);
}

If I put 10 shapes in the scene and add a colored material to shape 1 and 5, then 1-5 are the first material, and 6-10 are the second.

What I thought I should be able to do is just call something like gl.glLoadIdentity() and clear it out, but I can't find that for the material parameters. Can any one help?

thanks

sorry for the length, I tried to be as concise as possible. If anyone is interested in the lib or more info, I'm on git hub https://github.com/ghostandthemachine/DroidGraph


Solution

  • Each object you create knows exactly what material properties it's using. Just have it push/pop the relevant state on the stack.