]> code.bitgloo.com Git - clyne/gamedev2.git/commitdiff
THE CAT IS BACK
authorAndy Belle-Isle <drumsetmonkey@gmail.com>
Sun, 1 Sep 2019 23:02:49 +0000 (19:02 -0400)
committerAndy Belle-Isle <drumsetmonkey@gmail.com>
Sun, 1 Sep 2019 23:02:49 +0000 (19:02 -0400)
Makefile
Scripts/init.lua
Shaders/world.frag
Shaders/world.vert
src/components/Render.hpp
src/render.cpp
src/texture.cpp [new file with mode: 0644]
src/texture.hpp [new file with mode: 0644]

index 06db96231a38b27f9777ced845387436cf008d35..2e7b55d76e155b1491c944c2081696c6a68e73af 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -36,12 +36,12 @@ OBJEXT = o
 DEPEXT = d
 
 LIBDIR = lib
-LIBS   = -L$(LIBDIR) -lSDL2 -lpthread -lentityx -ldl -lluajit -lGLEW -lGL -lSDL2_image
+LIBS   = -L$(LIBDIR) -lSDL2 -lpthread -lentityx -ldl -lluajit -lGLEW -lGL -lSDL2_image -lSOIL
 
 CXXFLAGS = -ggdb -std=c++17 -Wall -Wextra -Werror -pedantic
 
 CXXINCS = -Isrc -I$(LIBDIR)/LuaJIT/src -I$(LIBDIR)/entityx \
-               -I$(LIBDIR)/LuaBridge/Source -I$(LIBDIR)/sol2/include
+               -I$(LIBDIR)/LuaBridge/Source -I$(LIBDIR)/sol2/include -I$(LIBDIR)/soil
 
 CXXSRC := $(shell find $(SRCDIR) -type f -name *.$(SRCEXT))
 CXXOBJ := $(patsubst $(SRCDIR)/%,$(OUTDIR)/%,$(CXXSRC:.$(SRCEXT)=.$(OBJEXT)))
index 59920c19bf4c0bfa5aa93a3cc3e0691e9cb517fb..3c9cf80c3650d8f55c56a7bcebeabaf06192f1d0 100644 (file)
@@ -14,7 +14,7 @@ bird = {
         print("Bird spawn")
     end,
     Render = {
-        texture = "",
+        texture = "Assets/cat.png",
         visible = true
     },
     Idle = function(self)
index c23c923fc9a7e46c67c97d238e986279a8ea0534..ded3ec0ea03826cd4d2c1f983812ee75400d26f2 100644 (file)
@@ -6,12 +6,12 @@ precision highp float;
 precision mediump float;
 #endif
 
-//uniform sampler2D texture;
+uniform sampler2D textu;
 
-//in vec3 texCoord;
+in vec2 texCoord;
 out vec4 FragColor;
 
 void main()
 {
-    FragColor = vec4(1.0, 0.0, 0.0, 1.0);
+    FragColor = texture(textu, texCoord);
 }
index 795997a8aef5a4c53e5d58a54d3dd13357ae880a..743ff3de4d25d875eda40845f6dbe045e4448c22 100644 (file)
@@ -2,14 +2,16 @@
 
 //layout(location = 0)in vec3 vertex;
 in vec3 vertex;
+in vec2 texc;
 
 uniform mat4 projection;
 uniform mat4 view;
 uniform mat4 model;
 
-//out vec3 texCoord;
+out vec2 texCoord;
 
 void main()
 {
+    texCoord = texc;
     gl_Position = projection * view * model * vec4(vertex, 1.0f);
 }
index 451f2d1885a25e2269a31a781b885dae01c99f5e..6cc26dbdd91f35a53c8b86528b1724c47dc26f75 100644 (file)
 #define COMPONENT_RENDER_HPP_
 
 #include "Component.hpp"
+#include "texture.hpp"
 
 struct Render : Component<Render>, entityx::Component<Render>
 {
 public:
-    std::string texture;
+    Texture texture;
     bool visible;
 
     Render(std::string _file) :
@@ -38,7 +39,7 @@ public:
             if (tab["visible"].get_type() == sol::type::boolean)
                 this->visible = tab["visible"];
             if (tab["texture"].get_type() == sol::type::string)
-                this->texture = tab["texture"];
+                this->texture = Texture(static_cast<std::string>(tab["texture"]));
         } else {
             throw std::string(
                 "Render component table formatted incorrectly"
index 27715357edc0cdb612e9adfbdb31455912dcb588..9355513a92dfa146a5f0582656ef6b9b2c63c43e 100644 (file)
@@ -37,6 +37,9 @@ void RenderSystem::update([[maybe_unused]] entityx::EntityManager& entities,
     GLuint p = worldShader.getUniform("projection");
     GLuint m = worldShader.getUniform("model");
     GLuint a = worldShader.getAttribute("vertex");
+    GLuint t = worldShader.getAttribute("texc");
+
+    GLuint q = worldShader.getUniform("textu");
 
     /***********
     *  SETUP  *
@@ -74,31 +77,43 @@ void RenderSystem::update([[maybe_unused]] entityx::EntityManager& entities,
     glEnable(GL_POLYGON_OFFSET_FILL);
 
     glEnableVertexAttribArray(a);
+    glEnableVertexAttribArray(t);
 
     /*************
     *  DRAWING  *
     *************/
 
     entities.each<Render, Position>(
-        [this, a](entityx::Entity, Render &r, Position &p) {
+        [this, a, q, t](entityx::Entity, Render &r, Position &p) {
 
         if (!r.visible)
             return;
 
+        float w = r.texture.width/2.0f;
+        float h = r.texture.height;
+
         GLuint tri_vbo;
         GLfloat tri_data[] = {
-                (float)p.x-10.0f, (float)p.y-10.0f, 00.0f,
-                (float)p.x+10.0f, (float)p.y-10.0f, 00.0f,
-                (float)p.x+00.0f, (float)p.y+10.0f, 00.0f,
+                (float)p.x-w, (float)p.y  , 00.0f, 0.0f, 1.0f,
+                (float)p.x+w, (float)p.y  , 00.0f, 1.0f, 1.0f,
+                (float)p.x-w, (float)p.y+h, 00.0f, 0.0f, 0.0f,
+
+                (float)p.x+w, (float)p.y  , 00.0f, 1.0f, 1.0f,
+                (float)p.x+w, (float)p.y+h, 00.0f, 1.0f, 0.0f,
+                (float)p.x-w, (float)p.y+h, 00.0f, 0.0f, 0.0f,
         };
 
+        glActiveTexture(GL_TEXTURE0);
+        glBindTexture(GL_TEXTURE_2D, r.texture.tex);
+        glUniform1i(q, 0);
+
         glGenBuffers(1, &tri_vbo);
         glBindBuffer(GL_ARRAY_BUFFER, tri_vbo);
         glBufferData(GL_ARRAY_BUFFER, sizeof(tri_data), tri_data, GL_STREAM_DRAW);
 
-        glVertexAttribPointer(a, 3, GL_FLOAT, GL_FALSE, 0, 0);
-        glDrawArrays(GL_TRIANGLES, 0, 3);
-    
+        glVertexAttribPointer(a, 3, GL_FLOAT, GL_FALSE, 5*sizeof(float), 0);
+        glVertexAttribPointer(t, 2, GL_FLOAT, GL_FALSE, 5*sizeof(float), (void*)(3*sizeof(float)));
+        glDrawArrays(GL_TRIANGLES, 0, 6);
     });
     
 
@@ -106,6 +121,7 @@ void RenderSystem::update([[maybe_unused]] entityx::EntityManager& entities,
     *  CLEANUP  *
     *************/
     glDisableVertexAttribArray(a);
+    glDisableVertexAttribArray(t);
 
     glDisable(GL_POLYGON_OFFSET_FILL);
     glDisable(GL_CULL_FACE);
@@ -163,6 +179,9 @@ int RenderSystem::init(void)
     worldShader.addUniform("model");
 
     worldShader.addAttribute("vertex");
+    worldShader.addAttribute("texc");
+
+    worldShader.addUniform("textu");
 
     glEnableVertexAttribArray(worldShader.getAttribute("vertex"));
     glUseProgram(worldShader.getProgram());
diff --git a/src/texture.cpp b/src/texture.cpp
new file mode 100644 (file)
index 0000000..5604812
--- /dev/null
@@ -0,0 +1,20 @@
+#include "texture.hpp"
+
+Texture::Texture(std::string filename)
+{
+    unsigned char* image = SOIL_load_image(filename.c_str(),
+                                           &width, &height, 0,
+                                           SOIL_LOAD_RGBA);
+
+    glGenTextures(1, &tex);                            // Turns "object" into a texture
+       glBindTexture(GL_TEXTURE_2D, tex);      // Binds "object" to the top of the stack
+       glPixelStoref(GL_UNPACK_ALIGNMENT, 1);
+       glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);      // Sets the "min" filter
+       glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);      // The the "max" filter of the stack
+       glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); // Wrap the texture to the matrix
+       glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); //
+       glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0,
+                                GL_RGBA, GL_UNSIGNED_BYTE, image);
+
+    SOIL_free_image_data(image);
+}
diff --git a/src/texture.hpp b/src/texture.hpp
new file mode 100644 (file)
index 0000000..16987f8
--- /dev/null
@@ -0,0 +1,38 @@
+/**
+ * @file texture.hpp
+ *
+ * Copyright (C) 2019  Belle-Isle, Andrew <drumsetmonkey@gmail.com>
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#ifndef TEXTURE_HPP_
+#define TEXTURE_HPP_
+
+#include <soil/SOIL.h>
+#include <SDL2/SDL_opengl.h>
+#include <string>
+
+class Texture
+{
+private:
+public:
+    GLuint tex;
+    int width;
+    int height;
+    Texture() {};
+    Texture(std::string);
+};
+
+#endif//TEXTURE_HPP_