# Am I using SDL\_RenderGeometry correctly?

**URL:** <https://discourse.libsdl.org/t/am-i-using-sdl-rendergeometry-correctly/32995>\
**Category:** SDL Development\
**Created:** [October 6, 2021, 1:48pm UTC](https://discourse.libsdl.org/t/am-i-using-sdl-rendergeometry-correctly/32995 "2021-10-06T13:48:25Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![thd79](https://discourse.libsdl.org/letter_avatar_proxy/v4/letter/t/e99b99/32.png) [@thd79](https://discourse.libsdl.org/u/thd79)\
**Post date:** [October 6, 2021, 1:48pm UTC](https://discourse.libsdl.org/t/am-i-using-sdl-rendergeometry-correctly/32995/1 "2021-10-06T13:48:25Z")

</div>

Using SDL2 built from main on 6/10/21 to test out the new SDL\_RenderGeometry. Am I doing something wrong? I’m trying to render a quad with a texture. If I render a rectangle as below, everything works as I expect:

```auto
SDL_Color col{ 0xff,0xff,0xff,0xff };
	std::vector<SDL_Vertex> verticies{
		{{300.0f,200.0f},	col,	{0.0f,0.0f}},
		{{800.0f,200.0f},	col,	{1.0f,0.0f}},
		{{300.0f,700.0f},	col,	{0.0f,1.0f}},
		{{800.0f,700.0f},	col,	{1.0f,1.0f}},
	};
	std::array<int, 6> indexList = { 0,1,2,2,1,3 };
	SDL_RenderGeometry(mRenderer, texture, verticies.data(), (int)verticies.size(), indexList.data(), (int)indexList.size());

```

This gives me the following output:

[![](https://live.staticflickr.com/65535/51556672117_bea5706f0e.jpg)](https://flic.kr/p/2mxTqpX)

But if I change the verticies to draw a trapezium instead, like this:

```auto
SDL_Color col{ 0xff,0xff,0xff,0xff };
	std::vector<SDL_Vertex> verticies{
		{{500.0f,200.0f},	col,	{0.0f,0.0f}},
		{{700.0f,200.0f},	col,	{1.0f,0.0f}},
		{{300.0f,700.0f},	col,	{0.0f,1.0f}},
		{{800.0f,700.0f},	col,	{1.0f,1.0f}},
	};
	std::array<int, 6> indexList = { 0,1,2,2,1,3 };
	SDL_RenderGeometry(mRenderer, texture, verticies.data(), (int)verticies.size(), indexList.data(), (int)indexList.size());

```

Then the result is not what I expect. You can clearly see the two triangles being drawn with textures that don’t make sense together.

[![](https://live.staticflickr.com/65535/51557466156_116c15107c.jpg)](https://flic.kr/p/2mxXush)

I’m totally new to this sort of api, so my question is: Is this output correct (I assume it is)?. If so, is there a way to use the api in a way that will map the texture onto the quad, rather than seperately onto two triangles? I would expect (perhaps incorrectly) to end up with something like this:  
[![](https://live.staticflickr.com/65535/51557772038_345b40c093.jpg)](https://flic.kr/p/2mxZ4o7)

If something like this isn’t possible, what is possible using textures and RenderGeometry?Perhaps I have a fundamental misunderstanding of the api.

---

<div class="post-metadata">

**Author:** ![sjr](https://discourse.libsdl.org/user_avatar/discourse.libsdl.org/sjr/32/5749_2.png) [@sjr](https://discourse.libsdl.org/u/sjr)\
**Post date:** [October 7, 2021, 7:09am UTC](https://discourse.libsdl.org/t/am-i-using-sdl-rendergeometry-correctly/32995/2 "2021-10-07T07:09:58Z")

</div>

The result seems “correct” for each triangle.

---

<div class="post-metadata">

**Author:** ![Sylvain\_Becker](https://discourse.libsdl.org/letter_avatar_proxy/v4/letter/s/3ab097/32.png) [@Sylvain\_Becker](https://discourse.libsdl.org/u/Sylvain_Becker)\
**Post date:** [October 7, 2021, 8:47am UTC](https://discourse.libsdl.org/t/am-i-using-sdl-rendergeometry-correctly/32995/3 "2021-10-07T08:47:10Z")

</div>

Also think this is ok.  
you expect something that would be done by:

- using different z coordinates (between bottom and top vertices)
- and/or maybe transforming the stack  
Not sure, but maybe there was this patch would be helpfull … [https://github.com/libsdl-org/SDL/issues/1229](https://github.com/libsdl-org/SDL/issues/1229)

---

<div class="post-metadata">

**Author:** ![thd79](https://discourse.libsdl.org/letter_avatar_proxy/v4/letter/t/e99b99/32.png) [@thd79](https://discourse.libsdl.org/u/thd79)\
**Post date:** [October 7, 2021, 9:24am UTC](https://discourse.libsdl.org/t/am-i-using-sdl-rendergeometry-correctly/32995/4 "2021-10-07T09:24:42Z")

</div>

Guess this is what’s meant to happen then. Though I must admit, I’m struggling to see much use for textures in RenderGeometry, if it works like this?

---

<div class="post-metadata">

**Author:** ![thd79](https://discourse.libsdl.org/letter_avatar_proxy/v4/letter/t/e99b99/32.png) [@thd79](https://discourse.libsdl.org/u/thd79)\
**Post date:** [October 7, 2021, 9:25am UTC](https://discourse.libsdl.org/t/am-i-using-sdl-rendergeometry-correctly/32995/5 "2021-10-07T09:25:21Z")

</div>

Thanks for the suggestion. I’m not sure how to make use of that patch… I downloaded the diff file and tried to apply it with git, but it failed.

---

<div class="post-metadata">

**Author:** ![Sylvain\_Becker](https://discourse.libsdl.org/letter_avatar_proxy/v4/letter/s/3ab097/32.png) [@Sylvain\_Becker](https://discourse.libsdl.org/u/Sylvain_Becker)\
**Post date:** [October 7, 2021, 11:29am UTC](https://discourse.libsdl.org/t/am-i-using-sdl-rendergeometry-correctly/32995/6 "2021-10-07T11:29:43Z")

</div>

Rendering triangles allows more precise cuts for atlas textures, different shapes, plus you can have per vertex color modulation. you can also integrate with other libs that outputs triangles.

The diff is old, but you can see how it works and try to do something with it.

---

<div class="post-metadata">

**Author:** ![thd79](https://discourse.libsdl.org/letter_avatar_proxy/v4/letter/t/e99b99/32.png) [@thd79](https://discourse.libsdl.org/u/thd79)\
**Post date:** [October 7, 2021, 11:41am UTC](https://discourse.libsdl.org/t/am-i-using-sdl-rendergeometry-correctly/32995/7 "2021-10-07T11:41:18Z")

</div>

Yeah, I’m using the SDL\_RenderGeometry to draw filled and shaded shapes using per vertex shading, and it works really nicely. Not sure how to make any real life use of the texture feature, but I guess that’s my inexperience!

---

<div class="post-metadata">

**Author:** ![Sylvain\_Becker](https://discourse.libsdl.org/letter_avatar_proxy/v4/letter/s/3ab097/32.png) [@Sylvain\_Becker](https://discourse.libsdl.org/u/Sylvain_Becker)\
**Post date:** [October 7, 2021, 2:49pm UTC](https://discourse.libsdl.org/t/am-i-using-sdl-rendergeometry-correctly/32995/8 "2021-10-07T14:49:39Z")

</div>

you can also do the shading _with_ a texture

---

<div class="post-metadata">

**Author:** ![thd79](https://discourse.libsdl.org/letter_avatar_proxy/v4/letter/t/e99b99/32.png) [@thd79](https://discourse.libsdl.org/u/thd79)\
**Post date:** [October 8, 2021, 9:57am UTC](https://discourse.libsdl.org/t/am-i-using-sdl-rendergeometry-correctly/32995/10 "2021-10-08T09:57:26Z")

</div>

Well, I thought about it some more, and with a few tricks I was able to get the effect I wanted. This is great, it basically completes the “SNES on steroids” claim/feature set for the SDL2 renderer - I can now project and scale backgrounds with mostly hardware acceleration, and without having to leave the comfort of the SDL2 renderer.

I love SDL!

[![](https://live.staticflickr.com/65535/51564397041_7ba40470df.jpg)](https://flic.kr/p/2myz1Li) by

or, using a more indicative texture:

[![](https://live.staticflickr.com/65535/51565084599_78c1d5683a.jpg)](https://flic.kr/p/2myCx9K) by

---

<div class="post-metadata">

**Author:** ![rmg.nik](https://discourse.libsdl.org/letter_avatar_proxy/v4/letter/r/73ab20/32.png) [@rmg.nik](https://discourse.libsdl.org/u/rmg.nik)\
**Post date:** [October 8, 2021, 11:08am UTC](https://discourse.libsdl.org/t/am-i-using-sdl-rendergeometry-correctly/32995/11 "2021-10-08T11:08:55Z")

</div>

Great!  
But how did you get the correct result?

---

<div class="post-metadata">

**Author:** ![thd79](https://discourse.libsdl.org/letter_avatar_proxy/v4/letter/t/e99b99/32.png) [@thd79](https://discourse.libsdl.org/u/thd79)\
**Post date:** [October 8, 2021, 12:13pm UTC](https://discourse.libsdl.org/t/am-i-using-sdl-rendergeometry-correctly/32995/12 "2021-10-08T12:13:36Z")

</div>

Well, it’s not truly correct - its more a case of making it look visually acceptable. It’s very very simple. Starting with something that looks bad:

[![](https://live.staticflickr.com/65535/51565579415_312f739357.jpg)](https://flic.kr/p/2myF5f4)

Instead of mapping the texture to one quad, divide that quad into bits, and map each bit to the appropraite part of the texture… here’s with 3x3 for 9 quads:

[![](https://live.staticflickr.com/65535/51565365269_f29c763d2b.jpg)](https://flic.kr/p/2myDYzT)

Ok, still looks bad, but seems obvious that by increasing the number of sub quads, we can arrive at something visually acceptable… here’s with 15x15:

[![](https://live.staticflickr.com/65535/51563884757_9ec2009359.jpg)](https://flic.kr/p/2mywotP)

So we are drawing 15x15x2 =550 triangles instead of two, and have something that looks good. We only need to calculate 225 projection points per frame. (which why I say mostly in hardware - we still need to do those projections on the CPU).

---

<div class="post-metadata">

**Author:** ![thd79](https://discourse.libsdl.org/letter_avatar_proxy/v4/letter/t/e99b99/32.png) [@thd79](https://discourse.libsdl.org/u/thd79)\
**Post date:** [October 8, 2021, 12:23pm UTC](https://discourse.libsdl.org/t/am-i-using-sdl-rendergeometry-correctly/32995/13 "2021-10-08T12:23:48Z")

</div>

Something else that allows you to get a better result with fewer quads is to draw 4 triangles per quad, using the centrepoint of each quad (as found by the intersection of the diagonals) as an extra vertex.

[![](https://live.staticflickr.com/65535/51563854162_a0539e736c.jpg)](https://flic.kr/p/2myweoj)

---

<div class="post-metadata">

**Author:** ![Sylvain\_Becker](https://discourse.libsdl.org/letter_avatar_proxy/v4/letter/s/3ab097/32.png) [@Sylvain\_Becker](https://discourse.libsdl.org/u/Sylvain_Becker)\
**Post date:** [October 8, 2021, 3:03pm UTC](https://discourse.libsdl.org/t/am-i-using-sdl-rendergeometry-correctly/32995/14 "2021-10-08T15:03:05Z")

</div>

Thanks for sharing. It seems a good workaround. There is a little distortion, but you can even split in smaller quads.

I’ve updated the previous patch so it applies correctly (and also look at what it ).  
It allow to modify the model view matrix, (for Opengl and d3d backend)  
so you can do a translation/rotation/re-scale of the renderer output.  
(if you want to restrict the transformation to a specific RenderCopy, you probably need to call RenderFlush before/after the RenderCopy)

This doesn’t seem enough for what you want.  
but it may be a good start, because I think you would need:

- imodify do a rotation around x axis, for the model view. (currtly it’s only aroud z)
- also allow to modify the projection matrix to have to change the perspective

> **[GitHub - 1bsyl/SDL at br\_transform\_stack](https://github.com/1bsyl/SDL/tree/br_transform_stack)**
>
> br\_transform\_stack

> <https://github.com/1bsyl/SDL/commit/196f5c3f40d1f4f6ca8580f190b84e46be70972b>
>
> https://github.com/libsdl-org/SDL/issues/1229

---

<div class="post-metadata">

**Author:** ![thd79](https://discourse.libsdl.org/letter_avatar_proxy/v4/letter/t/e99b99/32.png) [@thd79](https://discourse.libsdl.org/u/thd79)\
**Post date:** [October 8, 2021, 3:16pm UTC](https://discourse.libsdl.org/t/am-i-using-sdl-rendergeometry-correctly/32995/15 "2021-10-08T15:16:44Z")

</div>

Thanks, I’ll take a look at that. Regarding my “solution” - yes, some distortion remains, and the amount of subquads you need in order to make it look ok depends on the nature of the texture.

---

<div class="post-metadata">

**Author:** ![AntTheAlchemist](https://discourse.libsdl.org/user_avatar/discourse.libsdl.org/antthealchemist/32/3894_2.png) [@AntTheAlchemist](https://discourse.libsdl.org/u/AntTheAlchemist)\
**Post date:** [October 11, 2021, 7:19pm UTC](https://discourse.libsdl.org/t/am-i-using-sdl-rendergeometry-correctly/32995/16 "2021-10-11T19:19:43Z")

</div>

> [@thd79](#):
>
> If so, is there a way to use the api in a way that will map the texture onto the quad, rather than seperately onto two triangles?

Ryan told me off when I asked for this, lol!

---

<div class="post-metadata">

**Author:** ![slime](https://discourse.libsdl.org/user_avatar/discourse.libsdl.org/slime/32/3845_2.png) [@slime](https://discourse.libsdl.org/u/slime)\
**Post date:** [October 11, 2021, 7:32pm UTC](https://discourse.libsdl.org/t/am-i-using-sdl-rendergeometry-correctly/32995/17 "2021-10-11T19:32:02Z")

</div>

The index array (indices parameter) lets you construct 2 triangles from 4 vertices (a quad). That’s the way GPU APIs expose rendering quads, so that’s the best way to do it.

The original poster was expecting a perspective-correct projection instead of an affine projection when rendering geometry. The best way to achieve perspective-correct rendering is to use a perspective projection matrix and 3D position coordinates (both of which are outside the scope of SDL’s 2D rendering API). Tessellating the non-perspective-correct geometry can achieve similar results, as already discussed.

---

<div class="post-metadata">

**Author:** ![Sylvain\_Becker](https://discourse.libsdl.org/letter_avatar_proxy/v4/letter/s/3ab097/32.png) [@Sylvain\_Becker](https://discourse.libsdl.org/u/Sylvain_Becker)\
**Post date:** [October 11, 2021, 8:56pm UTC](https://discourse.libsdl.org/t/am-i-using-sdl-rendergeometry-correctly/32995/18 "2021-10-11T20:56:52Z")

</div>

@thd79  
I’ve quickly updated the patch to see if it was feasible with SDL\_Renderer.

So now you can set a rotation of ‘angle’ around the x axis (eg (1,0,0), at point named ‘center’ )  
([Rotation matrix - Wikipedia](https://en.wikipedia.org/wiki/Rotation_matrix) “Rotation matrix from axis and angle”)

```auto
SDL_RenderGetViewport(renderer, &viewport);
center.x = viewport.w/2;
center.y = viewport.h/2;
angle = -0.1;
SDL_RenderPushTransformRotationAxis(renderer, angle, &center, 1, 0, 0);

```

Then you can add a simple projection matrix:  
[https://www.scratchapixel.com/lessons/3d-basic-rendering/perspective-and-orthographic-projection-matrix/building-basic-perspective-projection-matrix](https://www.scratchapixel.com/lessons/3d-basic-rendering/perspective-and-orthographic-projection-matrix/building-basic-perspective-projection-matrix)

```auto
float fov = 140; 
float near = 1;
float far = 20;
SDL_RenderPushProjection(renderer, fov, near, far);

```

Render your stuff

```auto
RenderCopy()
SDL_RenderFlush()
SDL_RenderPopTransform(renderer);
SDL_RenderPopProjection(renderer);

```

There may be bugs, but it seems to render something which should look like what you expect.

---

<div class="post-metadata">

**Author:** ![thd79](https://discourse.libsdl.org/letter_avatar_proxy/v4/letter/t/e99b99/32.png) [@thd79](https://discourse.libsdl.org/u/thd79)\
**Post date:** [October 12, 2021, 12:19pm UTC](https://discourse.libsdl.org/t/am-i-using-sdl-rendergeometry-correctly/32995/19 "2021-10-12T12:19:51Z")

</div>

hehe, probably fears this question will be asked over and over 🙂

---

<div class="post-metadata">

**Author:** ![Charles\_Huber](https://discourse.libsdl.org/letter_avatar_proxy/v4/letter/c/ee59a6/32.png) [@Charles\_Huber](https://discourse.libsdl.org/u/Charles_Huber)\
**Post date:** [October 14, 2021, 3:05pm UTC](https://discourse.libsdl.org/t/am-i-using-sdl-rendergeometry-correctly/32995/20 "2021-10-14T15:05:43Z")

</div>

Would probably help if the SDL\_RenderGeometry docs mentioned that it only does [_affine_ texture mapping](https://en.wikipedia.org/wiki/Texture_mapping#Affine_texture_mapping) 🙂

---

<div class="post-metadata">

**Author:** ![thd79](https://discourse.libsdl.org/letter_avatar_proxy/v4/letter/t/e99b99/32.png) [@thd79](https://discourse.libsdl.org/u/thd79)\
**Post date:** [October 14, 2021, 3:36pm UTC](https://discourse.libsdl.org/t/am-i-using-sdl-rendergeometry-correctly/32995/21 "2021-10-14T15:36:12Z")

</div>

To be fair, this is not part of the official SDL release yet!

I guess now, with hardware triangle drawing and affine only texture mapping, we could think of the SDL2 renderer as a Playstation rather than a SNES?

[Next page](https://discourse.libsdl.org/t/am-i-using-sdl-rendergeometry-correctly/32995.md?page=2)
