Jump to content
  • Advertisement
Sign in to follow this  
FatalCatharsis

Orthographic matrices in D3D

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

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi, i'm using direct3D 11 and i'm having the worst time getting a 2d image onto the screen correctly. I'm following the 11th rastertek.com d3d11 tutorial on 2d rendering, and for the most part,I set it up the same way they have done. However, i'm not getting the same results. When i move the camera, the 2d image rendered onto the screen kinda.... tilts and gets clipped. here, this is a fraps video showing it.

http://www.youtube.c...h?v=XGgpMTSZniw


I've been following the tutorials fairly closely up to this point, except for taking a small departure with the camera class. I instead made my own inheriting class that takes keyboard and mouse input and moves the camera in a first person fashion, which is where i think this gets ginked up :/ . up till now, they haven't implemented any objects in the tutorials that change the view matrix. i have a direct 3d object, like in the tutorials that manages most things related to setting up and initializing direct 3d, and it's during this startup that i create the default world, projection, and orthographic matrices. I technically never use this world matrix cause each instance of my model class has it's own world matrix, and i don't know much about the uses of the projection matrix so I've wholly left it alone, but now that i'm using the orthographic matrix, it only just occurs to me, is the projection or orthographic matrix supposed to be updated each frame or something? the 2d polygon and texture render perfectly fine when not moving the camera, but then it seems to be stuck in the worlds xy plane, when moving the camera around. So, my question, do you think this has to do with the orthographic matrix or is it something else entirely?

Share this post


Link to post
Share on other sites
Advertisement

is the projection or orthographic matrix supposed to be updated each frame or something? the 2d polygon and texture render perfectly fine when not moving the camera, but then it seems to be stuck in the worlds xy plane, when moving the camera around. So, my question, do you think this has to do with the orthographic matrix or is it something else entirely?


This depends on what you mean by 'updating'. The projection matrix (either orthographic or perspective) itself can stay constant for the whole application. If you are reusing the same constant buffer variable for the perspective projection matrix or something similar, you naturally need to update (i.e. submit it CPU -> GPU -wise) whenever you are changing from perspective to orthographic projection.

To me this looks like you somehow factor the camera view matrix into the equation when you transform the 2D HUD UI vertices onto the screen in your vertex shader. You should not do this at all. When doing a 2D HUD, you can specify the vertices in (aspect-ratio-corrected) screen space (e.g. x coordinates range from [-1, 1] and y coordinates range from [-1, 1] or [-aspectratio, aspectratio]), and make your orthographic transform either identity, or a matrix that only performs aspect ratio correction, since the output of the vertex shader expects x values in the range [-1, 1] and y values in the range [-1, 1] which is a 1:1 aspect ratio.

Double-check the transform matrix that you feed to the Vertex Shader for your 2D UI. It shouldn't receive any camera view transforms, or per-frame changing world transforms. Just pass in "post-transformed" vertices, and potentially multiply by an orthographic matrix that corrects the aspect ratio.

I have seen some engines implement 2D HUDs by giving the HUD objects world space coordinates, and then update a billboard rotate matrix each frame to properly "counter-act" the effects of the view and projection matrices. If that tutorial is doing something like that, just skip over it, since it's unnecessary and silly. (only if you are implementing 3D billboarded sprites in world space, then it is naturally required)

Share this post


Link to post
Share on other sites
so how does direct3d determine when a vertex is transformed? As the tutorial did, i am setting it up so that there are quads on the screen where the vertices have 3 dimensions, with the z values set to 0.0f and the x and y values are screen coordinates, and then just feeding the vertices into the shader with the world matrix and view matrix, replacing the projection matrix with the ortho matrix. when i created a shader that didn't take a view matrix at all that took just the world and ortho matrices and multiplied the vertices by them, the polygon was simply not rendered. same without the world matrix and just the ortho matrix. i assume this is because d3d thinks they are untransformed vertices that need to be transformed. Funny, i read the tutorial a little more closely, and there was a little note next to where they send the 3 matrices to the shader saying,
[font="Arial"]
Due note also that if your view matrix is changing you will need to create a default one for 2D rendering and use it instead of the regular view matrix. In this tutorial using the regular view matrix is fine as the camera in this tutorial is stationary.

[/font]problem is, i don't know what the default view matrix should be :| .

(btw, if you havn't noticed, i'm still fairly new to d3d, so pardon my ignorance :P)

Share this post


Link to post
Share on other sites
hmm, now this is interesting. I did some further experimenting, and whatnot, and for awhile, when they said default, i figured they just wanted an identity matrix for the view matrix, but that was silly and didn't work. I honestly had no theory behind me to figure out what was wrong, so i was just pluggin' stuff in and eventually, i tried saving a copy of the view matrix at it's origin and original orientation when the scene is first rendered, and then sending that unchanging matrix into the shader along with the world and orthographic matrices, and that did it, works like a charm :P . Now, if someone might be so kind as to explain what the heck is happening there, i would be eternally grateful :\.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!