How can I display non-power-of-2-texture as a sprite in OpenGL (ES) without stretching?

I want to draw an arbitrary sprite size as png, say something TOTALLY CRAZY, like 56 wide x 30 high. It does not matter 2 in any dimension. In addition, I might want to draw another sprite that has 72 widths x 33. Having pointed this out because there are no "tricks" here, I need to handle the general case.

So, I have this png (with transparency) and I want to draw it as a sprite with absolutely no stretching, interpolation, etc. I want it to display 1: 1 with pixels on the screen. Pretend these sprites are pixel art (they may or may not be, but they are drawn for exact resolution).

I understand that the sprite drawing is 2 triangles as a quad, rendering my texture using texture coordinate matching - however, I understand this only for authority 2. And I also don’t understand how my “ATV” size is and adjust my GL matrices to I could make the sprite the same pixel size as my sprite

I use OpenGL ES, so using new extensions to use textures without power 2 is not acceptable.

I only draw 3-10 sprites at a time, so I am open to suggestions about efficiency, but I am most interested in the “accurate” results.

+3
source share
1 answer

To get the perfect coordinate system with pixels, you can set up your matrices as follows:

glViewport(0, window_width, 0, window_height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0, window_width, 0, window_height, 0, 1);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();

OpenGL 1:1 .

, , -, . , OpenGL ES , ( ), , Android , 512 x 128, .

, OpenGL 0 1, . , 48 , 64 x 64, x 0 0,75:

1 +--------+
  |        |
  |        |
  +-----+  |
  |\_O_/|  |
  |  O  |  |
  | / \ |  |
0 +-----+--+
  0   0.75 1   <- texture coordinates
  0     48 64  <- pixels

, . , 48 .

, x, y ( ), - :

float texcoord_x = (float)sprite_width / texture_width;
float texcoord_y = (float)sprite_height / texture_height;

glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, texture_id);

glBegin(GL_QUADS);
// bottom left
glTexCoord2f(0, 0);
glVertex2i(x, y);
// bottom right
glTexCoord2f(texcoord_x, 0);
glVertex2i(x + sprite_width, y);
// top right
glTexCoord2f(texcoord_x, texcoord_y);
glVertex2i(x + sprite_width, y + sprite_height);
// top left
glTexCoord2f(0, texcoord_y);
glVertex2i(x, y + sprite_height);
glEnd();

, OpenGL ES glVertex2i .., . , . .

+5

Source: https://habr.com/ru/post/1786937/


All Articles