Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add native winapi OpenGL example #3218

Closed
wants to merge 2 commits into from

Conversation

learn-more
Copy link
Contributor

Mashed together DirectX9 + glfw opengl2 into a new example that runs on Winapi + OpenGL2
Built on Visual studio 2017, win10, docking branch.

Platform windows work:
2020-05-10_00-16-09

Project downgraded to use the same platform toolset as the rest.

@learn-more
Copy link
Contributor Author

@ocornut is there something you want me to change on this, or add?
I tried to modify no other code, but the change in imgui_impl_win32.cpp was required to get this working.

@learn-more
Copy link
Contributor Author

Rebased on the new docking branch,
corrected paths in the .bat and project files for the new backends layout.

@ocornut
Copy link
Owner

ocornut commented Apr 7, 2023

Sorry for the late answer.... I'm digging through the many PR attempting this and every one of them has issue.
But this solution here seems particularly simple and elegant, can you explain why/how this is much simpler than #6086 ?

Would it make sense to add a ImGui_ImplWin32_InitForOpenGL(void* hwnd); function?
Would this work with the imgui_impl_opengl3.cpp example?

@learn-more
Copy link
Contributor Author

I don't see this being simpler than #6086, but this did have platform windows working.

The reason why there was not a lot of new coded added, was because the opengl2 backend did already bring everything that was needed,
and the win32 platform was almost compatible. (It just needed a modification so it owns the DC.)

@learn-more
Copy link
Contributor Author

@ocornut do you want me to fix the merge conflict here, or are you going to move forward with another PR?

@ocornut
Copy link
Owner

ocornut commented Apr 17, 2023

Hello,

Your PR seems simpler as it requires much less involvement from imgui_impl_win32, which is better.

The question is will it work with the opengl3 backend?
If you can spare some work to confirm that it works with opengl3 backend I would be interested in that.

I don't think it will conflict much so probably easy to fix conflicts.

May I suggest to change:

IMGUI_IMPL_API bool ImGui_ImplWin32_Init(void* hwnd, bool platformHasOwnDC = false);

to

IMGUI_IMPL_API bool ImGui_ImplWin32_Init(void* hwnd);
IMGUI_IMPL_API bool ImGui_ImplWin32_InitForOpenGL(void* hwnd);

@learn-more
Copy link
Contributor Author

@ocornut I have changed the code to use ImGui_ImplWin32_InitForOpenGL in the header.
Also added the same example, ported for OpenGL3 (just changed all 2 to 3 in the code seems to work fine).

@ocornut
Copy link
Owner

ocornut commented Apr 17, 2023

Apologies in previous messages I referred to #6086 which is not viewports-compliant.
I actually meant to compare your PR to #5170

@ocornut
Copy link
Owner

ocornut commented Apr 17, 2023

While experimenting I noticed that the example worked even when calling ImGui_ImplWin32_Init() instead of ImGui_ImplWin32_InitForOpenGL().

Can you clarify why you said CS_OWNDC is needed?

Things works well here and although this is putting a little more responsibility on user app, I think the complexity trade-off is well worth it and this may be the best solution to this problem.

@learn-more
Copy link
Contributor Author

Khronos specifies it:
https://www.khronos.org/opengl/wiki/Creating_an_OpenGL_Context_(WGL)#The_Window_Itself

This StackOverflow post goes into some detail:
https://stackoverflow.com/a/48670747/4928207

In OpenGL you need a context, which is attached with a DC. This means that the DC must exist while the context exists. Thus, you do need CS_OWNDC style for the window where OpenGL draws.
Call ReleaseDC after you have deleted the context.

ocornut pushed a commit that referenced this pull request Apr 19, 2023
…2772, #2600, #2359, #2022, #1553)

Removed mulit-viewpot stuff in this commit on master, will be re-added separately.
@ocornut
Copy link
Owner

ocornut commented Apr 19, 2023

I went through this and merged, was a little bit tedious as had to split this but it's now done.

  • a566ecc your commit but only the parts applicable to master.
  • 9308cfd my amends but only the parts applicable to master.
  • 4f692ba merge
  • 4bc51c6 remaining parts of your commit applicable to multi-viewports
  • f498f08 remaining parts of my amends

Closing... 🥁 🥁 🥁 🥁 🥁
#3218 yours, was initially for GL2 and docking only
#5170 was missing example app (incl main viewport creation) + lots of GL specific code in win32 backend
#6086 no multi-viewport support (hence simpler but incomplete), GL2.
#2772 GL3 specific code in win32 + hardcoded the whole thing for GL only
#2600 not a PR per-se but had lots of discussions.
#2359 no multi-viewport support, GL2.
#2022 no multi-viewport support, GL3.
#1553 no multi-viewport support, GL3.

Your solution won, but I'm assuming we may need to revisit something and I honestly worry this "low-level" example may attract issues so I am explicitly NOT recommending people using it, best to stick to SDL or GLFW for OpenGL. We'll see.

Thanks a lot!

@ocornut ocornut closed this Apr 19, 2023
@ocornut ocornut changed the title Add native winapi OpenGL2 example Add native winapi OpenGL example Apr 19, 2023
@learn-more learn-more deleted the win32_ogl2 branch April 19, 2023 17:19
ocornut added a commit that referenced this pull request Apr 20, 2023
ocornut added a commit that referenced this pull request Apr 20, 2023
…AlphaCompositing() in a566ecc (#3218)

This was still in docking, but moved back in upper locaton.
# Conflicts:
#	backends/imgui_impl_win32.cpp
kjblanchard pushed a commit to kjblanchard/imgui that referenced this pull request May 5, 2023
 and ocornut#6086, ocornut#2772, ocornut#2600, ocornut#2359, ocornut#2022, ocornut#1553)

Removed mulit-viewpot stuff in this commit on master, will be re-added separately.
kjblanchard pushed a commit to kjblanchard/imgui that referenced this pull request May 5, 2023
@wrightwriter
Copy link

wrightwriter commented Oct 26, 2023

Anything specific you need to do on core context creation to get this working?
I'm getting black viewport windows, and i'm 1:1 following the example code. I assume something must be wrong with my core context creation. No OpenGL errors.

Here's the code if anyone has an idea 🤷‍♂️ https://gist.github.com/wrightwriter/4666c989eadba9b8932bf632b01b00f1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants