Positioning Windows on multiple displays - windows

If I have 2 monitors that sit side by side and I want to position a window in the upper left of the 2nd monitor, would the correct coordinates be [SCREEN1_WIDTH, 0]? The 2nd monitor sits to the right of the other monitor.
SetWindowPos(myHwnd, 0, SCREEN1_WIDTH, 0, 0, 0, SWP_NOSIZE);
I dont have a second monitor to test this. Then would GetWindowRect(myHwnd, &r) return the same coordinates (absolute coords) or would it be relative to the 2nd monitor?

Related

BitBlt not getting its 'aim' consistant

Windows 11
I have three monitors in front of me. The one on the left has negative screen x coordinates, the centre one starts from zero and the one on the right, a 4K, carries on where the centre one left off. All nice and usual. So I execute
BitBlt(hMem2, 0, 0, bmWide, bmHigh, hdc, pt.x, pt.y, SRCCOPY | CAPTUREBLT);
BitBlt(hMem3, 0, 0, bmWide, bmHigh, hMem2, 0, 0, SRCCOPY);
BitBlt(hMem3, 0, 0, bmWide, bmHigh, hMem1, 0, 0, SRCINVERT);
BitBlt(hdc, pt.x, pt.y, bmWide, bmHigh, hMem3, 0, 0, SRCCOPY);
to read a block of screen (from hdc), add some overlay and write it back at POINT pt.
Does it? Nope.
It appears about 2000 pixels to the right. Consistently, no matter which monitor I put the test program on. The image painted is exactly what I want with all the screen background from where it came from so the first line is using pt correctly. It has my overlay. If it was landing in the right place it would all be wonderful.
All the hMems are loaded with a nice bitmap to hold the image.
I have tried setting
SetMapMode(hMem3, MM_TEXT);
to get 1 logical unit = 1 pixel on all of the HDCs.
I have experimented with versions of
SetThreadDpiAwarenessContext(DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE_V2);
but all to no avail. So I'm missing something. pt used as source is not the same as pt used as a destination.
Can anybody tell me where I'm going wrong?

Changing an object's position without changing its position visually

I have an instance of Object3D and visually it's at (0, 0, 0) but its actual position is shifted (and positions of this object's internals are shifted as well). So, basically geometry of this object is shifted in such a way that it compensates for position shift. So, I want to keep it visuallyy at (0, 0, 0) and change its position to (0, 0, 0).
In simple words, I want to shift object's position vector to this object's geometry position.
How can I do it?

How to use the DXGI flip model in a Direct2D windowed app?

I have a Win32 non-game windowed app that uses a Direct2D device context/HWND render target to draw a window. Currently it uses a DXGI swap chain with the DXGI_SWAP_EFFECT_DISCARD swap effect.
Microsoft recommends using the new flip model swap effects, either DXGI_SWAP_EFFECT_FLIP_SEQUENTIAL or DXGI_SWAP_EFFECT_FLIP_DISCARD. I'm interested in using them primarily because they would allow me to specify a list of dirty rects when calling Present1(), which should improve performance/power usage.
Simply changing the SwapEffect to either of the new flip model values produces a weird (but actually expected) result of drawing a black window each second frame, with artifacts of the previous frames visible onscreen.
So the question is: is it possible to use the new flip model swap effects in this situation, and if yes, how should things be set up?
Given that the app needs to draw the dirty rects into an otherwise valid buffer, it seems that a correct approach would involve maintaining two buffers with essentially the same content (one to draw into, and one to give to the DWM for composition), so not sure if it would be possible to achieve any performance gains this way in an app that doesn't redraw each frame completely. But perhaps I'm missing something important.
The swap chain is currently set up as follows:
swapChainDesc.Width = ...;
swapChainDesc.Height = ...;
swapChainDesc.Format = DXGI_FORMAT_B8G8R8A8_UNORM;
swapChainDesc.Stereo = false;
swapChainDesc.SampleDesc.Count = 1;
swapChainDesc.SampleDesc.Quality = 0;
swapChainDesc.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT;
swapChainDesc.BufferCount = 1;
swapChainDesc.Scaling = DXGI_SCALING_STRETCH;
swapChainDesc.SwapEffect = DXGI_SWAP_EFFECT_DISCARD;
swapChainDesc.AlphaMode = DXGI_ALPHA_MODE_UNSPECIFIED;
swapChainDesc.Flags = 0;
EDIT 1
It turns out that DXGI_SWAP_EFFECT_DISCARD forces BufferCount to 1, so my initial value of 2 was somewhat misleading, as only one buffer is used. Source (3rd comment).
Also the docs for DXGI_SWAP_EFFECT say that UWP apps are forced into the flip model, so this should be a solvable problem.
There are two good ways to do it.
The first way is a little heavier on energy usage. You can draw your contents into an intermediate buffer/render texture, and copy it to swapchain just before every present. That way you can only actually render the parts that changed in your intermediate buffer, and not care about what the state of the swapchain is.
The second way is more complicated, but can yield optimal energy usage. Instead of using intermediate buffer and drawing only what changes since the last frame there, you draw directly into the swapchain buffer. For this to work correctly, you need to redraw not what changes between current and last frame, but between current and (current - BufferCount) frame. For instance:
Frame 1 - you draw a green rectancle at (200 x 200) with dimensions of (150 x 150). The dirty region is entire frame because it's the first frame.
Frame 2 - you draw a blue rectangle at (250 x 250) with dimensions of (50 x 50). The dirty region is (250, 250, 300, 300).
Frame 3 - you draw a red rectangle at (225 x 225) with dimensions of (50 x 50). The dirty region is (225, 225, 50, 50).
If your buffer count is 2, that means when you draw frame 3, you need to not only redraw the dirty region of (225, 225, 50, 50), but also the dirty region of (250, 250, 300, 300).

