Solution

Files changed (1) hide show
  1. lab2-textures/lab2_main.cpp +68 -0
lab2-textures/lab2_main.cpp CHANGED
@@ -47,6 +47,8 @@ GLuint positionBuffer, colorBuffer, indexBuffer, texcoordBuffer, vertexArrayObje
47
47
 
48
48
  GLuint texture;
49
49
 
50
+ GLuint vertBuffer2, indexBuffer2, texcoordBuffer2, vertexArrayObject2;
51
+ GLuint texture2;
50
52
 
51
53
 
52
54
  ///////////////////////////////////////////////////////////////////////////////
@@ -119,6 +121,50 @@ void initialize()
119
121
  glBufferData(GL_ELEMENT_ARRAY_BUFFER, labhelper::array_length(indices) * sizeof(float), indices,
120
122
  GL_STATIC_DRAW);
121
123
 
124
+ ///////////////////////////////////////////////////////////////////////////
125
+ // Create the explosion quad
126
+ ///////////////////////////////////////////////////////////////////////////
127
+ const float verts2[] = {
128
+ 2.0f, 0.0f, -20.0f, // v0
129
+ 2.0f, 10.0f, -20.0f, // v1
130
+ 12.0f, 10.0f, -20.0f, // v2
131
+ 12.0f, 0.0f, -20.0f // v3
132
+ };
133
+ float texcoords2[] = {
134
+ 0.0f, 0.0f, // (u,v) for v0
135
+ 0.0f, 1.0f, // (u,v) for v1
136
+ 1.0f, 1.0f, // (u,v) for v2
137
+ 1.0f, 0.0f // (u,v) for v3
138
+ };
139
+ const int indices2[] = {
140
+ 0, 1, 3, // Triangle 1
141
+ 1, 2, 3 // Triangle 2
142
+ };
143
+ // Create the buffer objects
144
+ glGenBuffers(1, &vertBuffer2);
145
+ glBindBuffer(GL_ARRAY_BUFFER, vertBuffer2);
146
+ glBufferData(GL_ARRAY_BUFFER, labhelper::array_length(verts2) * sizeof(float), verts2, GL_STATIC_DRAW);
147
+
148
+ glGenBuffers(1, &texcoordBuffer2);
149
+ glBindBuffer(GL_ARRAY_BUFFER, texcoordBuffer2);
150
+ glBufferData(GL_ARRAY_BUFFER, labhelper::array_length(texcoords2) * sizeof(float), texcoords2,
151
+ GL_STATIC_DRAW);
152
+
153
+ glGenBuffers(1, &indexBuffer2);
154
+ glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, indexBuffer2);
155
+ glBufferData(GL_ELEMENT_ARRAY_BUFFER, labhelper::array_length(indices2) * sizeof(float), indices2,
156
+ GL_STATIC_DRAW);
157
+
158
+ glGenVertexArrays(1, &vertexArrayObject2);
159
+ glBindVertexArray(vertexArrayObject2);
160
+ glBindBuffer(GL_ARRAY_BUFFER, vertBuffer2);
161
+ glVertexAttribPointer(0, 3, GL_FLOAT, false /*normalized*/, 0 /*stride*/, 0 /*offset*/);
162
+ glBindBuffer(GL_ARRAY_BUFFER, texcoordBuffer2);
163
+ glVertexAttribPointer(2, 2, GL_FLOAT, false /*normalized*/, 0 /*stride*/, 0 /*offset*/);
164
+ glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, indexBuffer2);
165
+ glEnableVertexAttribArray(0);
166
+ glDisableVertexAttribArray(1);
167
+ glEnableVertexAttribArray(2);
122
168
 
123
169
  // The loadShaderProgram and linkShaderProgam functions are defined in glutil.cpp and
124
170
  // do exactly what we did in lab1 but are hidden for convenience
@@ -147,6 +193,22 @@ void initialize()
147
193
  glBindTexture(GL_TEXTURE_2D, 0);
148
194
 
149
195
  stbi_image_free(image);
196
+ glGenTextures(1, &texture2);
197
+ glBindTexture(GL_TEXTURE_2D, texture2);
198
+ unsigned char* image2 = stbi_load("../scenes/textures/explosion.png", &w, &h, &comp, STBI_rgb_alpha);
199
+ glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, w, h, 0, GL_RGBA, GL_UNSIGNED_BYTE, image2);
200
+ stbi_image_free(image2);
201
+ //Indicates that the active texture should be repeated,
202
+ //instead of for instance clamped, for texture coordinates > 1 or <-1.
203
+ glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
204
+ glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
205
+ glGenerateMipmap(GL_TEXTURE_2D);
206
+ glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAX_ANISOTROPY_EXT, 16);
207
+ // Sets the type of mipmap interpolation to be used on magnifying and
208
+ // minifying the active texture. These are the nicest available options.
209
+ glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
210
+ glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
211
+ glBindTexture(GL_TEXTURE_2D, 0);
150
212
  }
151
213
 
152
214
 
@@ -195,6 +257,12 @@ void display(void)
195
257
  glBindVertexArray(vertexArrayObject);
196
258
  glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);
197
259
 
260
+ glEnable(GL_BLEND);
261
+ glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
262
+ glBindTexture(GL_TEXTURE_2D, texture2);
263
+ glBindVertexArray(vertexArrayObject2);
264
+ glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);
265
+ glDisable(GL_BLEND);
198
266
 
199
267
  glUseProgram(0); // "unsets" the current shader program. Not really necessary.
200
268
  }