Skip to content

Commit 90ab5ff

Browse files
committed
Finished blending directory
1 parent e5cd2c7 commit 90ab5ff

File tree

5 files changed

+64
-56
lines changed

5 files changed

+64
-56
lines changed

11-blending/main.cpp

Lines changed: 52 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@ std::string meshVShaderPath = pwd + "/../shaders/mesh.vs";
2828
std::string meshFShaderPath = pwd + "/../shaders/mesh.fs";
2929
std::string colorVShaderPath = pwd + "/../shaders/singleColor.vs";
3030
std::string colorFShaderPath = pwd + "/../shaders/singleColor.fs";
31-
std::string grassVShaderPath = pwd + "/../shaders/grass.vs";
32-
std::string grassFShaderPath = pwd + "/../shaders/grass.fs";
31+
std::string blendingVShaderPath = pwd + "/../shaders/blending.vs";
32+
std::string blendingFShaderPath = pwd + "/../shaders/blending.fs";
3333
std::string modelPath = pwd + "/../../assets/backpack/backpack.obj";
3434
std::string cubePath = pwd + "/../../assets/cube/cube.obj";
3535

@@ -53,7 +53,7 @@ float lastX = width / 2.0f;
5353
float lastY = height / 2.0f;
5454

5555
// Verticies for a simple 2D quad to display grass texture on
56-
float grassVertices[] =
56+
float windowVertices[] =
5757
{
5858
// positions // texture Coords (swapped y coordinates because texture is flipped upside down)
5959
0.0f, 0.5f, 0.0f, 0.0f, 0.0f,
@@ -65,7 +65,7 @@ float grassVertices[] =
6565
1.0f, 0.5f, 0.0f, 1.0f, 0.0f
6666
};
6767

68-
std::vector<glm::vec3> grassLocations =
68+
std::vector<glm::vec3> windowsPositions =
6969
{
7070
glm::vec3(0.0f, 0.5f, -3.0f),
7171
glm::vec3(3.0f, 0.5f, -3.0f),
@@ -74,6 +74,8 @@ std::vector<glm::vec3> grassLocations =
7474
glm::vec3(-5.0f, 0.5f, -7.0f),
7575
glm::vec3(-6.25f, 0.5f, -2.0f),
7676
glm::vec3(1.75f, 0.5f, 5.5f),
77+
glm::vec3(3.0f, 0.5f, 10.0f),
78+
glm::vec3(0.0f, 0.5f, -10.0f),
7779
};
7880

7981
int main()
@@ -95,40 +97,42 @@ int main()
9597
// Build and compile shaders
9698
Shader meshShader(meshVShaderPath.c_str(), meshFShaderPath.c_str());
9799
Shader colorShader(colorVShaderPath.c_str(), colorFShaderPath.c_str());
98-
Shader grassShader(grassVShaderPath.c_str(), grassFShaderPath.c_str());
100+
Shader blendShader(blendingVShaderPath.c_str(), blendingFShaderPath.c_str());
99101

100102
// Configure OpenGL to use depth buffer
101103
Shader::enableGL(GL_DEPTH_TEST);
104+
Shader::enableGL(GL_BLEND);
105+
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
102106

103107
// Load model
104108
Model cube(cubePath.c_str());
105109

106110
// Creating 2D quad for grass texture
107-
unsigned int grassVAO, grassVBO;
108-
glGenVertexArrays(1, &grassVAO);
109-
glGenBuffers(1, &grassVBO);
110-
glBindVertexArray(grassVAO);
111-
glBindBuffer(GL_ARRAY_BUFFER, grassVBO);
112-
glBufferData(GL_ARRAY_BUFFER, sizeof(grassVertices), grassVertices, GL_STATIC_DRAW);
111+
unsigned int windowVAO, windowVBO;
112+
glGenVertexArrays(1, &windowVAO);
113+
glGenBuffers(1, &windowVBO);
114+
glBindVertexArray(windowVAO);
115+
glBindBuffer(GL_ARRAY_BUFFER, windowVBO);
116+
glBufferData(GL_ARRAY_BUFFER, sizeof(windowVertices), windowVertices, GL_STATIC_DRAW);
113117
glEnableVertexAttribArray(0);
114118
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 5 * sizeof(float), (void*)0);
115119
glEnableVertexAttribArray(1);
116120
glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 5 * sizeof(float), (void*)(3 * sizeof(float)));
117121

118122
// Create new texture object
119-
Texture grassTexture(pwd + "/../../assets/grass.png");
120-
grassTexture.setTextureId(0);
121-
grassTexture.setTextureUnit(GL_TEXTURE0);
123+
Texture windowTexture(pwd + "/../../assets/transparent_window.png");
124+
windowTexture.setTextureId(0);
125+
windowTexture.setTextureUnit(GL_TEXTURE0);
122126
// The default wrapping parameter is GL_REPEAT which is usually ok but when using transparent textures
123127
// we can have a small white line at the edge of the transparent texture, so using GL_CLAMP_TO_EDGE fixes that
124-
// grassTexture.setWrappingParams(GL_CLAMP_TO_EDGE);
128+
// windowTexture.setWrappingParams(GL_CLAMP_TO_EDGE);
125129
// Load texture
126-
grassTexture.loadTexture();
127-
grassTexture.useTexture();
130+
windowTexture.loadTexture();
131+
windowTexture.useTexture();
128132

