Use normalized texture coordinates

This commit is contained in:
Sylvain 2021-03-17 09:58:49 +01:00 committed by Sylvain Becker
parent 5828cc415a
commit f73c1eff10
7 changed files with 13 additions and 19 deletions

View file

@ -144,8 +144,7 @@ typedef struct SDL_Vertex
{ {
SDL_FPoint position; /**< Vertex position, in SDL_Renderer coordinates */ SDL_FPoint position; /**< Vertex position, in SDL_Renderer coordinates */
SDL_Color color; /**< Vertex color */ SDL_Color color; /**< Vertex color */
SDL_FPoint tex_coord; /**< Texture coordinates (0..texture width, 0..texture height), SDL_FPoint tex_coord; /**< Normalized texture coordinates, if needed */
if needed */
} SDL_Vertex; } SDL_Vertex;
@ -1472,7 +1471,6 @@ extern DECLSPEC int SDLCALL SDL_RenderGeometry(SDL_Renderer *renderer,
SDL_Vertex *vertices, int num_vertices, SDL_Vertex *vertices, int num_vertices,
int *indices, int num_indices); int *indices, int num_indices);
/** /**
* Read pixels from the current rendering target to an array of pixels. * Read pixels from the current rendering target to an array of pixels.
* *

View file

@ -3369,7 +3369,7 @@ SDL_RenderGeometry(SDL_Renderer *renderer,
if (texture) { if (texture) {
for (i = 0; i < num_vertices; ++i) { 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"); return SDL_SetError("Values of 'vertices' out of bounds");
} }
} }

View file

@ -1891,8 +1891,8 @@ D3D11_QueueGeometry(SDL_Renderer *renderer, SDL_RenderCommand *cmd, SDL_Texture
verts->color.w = v->color.a / 255.0f; verts->color.w = v->color.a / 255.0f;
if (texture) { if (texture) {
verts->tex.x = v->tex_coord.x / texture->w; verts->tex.x = v->tex_coord.x;
verts->tex.y = v->tex_coord.y / texture->h; verts->tex.y = v->tex_coord.y;
} else { } else {
verts->tex.x = 0.0f; verts->tex.x = 0.0f;
verts->tex.y = 0.0f; verts->tex.y = 0.0f;

View file

@ -1381,12 +1381,8 @@ static int
METAL_QueueGeometry(SDL_Renderer *renderer, SDL_RenderCommand *cmd, SDL_Texture *texture, 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) 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 count = indices ? num_indices : num_vertices;
int i;
int sz = 2 + 4 + (texture ? 2 : 0); int sz = 2 + 4 + (texture ? 2 : 0);
const size_t vertlen = sizeof (float) * sz * count; const size_t vertlen = sizeof (float) * sz * count;
float *verts = (float *) SDL_AllocateRenderVertices(renderer, vertlen, DEVICE_ALIGN(8), &cmd->data.draw.first); float *verts = (float *) SDL_AllocateRenderVertices(renderer, vertlen, DEVICE_ALIGN(8), &cmd->data.draw.first);
if (!verts) { if (!verts) {
@ -1395,7 +1391,7 @@ METAL_QueueGeometry(SDL_Renderer *renderer, SDL_RenderCommand *cmd, SDL_Texture
cmd->data.draw.count = count; 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]; SDL_Vertex *v = &vertices[indices ? indices[i] : i];
*(verts++) = v->position.x * scale_x; *(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; *(verts++) = v->color.a * inv255f;
if (texture) { if (texture) {
*(verts++) = normtex(v->tex_coord.x, texw); *(verts++) = v->tex_coord.x;
*(verts++) = normtex(v->tex_coord.y, texh); *(verts++) = v->tex_coord.y;
} }
} }

View file

@ -1085,8 +1085,8 @@ GL_QueueGeometry(SDL_Renderer *renderer, SDL_RenderCommand *cmd, SDL_Texture *te
*(verts++) = v->color.a * inv255f; *(verts++) = v->color.a * inv255f;
if (texture) { if (texture) {
*(verts++) = (v->tex_coord.x / texture->w) * texturedata->texw; *(verts++) = v->tex_coord.x * texturedata->texw;
*(verts++) = (v->tex_coord.y / texture->h) * texturedata->texh; *(verts++) = v->tex_coord.y * texturedata->texh;
} }
} }
return 0; return 0;

View file

@ -986,8 +986,8 @@ GLES2_QueueGeometry(SDL_Renderer *renderer, SDL_RenderCommand *cmd, SDL_Texture
*(verts++) = v->color.a * inv255f; *(verts++) = v->color.a * inv255f;
if (texture) { if (texture) {
*(verts++) = v->tex_coord.x / texture->w; *(verts++) = v->tex_coord.x;
*(verts++) = v->tex_coord.y / texture->h; *(verts++) = v->tex_coord.y;
} }
} }
return 0; return 0;

View file

@ -596,8 +596,8 @@ SW_QueueGeometry(SDL_Renderer *renderer, SDL_RenderCommand *cmd, SDL_Texture *te
for (i = 0; i < count; i++) { for (i = 0; i < count; i++) {
SDL_Vertex *v = &vertices[indices ? indices[i] : i]; SDL_Vertex *v = &vertices[indices ? indices[i] : i];
ptr->src.x = v->tex_coord.x; ptr->src.x = v->tex_coord.x * texture->w;
ptr->src.y = v->tex_coord.y; ptr->src.y = v->tex_coord.y * texture->h;
ptr->dst.x = v->position.x * scale_x + renderer->viewport.x; ptr->dst.x = v->position.x * scale_x + renderer->viewport.x;
ptr->dst.y = v->position.y * scale_y + renderer->viewport.y; ptr->dst.y = v->position.y * scale_y + renderer->viewport.y;