Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

I maybe know understand it all a little bit better so i thoug i make a new shorter question to eventually get an answer.

Get back or delete an existing OpenGL object, then change it and draw at new. How?

So if and Admin reads this post, he can feel free to delete it.

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. I allready came this far:

enter image description here

But now i want to animate things. My mind told me that it shoud be possible to change or modify an existing OpenGL object. In my situation there are 3 opjekts:

  1. Body
  2. Eyes
  3. Mouth

For example her is my code from the mouth:

public class GLMouth {

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

public GLMouth(){

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

      vertices[i]=(float)Math.cos(rad);     
      vertices[i+1]=(float)Math.sin(rad);   
      vertices[i+2]=0;                      
    }  

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


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

      vertices[i]=(float)Math.cos(rad);     
      vertices[i+1]=(float)Math.sin(rad);   
      vertices[i+2]=0;              
    }  

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



}

public void draw(GL10 gl){

    gl.glPushMatrix();
    gl.glTranslatef(0.0f, -1.0f, 0.0f);
    gl.glScalef(1.8f, 0.15f, 1.0f); 
    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(0.0f, -1.0f, 0.0f);
    gl.glScalef(1.8f, 1.0f, 1.0f); 
    gl.glColor4f(0.0f,0.0f,0.0f, 1.0f); 
    gl.glVertexPointer(3, GL10.GL_FLOAT, 0, vertBuff2);
    gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
    gl.glDrawArrays(GL10.GL_TRIANGLE_FAN, 0, points/2);
    gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);
    gl.glPopMatrix();

}

}

In my renderer i just creat a mouth object and then draw it with mouth.draw.

So i thought i have allready drawn the mouth and now i can cange for example the gl.glScalef(1.8f, 0.15f, 1.0f); from that existing mouth to zero with some time in between to let it look like the mouth is closing.

But how can i do that? I don't find anything with google, mostly because i don't know what i have to look up. If i understand OpenGL right, than it will just draw new mouths over the original.

edit Or it is a question about where is the opjekt? Can i save it an take it back (delete it etc) and draw it new with other properties.

share|improve this question

1 Answer 1

Very cool app idea! I suppose there are more than one way to handle this problem. First, you could make a sprite sheet for the mouth and use

glTranslafef() 

on the

GL10.GL_TEXTURE 

and handle the logic in the render loop.

You could make multiple GLMouth variation classes, but would be better OO to inherit the GLMouth class itself.

Alternatively, and arguably the simplest way to do it would be to modify your

GLMouth.onDraw();

To take additional parameters that specify what set of vertices to use and then build a vertex buffer for every part of the mouth you want to draw.

GLMouth.onDraw(GL10 gl,int Mouth_Version_Indicator){ 
if(Mouth_Version_Indicator == 0){ //Draw Default VertextBuffer 1 and VertexBuffer 2 }
if(Mouth_Version_Indicator == 1){ //Draw VertexBuffer 2 and VertexBuffer 3}
... }  

After that you declare a variable in your engine or renderer class to store whatever event you want to trigger the mouth movement has a occurred, and a counter to keep track of the number of frames:

onDrawFrame(){

if(mouth_trigger_event == true)
{
   if(counter > 2){mouth_trigger_event = false; counter = 0;}

  GLMouth.onDraw(gl, counter);
  counter++;
 }
else{
 GLMouth.onDraw(gl, 0); 
  }
 }

I hope this is gives you some logic to play around with. You will still need to do all the standard openGL-ES things. Cheers.

share|improve this answer
    
Hey Justin, thx for your answer but i didn't got it work so i finished my project without OpenGL. –  besnep Aug 24 '13 at 15:13

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.