| 1 | |
| 2 | |
| 3 | |
| 4 | |
| 5 | |
| 6 | |
| 7 | |
| 8 | |
| 9 | |
| 10 | |
| 11 | |
| 12 | |
| 13 | |
| 14 | |
| 15 | |
| 16 | |
| 17 | |
| 18 | |
| 19 | |
| 20 | |
| 21 | |
| 22 | |
| 23 | |
| 24 | |
| 25 | |
| 26 | |
| 27 | |
| 28 | |
| 29 | |
| 30 | |
| 31 | |
| 32 | |
| 33 | |
| 34 | |
| 35 | |
| 36 | |
| 37 | #include <glad/glad.h> |
| 38 | #include <gui/viewport.hh> |
| 39 | #include <imgui.h> |
| 40 | #include <database/locale.hh> |
| 41 | |
| 42 | namespace gui { |
| 43 | void ViewPort::Init() { |
| 44 | glGenFramebuffers(1, &m_FBO); |
| 45 | glBindFramebuffer(GL_FRAMEBUFFER, m_FBO); |
| 46 | |
| 47 | glGenTextures(1, &m_Texture); |
| 48 | glBindTexture(GL_TEXTURE_2D, m_Texture); |
| 49 | glTexImage2D( |
| 50 | GL_TEXTURE_2D, |
| 51 | 0, |
| 52 | GL_RGB, |
| 53 | m_Width, |
| 54 | m_Height, |
| 55 | 0, |
| 56 | GL_RGB, |
| 57 | GL_UNSIGNED_BYTE, |
| 58 | nullptr |
| 59 | ); |
| 60 | |
| 61 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); |
| 62 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); |
| 63 | |
| 64 | glFramebufferTexture2D( |
| 65 | GL_FRAMEBUFFER, |
| 66 | GL_COLOR_ATTACHMENT0, |
| 67 | GL_TEXTURE_2D, |
| 68 | m_Texture, |
| 69 | 0 |
| 70 | ); |
| 71 | |
| 72 | glGenRenderbuffers(1, &m_RBO); |
| 73 | glBindRenderbuffer(GL_RENDERBUFFER, m_RBO); |
| 74 | glRenderbufferStorage( |
| 75 | GL_RENDERBUFFER, |
| 76 | GL_DEPTH24_STENCIL8, |
| 77 | m_Width, |
| 78 | m_Height |
| 79 | ); |
| 80 | |
| 81 | glFramebufferRenderbuffer( |
| 82 | GL_FRAMEBUFFER, |
| 83 | GL_DEPTH_STENCIL_ATTACHMENT, |
| 84 | GL_RENDERBUFFER, |
| 85 | m_RBO |
| 86 | ); |
| 87 | |
| 88 | glBindFramebuffer(GL_FRAMEBUFFER, 0); |
| 89 | } |
| 90 | |
| 91 | void ViewPort::Resize(i32 width, i32 height) { |
| 92 | m_Width = width; |
| 93 | m_Height = height; |
| 94 | |
| 95 | glBindTexture(GL_TEXTURE_2D, m_Texture); |
| 96 | glTexImage2D( |
| 97 | GL_TEXTURE_2D, |
| 98 | 0, |
| 99 | GL_RGB, |
| 100 | width, |
| 101 | height, |
| 102 | 0, |
| 103 | GL_RGB, |
| 104 | GL_UNSIGNED_BYTE, |
| 105 | nullptr |
| 106 | ); |
| 107 | |
| 108 | glBindRenderbuffer(GL_RENDERBUFFER, m_RBO); |
| 109 | glRenderbufferStorage( |
| 110 | GL_RENDERBUFFER, |
| 111 | GL_DEPTH24_STENCIL8, |
| 112 | width, |
| 113 | height |
| 114 | ); |
| 115 | } |
| 116 | |
| 117 | void ViewPort::Draw(Info &info) { |
| 118 | string tit = info.i18n->GetWord("editorviewport") + "###View"; |
| 119 | ImGui::Begin(tit.c_str()); |
| 120 | info.viewportFocused = ImGui::IsWindowFocused(); |
| 121 | |
| 122 | ImVec2 avail = ImGui::GetContentRegionAvail(); |
| 123 | if ((i32)avail.x != m_Width || (i32)avail.y != m_Height) { |
| 124 | Resize((i32)avail.x, (i32)avail.y); |
| 125 | } |
| 126 | |
| 127 | ImGui::Image((ImTextureID)(intptr_t)m_Texture, avail, ImVec2(0, 1), ImVec2(1, 0)); |
| 128 | |
| 129 | if (ImGui::BeginPopupContextItem("scene props")) { |
| 130 | Vector4 col = info.sceneClearColor; |
| 131 | ImVec4 bytecol( |
| 132 | col.rgba.r, |
| 133 | col.rgba.g, |
| 134 | col.rgba.b, |
| 135 | col.rgba.a |
| 136 | ); |
| 137 | |
| 138 | ImGui::Text("%s", info.i18n->GetWord("clearcolor").c_str()); |
| 139 | ImGui::SameLine(); |
| 140 | ImGui::ColorEdit4("##clearcolor", (f32 *)&bytecol); |
| 141 | |
| 142 | col.rgba.a = bytecol.w; |
| 143 | col.rgba.r = bytecol.x; |
| 144 | col.rgba.g = bytecol.y; |
| 145 | col.rgba.b = bytecol.z; |
| 146 | info.sceneClearColor = col; |
| 147 | |
| 148 | ImGui::EndPopup(); |
| 149 | } |
| 150 | |
| 151 | ImGui::End(); |
| 152 | } |
| 153 | } |