imgui icon indicating copy to clipboard operation
imgui copied to clipboard

ImGui Dockspace not working when viewports are enabled

Open Platforming-Mayhem opened this issue 1 month ago • 10 comments

Version/Branch of Dear ImGui:

Branch: docking

Back-ends:

imgui_impl_glfw.cpp imgui_impl_opengl3.cpp

Compiler, OS:

Windows 10

Full config/build information:

No response

Details:

I'm setting up imgui docking and multi-viewports, I'm trying to create a viewport window that renders my game's graphics in an imgui window. However when I use:

if (ImGui::GetIO().ConfigFlags & ImGuiConfigFlags_DockingEnable) { ImGui::DockSpaceOverViewport(); }

it causes my render textures to clear and erase themselves so nothing gets rendered to screen or the viewport.

Screenshots/Video:

with ImGui::Dockspace image without ImGui::Dockspace image

Minimal, Complete and Verifiable Example code:

ImGui_ImplOpenGL3_NewFrame(); ImGui_ImplGlfw_NewFrame(); ImGui::NewFrame();

if (ImGui::GetIO().ConfigFlags & ImGuiConfigFlags_DockingEnable) { ImGui::DockSpaceOverViewport(); }

ImGui::Render(); glClear(GL_COLOR_BUFFER_BIT); ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData());

if (ImGui::GetIO().ConfigFlags & ImGuiConfigFlags_ViewportsEnable) { GLFWwindow* backup_current_context = glfwGetCurrentContext(); ImGui::UpdatePlatformWindows(); ImGui::RenderPlatformWindowsDefault(); glfwMakeContextCurrent(backup_current_context); }

Platforming-Mayhem avatar May 20 '24 18:05 Platforming-Mayhem

This is most likely an issue on your end. You should use a graphics debugger like RenderDoc to investigate.

PathogenDavid avatar May 20 '24 18:05 PathogenDavid

@PathogenDavid Why does it start working as soon as I disable ImGui::DockSpaceOverViewport();?

Platforming-Mayhem avatar May 20 '24 19:05 Platforming-Mayhem

Hard to say without debugging your app, which is why I recommended looking at a graphics debugger.

PathogenDavid avatar May 20 '24 19:05 PathogenDavid

For some reason the vs output changes depending on whether dockspace is active or not. If it is active, gl_position goes to NaN. If it is inactive, the values are correct. No Dockspace image Dockspace image

Platforming-Mayhem avatar May 20 '24 21:05 Platforming-Mayhem

It seems like my program doesn't allow: if (ImGui::GetIO().ConfigFlags & ImGuiConfigFlags_ViewportsEnable) { GLFWwindow* backup_current_context = glfwGetCurrentContext(); ImGui::UpdatePlatformWindows(); ImGui::RenderPlatformWindowsDefault(); glfwMakeContextCurrent(backup_current_context); } and: if (ImGui::GetIO().ConfigFlags & ImGuiConfigFlags_DockingEnable) { ImGui::DockSpaceOverViewport(); } to run at the same time for some reason.

Platforming-Mayhem avatar May 21 '24 12:05 Platforming-Mayhem

It's possibly an issue in your code and you may be not sharing the relevant code. It's ambiguous what those vertices you posted are (vertices for what?). Try to confirm if your render-to-texture is valid in both cases. Use Metrics/Debugger to inspect our draw calls.

ocornut avatar May 21 '24 13:05 ocornut

My textures are valid, it seems like my vertex positions become invalid. I'm not sure what is causing it to become invalid. image this is my vertex shader if it helps.

Platforming-Mayhem avatar May 21 '24 16:05 Platforming-Mayhem

it seems like my vertex positions become invalid.

OpenGL uses a bunch of global state, so it is possible that some of the state we alter inside ImGui_ImplOpenGL3_RenderDrawData() are altering your render because it doesn't explicitly set them up.

ocornut avatar May 21 '24 16:05 ocornut

My textures are valid, it seems like my vertex positions become invalid. I'm not sure what is causing it to become invalid.

Again, this can means different things and is very ambiguous to read without code. We still don't know how you are rendering the things that breaks.

If you have a texture working, then you can submit your framebuffer using ImGui::Image(), and then I don't understand what this shader is about because you shouldn't need one. So it seems like you are may doing something else but I cannot guess what it is.

