From 2012905a73e9dd29f68107826186b00407904933 Mon Sep 17 00:00:00 2001
From: Anselme <anselme16@hotmail.fr>
Date: Thu, 23 Jul 2015 17:43:46 +0200
Subject: [PATCH] added ASCIIModule and ASCIIEntity

---
 asciientity.cpp     | 22 +++++++++++++++++
 asciientity.h       | 60 +++++++++++++++++++++++++++++++++++++++++++++
 asciimodule.cpp     | 11 +++++++++
 asciimodule.h       | 22 +++++++++++++++++
 shader.cpp          |  5 ++++
 shader.h            |  1 +
 sparrowRenderer.pro |  8 ++++--
 7 files changed, 127 insertions(+), 2 deletions(-)
 create mode 100644 asciientity.cpp
 create mode 100644 asciientity.h
 create mode 100644 asciimodule.cpp
 create mode 100644 asciimodule.h

diff --git a/asciientity.cpp b/asciientity.cpp
new file mode 100644
index 0000000..fc0be16
--- /dev/null
+++ b/asciientity.cpp
@@ -0,0 +1,22 @@
+#include "asciientity.h"
+#include <glm/ext.hpp>
+
+#define TEX_ID 0
+
+void ASCIIMaterial::bindAttributes()
+{
+    shader->bindVec4(shader->getLocation("backColor"), backColor);
+    shader->bindVec4(shader->getLocation("fontColor"), fontColor);
+    if(glyphMap != NULL)
+    {
+        glyphMap->bind(TEX_ID);
+        shader->bindInteger(shader->getLocation("glyphMap"), TEX_ID);
+    }
+}
+
+void ASCIIEntity::updateModelView()
+{
+    modelView = glm::translate(glm::mat4(), glm::vec3(position, 0));
+    modelView = glm::scalemodel(modelView, glm::vec3(size, 1));
+}
+
diff --git a/asciientity.h b/asciientity.h
new file mode 100644
index 0000000..9a18172
--- /dev/null
+++ b/asciientity.h
@@ -0,0 +1,60 @@
+#ifndef ASCIIENTITY_H
+#define ASCIIENTITY_H
+
+#include <glew/glew.h>
+#include <glm/vec2.hpp>
+#include <glm/vec4.hpp>
+#include <glm/mat4x4.hpp>
+#include "material.h"
+
+class Texture;
+
+class ASCIIMaterial : public Material
+{
+    Texture* glyphMap;
+    glm::vec4 backColor;
+    glm::vec4 fontColor;
+
+public:
+    ASCIIMaterial(Texture* myGlyphMap = NULL,
+                  glm::vec4 myBackColor = glm::vec4(1, 1, 1, 0),
+                  glm::vec4 myFontColor = glm::vec4(0)) :
+        glyphMap(myGlyphMap),
+        backColor(myBackColor),
+        fontColor(myFontColor)
+    {
+        shader = ResourceBase::getShader("ascii");
+    }
+
+    virtual void bindAttributes();
+};
+
+class ASCIIEntity
+{
+    GLuint rows;
+    GLuint columns;
+    char* textBuffer;
+
+    glm::vec2 position;
+    glm::vec2 size;
+    glm::mat4 modelView;
+
+    ASCIIMaterial* mat;
+
+    void updateModelView();
+
+public:
+    ASCIIEntity(int nbRows = 10, int nbColumns = 40, ASCIIMaterial* myMat = NULL) :
+        position(glm::vec2(0)),
+        size(glm::vec2(1)),
+        mat(myMat),
+        rows(nbRows),
+        columns(nbColumns)
+    {
+        textBuffer = new char[rows*columns];
+        // TODO set all chars to ' '
+        updateModelView();
+    }
+};
+
+#endif // ASCIIENTITY_H
diff --git a/asciimodule.cpp b/asciimodule.cpp
new file mode 100644
index 0000000..9fbf52a
--- /dev/null
+++ b/asciimodule.cpp
@@ -0,0 +1,11 @@
+#include "asciimodule.h"
+
+ASCIIModule::ASCIIModule()
+{
+
+}
+
+void ASCIIModule::renderGL(Camera* myCamera)
+{
+
+}
diff --git a/asciimodule.h b/asciimodule.h
new file mode 100644
index 0000000..083d62b
--- /dev/null
+++ b/asciimodule.h
@@ -0,0 +1,22 @@
+#ifndef ASCIIMODULE_H
+#define ASCIIMODULE_H
+
+#include "module.h"
+#include <vector>
+
+class ASCIIEntity;
+class Shader;
+
+class ASCIIModule : public Module
+{
+    Shader* shader;
+    std::vector<ASCIIEntity*> entities;
+
+
+public:
+    ASCIIModule();
+    void addEntity(ASCIIEntity* myEntity);
+    virtual void renderGL(Camera* myCamera);
+};
+
+#endif // ASCIIMODULE_H
diff --git a/shader.cpp b/shader.cpp
index 57d311a..0d9ec0d 100644
--- a/shader.cpp
+++ b/shader.cpp
@@ -170,6 +170,11 @@ void Shader::bindVec3(GLuint location, glm::vec3 vec)
     glAssert(glUniform3fv(location, 1, glm::value_ptr(vec)));
 }
 
+void Shader::bindVec4(GLuint location, glm::vec4 vec)
+{
+    glAssert(glUniform4fv(location, 1, glm::value_ptr(vec)));
+}
+
 void Shader::bindVec3Array(GLuint location, glm::vec3* vec, int nb_elements)
 {
     glAssert(glUniform3fv(location, nb_elements, (GLfloat*)vec));
diff --git a/shader.h b/shader.h
index 1668af6..7bfa3d0 100644
--- a/shader.h
+++ b/shader.h
@@ -23,6 +23,7 @@ public:
     void bindFloat(GLuint location, float val);
     void bindMatrix(GLuint location, glm::mat4 mat);
     void bindVec3(GLuint location, glm::vec3 vec);
+    void bindVec4(GLuint location, glm::vec4 vec);
     void bindVec3Array(GLuint location, glm::vec3* vec, int nb_elements);
     void bindInteger(GLuint location, GLuint tex_id);
 };
diff --git a/sparrowRenderer.pro b/sparrowRenderer.pro
index b4badef..f3543db 100644
--- a/sparrowRenderer.pro
+++ b/sparrowRenderer.pro
@@ -38,7 +38,9 @@ SOURCES += shader.cpp \
     basicmodule.cpp \
     framebuffer.cpp \
     meshbuilder.cpp \
-    mesh.cpp
+    mesh.cpp \
+    asciimodule.cpp \
+    asciientity.cpp
 
 HEADERS += shader.h \
     camera.h \
@@ -60,7 +62,9 @@ HEADERS += shader.h \
     framebuffer.h \
     meshbuilder.h \
     mesh.h \
-    image.h
+    image.h \
+    asciimodule.h \
+    asciientity.h
 
 OTHER_FILES += *.frag *.vert *.glsl *.todo