Sign in to follow this  
39ster

Drawing texture on an angle

Recommended Posts

39ster    145
Im making a 2d rendering display and i can draw sprites (of any size) but i want to be able to draw them at an angle. Heres the code i got:
void GDisplay::draw_sprite(GSprite*sprite, int image, int left, int top, int width, int height, int x, int y, float xscale, float yscale, int rot)
{
    //adjust position to view and origin
    x -= (viewx + sprite->xorig);
    y -= (viewy + sprite->yorig);
    float w2 = width*xscale, h2 = height*yscale;
    if(x + w2 < 0)return;
    if(y + h2 < 0)return;
    if(x > w)return;    //if x > screen width
    if(y > h)return;    //if y > screen height
    if(image >= sprite->subimages)return;
    //Calculate texture co-ordinates
    GLfloat tw = (GLfloat)(width+left)/sprite->texture->width, th = (GLfloat)(height+top)/sprite->texture->height;
    GLfloat tx = (GLfloat)left/sprite->texture->width, ty = (GLfloat)top/sprite->texture->height;
    glBindTexture( GL_TEXTURE_2D, sprite->texture[image].textureId) ;

    glBegin(GL_QUADS) ;
        glTexCoord2f(tx, ty); glVertex3f(x, y, 0);
        glTexCoord2f(tw,ty); glVertex3f(x+w2, y,0);
        glTexCoord2f(tw, th); glVertex3f(x+w2, y+h2, 0);
        glTexCoord2f(tx, th); glVertex3f(x, y+h2, 0);
    glEnd();
}
HINT: sprite->width and sprite->height are the size of the sprite and sprite->texture->width and sprite->texture->height are the size of the sprite to the nearest power fo 2 I think i need to use glVertex to draw a rectangle on an angle but not sure how. I also tried using glTranslate to position the sprite and glRotate to rotate but my fps goes from 2000 to 800.

Share this post


Link to post
Share on other sites
39ster    145
Quote:
Original post by relsoft
How did you use glRotate? I doubt that it would be that slow.

Well im drawing over a 1000 sprites. I get 2000 fps using the code above and 800 when i use glRotate, glTranslate and glLoadIdentity. Heres the other way i did that slowed it down


void GDisplay::draw_sprite(GSprite*sprite, int image, int left, int top, int width, int height, int x, int y, float xscale, float yscale, int rot)
{
float w2 = width*xscale, h2 = height*yscale;
if(x + w2 < 0)return;
if(y + h2 < 0)return;
if(x > w)return; //if x > screen width
if(y > h)return; //if y > screen height
if(image >= sprite->subimages)return;
//Calculate co-ordinates
GLfloat tw = (GLfloat)(width+left)/sprite->texture->width, th = (GLfloat)(height+top)/sprite->texture->height;
GLfloat tx = (GLfloat)left/sprite->texture->width, ty = (GLfloat)top/sprite->texture->height;
glBindTexture( GL_TEXTURE_2D, sprite->texture[image].textureId) ;
glTranslatef(x, y, 0);
glRotatef(rot, 0, 0, 1);
glBegin(GL_QUADS) ;
glTexCoord2f(tx, ty); glVertex3f(0, 0, 0);
glTexCoord2f(tw,ty); glVertex3f(w2, 0,0);
glTexCoord2f(tw, th); glVertex3f(w2, h2, 0);
glTexCoord2f(tx, th); glVertex3f(0, h2, 0);
glEnd();
glLoadIdentity();
}
This does what i want it to but makes it alot slower.

Share this post


Link to post
Share on other sites
relsoft    255
Did you try to glPush/glPop combo instead of glLoadIdentity?

Like:

glPushMatrix();
glTranslatef(x, y, 0);
glRotatef(rot, 0, 0, 1);
// draw quad here...

glPopMatrix();

Or you could like:
Make a 2d rotation matrix then substitute that to glRotatef(rot, 0, 0, 1)
sa = sin(angle)
ca = sin(angle)

ca -sa 0 0 ---> x'= ca * x - sa * y
sa ca 0 0 ---> y'= sa * x + ca * y
0 0 1 0 ---> z'= z
0 0 0 1


Then glMulmatrixf. See if that helps...

Share this post


Link to post
Share on other sites
39ster    145
Ok now how do i go about making it rotate around a certain position in a sprite. At the moment it rotates around the top left hand corner (0, 0). I have two variables in sprite called xorig and yorig and i want it to rotate around that origin (eg, rotate around the centre of the sprite if xorig and yorig were half the width and height).
Heres the code i got but its not rotating around the centre.
    glPushMatrix();
glTranslatef(x, y, 0);
glRotatef(360-rot, 0, 0, 1);
glBegin(GL_QUADS) ;
glTexCoord2f(tx, ty); glVertex3f(-sprite->xorig, -sprite->yorig, 0);
glTexCoord2f(tw,ty); glVertex3f(w2-sprite->xorig, -sprite->yorig,0);
glTexCoord2f(tw, th); glVertex3f(w2-sprite->xorig, h2-sprite->yorig, 0);
glTexCoord2f(tx, th); glVertex3f(-sprite->xorig, h2-sprite->yorig, 0);
glEnd();
glPopMatrix();

Share this post


Link to post
Share on other sites
relsoft    255
Something like this should work (untested)
Note:
TW = Texturewidth in pixels
TH = Textureheight in pixels

TexHeight and TexWidth are texcoords from 0 to 1.0f (your tx and tw)


int HalfX = TexWidthi / 2;
int HalfY = TexHeighi / 2;

glTexCoord2f(tx, ty)
glPushMatrix()
glTranslatef( spritex, spritey, 0.0 )
glRotatef( angle, 0.0, 0.0, 1.0 )
glBegin( GL_TRIANGLE_STRIP )
glTexCoord2f( 0.0 , 0.0 ) : glVertex2i( -HalfX, -HalfY )
glTexCoord2f( 0.0 , TexHeight ) : glVertex2i( -HalfX, HalfY )
glTexCoord2f( TexWidth , 0.0 ) : glVertex2i( HalfX, -HalfY )
glTexCoord2f( TexWidth , TexHeight ) : glVertex2i( HalfX, HalfY )
glEnd()
glPopMatrix()





Share this post


Link to post
Share on other sites
_the_phantom_    11250
You do of course realise that a change from 2000fps to 800fps is only an increase in rendering time from 0.0005 seconds to 0.00125 second per frame, which isn't remotely significant, certainly considering to render at only 60 fps your drawing time can be as high as 0.016 seconds per frame.

Share this post


Link to post
Share on other sites
39ster    145
Quote:
Original post by relsoft
Something like this should work (untested)
Note:
TW = Texturewidth in pixels
TH = Textureheight in pixels

TexHeight and TexWidth are texcoords from 0 to 1.0f (your tx and tw)

*** Source Snippet Removed ***


Nope that didnt work (nothing displayed at all). I think thats equivelant to what i posted above...? (except the texcoord)

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this