[2.2]Window metrics incorrect?

Discussion area about developing with Ogre-Next (2.1, 2.2 and beyond)


ExPeTe
Gnoblar
Posts: 14
Joined: Mon Feb 04, 2019 8:53 pm
x 3

[2.2]Window metrics incorrect?

Post by ExPeTe »

Hello,
I just updated from 2.1 to 2.2 and it looks like something is wrong when the ogre window first spawns. I calculate a ray from the camera position through the mouse pointer to get the 3d position of objects. The result is wrong until I move or resize the Ogre window with my mouse. The content of the window slightly changes position when I first move or resize it. The values of width and height, obtained with "Window->getMetrics(width, height, left, top);" change, even when I just move the window. The actual size of the window doesn`t change.
This didn`t happen in 2.1.
After I change the window for the first time everything is ok again and stays ok.
In fullscreen everything works correctly.

Something else, that changed: The window spawns on the left of the screen, not in the middle like in 2.1.
How can I change the window size and position by code?

The function: Window->getMetrics(width, height, left, top); sets "left" to 0, but the window clearly is about 2mm away from the left of the screen.
"top" is correct. I`m not sure about width and height, because I don`t know if it includes borders or not and how wide borders are.

My config options:

Code: Select all

renderSystem->setConfigOption("Full Screen", "No");
renderSystem->setConfigOption("Video Mode", "920 x 1000 @ 32-bit colour"); 
renderSystem->setConfigOption("Display Frequency", "60 Hz");
renderSystem->setConfigOption("FSAA", "8"); 
renderSystem->setConfigOption("sRGB Gamma Conversion", "Yes");

I´m using openGl on Windows 10, VS2019, Graphicscard: GTX 670
User avatar
dark_sylinc
OGRE Team Member
OGRE Team Member
Posts: 5446
Joined: Sat Jul 21, 2007 4:55 pm
Location: Buenos Aires, Argentina
x 1348

Re: [2.2]Window metrics incorrect?

Post by dark_sylinc »

Hi!
ExPeTe wrote: Wed Jul 08, 2020 5:45 pm Hello,
I just updated from 2.1 to 2.2 and it looks like something is wrong when the ogre window first spawns. I calculate a ray from the camera position through the mouse pointer to get the 3d position of objects. The result is wrong until I move or resize the Ogre window with my mouse. The content of the window slightly changes position when I first move or resize it. The values of width and height, obtained with "Window->getMetrics(width, height, left, top);" change, even when I just move the window. The actual size of the window doesn`t change.
This didn`t happen in 2.1.
After I change the window for the first time everything is ok again and stays ok.
This sounds like it is most likely a bug. Thanks for reporting it.
ExPeTe wrote: Wed Jul 08, 2020 5:45 pm Something else, that changed: The window spawns on the left of the screen, not in the middle like in 2.1.
How can I change the window size and position by code?
Window::reposition and Window::requestResolution is what you're looking for.
ExPeTe wrote: Wed Jul 08, 2020 5:45 pm The function: Window->getMetrics(width, height, left, top); sets "left" to 0, but the window clearly is about 2mm away from the left of the screen.
"top" is correct. I`m not sure about width and height, because I don`t know if it includes borders or not and how wide borders are.
This information may be very useful. Thanks!

One more question: what is your monitor's resolution? (i.e the desktop's, not the application)
ExPeTe
Gnoblar
Posts: 14
Joined: Mon Feb 04, 2019 8:53 pm
x 3

Re: [2.2]Window metrics incorrect?

Post by ExPeTe »

Window::reposition and Window::requestResolution is what you're looking for.
Thanks, I completely overlooked these.
One more question: what is your monitor's resolution? (i.e the desktop's, not the application)
It is 1920x1080.
ExPeTe
Gnoblar
Posts: 14
Joined: Mon Feb 04, 2019 8:53 pm
x 3

Re: [2.2]Window metrics incorrect?

Post by ExPeTe »

Ok I tested the Window::reposition and Window::requestResolution functions. Using one of them at any point in my code fixes the window. It is really just the initial window that`s wrong.

Regarding the alignment:
If I set Window->reposition(0, -8); the window is positioned in the top left corner. The -8 moves the window to the left.
However the variable names indicate, that the first value should move the window horizontally and the second vertically:

Code: Select all

virtual void reposition( int32 leftPt, int32 topPt ) = 0;
Either the variable naming or the code is switched.
I don`t know if the alignment was correctly zeroed to the left in 2.1, because the window spawned in the middle I never paid attention to it.

