Skip to content

Commit 10c1402

Browse files
committed
Allow to use opacity texture
1 parent 62e8dd0 commit 10c1402

File tree

8 files changed

+66
-2
lines changed

8 files changed

+66
-2
lines changed

assets/shaders/basic_fragment.frag

+10-1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ uniform vec3 CameraPos;
1414

1515
uniform sampler2D diffuseTexture;
1616
uniform sampler2D specularTexture;
17+
uniform sampler2D opacityTexture;
18+
uniform bool UseOpacity;
1719

1820
void main()
1921
{
@@ -41,5 +43,12 @@ void main()
4143
specular = specularStrength * spec * LightColor;
4244
}
4345

44-
color = vec4((ambient + diffuse + specular) * objectColor, 1.0f);
46+
float opacity = 1.f;
47+
48+
if(UseOpacity)
49+
{
50+
opacity = texture(opacityTexture, texCoords).x;
51+
}
52+
53+
color = vec4((ambient + diffuse + specular) * objectColor, opacity);
4554
}

src/Application.cpp

+27
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,22 @@ void Application::DrawImguiUi()
226226
if(ImGui::Button("Load specular"))
227227
ImGuiFileDialog::Instance()
228228
->OpenDialog("ChooseSpecular", "Choose specular texture", ".png,.jpg,.jpeg", ".");
229+
230+
ImGui::Image((void*)(intptr_t)m_model->GetMaterial()->opacity.id, {128, 128});
231+
232+
ImGui::SameLine();
233+
234+
if(ImGui::Button("Load opacity"))
235+
ImGuiFileDialog::Instance()
236+
->OpenDialog("ChooseOpacity", "Choose opacity texture", ".png,.jpg,.jpeg", ".");
237+
238+
ImGui::SameLine();
239+
240+
bool useOpacity = m_model->GetUseOpacity();
241+
if(ImGui::Checkbox("Use opacity", &useOpacity))
242+
{
243+
m_model->UseOpacity(useOpacity);
244+
}
229245
}
230246
}
231247

@@ -262,6 +278,17 @@ void Application::DrawImguiUi()
262278
ImGuiFileDialog::Instance()->Close();
263279
}
264280

281+
if (ImGuiFileDialog::Instance()->Display("ChooseOpacity"))
282+
{
283+
if (ImGuiFileDialog::Instance()->IsOk())
284+
{
285+
std::string path = ImGuiFileDialog::Instance()->GetFilePathName();
286+
LoadTexture(path, TextureType::Opacity);
287+
}
288+
289+
ImGuiFileDialog::Instance()->Close();
290+
}
291+
265292
if(ImGui::CollapsingHeader("Actions"))
266293
{
267294
if (ImGui::Button("Reset camera position"))

src/Material.cpp

+4
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ void Material::UnloadMaterial()
44
{
55
specular.Unload();
66
diffuse.Unload();
7+
opacity.Unload();
78
}
89

910
void Material::ReplaceTexture(Texture newTexture, TextureType type)
@@ -17,6 +18,9 @@ void Material::ReplaceTexture(Texture newTexture, TextureType type)
1718
case TextureType::Specular:
1819
texture = &specular;
1920
break;
21+
case TextureType::Opacity:
22+
texture = &opacity;
23+
break;
2024
default:
2125
throw NotImplementedException();
2226
}

src/Material.h

+1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ struct Material
88
{
99
Texture diffuse;
1010
Texture specular;
11+
Texture opacity;
1112

1213
void UnloadMaterial();
1314
void ReplaceTexture(Texture newTexture, TextureType type);

src/Model.cpp

+10
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,16 @@ void Model::SetShader(Shader* shader)
2525
m_shader = shader;
2626
}
2727

28+
void Model::UseOpacity(bool status)
29+
{
30+
m_useOpacity = status;
31+
}
32+
33+
bool Model::GetUseOpacity()
34+
{
35+
return m_useOpacity;
36+
}
37+
2838
void Model::Unload()
2939
{
3040
// m_material->UnloadMaterial();

src/Model.h

+4
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ class Model : public Object
1717
Shader* m_shader;
1818
Material* m_material;
1919
std::vector<Mesh>* m_meshes;
20+
bool m_useOpacity = false;
2021

2122
public:
2223

@@ -27,6 +28,9 @@ class Model : public Object
2728
void SetMaterial(Material* material);
2829
void SetShader(Shader* shader);
2930

31+
void UseOpacity(bool status);
32+
bool GetUseOpacity();
33+
3034
void Unload();
3135

3236
};

src/Renderer.cpp

+8
Original file line numberDiff line numberDiff line change
@@ -31,16 +31,21 @@ void Renderer::Draw(Model* model, Camera* camera, DirLight* light)
3131
shader->SetVec3Uniform("LightDir", light->GetForwardVector());
3232
shader->SetVec3Uniform("LightColor", light->color);
3333
shader->SetFloatUniform("LightShiness", light->shiness);
34+
shader->SetBoolUniform("UseOpacity", model->m_useOpacity);
3435

3536
shader->SetIntUniform("diffuseTexture", 0);
3637
shader->SetIntUniform("specularTexture", 1);
38+
shader->SetIntUniform("opacityTexture", 2);
3739

3840
glActiveTexture(GL_TEXTURE0);
3941
glBindTexture(GL_TEXTURE_2D, model->m_material->diffuse.id);
4042

4143
glActiveTexture(GL_TEXTURE1);
4244
glBindTexture(GL_TEXTURE_2D, model->m_material->specular.id);
4345

46+
glActiveTexture(GL_TEXTURE2);
47+
glBindTexture(GL_TEXTURE_2D, model->m_material->opacity.id);
48+
4449
for (Mesh mesh : model->m_meshes[0])
4550
{
4651
glBindVertexArray(mesh.m_meshData.vao);
@@ -53,6 +58,9 @@ void Renderer::Draw(Model* model, Camera* camera, DirLight* light)
5358

5459
glActiveTexture(GL_TEXTURE1);
5560
glBindTexture(GL_TEXTURE_2D, 0);
61+
62+
glActiveTexture(GL_TEXTURE2);
63+
glBindTexture(GL_TEXTURE_2D, 0);
5664
}
5765

5866
void Renderer::Shutdown()

src/Texture.h

+2-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@
55
enum class TextureType
66
{
77
Diffuse,
8-
Specular
8+
Specular,
9+
Opacity
910
};
1011

1112
struct Texture

0 commit comments

Comments
 (0)