#ifndef MOVING_OBJECTS_H #define MOVING_OBJECTS_H struct Projectile : public MovingGameObject { Projectile() : MovingGameObject("Projectile") { dangerous = true; } bool init() override { init_from_object("ball.obj"); setup_standard_shaders(); setup_texture("brick.jpg"); setup_locations(); printf("vertices.size() %d\n", element_count); return true; } void movement() override { // Update positions! for(int i = 0; i < locations.size(); i++){ locations[i].x += velocity[i].x; locations[i].y += velocity[i].y; if(locations[i].y < -1) velocity[i].y *= -1; locations[i].z += velocity[i].z; velocity[i].w -= 1.0; if(velocity[i].w < 0.0) remove(i); } } void draw(mat4 vp){ update_locations(); // copy new locations to video memory draw_object(vp); } void contact_at(size_t idx){ remove(idx); } }; struct Fragment : public MovingGameObject { Fragment() : MovingGameObject("Fragment") { dangerous = true; } bool init() override { init_from_object("ball.obj"); setup_shaders("frag_vertex.glsl", "frag_fragment.glsl"); setup_locations(); return true; } void movement() override { // Update positions! for(int i = 0; i < locations.size(); i++){ locations[i].x += velocity[i].x; locations[i].y += velocity[i].y; if(locations[i].y < -1) velocity[i].y *= -1; locations[i].z += velocity[i].z; velocity[i].w -= 1.0; if(velocity[i].w < 0.0){ velocity[i] = velocity[velocity.size() - 1]; velocity.pop_back(); locations[i] = locations[locations.size() - 1]; locations.pop_back(); } velocity[i].y -= 0.001; } } void draw(mat4 vp) { update_locations(); // copy new locations to video memory glUseProgram(program); glUniformMatrix4fv(mvp_uniform, 1, 0, glm::value_ptr(vp)); glEnableVertexAttribArray(vertex_attrib); glBindBuffer(GL_ARRAY_BUFFER, vbuf); glVertexAttribPointer(vertex_attrib, 3, GL_FLOAT, GL_FALSE, 0, 0); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ebuf); glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 0, lbuf); glDrawElementsInstanced(GL_TRIANGLES, element_count, GL_UNSIGNED_INT, 0, locations.size()); } }; EXTERN vector objects; EXTERN Fragment *fragments; EXTERN Projectile *projectiles; #endif