EDIT: :!: It `s a Windows 10 "feature".
For some reason Microsoft thought it`s a good idea to add an invisible border to the left, bottom and right of a window... . I can confirm, that there is the same 8(or 7?) pixel gap between the window and the taskbar, when I use: renderSystem->setConfigOption("Video Mode", "920 x 1080 @ 32-bit colour"); The window is not actually 1080 high. Ogre reduces the height by the height of the taskbar plus the invisible border.
I found this: https://stackoverflow.com/questions/424 ... ffset-by-7


Window::requestResolution is different than resizing the window with the mouse. When I enlarge the window with the mouse it just stretches the content of the window. It looks like a stretched texture.
Window::requestResolution properly changes the resolution, 1920x1080 fills the entire screen except taskbar, looks correct to me.
It would be nice if resizing the window with the mouse would also change the resolution.
Should this be the default behavior? Or is there a setting for it?

EDIT: Pressing the "maximize" button on the window with the mouse also just stretches the window content(including overlays), and does not change the resolution. But the window covers the complete screen except the taskbar - this part is correct.
User avatar
dark_sylinc
OGRE Team Member
OGRE Team Member
Posts: 5446
Joined: Sat Jul 21, 2007 4:55 pm
Location: Buenos Aires, Argentina
x 1348

Re: [2.2]Window metrics incorrect?

Post by dark_sylinc »

ExPeTe wrote: Wed Jul 08, 2020 11:35 pm EDIT: :!: It `s a Windows 10 "feature".
For some reason Microsoft thought it`s a good idea to add an invisible border to the left, bottom and right of a window... . I can confirm, that there is the same 8(or 7?) pixel gap between the window and the taskbar, when I use: renderSystem->setConfigOption("Video Mode", "920 x 1080 @ 32-bit colour"); The window is not actually 1080 high. Ogre reduces the height by the height of the taskbar plus the invisible border.
I found this: https://stackoverflow.com/questions/424 ... ffset-by-7
Ouch! I gave up on Windows 10. They keep changing/breaking basic stuff regarding windows, keyboard and mouse cursor.
ExPeTe wrote: Wed Jul 08, 2020 11:35 pm Window::requestResolution is different than resizing the window with the mouse. When I enlarge the window with the mouse it just stretches the content of the window. It looks like a stretched texture.
Window::requestResolution properly changes the resolution, 1920x1080 fills the entire screen except taskbar, looks correct to me.
It would be nice if resizing the window with the mouse would also change the resolution.
Should this be the default behavior? Or is there a setting for it?

EDIT: Pressing the "maximize" button on the window with the mouse also just stretches the window content(including overlays), and does not change the resolution. But the window covers the complete screen except the taskbar - this part is correct.
I cannot reproduce that behavior.

Using our default/raw message pump, the following callstack happens:

Code: Select all

>	RenderSystem_GL3Plus_d.dll!Ogre::Win32Window::windowMovedOrResized() Line 899	C++
 	OgreMain_d.dll!Ogre::WindowEventUtilities::_WndProc(HWND__ * hWnd, unsigned int uMsg, unsigned __int64 wParam, __int64 lParam) Line 215	C++
 	
// Relevant C++ code:
case WM_MOVE:
    //log->logMessage("WM_MOVE");
    win->windowMovedOrResized();
    for(index = start; index != end; ++index)
        (index->second)->windowMoved(win);
    break;
Using SDL2's message pump, the following happens:

Code: Select all

 	RenderSystem_GL3Plus_d.dll!Ogre::Win32Window::windowMovedOrResized() Line 899	C++
>	Sample_PbsMaterials.exe!Demo::GraphicsSystem::handleWindowEvent(const SDL_Event & evt) Line 412	C++
 	Sample_PbsMaterials.exe!Demo::GraphicsSystem::update(float timeSinceLast) Line 368	C++
 	Sample_PbsMaterials.exe!Demo::MainEntryPoints::mainAppSingleThreaded(HINSTANCE__ * hInst, HINSTANCE__ * hPrevInstance, char * strCmdLine, int nCmdShow) Line 119	C++
 	
// Relevant C++ code:
case SDL_WINDOWEVENT_SIZE_CHANGED:
    int w,h;
    SDL_GetWindowSize( mSdlWindow, &w, &h );
#if OGRE_PLATFORM == OGRE_PLATFORM_LINUX
    mRenderWindow->requestResolution( w, h );
#endif
    mRenderWindow->windowMovedOrResized();
    break;
Perhaps you are not calling WindowEventUtilities::messagePump, nor implementing a custom message pump and forwarding WM_MOVE msgs to call windowMovedOrResized() ?
ExPeTe
Gnoblar
Posts: 14
Joined: Mon Feb 04, 2019 8:53 pm
x 3

