glOrtho results are not what I expected - qt

I am a little perplexed as to the trouble I am having with glOrtho(). My code works in the following case:
// Scenario 1
...
glOrtho(0, _width, 0, _height, 0, 1);
...
glRasterPos2i(0, 0);
...
However, I am unhappy with this coordinate system, I would like it to use:
// Scenario 2
...
glOrtho(0, _width, -_height, 0, 0, 1);
...
glRasterPos2i(0, -_height);
...
Unfortunately, only changing the two lines above in my code leaves me with a blank screen. I assumed I did not understand how glOrtho() and glRasterPos2i() work, as I am fairly new to OpenGl, so I tried the following:
// Scenario 3
...
glOrtho(0, _width, -_height, 1, 0, 1);
...
glRasterPos2i(0, -_height);
...
And, to my surprise, it worked! Why is this? The above code is not sufficient for my purposes, so I will stick with scenario 1 unless I can solve my problem. Does anyone have any insight as to why Scenario 1 and 3 work, but Scenario 2 does not? According to my web searches, Scenario 2 should work, so I must misunderstand something.
If it helps, I am using glDrawPixels() to draw an image, where _height and _width are the height and width of the image.
I am using Windows 7 and Qt 4.7.4.
If you need any more info, let me know.
edit: Typo in the original, it should read glOrtho(0, _width, -_height, 0, 0, 1) instead of glOrtho(0, _width, _height, 0, 0, 1).

You raster position is on the corner of the window. My guess is that round-off error is causing the position to be clipped in some cases but not others.
I think that the answer is glWindowPos(), which was added in version 1.4. Like the name says, it sets the raster position directly in window coordinates, unaffected by the modelview and projection matrices. Importantly, the raster position will always be valid, even if the position is on or outside of the window bounds. The disadvantage is that you'll have to do the coordinate mapping yourself before calling glWindowPos().

Related

How to calibrate analog joystick to screen

I am having a problem with my analog joystick. I have set it up to move the mouse position accordingly to the joystick values. On a technicality, it does work, as it does control the mouse. However, the calibration is off and when I test it, the mouse stays off-centered on the screen at rest and can only move around from that "center" position. I have scoured the internet trying to find a solution, but alas I must ask.
Snippet:
int xMapped = map(xread, 0, 1023, 0, 1920);
int yMapped = map(yread, 0, 1023, 0, 1080);
I have done testing to the joystick and can confirm the min and max values are correct.
(I feel like I may have weirdly worded the situation, so here is a ms paint visual :D)
Expectation
Circle-Mouse
Rounded Rectangle-Range
Rectangle-Screen
What Happens

masking, or clipping mask with p5.js