129-
grassShader.use();
133+
blendShader.use();
130134
// Point shader's uniform sampler2D to point to texture unit 0
131-
grassShader.setInt("texture1", 0);
135+
blendShader.setInt("texture1", 0);
132136

133137
// Uncomment to render models in wireframe
134138
// glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
@@ -151,21 +155,21 @@ int main()
151155
glm::mat4 projection = glm::perspective(camera.getFov(), width / height, 0.1f, 100.0f);
152156
glm::mat4 view = camera.calculateLookAtMatrix(camera.getPosition(), camera.getPosition() + camera.getFront(), camera.getUp());
153157

154-
colorShader.use();
155-
colorShader.setMatrix4fv("projection", 1, GL_FALSE, projection);
156-
colorShader.setMatrix4fv("view", 1, GL_FALSE, view);
158+
meshShader.use();
159+
meshShader.setMatrix4fv("projection", 1, GL_FALSE, projection);
160+
meshShader.setMatrix4fv("view", 1, GL_FALSE, view);
157161

158162
// Render our floor in scene without writing any values to stencil buffer, we only care about the other rendered cubes
159163
glm::mat4 model = glm::mat4(1.0f);
160164
model = glm::translate(model, glm::vec3(0.0f, -1.1f, 0.0f));
161165
model = glm::scale(model, glm::vec3(1.0f, 0.01f, 1.0f));
162-
colorShader.setMatrix4fv("model", 1, GL_FALSE, model);
163-
colorShader.setVec3("color", glm::value_ptr(glm::vec3(0.2f, 0.4f, 0.7f)));
164-
cube.draw(colorShader);
166+
meshShader.setMatrix4fv("model", 1, GL_FALSE, model);
167+
meshShader.setVec3("color", glm::value_ptr(glm::vec3(0.2f, 0.4f, 0.7f)));
168+
cube.draw(meshShader);
165169

166-
meshShader.use();
167-
meshShader.setMatrix4fv("projection", 1, GL_FALSE, projection);
168-
meshShader.setMatrix4fv("view", 1, GL_FALSE, view);
170+
// meshShader.use();
171+
// meshShader.setMatrix4fv("projection", 1, GL_FALSE, projection);
172+
// meshShader.setMatrix4fv("view", 1, GL_FALSE, view);
169173

170174
// Render cubes
171175
// ------------------------------------------------
@@ -195,17 +199,29 @@ int main()
195199
meshShader.setMatrix4fv("model", 1, GL_FALSE, model);
196200
cube.draw(meshShader);
197201

198-
// Render grass
202+
// Render transparent windows
199203
// ------------------------------------------------
200-
grassShader.use();
201-
grassShader.setMatrix4fv("projection", 1, GL_FALSE, projection);
202-
grassShader.setMatrix4fv("view" , 1, GL_FALSE, view);
203-
glBindVertexArray(grassVAO);
204-
for(unsigned int i = 0; i < grassLocations.size(); i++)
204+
// Sorting windows positions according to their distance from camera
205+
// We need to render the windows from the farthest window to the closest one
206+
// We just create a map with the distance as key and the map will automatically sort the keys for us from low to high
207+
std::map<float, glm::vec3> sortedWindows;
208+
for(unsigned int i = 0; i < windowsPositions.size(); i++)
209+
{
210+
float distance = glm::length(camera.getPosition() - windowsPositions[i]);
211+
sortedWindows[distance] = windowsPositions[i];
212+
}
213+
214+
blendShader.use();
215+
blendShader.setMatrix4fv("projection", 1, GL_FALSE, projection);
216+
blendShader.setMatrix4fv("view" , 1, GL_FALSE, view);
217+
glBindVertexArray(windowVAO);
218+
// Iterate sorted windows in reverse order so we start from the highest (farthest) to the lowest (closest)
219+
// and render the windows in this order
220+
for(std::map<float, glm::vec3>::reverse_iterator iter = sortedWindows.rbegin(); iter != sortedWindows.rend(); ++iter)
205221
{
206222
model = glm::mat4(1.0f);
207-
model = glm::translate(model, grassLocations[i]);
208-
grassShader.setMatrix4fv("model", 1, GL_FALSE, model);
223+
model = glm::translate(model, iter->second);
224+
blendShader.setMatrix4fv("model", 1, GL_FALSE, model);
209225
glDrawArrays(GL_TRIANGLES, 0, 6);
210226
}
211227

11-blending/shaders/blending.fs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#version 330 core
2+
3+
in vec2 texCoords;
4+
5+
out vec4 fragColor;
6+
7+
uniform sampler2D texture1;
8+
9+
void main()
10+
{
11+
fragColor = texture(texture1, texCoords);;
12+
}
File renamed without changes.

11-blending/shaders/grass.fs

Lines changed: 0 additions & 16 deletions
This file was deleted.

11-blending/shaders/mesh.fs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,10 @@
11
#version 330 core
22

3-
// in vec2 texCoords;
4-
53
out vec4 fragColor;
64

75
uniform vec3 color;
8-
// uniform sampler2D texture_diffuse1;
96

107
void main()
118
{
12-
// fragColor = texture(texture_diffuse1, texCoords);
139
fragColor = vec4(color, 1.0);
1410
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy