]> code.bitgloo.com Git - clyne/gamedev.git/commitdiff
particle textures fixed
authorClyne Sullivan <tullivan99@gmail.com>
Sun, 8 Jan 2017 22:42:07 +0000 (17:42 -0500)
committerClyne Sullivan <tullivan99@gmail.com>
Sun, 8 Jan 2017 22:42:07 +0000 (17:42 -0500)
include/particle.hpp
include/texture.hpp
src/particle.cpp
src/texture.cpp
xml/!town.xml

index a022583d26d5bda01bde335791bdb5a7cbe99e1c..63f23e379de6c577c526c2f1c0dde9b8fd0b1a96 100644 (file)
@@ -19,8 +19,10 @@ struct Particle {
        ParticleType type;
        int timeLeft;
 
+       //const Texture& color; // TODO
+
        Particle(vec2 p, ParticleType t = ParticleType::Drop, int tl = 3000)
-               : location(p), type(t), timeLeft(tl) {} // TODO times
+               : location(p), type(t), timeLeft(tl) {}
 } __attribute__ ((packed));
 
 class ParticleSystem : public entityx::System<ParticleSystem> {
index 878955e524388373f48ff504a760538e54bdd435..3cb8d1fa5b39bac83b1686b1b0ccf7e507411962 100644 (file)
@@ -19,7 +19,7 @@
  * Handles a single texture, loaded from the given file.
  */
 class Texture {
-private:
+protected:
        std::string name; /**< The name (path) of the loaded file. */
        GLuint tex;       /**< The GLuint for the loaded texture. */
        vec2 dim;         /**< The dimensions of the loaded texture. */
index 1cab0e9125f1e2e871f8201e1e202f3d8fa8e419..c9c79c7274cc85a110b5872ce37a278d4c5fd17b 100644 (file)
@@ -28,71 +28,60 @@ void ParticleSystem::addMultiple(const int& count, const ParticleType& type, std
 
 void ParticleSystem::render(void)
 {
-       static GLuint particleVBO = 9999, texVBO = 0;
+       static GLuint particleVBO = 9999;
+       // six vertices, 3d coord + 2d tex coord = 5
+       constexpr auto entrySize = (6 * 5) * sizeof(GLfloat);
 
        if (particleVBO == 9999) {
+               // generate VBO
                glGenBuffers(1, &particleVBO);
                glBindBuffer(GL_ARRAY_BUFFER, particleVBO);
-               glBufferData(GL_ARRAY_BUFFER, parts.capacity() * 18 * sizeof(GLfloat), nullptr,
+               glBufferData(GL_ARRAY_BUFFER, parts.capacity() * entrySize, nullptr,
                        GL_STREAM_DRAW);
-
-               glGenBuffers(1, &texVBO);
-               glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, texVBO);
-               glBufferData(GL_ELEMENT_ARRAY_BUFFER, parts.capacity() * 12 * sizeof(GLfloat), nullptr,
-                       GL_STATIC_DRAW);
-               int top = (parts.capacity() - 1) * 12 * sizeof(GLfloat), i = 0;
-               unsigned char zero = 0;
-               while (i < top) {
-                       glBufferSubData(GL_ELEMENT_ARRAY_BUFFER, i, 1, &zero);
-                       i++;
-               }
        }
 
-       glBindBuffer(GL_ARRAY_BUFFER, particleVBO);
-
        // clear dead particles
        parts.erase(std::remove_if(parts.begin(), parts.end(),
                [](const Particle& p) { return p.timeLeft <= 0; }), parts.end());
 
        // copy data into VBO
+       glBindBuffer(GL_ARRAY_BUFFER, particleVBO);
+
        int offset = 0;
        for (const auto& p : parts) {
-
-               GLfloat coords[18] = {
-                       p.location.x, p.location.y, -1,
-                       p.location.x, p.location.y + 5, -1,
-                       p.location.x + 5, p.location.y + 5, -1,
-                       p.location.x + 5, p.location.y + 5, -1,
-                       p.location.x + 5, p.location.y, -1,
-                       p.location.x, p.location.y, -1
+               static const auto hl = game::HLINE;
+               GLfloat coords[30] = {
+                       p.location.x,      p.location.y,      -1, 0, 0,
+                       p.location.x,      p.location.y + hl, -1, 0, 1,
+                       p.location.x + hl, p.location.y + hl, -1, 1, 1,
+                       p.location.x + hl, p.location.y + hl, -1, 1, 1,
+                       p.location.x + hl, p.location.y,      -1, 0, 1,
+                       p.location.x,      p.location.y,      -1, 0, 0
                };
 
-               glBufferSubData(GL_ARRAY_BUFFER, offset, 18 * sizeof(GLfloat), coords);
-               offset += 18 * sizeof(GLfloat);
+               glBufferSubData(GL_ARRAY_BUFFER, offset, entrySize, coords);
+               offset += entrySize;
        }
 
+       // begin actual rendering
        Render::worldShader.use();
        Render::worldShader.enable();
-       Colors::blue.use();
-       //glUniform4f(Render::worldShader.uniform[WU_tex_color], 0.0f, 0.0f, 1.0f, 1.0f);
 
-       glEnableClientState(GL_VERTEX_ARRAY);
+       Colors::white.use();
 
+       // set coords
        glBindBuffer(GL_ARRAY_BUFFER, particleVBO);
-       glVertexAttribPointer(Render::worldShader.coord, 3, GL_FLOAT, GL_FALSE, 0, 0);
-
-       glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, texVBO);
-       glVertexAttribPointer(Render::worldShader.tex, 2, GL_FLOAT, GL_FALSE, 0, 0);
+       glVertexAttribPointer(Render::worldShader.coord, 3, GL_FLOAT, GL_FALSE,
+               5 * sizeof(GLfloat), 0);
+       // set tex coords
+       glVertexAttribPointer(Render::worldShader.tex, 2, GL_FLOAT, GL_FALSE,
+               5 * sizeof(GLfloat), reinterpret_cast<void*>(3 * sizeof(GLfloat)));
 
        glDrawArrays(GL_TRIANGLES, 0, parts.size() * 6);
 
-       glDisableClientState(GL_VERTEX_ARRAY);
-
+       glBindBuffer(GL_ARRAY_BUFFER, 0);
        Render::worldShader.disable();
        Render::worldShader.unuse();
-
-       glBindBuffer(GL_ARRAY_BUFFER, 0);
-       glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
 }
 
 void ParticleSystem::update(entityx::EntityManager &en, entityx::EventManager &ev, entityx::TimeDelta dt)
index da39ec053c2037fdf0f73d24ca6fcfcf40ca1471..b47c3c74d498856e954107bd2fd0ab07ebeaaf1f 100644 (file)
@@ -52,12 +52,12 @@ ColorTex::ColorTex(const Color& color)
        };
 
        GLuint object;
-       glActiveTexture(GL_TEXTURE0);
        glGenTextures(1, &object);                              // Turns "object" into a texture
        glBindTexture(GL_TEXTURE_2D, object);   // Binds "object" to the top of the stack
        glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 1, 1, 0, GL_RGBA, GL_UNSIGNED_BYTE, data);
 
-       Texture("", object, vec2());
+       tex = object;
+       dim = vec2(1, 1);
 }
 
 static std::vector<Texture> loadedTextures;
index 230cbdc9083caa751507ec4f0bb2ccacfa4b7467..0a281240eed22257c18a2bd4126b5b57ef054942 100644 (file)
@@ -4,7 +4,7 @@
 <World>
     <style background="0" bgm="assets/music/embark.wav" folder="assets/style/classic/"/>
     <generation width="320"/>
-       <weather>Sunny</weather>
+       <weather>Snowy</weather>
     <time>6000</time>
     <link right="!town2.xml"/>
     <spawnx>-300</spawnx>