# OpenGL GL vs. D3D Texture/Viewport/Scissors Coordinates

This topic is 1132 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I've been having this "liittle problem" for a while :

http://s7.postimg.org/k32r23lmz/Diff.png

Everything (except the ImGui Window is rendered to a texture and after that that texture gets rendered to the window).

The problem is the way APIs handle texture coordinates/Viewport/Scissors Rect. Knowing that OpenGL (0,0) tex-coord is at the left-bottom and glTexImage2D assumes that the image that is organized from bottom to the top it was really tempting to try to "force" OpenGL to deal with texture coordinates like D3D.

This works fine if we ignore the Frame Buffers.So In order to fake that I've flipped the projection matrices Y-cords, and I've inverted the Front Face Triangle Winding. In that case everything works fine, except for the moment when you actually draw to the window. Well the end result is flipped by Y.

One way to fix this is to reverse the flip when drawing to the default frame buffer(the screen).
With my custom API I know when a draw call is going to be submitted to the screen, the problem is that "in general" I don't know the name of the uniform that hold the projection matrix, In theory the projection matrix could be hardcoded into the shader code.

And basically I'm stuck here.. I could hardcode that case, but I really want find a solution to that problem?

...or maybe trying to make OpenGL look more D3D-ish is not the right solution?? What is you approach?

##### Share on other sites

the problem is that "in general" I don't know the name of the uniform that hold the projection matrix, In theory the projection matrix could be hardcoded into the shader code.

Besides texture data layout difference (stupid vertical flipping), OpenGL also uses a stupid range of Z values in post-projection space (-1 to 1, instead of 0 to 1)... so you already need to have a mechanism in your engine that will construct projection matrices differently depending on which API you're using.
In my engine, I've pretty much covered up all the differences between the APIs, except for this one leaky abstraction -- the right way to build a projection matrix  ... So this is simply left unsolved in my low-level renderer. Instead it unfortunately resides in a high level math library for building matrices...

And yeah, FWIW, I use D3D conventions everywhere as they're more common across my target platforms.

##### Share on other sites

Besides texture data layout difference (stupid vertical flipping), OpenGL also uses a stupid range of Z values in post-projection space (-1 to 1, instead of 0 to 1)

Doesn't glClipControl solve these problems?

##### Share on other sites

Besides texture data layout difference (stupid vertical flipping), OpenGL also uses a stupid range of Z values in post-projection space (-1 to 1, instead of 0 to 1)

Doesn't glClipControl solve these problems?

If GL4.4 is your min-spec, sure
Also assuming that the dodgey drivers that just silently ignored calls to that function have all been fixed by now

Note though that you still have to manually flip parameters to glViewport, glScissor, etc, as glClipControl has no effect on those functions.

##### Share on other sites

You don't like the way of inversing all automatically based on the define which says the actual render system to stay D3D conventions everywhere ?

##### Share on other sites

Besides texture data layout difference (stupid vertical flipping), OpenGL also uses a stupid range of Z values in post-projection space (-1 to 1, instead of 0 to 1)

Doesn't glClipControl solve these problems?

If GL4.4 is your min-spec, sure
Also assuming that the dodgey drivers that just silently ignored calls to that function have all been fixed by now

Note though that you still have to manually flip parameters to glViewport, glScissor, etc, as glClipControl has no effect on those functions.

Yep, it is easy to forget about the viewport and the scissors...

##### Share on other sites

Well for now this is what i did:

Since gltexImage2D thinks that I'm passing the data in "bottom-top" order I've decided to try make it "work" the D3D way.

1st: In order to match the dynamically rendered textures with the loaded onces. I've added a "flip-Y" version for my projection matrices. This however will change the winding of the front-facing triangles. This was easily solved in my solution because I have one big Structure that describes a single draw call, and I can easily swap "implicitly" my triangle winding.

2st: Flipping the Y component will make the things that are rendered directly to "framebuffer 0(or the 'screen')" flipped. So "flip y" logic shouldn't be applied on framebuffer 0. This is currently solved by hand, becase I'm my "DrawCall strcture" I really don't know where the uniform for projection matrix is located, so I leave this to the user.

So my solution looks like this:

	static SELF_TYPE GetPerspectiveFovRH(
const DATA_TYPE& fov,
const DATA_TYPE& aspect,
const DATA_TYPE& nearZ,
const DATA_TYPE& farZ,
const int GL_Flip_Y = 0) // Does absolutely nothing when rendering with D3D, for OpenGL it just adds the -1 scaling on the Y axis...

