I'm using SlimDX/C# to write a Direct3D application. I configured the camera as per textbox way:
private float cameraZ = 5.0f;
camera = new Camera();
camera.FieldOfView =(float)(Math.PI/2);
camera.NearPlane = 0.5f;
camera.FarPlane = 1000.0f;
camera.Location = new Vector3(0.0f, 0.0f, cameraZ);
camera.Target = Vector3.Zero;
camera.AspectRatio = (float)InitialWidth / InitialHeight;
The drawing and rotational method are all decent Matrix.RotationYawPitchRoll and mesh.DrawSubset(0). Everything else appear normal.
My Problem is that my 3d mesh (thin square box), when look from the side, and stand vertically, it appear thicker than when it's horizontal. I've tried to change the AspectRatio to 1, it's worse. So through trial and error, I found out that it's looks much normal when the AspectRatio is around 2.6. Why is that and what could be wrong?
I've figured out the problem and answer already.
Apparently I did scale the mesh, and to match the aspect ratio, and I apply the Matrix.Scaling after Matrix.RotationYawPitchRoll. When I rotate the mesh facing forward only I realize that it looks the same no matter vertically or horizontally, the scaling is stretching it sideway no mather how I rotate. Swap the 2 matrix does fix my problem.
Thanks anyway
Related
I've made some modifications to https://github.com/NVIDIA/video-sdk-samples/tree/master/nvEncDXGIOutputDuplicationSample to convert the desktop image to a cv mat.
I have a dx11 2d texture and I want the 640x640 center of the texture (cropping in opencv is slow). CopySubresourceRegion (https://learn.microsoft.com/en-us/windows/win32/api/d3d11/nf-d3d11-id3d11devicecontext-copysubresourceregion) works until I pass a D3D11_BOX. When I convert it to a cv mat, it's just a black screen.
my_box.left = 1600;
my_box.top = 480;
my_box.right = 1600;
my_box.bottom = 480;
Can anyone give me a hint as to what I might be missing?
So there's a lot of x/y box options out there. I initially thought this was negatives. As in, you will define the area to not cap and you get the inverse. This is a very popular approach. This is a little goofier than that.
So lets say you want center. That's dimension(so w or h)/2-desired_box_size/2. The answer you get on each of these will be left and top. From here, you'll just add however much to get the box size. For a 3840 by 1600 monitor with a 640x640 crop your box looks like this:
my_box.front = 0;
my_box.back = 1;
my_box.left = 1600;
my_box.top = 480;
my_box.right = 2240;
my_box.bottom = -160;
Note front and back. The box is 3d as it says, but since we're working with a 2d text, we want to go 1 pixel deep. As stated in the docs, it's going to be back-front.
This is my 3rd day of googling and tinkering with my code, and I'm hopeless, I honestly need help, so please bear with me here.
Explanation
I'll start with explaining the basic concept. There is a 'character' in the middle of the screen, always perfectly centered. Under that layer is a big square, which I will refer to as the canvas. As you move around, the game is actually just simulating the movement, a 'virtual camera' if you will, so the character doesn't actually move, the canvas just moves behind it.
My Problem
In this game, the camera needs to be able to 'zoom' in and out, so to do this, I adjust the size of the canvas and everything else depending on the zoom value. (1 is default) The width and height of the canvas is always 8000 * the zoom value. This is fine, however, I am having major difficulty positioning the canvas so that it remains in the same relative position as you scale in and out - or rather a 'zoom to point' or 'zoom from point' effect. I simply cannot create the math for this.
The Code
local oldScale = g.scale;
local newScale = n.scaleTest.Value;
g.scale = newScale;
g.x = g.x + (g.x * newScale);
g.y = g.y + (g.y * newScale);
g.scale represents the zoom value, everything will be adjusted to this size.
g.x and g.y are the coordinates of the canvas. The math in the code is completely incorrect.
n.scaleTest.Value is my temporary variable for the newly set scale. It can change at any time.
Thoughts / Other Important Information
I can retrieve the AbsolutePosition and AbsoluteSize of the canvas as well. I have considered implementing the distance from the canvas's edges to the character into my calculations, but I am not completely sure how to do this.
Thank you for any help, I would sincerely appreciate it, I am very stuck and don't know who to ask.
I make a game using XNA. I would like to align sprite in the center of the screen. This works well when fullscreen is set to false. But when I set IsFullScreen to true, it doesn't work.
I activated the console and print the screen size on it. The size is good (1366 * 768). I also print the current position of the mouse pointer, and when I'm at the bottom right corner, it shows 1279*719, that's why my sprites are not center-align. Why ?
width = GraphicsAdapter.DefaultAdapter.CurrentDisplayMode.Width;
height = GraphicsAdapter.DefaultAdapter.CurrentDisplayMode.Height;
_positionStart = new Vector2(width * 0.5F - _startButton.Width * 0.5F, height * 0.5F - 20);
PS : I'm using XNA 4.0 with VS2012
PS2 : sorry for my grammar, I don't speak English fluently :)
Probably because the top left corner of your sprite is drawn in the middle of the screen which means that the sprite will be drawn slightly on the right side and slightly lower too.
Try values lover than 0.5, maybe 0,45, or even less. This should help.
Well, if the other answer didn't work, a longer, but much more efficient way to do this would be too either:
Set a Rectangle() to the object, and make that act as a bounding box, and use the .Center feature to align the center of it.
Divide the height and the width by 2, as you did, but then minus half the height and width of the image itself (If you can't find that then just use a bounding box as above).
Hope this helps, I know this post is a bit old now, but someone else might stumble across this, and appreciate this answer!
I have a UIImage that results from a function. It's something like the following.
image1.image = [self reflectedImage:img];
where image1 is a UIImage control. Before plugging it into image1, I want to further distort it to the right or left a little into a parallelogram as shown below. So I just want to relocate point c and d.
I've read several dozen articles here and there. I'm not sure if I can do it with CGAffineTransformMake. This web page suggests that I could. Unfortunately, there is no sample project to see how it works. Unfortunately, I haven't found a single web site that shows me how to distort a simple rectangle image into a parallelogram. So what is the easiest way of doing it? Do I need to create a layer so that I can use CATransform3D?
Thank you for your help.
I found out an alternative way which is way simpler than the other in this topic. Just apply this transformation to your view:
imgView.transform = CGAffineTransformMake(1.0, 0.0, proportion, 1.0, 0.0, 0.0);
where proportion is between -1 and 1
This is called a shear transformation, look at the link below for more
http://iphonedevelopment.blogspot.it/2008/10/cgaffinetransform-11-little-more.html
What you have to do is give your UIImageView some perspective. By default a layers transform do not have perspective, so you must also setup this: transform.m34 = 1.0 / -2000;
Also change the anchorpoint of your view so that it rotates along the top edge of the view. For you it becomes (0,0.5).
The perspective gives it that parallelogram look as if the view has depth (its also called 2.5D as its not pure 3D animation, its pseudo 3D). Set the anchor point so that it rotates along that edge and finally give it an angle. i.e. rotate by how much?
// Rotate by 30 degrees
CGAffineTransform rotationTransform = CGAffineTransformIdentity;
rotationTransform = CGAffineTransformRotate(rotationTransform, DegreesToRadians(30));
swingView.transform = rotationTransform;
all of this will give you what you want...
Using XNA, I'm trying to make an adventure game engine that lets you make games that look like they fell out of the early 90s, like Day of the Tentacle and Sam & Max Hit the Road. Thus, I want the game to actually run at 320x240 (I know, it should probably be 320x200, but shh), but it should scale up depending on user settings.
It works kind of okay right now, but I'm running into some problems where I actually want it to look more pixellated that it currently does.
Here's what I'm doing right now:
In the game initialization:
public Game() {
graphics = new GraphicsDeviceManager(this);
graphics.PreferredBackBufferWidth = 640;
graphics.PreferredBackBufferHeight = 480;
graphics.PreferMultiSampling = false;
Scale = graphics.PreferredBackBufferWidth / 320;
}
Scale is a public static variable that I can check anytime to see how much I should scale my game relative to 320x240.
In my drawing function:
spriteBatch.Begin(SpriteSortMode.BackToFront, BlendState.NonPremultiplied, SamplerState.PointClamp, DepthStencilState.Default, RasterizerState.CullNone, null, Matrix.CreateScale(Game.Scale));
This way, everything is drawn at 320x240 and blown up to fit the current resolution (640x480 by default). And of course I do math to convert the actual coordinates of the mouse into 320x240 coordinates, and so forth.
Now, this is great and all, but now I'm getting to the point where I want to start scaling my sprites, to have them walk into the distance and so forth.
Look at the images below. The upper-left image is a piece of a screenshot from when the game is running at 640x480. The image to the right of it is how it "should" look, at 320x240. The bottom row of images is just the top row blown up to 300% (in Photoshop, not in-engine) so you can see what I'm talking about.
In the 640x480 image, you can see different "line thicknesses;" the thicker lines are how it should really look (one pixel = 2x2, because this is running at 640x480), but the thinner lines (1x1 pixel) also appear, when they shouldn't, due to scaling (see the images on the right).
Basically I'm trying to emulate a 320x240 display but blown up to any resolution using XNA, and matrix transformations aren't doing the trick. Is there any way I could go about doing this?
Render everything in the native resolution to a RenderTarget instead of the back buffer:
SpriteBatch targetBatch = new SpriteBatch(GraphicsDevice);
RenderTarget2D target = new RenderTarget2D(GraphicsDevice, 320, 240);
GraphicsDevice.SetRenderTarget(target);
//perform draw calls
Then render this target (your whole screen) to the back buffer:
//set rendering back to the back buffer
GraphicsDevice.SetRenderTarget(null);
//render target to back buffer
targetBatch.Begin();
targetBatch.Draw(target, new Rectangle(0, 0, GraphicsDevice.DisplayMode.Width, GraphicsDevice.DisplayMode.Height), Color.White);
targetBatch.End();