From f73c1eff109a377856bc6a1d53f3dc280c82b9ab Mon Sep 17 00:00:00 2001 From: Sylvain Date: Wed, 17 Mar 2021 09:58:49 +0100 Subject: [PATCH] Use normalized texture coordinates --- include/SDL_render.h | 4 +--- src/render/SDL_render.c | 2 +- src/render/direct3d11/SDL_render_d3d11.c | 4 ++-- src/render/metal/SDL_render_metal.m | 10 +++------- src/render/opengl/SDL_render_gl.c | 4 ++-- src/render/opengles2/SDL_render_gles2.c | 4 ++-- src/render/software/SDL_render_sw.c | 4 ++-- 7 files changed, 13 insertions(+), 19 deletions(-) diff --git a/include/SDL_render.h b/include/SDL_render.h index 9d4be913a..797274c07 100644 --- a/include/SDL_render.h +++ b/include/SDL_render.h @@ -144,8 +144,7 @@ typedef struct SDL_Vertex { SDL_FPoint position; /**< Vertex position, in SDL_Renderer coordinates */ SDL_Color color; /**< Vertex color */ - SDL_FPoint tex_coord; /**< Texture coordinates (0..texture width, 0..texture height), - if needed */ + SDL_FPoint tex_coord; /**< Normalized texture coordinates, if needed */ } SDL_Vertex; @@ -1472,7 +1471,6 @@ extern DECLSPEC int SDLCALL SDL_RenderGeometry(SDL_Renderer *renderer, SDL_Vertex *vertices, int num_vertices, int *indices, int num_indices); - /** * Read pixels from the current rendering target to an array of pixels. * diff --git a/src/render/SDL_render.c b/src/render/SDL_render.c index e7aa73fcb..746bdf04d 100644 --- a/src/render/SDL_render.c +++ b/src/render/SDL_render.c @@ -3369,7 +3369,7 @@ SDL_RenderGeometry(SDL_Renderer *renderer, if (texture) { for (i = 0; i < num_vertices; ++i) { - if (vertices[i].tex_coord.x < 0 || vertices[i].tex_coord.y < 0 || vertices[i].tex_coord.x >= texture->w || vertices[i].tex_coord.y >= texture->h) { + if (vertices[i].tex_coord.x < 0.0f || vertices[i].tex_coord.y < 0.0f || vertices[i].tex_coord.x > 1.0f || vertices[i].tex_coord.y > 1.0f) { return SDL_SetError("Values of 'vertices' out of bounds"); } } diff --git a/src/render/direct3d11/SDL_render_d3d11.c b/src/render/direct3d11/SDL_render_d3d11.c index cdb3c402f..f85a1cad7 100644 --- a/src/render/direct3d11/SDL_render_d3d11.c +++ b/src/render/direct3d11/SDL_render_d3d11.c @@ -1891,8 +1891,8 @@ D3D11_QueueGeometry(SDL_Renderer *renderer, SDL_RenderCommand *cmd, SDL_Texture verts->color.w = v->color.a / 255.0f; if (texture) { - verts->tex.x = v->tex_coord.x / texture->w; - verts->tex.y = v->tex_coord.y / texture->h; + verts->tex.x = v->tex_coord.x; + verts->tex.y = v->tex_coord.y; } else { verts->tex.x = 0.0f; verts->tex.y = 0.0f; diff --git a/src/render/metal/SDL_render_metal.m b/src/render/metal/SDL_render_metal.m index dc61997ae..eb3129b17 100644 --- a/src/render/metal/SDL_render_metal.m +++ b/src/render/metal/SDL_render_metal.m @@ -1381,12 +1381,8 @@ static int METAL_QueueGeometry(SDL_Renderer *renderer, SDL_RenderCommand *cmd, SDL_Texture *texture, SDL_Vertex *vertices, int num_vertices, int *indices, int num_indices, float scale_x, float scale_y) { - const float texw = (float) (texture ? texture->w : 0); - const float texh = (float) (texture ? texture->h : 0); int count = indices ? num_indices : num_vertices; - int i; int sz = 2 + 4 + (texture ? 2 : 0); - const size_t vertlen = sizeof (float) * sz * count; float *verts = (float *) SDL_AllocateRenderVertices(renderer, vertlen, DEVICE_ALIGN(8), &cmd->data.draw.first); if (!verts) { @@ -1395,7 +1391,7 @@ METAL_QueueGeometry(SDL_Renderer *renderer, SDL_RenderCommand *cmd, SDL_Texture cmd->data.draw.count = count; - for (i = 0; i < count; i++) { + for (int i = 0; i < count; i++) { SDL_Vertex *v = &vertices[indices ? indices[i] : i]; *(verts++) = v->position.x * scale_x; @@ -1407,8 +1403,8 @@ METAL_QueueGeometry(SDL_Renderer *renderer, SDL_RenderCommand *cmd, SDL_Texture *(verts++) = v->color.a * inv255f; if (texture) { - *(verts++) = normtex(v->tex_coord.x, texw); - *(verts++) = normtex(v->tex_coord.y, texh); + *(verts++) = v->tex_coord.x; + *(verts++) = v->tex_coord.y; } } diff --git a/src/render/opengl/SDL_render_gl.c b/src/render/opengl/SDL_render_gl.c index c048485aa..cd531dc28 100644 --- a/src/render/opengl/SDL_render_gl.c +++ b/src/render/opengl/SDL_render_gl.c @@ -1085,8 +1085,8 @@ GL_QueueGeometry(SDL_Renderer *renderer, SDL_RenderCommand *cmd, SDL_Texture *te *(verts++) = v->color.a * inv255f; if (texture) { - *(verts++) = (v->tex_coord.x / texture->w) * texturedata->texw; - *(verts++) = (v->tex_coord.y / texture->h) * texturedata->texh; + *(verts++) = v->tex_coord.x * texturedata->texw; + *(verts++) = v->tex_coord.y * texturedata->texh; } } return 0; diff --git a/src/render/opengles2/SDL_render_gles2.c b/src/render/opengles2/SDL_render_gles2.c index 7cf67b913..775c0f4a8 100644 --- a/src/render/opengles2/SDL_render_gles2.c +++ b/src/render/opengles2/SDL_render_gles2.c @@ -986,8 +986,8 @@ GLES2_QueueGeometry(SDL_Renderer *renderer, SDL_RenderCommand *cmd, SDL_Texture *(verts++) = v->color.a * inv255f; if (texture) { - *(verts++) = v->tex_coord.x / texture->w; - *(verts++) = v->tex_coord.y / texture->h; + *(verts++) = v->tex_coord.x; + *(verts++) = v->tex_coord.y; } } return 0; diff --git a/src/render/software/SDL_render_sw.c b/src/render/software/SDL_render_sw.c index 9d73ba976..fbc948ecf 100644 --- a/src/render/software/SDL_render_sw.c +++ b/src/render/software/SDL_render_sw.c @@ -596,8 +596,8 @@ SW_QueueGeometry(SDL_Renderer *renderer, SDL_RenderCommand *cmd, SDL_Texture *te for (i = 0; i < count; i++) { SDL_Vertex *v = &vertices[indices ? indices[i] : i]; - ptr->src.x = v->tex_coord.x; - ptr->src.y = v->tex_coord.y; + ptr->src.x = v->tex_coord.x * texture->w; + ptr->src.y = v->tex_coord.y * texture->h; ptr->dst.x = v->position.x * scale_x + renderer->viewport.x; ptr->dst.y = v->position.y * scale_y + renderer->viewport.y;