I also don't know if by texture you are referring to the texture used as part of the render toward generating your framebuffer-rendered-to-texture, or if you are talking about your framebuffer-rendered-to-texture. Without code we can only guess.

ocornut avatar May 21 '24 16:05 ocornut

Through testing a bunch I've found out that my model matrix is getting screwed up for some reason. Also just to give some context, I am using ImGui to display a render texture using ImGui::Image. However the part that is going wrong happens during the rendering of the render texture, and the render texture is rendering a scene that contains a gameObject with a texture that looks like this: watermark The GameObject's texture is being passed correctly, but the render texture isn't rendering anything.

void K::Transform::PassModelMatrix(K::Transform* parent) 
{
	K::Matrix4x4 globalModelMatrix = K::Matrix4x4::IdentityMatrix();

	if (parent == nullptr)
	{
		//World space transforms are in control
		*this->localScale = *this->scale;
		*this->localRotation = *this->rotation;
		*this->localPosition = *this->position;
		//Scaling Matrix
		globalModelMatrix.m[0][0] *= this->scale->x;
		globalModelMatrix.m[1][1] *= this->scale->y;
		globalModelMatrix.m[2][2] *= this->scale->z;
		//Rotation Matrix
		globalModelMatrix = K::Matrix4x4::Matrix_MultiplyMatrix(globalModelMatrix, *K::Quaternion::Euler(this->rotation)->QuaternionToMatrix());
		//Translation Matrix
		globalModelMatrix.m[3][0] += this->position->x;
		globalModelMatrix.m[3][1] += this->position->y;
		globalModelMatrix.m[3][2] += this->position->z;
	}
	else
	{
		if (*this->position != this->previousPosition)
		{
			*this->localPosition = *this->position;
			K::Matrix4x4 invert = K::QuickInverse(parent->modelMatrix);
			K::MultiplyMatrixVector(*this->position, *this->localPosition, invert);
		}
		if (*this->rotation != this->previousRotation)
		{
			*this->localRotation = *this->rotation - *parent->rotation;
		}
		if (*this->scale != this->previousScale)
		{
			*this->localScale = *this->scale / *parent->scale;
		}

		//Local space transforms are in control
		*this->scale = *this->localScale * *parent->scale;
		*this->rotation = *this->localRotation + *parent->rotation;
		K::MultiplyMatrixVector(*this->localPosition, *this->position, parent->modelMatrix);
		//Scaling Matrix
		globalModelMatrix.m[0][0] *= this->scale->x;
		globalModelMatrix.m[1][1] *= this->scale->y;
		globalModelMatrix.m[2][2] *= this->scale->z;
		//Rotation Matrix
		globalModelMatrix = K::Matrix4x4::Matrix_MultiplyMatrix(globalModelMatrix, *K::Quaternion::Euler(this->rotation)->QuaternionToMatrix());
		//Translation Matrix
		globalModelMatrix.m[3][0] += this->position->x;
		globalModelMatrix.m[3][1] += this->position->y;
		globalModelMatrix.m[3][2] += this->position->z;
	}

	this->modelMatrix = globalModelMatrix;

	this->previousPosition = *this->position;
	this->previousRotation = *this->rotation;
	this->previousScale = *this->scale;
}

This is the code(^) used to construct the model matrix, this->modelMatrix is the final model matrix that gets passed to the shader.

void GameObject::PassTransformationMatrix()
{
	if (this->parent == nullptr) 
		this->GetTransform()->PassModelMatrix();
	else 
		this->GetTransform()->PassModelMatrix(this->parent->GetTransform());
	glUniformMatrix4fv(glGetUniformLocation(this->material->GetShader()->shader, "modelMatrix"), 1, GL_FALSE, &this->GetTransform()->modelMatrix.m[0][0]);
}

Platforming-Mayhem avatar May 21 '24 17:05 Platforming-Mayhem

I managed to fix the issue, it was an issue with how I setup my rotation Matrix and how it was applying to the model matrix. It ended up exploding everything. Thanks for helping me, even though it wasn't an issue with your codebase :)

Platforming-Mayhem avatar May 22 '24 18:05 Platforming-Mayhem