Re: [2.2]Window metrics incorrect?

Post by ExPeTe »

Thanks for your help! You are right, the resolution does get set and when I change the size while keeping the aspect ratio the same it looks correct.
:idea: The problem was that I didn`t update the camera`s aspect ratio in the render loop. I never knew or realized that was necessary.

Adding: myCamera->setAspectRatio(Ogre::Real(myWindow->getWidth()) / Ogre::Real(myWindow->getHeight())); fixed it.


Here is the minimal reproduction code for the initial window being wrong:

Code: Select all

#include "OgreWindowEventUtilities.h"
#include "Vao/OgreVaoManager.h"
#include "OgreRoot.h"
#include "OgreRenderSystem.h"
#include "OgreWindow.h"
#include "OgreCamera.h"

#include "Compositor/OgreCompositorManager2.h"
int main()
{
	Ogre::Root* myRoot = OGRE_NEW Ogre::Root();



#if defined(_DEBUG)
	myRoot->loadPlugin("RenderSystem_Direct3D11_d.dll");
	myRoot->loadPlugin("RenderSystem_GL3Plus_d.dll");
#else
	myRoot->loadPlugin("RenderSystem_GL3Plus.dll");
	myRoot->loadPlugin("RenderSystem_Direct3D11.dll");

	//Linux
	//root->loadPlugin("/usr/local/lib/OGRE/RenderSystem_GL");  
#endif

	Ogre::RenderSystemList::const_iterator renderers = myRoot->getAvailableRenderers().begin();

	while (renderers != myRoot->getAvailableRenderers().end())
	{
		Ogre::String rName = (*renderers)->getName();

		if (rName == "OpenGL 3+ Rendering Subsystem")//Direct3D11 Rendering Subsystem, OpenGL 3+ Rendering Subsystem
			break;

		renderers++;
	}

	Ogre::RenderSystem* renderSystem = *renderers;


	renderSystem->setConfigOption("Full Screen", "No");
	renderSystem->setConfigOption("Video Mode", "1000 x 1080 @ 32-bit colour"); //1920
	//renderSystem->setConfigOption("Display Frequency", "60 Hz");
	renderSystem->setConfigOption("FSAA", "8"); //stutters at 16
	//renderSystem->setConfigOption("Fixed Pipeline Enabled", "Yes");
	//renderSystem->setConfigOption("RTT Preferred Mode", "FBO");

	//renderSystem->setConfigOption("VSync", "Yes");

	renderSystem->setConfigOption("sRGB Gamma Conversion", "Yes");
	myRoot->setRenderSystem(renderSystem);


	Ogre::Window* myWindow = myRoot->initialise(true, "Ogre Window");
	//	myWindow = myRoot->createRenderWindow("Ogre Window", 1920, 1080, false, params);

	//myWindow->reposition(0, -8);
	//myWindow->requestFullscreenSwitch(false, false, 0, 500, 1080, 0, 0);
	//myWindow->reposition(0, 0);
	//myWindow->requestResolution(1920, 1080);



	Ogre::SceneManager* mySceneManager = myRoot->createSceneManager(Ogre::ST_GENERIC, 1, "InstanceName");
	//Ogre::SceneNode* myRootSceneNode = mySceneManager->getRootSceneNode();


	Ogre::ResourceGroupManager::getSingleton().addResourceLocation("../media/models", "FileSystem", "General");
	Ogre::ResourceGroupManager::getSingleton().addResourceLocation("../media/materials/textures", "FileSystem", "General");
	Ogre::ResourceGroupManager::getSingleton().addResourceLocation("../media/materials/textures/Cubemaps", "FileSystem", "General");
	Ogre::ResourceGroupManager::getSingleton().addResourceLocation("../media/packs/OgreCore.zip", "Zip");

	Ogre::ResourceGroupManager::getSingleton().addResourceLocation("../media/2.0/scripts/materials/TutorialSky_Postprocess", "FileSystem");
	Ogre::ResourceGroupManager::getSingleton().addResourceLocation("../media/2.0/scripts/materials", "FileSystem");

	// Load all resources except compositor
	Ogre::ResourceGroupManager::getSingleton().initialiseAllResourceGroups(false);


	Ogre::ResourceGroupManager::getSingleton().addResourceLocation("../media/2.0/scripts/Compositors", "FileSystem", "Compositors");
	Ogre::ResourceGroupManager::getSingleton().initialiseResourceGroup("Compositors", false);


	myRoot->clearEventTimes();


	Ogre::Camera* myCamera = mySceneManager->createCamera("cameraName");
	myCamera->setPosition(0, 0, 0);
	myCamera->lookAt(50, 0, 0);
	myCamera->setNearClipDistance(0.12f); //1.0f
	myCamera->setFarClipDistance(3000.0f);
	myCamera->setAspectRatio(Ogre::Real(myWindow->getWidth()) / Ogre::Real(myWindow->getHeight()));


	Ogre::CompositorManager2* compositorManager = myRoot->getCompositorManager2();

	compositorManager->addWorkspace(mySceneManager, myWindow->getTexture(), myCamera, "TutorialSky_PostprocessWorkspace", true);//


	while (!myWindow->isClosed())			
	{
		//std::cout << "myWindow->isClosed(): " << myWindow->isClosed() << std::endl;
		myCamera->setAspectRatio(Ogre::Real(myWindow->getWidth()) / Ogre::Real(myWindow->getHeight()));
		myRoot->renderOneFrame();

		Ogre::WindowEventUtilities::messagePump();
	}
}
After moving the window for the first time you should see its content moving. The invisible borders are there too.