struct DrawCall //pseudo code...
{
Buffer* vbuffer;
Buffer* ibuffer;
Program* program;
Buffer** cbuffer;
//other stuff...
GLenum frontFace;
CullMode cullMode;
GLint framebuffer;
int framebufferHeight; // a cached value used for flipping the scissor/viewport rects.
bool GL_Flip_Y_Mode; // Does absolutely nothing under D3D. For the OpenGL case, this will flip the frontFace value, this will also be used for flipping the scissor/viewport rects.
}


And basically "GL_Flip_Y_Mode" is true when the framebuffer != 0 (this is done again by hand, in order not to lose control).

The things are a bit m?ore complicated because I'm trying to implement a "general purpose solution", however having an exact requirements could lead to a lot of simplifications.

?

##### Share on other sites

Well for now this is what i did:

Since gltexImage2D thinks that I'm passing the data in "bottom-top" order I've decided to try make it "work" the D3D way.

1st: In order to match the dynamically rendered textures with the loaded onces. I've added a "flip-Y" version for my projection matrices. This however will change the winding of the front-facing triangles. This was easily solved in my solution because I have one big Structure that describes a single draw call, and I can easily swap "implicitly" my triangle winding.

2st: Flipping the Y component will make the things that are rendered directly to "framebuffer 0(or the 'screen')" flipped. So "flip y" logic shouldn't be applied on framebuffer 0. This is currently solved by hand, becase I'm my "DrawCall strcture" I really don't know where the uniform for projection matrix is located, so I leave this to the user.

So my solution looks like this:

	static SELF_TYPE GetPerspectiveFovRH(
const DATA_TYPE& fov,
const DATA_TYPE& aspect,
const DATA_TYPE& nearZ,
const DATA_TYPE& farZ,
const int GL_Flip_Y = 0) // Does absolutely nothing when rendering with D3D, for OpenGL it just adds the -1 scaling on the Y axis...

struct DrawCall //pseudo code...
{
Buffer* vbuffer;
Buffer* ibuffer;
Program* program;
Buffer** cbuffer;
//other stuff...
GLenum frontFace;
CullMode cullMode;
GLint framebuffer;
int framebufferHeight; // a cached value used for flipping the scissor/viewport rects.
bool GL_Flip_Y_Mode; // Does absolutely nothing under D3D. For the OpenGL case, this will flip the frontFace value, this will also be used for flipping the scissor/viewport rects.
}


And basically "GL_Flip_Y_Mode" is true when the framebuffer != 0 (this is done again by hand, in order not to lose control).

The things are a bit m?ore complicated because I'm trying to implement a "general purpose solution", however having an exact requirements could lead to a lot of simplifications.

?

I've seen renderers designed this way, and they often lead to a lot of headache, especially when you start porting it to different platforms. At one point we had our post processing pipeline pointlessly flipping a rendertarget around 15 times, due to "if (isRenderTarget) flip();"

The (IMO) cleaner option is to at data build time flip all texture data to match the rendertarget orientation for the target platform. This may require the texcoords in your geometry to also be vertically flipped - which may or may not be needed anyway because different art packages may or may not follow any given texture orientation.

But, once you have that data pipeline there, its rather nice having everything consistent at runtime!

##### Share on other sites
But, once you have that data pipeline there, its rather nice having everything consistent at runtime!

Yes this was my pribary concern. The simplest(and the only example that comes to my mind) is a runtime generated quad for post processing, trying to use the native UVs on every platform is a bit tricy becase of that.

I could modify my solution to only need to flip the perspective matrix if under GL and rendering to framebuffer != 0.

Thanks a lot for the feed back, I will use my current set-up in order to see how it actually behaves in practice, and I will give feedback.

Other solutions are still very welcome!

##### Share on other sites

In my engine, I've pretty much covered up all the differences between the APIs, except for this one leaky abstraction -- the right way to build a projection matrix  ... So this is simply left unsolved in my low-level renderer. Instead it unfortunately resides in a high level math library for building matrices...

I solve this by having the math library support its own brand of projection matrix (which I believe is a left-handed Direct3D formula), which makes it complete as a library, but I simply don’t use it.
Any matrices that are graphics-specific are created by passing the matrix to the graphics module and having it create it for me.
CGfx::ProjectionMatrix( CMatrix4x4 &_mMat, LSFLOAT _fFoV, etc. )

No ugly leaks. If you are creating a projection matrix to be used for graphics, use the graphics library to construct it. Simple.

If you have tools that have access to the math library and not the graphics library, I would simply have the math library support both “ProjectionDX()” and “ProjectionGL()” and let the tools decide (likely at run-time rather than via macros).  Again, to keep from leaking graphics macros into the math library.

L. Spiro

1. 1
2. 2
Rutin
21
3. 3
4. 4
A4L
15
5. 5

• 13
• 26
• 10
• 11
• 9
• ### Forum Statistics

• Total Topics
633737
• Total Posts
3013606
×