简体   繁体   中英

Drawing a texture in opengl in C

I tried loading a texture with SOIL and drawing it with a function I created which gets a texture and many details of it - and then draws it.

The problem is - that when I draw it - the location of the picture changes when the width and height changes. I want it not to - I want it to choose the left bottom point and draw from there as in this painting : http://postimg.org/image/kalv23v3p/ (forgive me for my lousy computer painting skill) so I can - for example - make an HP bar that can decrease and increase with the same X and Y and stay the same position. The problem (according to my understanding of the functions) is in glTranslatef :P

 void DrawImage(char filename,
                       int xx,
                       int yy,
                       int ww,
                       int hh,
                       int angle,
                       int xp,
                       int yp) 
{
  glEnable(GL_TEXTURE_2D);
  glBindTexture(GL_TEXTURE_2D, filename);

  glLoadIdentity();
  glTranslatef(xx + (ww / 2),yy + (hh / 2),0.0);
  glRotatef(angle,0.0,0.0,1.0);
  glTranslatef(-(xx + (ww / 2) + xp),-(yy + (hh / 2) + yp),0.0);

  // Draw a textured quad
  glBegin(GL_QUADS);
  glTexCoord2f(0, 0); glVertex2f(xx,yy);
  glTexCoord2f(0, 1); glVertex2f(xx,yy + hh);
  glTexCoord2f(1, 1); glVertex2f(xx + ww,yy + hh);
  glTexCoord2f(1, 0); glVertex2f(xx + ww,yy);

  glDisable(GL_TEXTURE_2D);
  glPopMatrix();

  glMatrixMode(GL_PROJECTION);
  glPopMatrix();

  glMatrixMode(GL_MODELVIEW);
  glEnd();
}

when the filename is actually the texture itself (GLuint usually). Basically the function is from here but I changed it so it'll get all of these parameters. Any idea how to solve my problem and make the code work how I want it to?

I guess ww and hh are your with and height params...

With glTranslatef you move everything around if you use ww and hh for it's params. Try to change

glTranslatef(xx + (ww / 2),yy + (hh / 2),0.0);

To

glTranslatef(xx, yy, 0.0);

and

glTranslatef(-(xx + (ww / 2) + xp),-(yy + (hh / 2) + yp),0.0);

To

glTranslatef(-(xx + xp),-(yy + yp),0.0);

Now if you want to resize don't use glTranslatef. Use glScalef to resize.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM