master
ブランチ master
ShaderPlayground / src / gui / viewport.cc
4.3 KB、153行 生 Blame 履歴
1/*
2# 076 License
3
4Copyright (c) 076 Studio LLC
5
6Permission is hereby granted to any person obtaining a copy of the software
7Shader Playground (the "Software") to use, modify, merge, copy, publish, distribute,
8sublicense, and/or sell copies of the Software, subject to the following conditions:
9
10 1. **Origin Attribution**:
11 - You must not misrepresent the origin of the Software; you must not claim
12 you created the original Software.
13 - If the Software is used in a product, you must either:
14 a. Provide clear attribution in the product's documentation, user interface,
15 or other visible areas, **OR**
16 b. Pay the original developers a fee they specify in writing.
17 2. **Usage Restriction**:
18 - The Software, or any derivative works, dependencies, or libraries
19 incorporating it, must not be used for censorship or to suppress freedom of
20 speech, expression, or creativity. Prohibited uses include, but are not
21 limited to:
22 - Censorship of so-called "hate speech", visuals, non-mainstream opinions,
23 ideas, or objective reality.
24 - Tools or systems designed to restrict access to information or
25 artistic works.
26 3. **Notice Preservation**:
27 - This license and the above copyright notice must remain intact in all copies
28 of the source code.
29
30THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
31INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
32PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
33HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
34CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE
35OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
36*/
37#include <glad/glad.h>
38#include <gui/viewport.hh>
39#include <imgui.h>
40#include <database/locale.hh>
41
42namespace 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} // namespace gui