I want to use one shape (for instance a rectangle) to act as a mask or clipping path for another shape (for instance a circle, or line) in P5.js
I can see solutions for using images as masks, but not shapes. It seems mask() is not a function of shapes:
https://p5js.org/reference/#/p5.Image/mask
yes, you can.
create an extra rendering context with createGraphics().
In the draw loop draw something to this context which will be your
mask. Whatever should be visible in your result has to be colored
with the alpha channel, for example fill('rgba(0, 0, 0, 1)'.
Apply the mask to your original image myImage.mask(circleMask).
Your original image has now been modified by the mask, render it on
the screen: image(myImage, x, y, w, h)
Here is a working code example:
let circleMask;
let myImage;
function setup() {
createCanvas(400, 400);
circleMask = createGraphics(128, 128);
myImage = loadImage('FzFH41IucIY.jpg');
}
function draw() {
background(255);
circleMask.fill('rgba(0, 0, 0, 1)');
circleMask.circle(64, 64, 128);
myImage.mask(circleMask);
image(myImage, 200 - 64, 200 - 64, 128, 128);
}
There isn't a way to do this out of the box with P5.js.
Right now your question is more of a math question than it is a P5.js question. I'd recommend searching for something like "circle rectangle intersection" for a ton of results, including this one: Circle-Rectangle collision detection (intersection)
Depending on what you want to do, you could get away with drawing the shapes to images and then using those images as a mask. But more likely you're going to have to calculate the intersection yourself. You might be able to find a library that does this for you, but again, there isn't a simple out of the box way with P5.js.

QPainter::drawPixmap() doesn't look good and has low quality?

I'm trying to draw an icon(.png) inside a QWidget with QPainter::drawPixmap()
:
QPixmap _source = "/.../.png";
painter.setRenderHint(QPainter::HighQualityAntialiasing);
painter.drawPixmap(rect(), _source);
but in comparing to QLabel (for example) and in lower size (19*19 in my case) the result isn't perfect.
What can I do?
****Edit****
QLabel with pixmap # size 19*19:
My painting # size 19*19 via SmoothPixmapTransform render type:
You are setting the wrong render hint, you need QPainter::SmoothPixmapTransform to get smooth resizing. By default the nearest neighbor method is used, which is fast but has very low quality and pixelates the result.
QPainter::HighQualityAntialiasing is for when drawing lines and filling paths and such, i.e. when rasterizing geometry, it has no effect on drawing raster graphics.
EDIT: It seems there is only so much SmoothPixmapTransform can do, and when the end result is so tiny, it isn't much:
QPainter p(this);
QPixmap img("e://img.png");
p.drawPixmap(QRect(50, 0, 50, 50), img);
p.setRenderHint(QPainter::SmoothPixmapTransform);
p.drawPixmap(QRect(0, 0, 50, 50), img);
img = img.scaled(50, 50, Qt::IgnoreAspectRatio, Qt::SmoothTransformation);
p.drawPixmap(100, 0, img);
This code produces the following result:
There is barely any difference between the second and third image, manually scaling the source image to the desired dimensions and drawing it produces the best result. This is certainly not right, it is expected from SmoothTransformation to produce the same result, but for some reason its scaling is inferior to the scale() method of QPixmap.

JavaFX: how to clear the canvas

Let's say I have drawn a rectangle on my canvas and I want to clean it in order to draw some other figure / polygon / arc ....
How can I do it? I have tried it in many ways but none has worked.
I think this may work but I'm not sure:
GraphicsContext gc = myCanvas.getGraphicsContext2D();
gc.setFill(Color.ALICEBLUE);
gc.fillRect(0, 0, 300, 200);
Could you tell me if this will work consistently and whether it is the standard way to achieve this goal?
The method clearRect seems to be dedicated for this:
gc.clearRect(0, 0, canvas.getWidth(), canvas.getHeight());

Use window/viewport to flip QPainter y-axis

I'm using Qt 4.7 QPainter to draw some polygons, etc into a widget. I am hoping to alter the coordinate system so that (0,0) is at the center of my widget, and the x/y axis behave in a standard "Cartesian" way (ie. y increases going "up" and decreases going "down"). In other words, I want the coordinates to be "math"-like not "computer graphics"-like, if you know what I mean. :-)
I'm trying to do this using setViewport() and setWindow() rather than do the math myself, as it would be nice to be able to just call the draw methods directly with my coordinates.
Here's what I've got so far:
// Setup coordinates
double screenWidth = width();
double screenHeight = height();
double windowWidth = 100.0;
double windowHeight = (screenHeight / screenWidth) * windowWidth;
painter.setViewport(0, 0, screenWidth, screenHeight);
painter.setWindow(-(windowWidth / 2.0), -(windowHeight / 2.0), windowWidth, windowHeight);
// Draw stuff
painter.setPen(Qt::NoPen);
painter.setBrush(Qt::blue);
painter.drawRect(-10, -10, 20, 20);
Now this works just fine, in that it draws a nice blue square in the middle of the screen. The problem is, I have to say that the upper left corner is (-10, -10). I'd like to be able to make it (-10, 10), as that is what it would be in Cartesian coords.
I tried messing with setWindow/setViewport to get this "y-axis flip", but to no avail. This seems like a really easy/basic thing to do, but after scouring the Qt docs and the web, I can't figure it out!
Thanks,
Chris
Use class QMatrix. It specifies 2D transformations. QMatrix is set to QPainter.
But remember, in your case, if you convert your widget's coords to Cartesian coords, you will have to put first point at (-10,-10) (not at (-10,10) as you did mentioned) to draw a rect, which has center at (0,0), because Y-axis now grows up and X-Axis now grows right.
All you need is to transform your coord system this way:
translate origin from (0,0) to the middle of the widget:
scale Y-axis by -1 factor:
Here is the code, typed in paintEvent() function of a widget:
QPainter pn( this );
int w_2 = width() / 2;
int h_2 = height() / 2;
{ // X- and Y-Axis drawing
pn.setPen( Qt::blue );
pn.drawLine( 0, h_2, width(), h_2); // X-Axis
pn.drawLine( w_2, 0 , w_2, height() ); // Y-Axis
}
QMatrix m;
m.translate( w_2, h_2 );
m.scale( 1, -1 );
pn.setMatrix( m );
pn.setPen( Qt::NoPen );
pn.setBrush( QBrush( Qt::blue, Qt::Dense4Pattern ) );
pn.drawRect( -10, -10, 20, 20 );
result:
update apr 07, 2014
This question was asked a long time ago and many things have changed since. For those asking themselves the same question today (beginnings of 2014) then my personal answer is that since Qt 4.3 it is possible to solve problem with text flipping more easier.
You are right. Text also gets filpped because it is drawn with the same painter. You can draw text at the end, when all flipped drawings are done, if it is possible. This method is not convinient because of new calculations of texts position. Also you will need to drop settings for painter.
Now I would recommend you to use QGraphicsView, because of huge support of 2D painting. Also for each QGraphicsItem ItemIgnoresTransformations flag can be set, which allows it to ignore inherited transformations (i.e., its position is still anchored to its parent, but the parent or view rotation, zoom or shear transformations are ignored). This flag is useful for keeping text label items horizontal and unscaled, so they will still be readable if the graphics view is transformed
The above answer will also flip text, "p" will be "b". To avoid that you have to flip back the y-axis before text is drawn, and you have to change sign on y-coord for the text position when you draw it. This is a little bit ugly I think, or is there a better way?
As stated above, drawing text also appears flipped upside down. There is an easy solution to it, see below. We will temporary disable the world transform for the text drawing. Note that text is not scaled anymore.
in your painting code we want to draw text on coordinate QPointF P;
Painter pn( this );
// calculate the point with the transform
QPointF p = pm.transform().map(P);
// Disable Transform temporary
pn.setWorldMatrixEnabled(false);
// draw it ordinary, no scaling etc
pn.drawText(p, QString("HI FRIENDS!"));
// Enable the transform again
pn.setWorldMatrixEnabled(true);
I needed to flip the y-axis in order to paint lines and polygons using Qt from points defined in Java coordinates. I imagine others will need to do this in porting from Java to Qt coordinate systems. The discussion above was helpful. My solution was:
painter.translate(0,height());
painter.scale(1.0, -1.0);
and then proceed to draw the lines and polygons.

Resources