[android-developers] Re: Rotate bitmap in opengl

2010-11-03 Thread Jason
Hmm ok.. I think I may have overlooked a key piece of data here..
namely the structure of the matrix being rotated.

I use a simple grid to represent most of my 2D shapes, but I assign
the coordinates of the grid such that when it is positioned at 0,0 it
is the center of the grid that is at these coordinates.  This means I
don't need to translate to the half-values you mention.  I'm guessing
that your grid (matrix) has all positive values for its coordinates,
whereas I have half the values as negatives which means my grid is
already "center aligned".

You may want to try the same as it will save the extra GL calls and if
you have a lot of objects this could become meaningful.



On Nov 4, 12:37 am, "Alistair."  wrote:
> Thanks Jason. One month later and I finally got it working. You were
> correct but it has taken a bit of time me to get my head around what
> was going on. You suggestion wouldrotatethe texture around the point
> at which it was drawn but I wanted torotateit around it's mid-point.
> I see now that the secret is to:
>
> - translate the origin to a draw point plus half the width and height.
> -rotate
> - translate back by half the width and height.
> - draw
>
> Here is my code. Note to anyone looking at this that I have just tried
> it out and I am sure it can be tidied up. Also most importantly I am
> adjusting the co-ordinate system to match that of canvas where the top
> left is (0,0). This is because by drawing primitives as set to work in
> Canvas as well.
>
>             surface.glMatrixMode(GL10.GL_MODELVIEW);
>
>             Grid.beginDrawing(surface, true, false);
>
>             surface.glBindTexture(GL10.GL_TEXTURE_2D, ids[index]);
>
>             surface.glLoadIdentity();
>
>             surface.glPushMatrix();
>
>             // Draw using the DrawTexture extension.
>             float drawWidth = texture.getDrawWidth();
>             float drawHeight = texture.getDrawHeight();
>             float ypos = screenHeight - drawHeight - y;
>
>             if (angle>0)
>             {
>                 float halfWidth = drawWidth/2;
>                 float halfHeight = drawHeight/2;
>
>                 surface.glTranslatef(x+halfWidth, ypos+halfHeight, 0);
>
>                 surface.glRotatef(-angle, 0.0f, 0.0f, 1.0f);
>
>                 surface.glTranslatef(-halfWidth, -halfHeight, 0);
>             }
>             else
>             {
>                 surface.glTranslatef(x, ypos, 0);
>             }
>
>             texture.getGrid().draw(surface, true, true);
>
>             surface.glPopMatrix();
>
>             Grid.endDrawing(surface);
>
>             surface.glDisable(GL10.GL_TEXTURE_2D);
>
> Al.
>
> On Oct 15, 3:37 pm, Jason  wrote:
>
>
>
>
>
>
>
> > You want to call glLoadIdentity before anything else, and remember
> > that the commands are on a stack, so are processed in "reverse" order.
>
> > The glLoadIdentity will reset everything, so your matrix will be at
> > 0,0.  This means you draw,rotate, then translate; but you push the
> > commands to do this onto the stack in reverse order.
>
> > Here's what I do:
>
> > // Load the identity matrix so we always start from 0,0
> > gl.glLoadIdentity();
>
> > // push a copy of the matrix on the stack to manipulate
> > gl.glPushMatrix();
>
> > // translate to target coordinates.
> > gl.glTranslatef(x, y, 0.0f);
>
> > // Scale if needed
> > // gl.glScalef(scaleX, scaleY, 1.0f);
>
> > //Rotate(must be in degrees)
> > gl.glRotatef((float)Math.toDegrees(angle), 0, 0, 1.0f);
>
> > // push the draw command last
> > grid.draw(gl);
>
> > // finally pop the matrix
> > gl.glPopMatrix();
>
> > There is NO need to re-translate back anywhere.. and certainly no need
> > to perform an additional rotation as suggested.
>
> > On Oct 15, 8:42šam, Kostya Vasilyev  wrote:
>
> > > Add anotherrotatecall before the non-working translate, with the inverse
> > > angle (in your case, the value in 'angle' without the '-').
>
> > > This will reverse the rotation so the translate call can undo the
> > > translation.
>
> > > --
> > > Kostya Vasilyev --http://kmansoft.wordpress.com
>
> > > 15.10.2010 1:30 ÐÏÌØÚÏ×ÁÔÅÌØ "Alistair." 
> > > ÎÁÐÉÓÁÌ:
>
> > > Alas, that had no effect. I have _something_ sort of working
>
> > > š š š š š šsurface.glMatrixMode(GL10.GL_MODELVIEW);
>
> > > surface.glEnable(GL10.GL_TEXTURE_2D);
>
> > > surface.glBindTexture(GL10.GL_TEXTUR...
> > > š š š š š šGrid.beginDrawing(surface, true, false);
>
> > > š š š š š šsurface.glPushMatrix();
>
> > > // Draw using the DrawTexture extension.
> > > š š š š š šint drawHeight = texture.getDrawHeight();
> > > š š š š š šint drawWidth = texture.getDrawWidth();
> > > š š š š š šfloat ypos = screenHeight - drawHeight- y;
>
> > > š š š š š šsurface.glLoadIdentity();
>
> > > š š š š š šsurface.glTranslatef(x, ypos, 0);
>
> > > surface.glRotatef(-angle, 0.0f, 0.0f, 1.0f);
> > > š š š š š šsurface.glTranslatef(-x, -ypos, 0); // THIS NOT DOING WHAT
> > > I THOUGHT WOULD
>
> > > š š š š š štexture.getGrid(

[android-developers] Re: Rotate bitmap in opengl

2010-11-03 Thread Alistair.
Thanks Jason. One month later and I finally got it working. You were
correct but it has taken a bit of time me to get my head around what
was going on. You suggestion would rotate the texture around the point
at which it was drawn but I wanted to rotate it around it's mid-point.
I see now that the secret is to:

- translate the origin to a draw point plus half the width and height.
- rotate
- translate back by half the width and height.
- draw

Here is my code. Note to anyone looking at this that I have just tried
it out and I am sure it can be tidied up. Also most importantly I am
adjusting the co-ordinate system to match that of canvas where the top
left is (0,0). This is because by drawing primitives as set to work in
Canvas as well.

surface.glMatrixMode(GL10.GL_MODELVIEW);

Grid.beginDrawing(surface, true, false);

surface.glBindTexture(GL10.GL_TEXTURE_2D, ids[index]);

surface.glLoadIdentity();

surface.glPushMatrix();

// Draw using the DrawTexture extension.
float drawWidth = texture.getDrawWidth();
float drawHeight = texture.getDrawHeight();
float ypos = screenHeight - drawHeight - y;

if (angle>0)
{
float halfWidth = drawWidth/2;
float halfHeight = drawHeight/2;

surface.glTranslatef(x+halfWidth, ypos+halfHeight, 0);

surface.glRotatef(-angle, 0.0f, 0.0f, 1.0f);

surface.glTranslatef(-halfWidth, -halfHeight, 0);
}
else
{
surface.glTranslatef(x, ypos, 0);
}

texture.getGrid().draw(surface, true, true);

surface.glPopMatrix();

Grid.endDrawing(surface);

surface.glDisable(GL10.GL_TEXTURE_2D);

Al.

On Oct 15, 3:37 pm, Jason  wrote:
> You want to call glLoadIdentity before anything else, and remember
> that the commands are on a stack, so are processed in "reverse" order.
>
> The glLoadIdentity will reset everything, so your matrix will be at
> 0,0.  This means you draw, rotate, then translate; but you push the
> commands to do this onto the stack in reverse order.
>
> Here's what I do:
>
> // Load the identity matrix so we always start from 0,0
> gl.glLoadIdentity();
>
> // push a copy of the matrix on the stack to manipulate
> gl.glPushMatrix();
>
> // translate to target coordinates.
> gl.glTranslatef(x, y, 0.0f);
>
> // Scale if needed
> // gl.glScalef(scaleX, scaleY, 1.0f);
>
> // Rotate (must be in degrees)
> gl.glRotatef((float)Math.toDegrees(angle), 0, 0, 1.0f);
>
> // push the draw command last
> grid.draw(gl);
>
> // finally pop the matrix
> gl.glPopMatrix();
>
> There is NO need to re-translate back anywhere.. and certainly no need
> to perform an additional rotation as suggested.
>
> On Oct 15, 8:42šam, Kostya Vasilyev  wrote:
>
>
>
>
>
>
>
> > Add another rotate call before the non-working translate, with the inverse
> > angle (in your case, the value in 'angle' without the '-').
>
> > This will reverse the rotation so the translate call can undo the
> > translation.
>
> > --
> > Kostya Vasilyev --http://kmansoft.wordpress.com
>
> > 15.10.2010 1:30 ÐÏÌØÚÏ×ÁÔÅÌØ "Alistair." 
> > ÎÁÐÉÓÁÌ:
>
> > Alas, that had no effect. I have _something_ sort of working
>
> > š š š š š šsurface.glMatrixMode(GL10.GL_MODELVIEW);
>
> > surface.glEnable(GL10.GL_TEXTURE_2D);
>
> > surface.glBindTexture(GL10.GL_TEXTUR...
> > š š š š š šGrid.beginDrawing(surface, true, false);
>
> > š š š š š šsurface.glPushMatrix();
>
> > // Draw using the DrawTexture extension.
> > š š š š š šint drawHeight = texture.getDrawHeight();
> > š š š š š šint drawWidth = texture.getDrawWidth();
> > š š š š š šfloat ypos = screenHeight - drawHeight- y;
>
> > š š š š š šsurface.glLoadIdentity();
>
> > š š š š š šsurface.glTranslatef(x, ypos, 0);
>
> > surface.glRotatef(-angle, 0.0f, 0.0f, 1.0f);
> > š š š š š šsurface.glTranslatef(-x, -ypos, 0); // THIS NOT DOING WHAT
> > I THOUGHT WOULD
>
> > š š š š š štexture.getGrid().draw(surface, true, false);
>
> > š š š š š šsurface.glPopMatrix();
>
> > š š š š š šGrid.endDrawing(surface);
>
> > surface.glDisable(GL10.GL_TEXTURE_2D);
> > What I can't seem to get working now it the re-translate back to the
> > original point after the rotation.
>
> > On Oct 12, 6:02 am, Kunal Kant  wrote:
>
> > > remove that line " surface.glLoad...
> > > wrote:
>
> > > > I am trying to rotate a bitmap in OpenGL. I have searched around and
> > > > come up w...
> > > > android-developers+unsubscr...@googlegroups.com > > >  cr...@googlegroups.com>
> > cr...@googlegroups.com>
>
> > > > For more options, visit this group at
> > > >http://groups.google.com/group/android-developers?hl=en...

-- 
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, 

[android-developers] Re: Rotate bitmap in opengl

2010-10-15 Thread Jason
You want to call glLoadIdentity before anything else, and remember
that the commands are on a stack, so are processed in "reverse" order.

The glLoadIdentity will reset everything, so your matrix will be at
0,0.  This means you draw, rotate, then translate; but you push the
commands to do this onto the stack in reverse order.

Here's what I do:

// Load the identity matrix so we always start from 0,0
gl.glLoadIdentity();

// push a copy of the matrix on the stack to manipulate
gl.glPushMatrix();

// translate to target coordinates.
gl.glTranslatef(x, y, 0.0f);

// Scale if needed
// gl.glScalef(scaleX, scaleY, 1.0f);

// Rotate (must be in degrees)
gl.glRotatef((float)Math.toDegrees(angle), 0, 0, 1.0f);

// push the draw command last
grid.draw(gl);

// finally pop the matrix
gl.glPopMatrix();

There is NO need to re-translate back anywhere.. and certainly no need
to perform an additional rotation as suggested.

On Oct 15, 8:42 am, Kostya Vasilyev  wrote:
> Add another rotate call before the non-working translate, with the inverse
> angle (in your case, the value in 'angle' without the '-').
>
> This will reverse the rotation so the translate call can undo the
> translation.
>
> --
> Kostya Vasilyev --http://kmansoft.wordpress.com
>
> 15.10.2010 1:30 пользователь "Alistair." 
> написал:
>
> Alas, that had no effect. I have _something_ sort of working
>
>            surface.glMatrixMode(GL10.GL_MODELVIEW);
>
> surface.glEnable(GL10.GL_TEXTURE_2D);
>
> surface.glBindTexture(GL10.GL_TEXTUR...
>            Grid.beginDrawing(surface, true, false);
>
>            surface.glPushMatrix();
>
> // Draw using the DrawTexture extension.
>            int drawHeight = texture.getDrawHeight();
>            int drawWidth = texture.getDrawWidth();
>            float ypos = screenHeight - drawHeight- y;
>
>            surface.glLoadIdentity();
>
>            surface.glTranslatef(x, ypos, 0);
>
> surface.glRotatef(-angle, 0.0f, 0.0f, 1.0f);
>            surface.glTranslatef(-x, -ypos, 0); // THIS NOT DOING WHAT
> I THOUGHT WOULD
>
>            texture.getGrid().draw(surface, true, false);
>
>            surface.glPopMatrix();
>
>            Grid.endDrawing(surface);
>
> surface.glDisable(GL10.GL_TEXTURE_2D);
> What I can't seem to get working now it the re-translate back to the
> original point after the rotation.
>
> On Oct 12, 6:02 am, Kunal Kant  wrote:
>
> > remove that line " surface.glLoad...
> > wrote:
>
> > > I am trying to rotate a bitmap in OpenGL. I have searched around and
> > > come up w...
> > > android-developers+unsubscr...@googlegroups.com > >  cr...@googlegroups.com>
> cr...@googlegroups.com>
>
>
>
> > > For more options, visit this group at
> > >http://groups.google.com/group/android-developers?hl=en...

-- 
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en


Re: [android-developers] Re: Rotate bitmap in opengl

2010-10-14 Thread Kostya Vasilyev
Add another rotate call before the non-working translate, with the inverse
angle (in your case, the value in 'angle' without the '-').

This will reverse the rotation so the translate call can undo the
translation.

--
Kostya Vasilyev -- http://kmansoft.wordpress.com

15.10.2010 1:30 пользователь "Alistair." 
написал:

Alas, that had no effect. I have _something_ sort of working


   surface.glMatrixMode(GL10.GL_MODELVIEW);


surface.glEnable(GL10.GL_TEXTURE_2D);

surface.glBindTexture(GL10.GL_TEXTUR...
   Grid.beginDrawing(surface, true, false);

   surface.glPushMatrix();


// Draw using the DrawTexture extension.
   int drawHeight = texture.getDrawHeight();
   int drawWidth = texture.getDrawWidth();
   float ypos = screenHeight - drawHeight- y;

   surface.glLoadIdentity();

   surface.glTranslatef(x, ypos, 0);


surface.glRotatef(-angle, 0.0f, 0.0f, 1.0f);
   surface.glTranslatef(-x, -ypos, 0); // THIS NOT DOING WHAT
I THOUGHT WOULD

   texture.getGrid().draw(surface, true, false);

   surface.glPopMatrix();

   Grid.endDrawing(surface);


surface.glDisable(GL10.GL_TEXTURE_2D);
What I can't seem to get working now it the re-translate back to the
original point after the rotation.


On Oct 12, 6:02 am, Kunal Kant  wrote:
> remove that line " surface.glLoad...
> wrote:

>
>
>
>
>
>
>
> > I am trying to rotate a bitmap in OpenGL. I have searched around and
> > come up w...
> > android-developers+unsubscr...@googlegroups.com

> > For more options, visit this group at
> >http://groups.google.com/group/android-developers?hl=en...

-- 
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

[android-developers] Re: Rotate bitmap in opengl

2010-10-14 Thread Alistair.
Alas, that had no effect. I have _something_ sort of working


surface.glMatrixMode(GL10.GL_MODELVIEW);

surface.glEnable(GL10.GL_TEXTURE_2D);

surface.glBindTexture(GL10.GL_TEXTURE_2D, ids[index]);

Grid.beginDrawing(surface, true, false);

surface.glPushMatrix();

// Draw using the DrawTexture extension.
int drawHeight = texture.getDrawHeight();
int drawWidth = texture.getDrawWidth();
float ypos = screenHeight - drawHeight- y;

surface.glLoadIdentity();

surface.glTranslatef(x, ypos, 0);

surface.glRotatef(-angle, 0.0f, 0.0f, 1.0f);

surface.glTranslatef(-x, -ypos, 0); // THIS NOT DOING WHAT
I THOUGHT WOULD

texture.getGrid().draw(surface, true, false);

surface.glPopMatrix();

Grid.endDrawing(surface);

surface.glDisable(GL10.GL_TEXTURE_2D);

What I can't seem to get working now it the re-translate back to the
original point after the rotation.

On Oct 12, 6:02 am, Kunal Kant  wrote:
> remove that line " surface.glLoadIdentity();"
> i think it will rotate
>
> On Sun, Oct 10, 2010 at 10:46 PM, Alistair.
> wrote:
>
>
>
>
>
>
>
> > I am trying to rotate a bitmap in OpenGL. I have searched around and
> > come up with this
>
> >    public void drawTexture(Texture texture, int index, float x, float
> > y, float angle)
> >    {
> >        int[] ids = texture.getTextureIds();
>
> >        if (ids != null)
> >        {
> >            surface.glEnable(GL10.GL_TEXTURE_2D);
>
> >            surface.glBindTexture(GL10.GL_TEXTURE_2D, ids[index]);
>
> >            // Draw using the DrawTexture extension.
> >            int drawWidth = texture.getDrawWidth();
> >            int drawHeight = texture.getDrawHeight();
>
> >            surface.glPushMatrix();
>
> >            surface.glLoadIdentity();
>
> >            surface.glRotatef(angle, 0.0f, 0.0f, 1.0f);
>
> >            ((GL11Ext) surface).glDrawTexfOES(x, screenHeight -
> > drawHeight - y, 0, drawWidth, drawHeight);
>
> >            surface.glPopMatrix();
>
> >            surface.glDisable(GL10.GL_TEXTURE_2D);
>
> >        }
> >    }
>
> > Drawing the bitmaps indicated with the "ids" works fine but no
> > rotation happens. I am no expert at openGL. Is is possible I need to
> > set some sort of mode prior to the rotation?
>
> > --
> > You received this message because you are subscribed to the Google
> > Groups "Android Developers" group.
> > To post to this group, send email to android-developers@googlegroups.com
> > To unsubscribe from this group, send email to
> > android-developers+unsubscr...@googlegroups.com > cr...@googlegroups.com>
> > For more options, visit this group at
> >http://groups.google.com/group/android-developers?hl=en

-- 
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en


[android-developers] Re: Rotate bitmap in opengl

2010-10-13 Thread Alistair.
After a great deal of searching I have discovered that you cannot use
glRotate and glDrawTexfOES. I will have to use the quad grid from the
SpriteMethodTest in apps-for-android.


On Oct 10, 6:16 pm, "Alistair."  wrote:
> I am trying to rotate a bitmap in OpenGL. I have searched around and
> come up with this
>
>     public void drawTexture(Texture texture, int index, float x, float
> y, float angle)
>     {
>         int[] ids = texture.getTextureIds();
>
>         if (ids != null)
>         {
>             surface.glEnable(GL10.GL_TEXTURE_2D);
>
>             surface.glBindTexture(GL10.GL_TEXTURE_2D, ids[index]);
>
>             // Draw using the DrawTexture extension.
>             int drawWidth = texture.getDrawWidth();
>             int drawHeight = texture.getDrawHeight();
>
>             surface.glPushMatrix();
>
>             surface.glLoadIdentity();
>
>             surface.glRotatef(angle, 0.0f, 0.0f, 1.0f);
>
>             ((GL11Ext) surface).glDrawTexfOES(x, screenHeight -
> drawHeight - y, 0, drawWidth, drawHeight);
>
>             surface.glPopMatrix();
>
>             surface.glDisable(GL10.GL_TEXTURE_2D);
>
>         }
>     }
>
> Drawing the bitmaps indicated with the "ids" works fine but no
> rotation happens. I am no expert at openGL. Is is possible I need to
> set some sort of mode prior to the rotation?

-- 
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en