@@ -28,8 +28,8 @@ std::string meshVShaderPath = pwd + "/../shaders/mesh.vs";
28
28
std::string meshFShaderPath = pwd + " /../shaders/mesh.fs" ;
29
29
std::string colorVShaderPath = pwd + " /../shaders/singleColor.vs" ;
30
30
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" ;
33
33
std::string modelPath = pwd + " /../../assets/backpack/backpack.obj" ;
34
34
std::string cubePath = pwd + " /../../assets/cube/cube.obj" ;
35
35
@@ -53,7 +53,7 @@ float lastX = width / 2.0f;
53
53
float lastY = height / 2 .0f ;
54
54
55
55
// Verticies for a simple 2D quad to display grass texture on
56
- float grassVertices [] =
56
+ float windowVertices [] =
57
57
{
58
58
// positions // texture Coords (swapped y coordinates because texture is flipped upside down)
59
59
0 .0f , 0 .5f , 0 .0f , 0 .0f , 0 .0f ,
@@ -65,7 +65,7 @@ float grassVertices[] =
65
65
1 .0f , 0 .5f , 0 .0f , 1 .0f , 0 .0f
66
66
};
67
67
68
- std::vector<glm::vec3> grassLocations =
68
+ std::vector<glm::vec3> windowsPositions =
69
69
{
70
70
glm::vec3 (0 .0f , 0 .5f , -3 .0f ),
71
71
glm::vec3 (3 .0f , 0 .5f , -3 .0f ),
@@ -74,6 +74,8 @@ std::vector<glm::vec3> grassLocations =
74
74
glm::vec3 (-5 .0f , 0 .5f , -7 .0f ),
75
75
glm::vec3 (-6 .25f , 0 .5f , -2 .0f ),
76
76
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 ),
77
79
};
78
80
79
81
int main ()
@@ -95,40 +97,42 @@ int main()
95
97
// Build and compile shaders
96
98
Shader meshShader (meshVShaderPath.c_str (), meshFShaderPath.c_str ());
97
99
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 ());
99
101
100
102
// Configure OpenGL to use depth buffer
101
103
Shader::enableGL (GL_DEPTH_TEST);
104
+ Shader::enableGL (GL_BLEND);
105
+ glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
102
106
103
107
// Load model
104
108
Model cube (cubePath.c_str ());
105
109
106
110
// 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);
113
117
glEnableVertexAttribArray (0 );
114
118
glVertexAttribPointer (0 , 3 , GL_FLOAT, GL_FALSE, 5 * sizeof (float ), (void *)0 );
115
119
glEnableVertexAttribArray (1 );
116
120
glVertexAttribPointer (1 , 2 , GL_FLOAT, GL_FALSE, 5 * sizeof (float ), (void *)(3 * sizeof (float )));
117
121
118
122
// 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);
122
126
// The default wrapping parameter is GL_REPEAT which is usually ok but when using transparent textures
123
127
// 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);
125
129
// Load texture
126
- grassTexture .loadTexture ();
127
- grassTexture .useTexture ();
130
+ windowTexture .loadTexture ();
131
+ windowTexture .useTexture ();
128
132
129
- grassShader .use ();
133
+ blendShader .use ();
130
134
// Point shader's uniform sampler2D to point to texture unit 0
131
- grassShader .setInt (" texture1" , 0 );
135
+ blendShader .setInt (" texture1" , 0 );
132
136
133
137
// Uncomment to render models in wireframe
134
138
// glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
@@ -151,21 +155,21 @@ int main()
151
155
glm::mat4 projection = glm::perspective (camera.getFov (), width / height, 0 .1f , 100 .0f );
152
156
glm::mat4 view = camera.calculateLookAtMatrix (camera.getPosition (), camera.getPosition () + camera.getFront (), camera.getUp ());
153
157
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);
157
161
158
162
// Render our floor in scene without writing any values to stencil buffer, we only care about the other rendered cubes
159
163
glm::mat4 model = glm::mat4 (1 .0f );
160
164
model = glm::translate (model, glm::vec3 (0 .0f , -1 .1f , 0 .0f ));
161
165
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 );
165
169
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);
169
173
170
174
// Render cubes
171
175
// ------------------------------------------------
@@ -195,17 +199,29 @@ int main()
195
199
meshShader.setMatrix4fv (" model" , 1 , GL_FALSE, model);
196
200
cube.draw (meshShader);
197
201
198
- // Render grass
202
+ // Render transparent windows
199
203
// ------------------------------------------------
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)
205
221
{
206
222
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);
209
225
glDrawArrays (GL_TRIANGLES, 0 , 6 );
210
226
}
211
227
0 commit comments