# How to draw an OpenGL object in another on Android? -> Eyes and Eyeballs

As i allready wrote in another question:

I'm working on OpenGL since a Week, so i'm new to the most stuff. (also my englisch isn't the best. Hop you can understand the most anyway) So i'm working on an Tamagotchi App for a school project and want to make it with OpenGL. -> How to change/modify or animate an existing OpenGL object on Android?

So this is a further question.

My Tamagotchi is haveing to eyes. Like this:

To make it look real i want the eyes to blink and the eyeballs to move arround. But the question here is what happens with the eyeball when the eyes are closed. In my Situation you woud not see the eyeballs because they are the same color then the body of the Tamagotchi. But i want the eyeballs to be a part of the eye. So if the Eye is closing the eyeballs shoud disapear where the eyes are closed whereever the eyeball is at the moment.

Here you can see the eye closed (down scaled y) but the eyeball is still visible....

I want the eyeball to be cut of or not shown where the eye is closed like this:

But i have no idear how to solve this.

Edit Here is my Code for someone who want to look at it. But i think it is not necessary.

public class GLEyes {

private int points = 1000;
private float vertices[]={0.0f,0.0f,0.0f};
private FloatBuffer vertBuff;

//Der Kreis wird von der Mitte der Fläche gezeichnet

public GLEyes(){

vertices = new float[points * 2];
for(int i=0; i< (points+3); i+=3)
{
double rad = 2*(Math.PI *i) / points; //Berechnung des radius

vertices[i]=(float)Math.cos(rad);     //X Achsen Abschnitt
vertices[i+1]=(float)Math.sin(rad);   //Y Achsen Abschnitt
vertices[i+2]=0;                      //Z Achsen Abschnitt wird nur für 3D gebraucht
}

ByteBuffer bBuff=ByteBuffer.allocateDirect(vertices.length*4);
bBuff.order(ByteOrder.nativeOrder());
vertBuff=bBuff.asFloatBuffer();
vertBuff.put(vertices);
vertBuff.position(0);
}

public void draw(GL10 gl){

lefteye(gl);
righteye(gl);

}

private void lefteye (GL10 gl){
gl.glPushMatrix();
gl.glTranslatef(1, 1, 0);
gl.glScalef(0.5f, 0.45f, 1.0f); //Skalierung einstellen
gl.glColor4f(0.0f,0.0f,0.0f, 1.0f);
gl.glVertexPointer(3, GL10.GL_FLOAT, 0, vertBuff);
gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
gl.glDrawArrays(GL10.GL_TRIANGLE_FAN, 0, points/2);
gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);
gl.glPopMatrix();

gl.glPushMatrix();
gl.glTranslatef(1, 1, 0);
gl.glScalef(0.1f, 0.1f, 1.0f); //Skalierung einstellen
gl.glColor4f(1.0f,1.0f,1.0f, 1.0f);
gl.glVertexPointer(3, GL10.GL_FLOAT, 0, vertBuff);
gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
gl.glDrawArrays(GL10.GL_TRIANGLE_FAN, 0, points/2);
gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);
gl.glPopMatrix();
}

private void righteye (GL10 gl){
gl.glPushMatrix();
gl.glTranslatef(-1, 1, 0);
gl.glScalef(0.5f, 0.45f, 1.0f); //Skalierung einstellen
gl.glColor4f(0.0f,0.0f,0.0f, 1.0f);
gl.glVertexPointer(3, GL10.GL_FLOAT, 0, vertBuff);
gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
gl.glDrawArrays(GL10.GL_TRIANGLE_FAN, 0, points/2);
gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);
gl.glPopMatrix();

gl.glPushMatrix();
gl.glTranslatef(-1, 1, 0);
gl.glScalef(0.1f, 0.1f, 1.0f); //Skalierung einstellen
gl.glColor4f(1.0f,1.0f,1.0f, 1.0f);
gl.glVertexPointer(3, GL10.GL_FLOAT, 0, vertBuff);
gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
gl.glDrawArrays(GL10.GL_TRIANGLE_FAN, 0, points/2);
gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);
gl.glPopMatrix();

}

}

• So, this is an artistic decision you need help with? Or is there something technical you're asking? Mar 28, 2013 at 18:20
• @Byte56 : Technical. Firstly i don't know i this is possible. And secondly i don't know how to solve technical. Mar 28, 2013 at 18:39
• Can you give a visual example of what you want to achieve then? Mar 28, 2013 at 18:40
• @Byte56 : i added a picture Mar 28, 2013 at 18:54
• If the center of the eye is white, won't it be invisible compared to the face? Mar 28, 2013 at 21:38

## 1 Answer

For 2D Graphics you can use the Painter's Algorithm, or alternatively the Reverse Painter's Algorithm. Means you have to draw the pupil first, then the face. The face will be drawn over the pupil this way.

For 3D Graphics that's not sufficient, you need to give everything a Z-Coordinate and activate Depth Testing.

• I realy don't get how this shoud solve my problem? Isn't it then just drawn behind the eyes and i can never se it? My project is only in 2D so i don't have to worry about Z-Coordinate. Mar 29, 2013 at 12:38
• Draw the eyelids over the eye. Mar 29, 2013 at 14:03
• I have done that, but as i thought the eye is just behind. img827.imageshack.us/img827/1221/eyesscren.png or what do you mean? I think i'm doing something wrong. My goal is to have something like that: img4.imageshack.us/img4/5877/goalll.png when the eyelids are closing. Mar 29, 2013 at 14:41
• @besnep I have re-uploaded your images to stack.imgur: i.stack.imgur.com/AynlL.png and i.stack.imgur.com/6sMAM.png. See this for more details. (You might want to re-create your comment.) Sep 1, 2015 at 19:11