There is also a difference between: renderSystem->setConfigOption("Video Mode", "1920 x 1080 @ 32-bit colour"); and myWindow->requestResolution(1920, 1080); in windowed mode.
The latter seems to ignore the taskbar and the invisible border on the right(and maybe bottom).

If I open any of the samples and set "Full Screen" to "No" and "Video Mode" to 1920x1080 the entire top bar of the window is missing.
On a positive note: All the invisible borders are missing too. Maybe you fixed it there already?
Ouch! I gave up on Windows 10. They keep changing/breaking basic stuff regarding windows, keyboard and mouse cursor.
Yeah, sounds bad. I know why I waited till 2 months ago before I started to use it. Looks like it still hasn`t settled.
For me, fixing that isn`t a high priority, I can work around it.

Edit: Looks like Hearthstone(made with unity) hasn`t fixed that either. :mrgreen: If I set the resolution to 1920x1080 in windowed mode it has the invisible border on the left and part of the window is too far right. The bottom part of the window is behind the taskbar.
User avatar
dark_sylinc
OGRE Team Member
OGRE Team Member
Posts: 5446
Joined: Sat Jul 21, 2007 4:55 pm
Location: Buenos Aires, Argentina
x 1348

Re: [2.2]Window metrics incorrect?

Post by dark_sylinc »

ExPeTe wrote: Mon Jul 13, 2020 11:44 am Adding: myCamera->setAspectRatio(Ogre::Real(myWindow->getWidth()) / Ogre::Real(myWindow->getHeight())); fixed it.
Quick question: was myCamera->getAutoAspectRatio() false?
ExPeTe
Gnoblar
Posts: 14
Joined: Mon Feb 04, 2019 8:53 pm
x 3

Re: [2.2]Window metrics incorrect?

Post by ExPeTe »

Quick question: was myCamera->getAutoAspectRatio() false?
Yes, it is false.

I tried to set it true with setAutoAspectRatio and it does indeed automatically update the aspect ratio. I should have found that myself, but I lacked some understanding of the relation between camera and window. Now it is obvious.


Btw: top and left are reversed here:

Code: Select all

 void Win32Window::reposition( int32 left, int32 top )
    {
        if( mHwnd && !mRequestedFullscreenMode )
        {
            SetWindowPos( mHwnd, 0, top, left, 0, 0,
                          SWP_NOSIZE | SWP_NOZORDER | SWP_NOACTIVATE );
        }
    }
same in d3d11:

Code: Select all

 void D3D11WindowHwnd::reposition( int32 left, int32 top )
    {
        if( mHwnd && !mRequestedFullscreenMode )
        {
            SetWindowPos( mHwnd, 0, top, left, 0, 0,
                          SWP_NOSIZE | SWP_NOZORDER | SWP_NOACTIVATE );
        }
    }
User avatar
dark_sylinc
OGRE Team Member
OGRE Team Member
Posts: 5446
Joined: Sat Jul 21, 2007 4:55 pm
Location: Buenos Aires, Argentina
x 1348

Re: [2.2]Window metrics incorrect?

Post by dark_sylinc »

ExPeTe wrote: Sun Jul 19, 2020 11:53 pm Btw: top and left are reversed here:
Thanks!
Fixed
ExPeTe
Gnoblar
Posts: 14
Joined: Mon Feb 04, 2019 8:53 pm
x 3

Re: [2.2]Window metrics incorrect?

Post by ExPeTe »

I found the problem that causes the incorrect initial window and metrics, and made a pull request with the fix:
https://github.com/OGRECave/ogre-next/pull/120