How do I make a 3D table using Processing.JS?

I want to run a for loop that will have a line with a color of brown. This line will get smaller and smaller, but not too small.
The final image will look like this, but with the table top being colored in brown:
//Back wall
fill(102, 102, 102);
rect(50,50,300,300);
//Top Left Corner
line(50,50,0,0);
//Top Right Corner
line(350,50,400,0);
//Bottom Left Corner
line(350,350,400,400);
//Bottom Riight Corner
line(50,350,0,400);
//Table
//Top Left
fill(48, 17, 0);
rect(163,312,3,38);
//Top Right
fill(48, 17, 0);
rect(230,312,3,38);
//Mesa
fill(48, 17, 0);
rect(126,322,142,5);
//Right Side
line(126,322,168,312);
//Top Side
line(234,312,168,312);
//Right Side
line(269,322,232,312);
//Bottom Left Leg
rect(126,327,5,41);
line(126,368,126,322);
//Bottom Right Leg
rect(263,327,5,41);
line(269,368,268,322);
I have tried this for loop:
for(var x = 200; x > 100; x--){
stroke(61, 34, 0);
line(x,200,x,200);
}
The x value will decrease until x = 100. But, it is not showing the the line getting smaller EVEN after making sure the background(); is out of the loop.
P.S. The pieces of code given are separate.
You're only changing the x coordinate of the lines you're drawing, so the line is moving horizontally, not vertically.
If you want the line to move vertically (to color in the table) and horizontally (to make it smaller as it gets "further away"), you'll have to change both the x and y values you pass into the line() function.
But you're making this harder than it needs to be. There is no reason for you to draw a bunch of lines to get this shape. Just use the beginShape() function to draw the polygon directly. Something like this:
beginShape();
vertex(100, 100); //upper-left
vertex(200, 100); //upper-right
vertex(250, 200); //lower-right
vertex(50, 200); //lower=left
endShape(CLOSE);
Note that this is just an example, and you'll have to play around with the values to draw it in the correct location. But the point is that you don't have to use a for loop to draw lines just to draw a polygon.
Since you're trying to draw a 3D scene, you should also note that you can simply use 3D coordinates along with the vertex() function to draw in 3D. No need to try to force the perspective yourself.

Translation and rotation around center

I'm trying to achieve something simple: Set a translation on X axis, and rotate the object around it's center by a fixed angle.
To achieve this, as far my current knowledge, it's necessary to move the object to the center, rotate, and move back to the original position. Okay. The problem I get although, is that it looks like the object rotate it's local axis and do the last translation along these axis, so it ends in a wrong position.
This is my code:
public void draw(GL10 gl) {
gl.glLoadIdentity();
GLU.gluLookAt(gl, 0, 0, 5, 0, 0, 0, 0, 1, 0);
gl.glTranslatef(x, 0, 0);
gl.glTranslatef(-x, 0, 0);
gl.glRotatef(-80, 0, 1, 0);
gl.glTranslatef(x, 0, 0);
gl.glBindTexture(GL10.GL_TEXTURE_2D, textureId);
gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
gl.glEnableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
gl.glFrontFace(GL10.GL_CW);
gl.glVertexPointer(3, GL10.GL_FLOAT, 0, verticesBuffer);
gl.glTexCoordPointer(2, GL10.GL_FLOAT, 0, textureBuffer);
gl.glDrawElements(GLES20.GL_TRIANGLES, indices.length, GLES10.GL_UNSIGNED_SHORT, indicesBuffer);
}
Before the rotation the object should be at 0,0,0. It rotates correctly. But then it comes near to the screen as if the x axis would be pointing to me (80°).
Note: I let only "opengl" as tag, since this is a general OpenGL question, the answer should not be Android related.
This is the deprecated way of doing this, but I guess that is no excuse for not answering the question.
OpenGL performs matrices multiplications in reverse order if multiple transforms are applied to a vertex. For example, If a vertex is transformed by MA first, and transformed by MB second, then OpenGL performs MB x MA first before multiplying the vertex. So, the last transform comes first and the first transform occurs last in your code.
gl.glPushMatrix();
gl.glTranslatef(globalX, 0, 0);
gl.glTranslatef(localX, 0 ,0);
gl.glRotatef(-80, 0, 1, 0);
gl.glTranslatef(-globalX, 0, 0);
gl.glPopMatrix();
First move from where you are in a hierarchy of transforms to the origin.
Then rotate around that origin.
Apply some local movement along any axis.
Move the object back to its global positioning.
Use glPushMatrix() and glPopMatrix() to undo changes for elements in the same level of relative positioning, this is having the same parent element to which they are relatively positioned.
The push preserves translations from previous (parent) objects that OpenGL applies after operations in the local code above, as it is the order of a common stack (LIFO), in this case the matrix stack.

Resources