简体   繁体   English

在C中的opengl中绘制纹理

[英]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. 我尝试使用SOIL加载纹理并使用我创建的函数对其进行绘制,该函数获取纹理及其许多详细信息-然后进行绘制。

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. 我不希望这样-我希望它选择左下角的点并从该点开始绘制,就像在这幅画中一样: http : //postimg.org/image/kalv23v3p/ (原谅我糟糕的计算机绘画技巧),所以我可以-例如-制作一个HP杆,该杆可以在相同的X和Y位置保持不变并在相同的位置上保持不变。 The problem (according to my understanding of the functions) is in glTranslatef :P 问题(根据我对功能的理解)在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). filename实际上是纹理本身时(通常为GLuint)。 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... 我猜ww和hh是您的with和height参数...

With glTranslatef you move everything around if you use ww and hh for it's params. 如果使用ww和hh作为参数,则使用glTranslatef可以移动所有内容。 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. 现在,如果要调整大小,请不要使用glTranslatef。 Use glScalef to resize. 使用glScalef调整大小。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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