How to create a letterbox shader to reserve an aspect ratio in wgpu - math

I want a shader that reserves the aspect ratio like you see in many games. I have a quad that covers the screen in a second render pass along with the frame as a texture, screen width and height, and target resolution width and height. However I cant figure out the math.
Here is the data I have to work with:
struct PostprocessOutput {
#builtin(position) clip_position: vec4<f32>,
#location(0) tex_coords: vec2<f32>,
#location(1) resolution: vec2<f32>,
#location(2) window_size: vec2<f32>,
}

Related

How to make react app with pixel sizing adapt to different screen sizes?

I've been making a website with react and I've been using css with classNames. I also was following a figma document where the figma screens were a certain size lets say x pixels by y pixels. But, on other computers, it might have a larger or smaller pixel dimension (a pixels by b pixels). I've used the x by y pixel for div dimensions as well as button sizes and more. So, when I open my application on other computers with a larger pixel dimension, a portion of the screen is just white, and it still displays the website as if it were on a smaller screen. Is there a way to fix this so that even though I used pixel values my app will adjust to different sized screens? Or, do I have to go back and change everything to percentages?
You need to think about proportion when building your UI.
1- Use percentage(%) for width and aspectRatio for height, or vice versa.
container: {
width: "100%",
aspectRatio: 10 / 3, //height will be "30%" of your width
}
2- Use flex for the jobs percentage can't do. For example, if you have arbitrary size of items in a list and you want them to share equal sizes. Assign each of them with flex: 1
3- Use rem from EStyleSheet instead of pixels. rem is a scale factor. For example, if your rem is 2 and your “11rem” will become “11*2” = “22”. If we make rem proportion to the screen sizes, your UI will scale with any screen sizes.
//we define rem equals to the entireScreenWidth / 380
const entireScreenWidth = Dimensions.get('window').width;
EStyleSheet.build({$rem: entireScreenWidth / 380});
enter code here
//how to use rem
container: {
width: "100%",
aspectRatio: 10 / 3, //height will be "30%"
padding: "8rem", //it'll scale depend on the screen sizes.
}
4- Use scrollView for contents that could potentially scale out of the boxes. For example, a TextView
5- Every time you think about using pixels, consider use rem in method 3.

Enforce QWidget aspect ratio using sizeIncrement

I am designing a form in Qt Creator Designer. I want to make a QWidget that will always keep 4:3 aspect ratio and will keep the remaining space around itself empty.
I put it in a grid layout and surounded it with spacers which I thought would consume any extra space:
After that, I set it's minimum size to 48x32 and it's size increment to 2x3:
I tried all size policies but it never respects the aspect ratio. It also doesn't properly try to fit the whole area.
How do I set it up correctly?

Qt adjustSize() fuzziness - what is it actually doing?

I am running into a weird scenario where I don't understand the output of adjustSize(). I am calling adjustSize() on a QLabel, and the resulting size does not match the minimumSizeHint or the sizeHint - it is in between. The only other factor I can imagine is the length of the text inside the QLabel, but I get the same result for differing lengths, so that doesn't seem right either.
Here is the exact scenario:
I have a horizontal layout containing two QLabels.
One QLabel contains text and has a fixed width fw and minimum height mh. If all the text can fit within that size, the dimensions should be exactly fw x mh. If there is more text, the height should increase.
The second QLabel contains a pixmap and should always maintain its aspect ratio. The minimum width and minimum height exactly match the pixmap's original dimensions. If the height of the text-based QLabel increases, this second QLabel needs to increase in both width and height to match the text-based QLabel's new height while maintaining the aspect ratio of the pixmap.
This seems like it should be readily do-able. After the text of the first QLabel is changed (dynamically to any arbitrary length (within reason, not going outside the bounds of the screen or anything)), I call adjustSize() on that first QLabel. Then, based on the resulting height, I calculate the scale factor that the pixmap would have to increase by to match that height and reset the pixmap with scaled height and scaled width.
The problem is that the height of the text-based QLabel does not behave in any reasonable way I can discern. Even with drastically smaller text than what can fit in the starting size, adjustSize() still results in the height increasing past that starting minimum. Here I'm going to use some exact numbers to illustrate the issue. The minimum height is 385. The sizeHint says 401, and adjustSize results in 390. Why is the sizeHint 401 when the text can easily fit in the minimum of 385? And why is 390 chosen by adjustSize(), matching neither value?

PDFsharp drawing images at 72 DPI, if more resolution it is drawn "smaller"

I'm drawing an image of 100 x 100 pixels in a PDFSharp document.
The image is drawn using:
g.DrawImage(image, new Point(x, y));
The issue here is that if I draw a rectangle starting in the same coordinates x,y and using 100x100 as dimensiones... the rectangle is larger than the image.
If I use the other overloads in DrawImage, setting the container rectangle, the image fits the rectangle but it loses quality (it is enlarged).
I think it is a problem of different resolutions or something like that.
Any ideas?
UPDATE: I resized the image to 133x133 to fit the 100x100 rectangle. What is the reason of this difference? A 33% difference.
MY SOLUTION: When retrieving the image and scaling it to fit the rectangle, you need to take into account that the size of your image is in PIXELS and when you draw in the PDF it is in points. If your image is in 96 DPI, you need to increase its dimension multiplying by "96/72" (that is the 33% I got), and that way you will see what you expect (even if you draw it using a container rectangle or just the starting coordinates).
Set image.Interpolate = false; to disable anti-aliasing and increase the sharpness of the small image.
There are no "pixels" in PDF. DrawImage has overloads that allow to draw the image with a specific size.
If the size is omitted (as you do), the size will be determined by the DPI setting of the image. Could it be your image is set to 96 DPI?
The rectangle 100x100 uses points - and there are 72 points per inch.
The image does not lose quality when you set the size. The "quality loss" depends on the zoom level of the viewer.
You can set a hint to prevent Adobe Reader from anti-aliasing the image.
Update:
Set image.Interpolate = false; to disable anti-aliasing.

How to fit in view the pixmaps in QGraphicsView/QGraphicsScene without changing aspect ratio

I am using QGraphicsView/QGraphicsScene to display an image. The image is always displayed to its original size with scroll bars at the ends. I want QGraphicsView to fit the image automatically as per the the size of the window keeping aspect ratio.
I tried this but nothing happened:
ui->graphicsView->fitInView(0,0,ui->graphicsView->width(),ui->graphicsView->height(),Qt::KeepAspectRatio);
You are providing the rectangle of the view and not that of the scene.
This should work:
ui->graphicsView->fitInView(scene->itemsBoundingRect() ,Qt::KeepAspectRatio);
itemsBoundingRect calculates and returns the bounding rect of all items on the scene. So the graphicsview's view matrix will be scaled in order to fit the contents of the scene.
I would advise you to reimplement resizeEvent and have this call there as well.

Resources