From 5c46e0f0a924989201c6784b0f956bc442f14a7e Mon Sep 17 00:00:00 2001 From: majortom6 Date: Sun, 19 Feb 2017 09:17:35 -0600 Subject: -removed glm library, its in the debian repos -made opengl and sdl folders in includes, moved various *hpps to them --- inc/opengl/basicshader.frag | 9 ------ inc/opengl/basicshader.geom | 33 ------------------- inc/opengl/basicshader.vert | 15 --------- inc/opengl/camera.hpp | 57 +++++++++++++++++++++++++++++++++ inc/opengl/geoshader.hpp | 30 ++++++++++++++++++ inc/opengl/graphicsdata.hpp | 49 +++++++++++++++++++++++++++++ inc/opengl/rectdrawer.hpp | 47 +++++++++++++++++++++++++++ inc/opengl/shaders/basicshader.frag | 9 ++++++ inc/opengl/shaders/basicshader.geom | 33 +++++++++++++++++++ inc/opengl/shaders/basicshader.vert | 15 +++++++++ inc/opengl/shaders/theshader.frag | 7 +++++ inc/opengl/shaders/theshader.geom | 24 ++++++++++++++ inc/opengl/shaders/theshader.vert | 15 +++++++++ inc/opengl/spritebatch.hpp | 63 +++++++++++++++++++++++++++++++++++++ inc/opengl/theshader.frag | 7 ----- inc/opengl/theshader.geom | 24 -------------- inc/opengl/theshader.vert | 15 --------- inc/opengl/transform.hpp | 52 ++++++++++++++++++++++++++++++ 18 files changed, 401 insertions(+), 103 deletions(-) delete mode 100644 inc/opengl/basicshader.frag delete mode 100644 inc/opengl/basicshader.geom delete mode 100644 inc/opengl/basicshader.vert create mode 100644 inc/opengl/camera.hpp create mode 100644 inc/opengl/geoshader.hpp create mode 100644 inc/opengl/graphicsdata.hpp create mode 100644 inc/opengl/rectdrawer.hpp create mode 100644 inc/opengl/shaders/basicshader.frag create mode 100644 inc/opengl/shaders/basicshader.geom create mode 100644 inc/opengl/shaders/basicshader.vert create mode 100644 inc/opengl/shaders/theshader.frag create mode 100644 inc/opengl/shaders/theshader.geom create mode 100644 inc/opengl/shaders/theshader.vert create mode 100644 inc/opengl/spritebatch.hpp delete mode 100644 inc/opengl/theshader.frag delete mode 100644 inc/opengl/theshader.geom delete mode 100644 inc/opengl/theshader.vert create mode 100644 inc/opengl/transform.hpp (limited to 'inc/opengl') diff --git a/inc/opengl/basicshader.frag b/inc/opengl/basicshader.frag deleted file mode 100644 index 9aa9d0b..0000000 --- a/inc/opengl/basicshader.frag +++ /dev/null @@ -1,9 +0,0 @@ -#version 320 es -in highp vec3 fColor; -out highp vec4 outColor; - - -void main() -{ - outColor = vec4(fColor, 1.0); -} \ No newline at end of file diff --git a/inc/opengl/basicshader.geom b/inc/opengl/basicshader.geom deleted file mode 100644 index 3540d10..0000000 --- a/inc/opengl/basicshader.geom +++ /dev/null @@ -1,33 +0,0 @@ -#version 320 es -layout(points) in; -layout(line_strip, max_vertices = 64) out; - -in highp vec3 vColor[]; -in highp float vSides[]; -out highp vec3 fColor; - -const float PI = 3.1415926; - -void main() -{ - fColor = vColor[0]; - - for (int i = 0; i <= 4; i++) { // Angle between each side in radians - - vec4 offset = vec4(vSides[0] *.8,-vSides[0] *1.1, 0.0, 0.0); - if(i==1) - offset = vec4(vSides[0] *.8,vSides[0] *1.1, 0.0, 0.0); - if(i==2) - offset = vec4(-vSides[0] *.8,vSides[0] *1.1, 0.0, 0.0); - if(i==3) - offset = vec4(-vSides[0] *.8,-vSides[0] *1.1, 0.0, 0.0); - if(i==4) - offset = vec4(vSides[0] *.8,-vSides[0] *1.1, 0.0, 0.0); - - - gl_Position = gl_in[0].gl_Position - offset; - - EmitVertex(); - } - EndPrimitive(); -} diff --git a/inc/opengl/basicshader.vert b/inc/opengl/basicshader.vert deleted file mode 100644 index f17aef5..0000000 --- a/inc/opengl/basicshader.vert +++ /dev/null @@ -1,15 +0,0 @@ -#version 320 es -in highp vec2 pos; -in highp vec3 color; -in highp float sides; -uniform mat4 MVP; - -out highp vec3 fColor; -out highp float vSides; - -void main() -{ - gl_Position = MVP * vec4(pos, 0.0, 1.0); - fColor = color; - vSides = sides; -} diff --git a/inc/opengl/camera.hpp b/inc/opengl/camera.hpp new file mode 100644 index 0000000..62ae837 --- /dev/null +++ b/inc/opengl/camera.hpp @@ -0,0 +1,57 @@ +#ifndef camera_h +#define camera_h + +struct Camera +{ + public: + Camera(const glm::vec3& pos, float fov, float aspect, float zNear, float zFar){ + this->pos = pos; + this->forward = glm::vec3(0.0f, 0.0f, -1.0f); + this->up = glm::vec3(0.0f, 1.0f, 0.0f); + this->projection = glm::perspective(fov, aspect, zNear, zFar); + } + + inline glm::mat4 GetViewProjection() const { + return projection * glm::lookAt(pos, pos + forward, up); + } + + void MoveForward(){ + pos += forward * amt; + } + + void MoveBackward(){ + pos += forward * -amt; + } + + void MoveRight(){ + pos += -glm::cross(up, forward) * amt; + } + + void MoveLeft(){ + pos += glm::cross(up, forward) * amt; + } + + void MoveUp(){ + pos += glm::vec3(0,.2,0); + } + + void MoveDown(){ + pos -= glm::vec3(0,.2,0); + } + + void Pitch(float angle){ + glm::vec3 right = glm::normalize(glm::cross(up, forward)); + + forward = glm::vec3(glm::normalize(glm::rotate(angle, right) * glm::vec4(forward, 0.0))); + up = glm::normalize(glm::cross(forward, right)); + } + + private: + float amt = .2; + glm::mat4 projection; + glm::vec3 pos; + glm::vec3 forward; + glm::vec3 up; +}; + +#endif diff --git a/inc/opengl/geoshader.hpp b/inc/opengl/geoshader.hpp new file mode 100644 index 0000000..2619a92 --- /dev/null +++ b/inc/opengl/geoshader.hpp @@ -0,0 +1,30 @@ +#ifndef geoshader_h +#define geoshader_h + +#include +#include + +#include "constants.hpp" +#include "graphicsdata.hpp" +#include "transform.hpp" + +class GeoShader +{ + public: + GeoShader(const std::string& fileName); + + void Bind(); + void Update(const Transform& transform, const Camera& camera); + virtual ~GeoShader(); + GLuint m_program; + + private: + std::string LoadShader(const std::string& fileName); + void CheckShaderError(GLuint shader, GLuint flag, bool isProgram, const std::string& errorMessage); + GLuint CreateShader(const std::string& text, GLenum shaderType); + + GLuint m_shaders[NUM_SHADERS]; + GLuint m_uniforms[NUM_UNIFORMS]; +}; + +#endif diff --git a/inc/opengl/graphicsdata.hpp b/inc/opengl/graphicsdata.hpp new file mode 100644 index 0000000..df8d76e --- /dev/null +++ b/inc/opengl/graphicsdata.hpp @@ -0,0 +1,49 @@ +#ifndef graphicsdata_h +#define graphicsdata_h + +#include +#include + +#include "camera.hpp" + +class GraphicsData +{ + public: + + float x; + float y; + float r; + float g; + float b; + float sides; + + GraphicsData(){ + this->x = 0; + this->y = 0; + this->r = 0; + this->g = 0; + this->b = 0; + this->sides = 0; + } + + + GraphicsData(float x,float y,float r,float g,float b,float sides){ + this->x = 0; + this->y = 0; + this->r = 0; + this->g = 0; + this->b = 0; + this->sides = 0; + } + + float getX(){ + return x; + } + + float getY(){ + return y; + } + +}; + +#endif diff --git a/inc/opengl/rectdrawer.hpp b/inc/opengl/rectdrawer.hpp new file mode 100644 index 0000000..f2ca1e5 --- /dev/null +++ b/inc/opengl/rectdrawer.hpp @@ -0,0 +1,47 @@ +#ifndef rectdrawer_h +#define rectdrawer_h + + +#include +#include +#include +#include "geoshader.hpp" +#include "graphicsdata.hpp" +#include "spritebatch.hpp" + + +//merge this with spritebatch +class Rectdrawer +{ +public: + Rectdrawer(GeoShader theshader); + ~Rectdrawer(); + + + //void init(); + void begin(); + void end(); + void draw(const GraphicsData& gfxData); + void renderBatch(); + +private: + // Creates all the needed RenderBatches + void createRenderBatches(); + + + GLuint _vbo; + GLuint _vao; + + + std::vector _gfxPtr; ///< This is for sorting + std::vector _gfx; ///< These are the actual glyphs + std::vector _renderBatches; + + GeoShader shader; + + +}; + + +#endif + diff --git a/inc/opengl/shaders/basicshader.frag b/inc/opengl/shaders/basicshader.frag new file mode 100644 index 0000000..9aa9d0b --- /dev/null +++ b/inc/opengl/shaders/basicshader.frag @@ -0,0 +1,9 @@ +#version 320 es +in highp vec3 fColor; +out highp vec4 outColor; + + +void main() +{ + outColor = vec4(fColor, 1.0); +} \ No newline at end of file diff --git a/inc/opengl/shaders/basicshader.geom b/inc/opengl/shaders/basicshader.geom new file mode 100644 index 0000000..3540d10 --- /dev/null +++ b/inc/opengl/shaders/basicshader.geom @@ -0,0 +1,33 @@ +#version 320 es +layout(points) in; +layout(line_strip, max_vertices = 64) out; + +in highp vec3 vColor[]; +in highp float vSides[]; +out highp vec3 fColor; + +const float PI = 3.1415926; + +void main() +{ + fColor = vColor[0]; + + for (int i = 0; i <= 4; i++) { // Angle between each side in radians + + vec4 offset = vec4(vSides[0] *.8,-vSides[0] *1.1, 0.0, 0.0); + if(i==1) + offset = vec4(vSides[0] *.8,vSides[0] *1.1, 0.0, 0.0); + if(i==2) + offset = vec4(-vSides[0] *.8,vSides[0] *1.1, 0.0, 0.0); + if(i==3) + offset = vec4(-vSides[0] *.8,-vSides[0] *1.1, 0.0, 0.0); + if(i==4) + offset = vec4(vSides[0] *.8,-vSides[0] *1.1, 0.0, 0.0); + + + gl_Position = gl_in[0].gl_Position - offset; + + EmitVertex(); + } + EndPrimitive(); +} diff --git a/inc/opengl/shaders/basicshader.vert b/inc/opengl/shaders/basicshader.vert new file mode 100644 index 0000000..f17aef5 --- /dev/null +++ b/inc/opengl/shaders/basicshader.vert @@ -0,0 +1,15 @@ +#version 320 es +in highp vec2 pos; +in highp vec3 color; +in highp float sides; +uniform mat4 MVP; + +out highp vec3 fColor; +out highp float vSides; + +void main() +{ + gl_Position = MVP * vec4(pos, 0.0, 1.0); + fColor = color; + vSides = sides; +} diff --git a/inc/opengl/shaders/theshader.frag b/inc/opengl/shaders/theshader.frag new file mode 100644 index 0000000..4f870cb --- /dev/null +++ b/inc/opengl/shaders/theshader.frag @@ -0,0 +1,7 @@ +#version 320 es +in highp vec3 fColor; +out highp vec4 outColor; +void main() +{ + outColor = vec4(fColor, 1.0); +} \ No newline at end of file diff --git a/inc/opengl/shaders/theshader.geom b/inc/opengl/shaders/theshader.geom new file mode 100644 index 0000000..a49e6bc --- /dev/null +++ b/inc/opengl/shaders/theshader.geom @@ -0,0 +1,24 @@ +#version 320 es +layout(points) in; +layout(triangle_strip, max_vertices = 64) out; + +in highp vec3 vColor[]; +in highp float vSides[]; +out highp vec3 fColor; + +const float PI = 3.1415926; + +void main() +{ + fColor = vColor[0]; + + for (float i = 0.0; i <= vSides[0]; i+=1.0) { // Angle between each side in radians + float ang = PI * 2.0 / vSides[0] * i; + // Offset from center of point (0.3 to accomodate for aspect ratio) + vec4 offset = vec4(cos(ang) * 0.3, -sin(ang) * 0.4, 0.0, 0.0); + gl_Position = gl_in[0].gl_Position - offset; + + EmitVertex(); + } + EndPrimitive(); +} \ No newline at end of file diff --git a/inc/opengl/shaders/theshader.vert b/inc/opengl/shaders/theshader.vert new file mode 100644 index 0000000..b7cfe3b --- /dev/null +++ b/inc/opengl/shaders/theshader.vert @@ -0,0 +1,15 @@ +#version 320 es +in highp vec2 pos; +in highp vec3 color; +in highp float sides; +uniform mat4 MVP; + +out highp vec3 fColor; +out highp float vSides; + +void main() +{ + gl_Position = MVP * vec4(pos, 0.0, 1.0); + fColor = color; + vSides = sides; +} diff --git a/inc/opengl/spritebatch.hpp b/inc/opengl/spritebatch.hpp new file mode 100644 index 0000000..3ece593 --- /dev/null +++ b/inc/opengl/spritebatch.hpp @@ -0,0 +1,63 @@ +#ifndef spritebatch_h +#define spritebatch_h + +#include +#include "graphicsdata.hpp" +#include +#include "geoshader.hpp" +#include + + +class RenderBatch { +public: + RenderBatch(GLuint Offset, GLuint NumVertices) : offset(Offset), + numVertices(NumVertices){ + } + GLuint offset; + GLuint numVertices; + //GLuint texture; +}; + + +class SpriteBatch +{ + public: + SpriteBatch(GeoShader theshader); + ~SpriteBatch(); + // Initializes the spritebatch + void init(); + // Begins the spritebatch + void begin(); + // Ends the spritebatch + void end(); + // Adds a glyph to the spritebatch + void draw(const GraphicsData& gfxData); + // Renders the entire SpriteBatch to the screen + void renderBatch(); + + private: + // Creates all the needed RenderBatches + void createRenderBatches(); + // Generates our VAO and VBO + void createVertexArray(); + // Sorts glyphs according to _sortType + //void sortGlyphs(); + // Comparators used by sortGlyphs() + //static bool compareFrontToBack(Glyph* a, Glyph* b); + //static bool compareBackToFront(Glyph* a, Glyph* b); + //static bool compareTexture(Glyph* a, Glyph* b); + + GLuint _vbo; + GLuint _vao; + + //GlyphSortType _sortType; + + std::vector _gfxPtr; ///< This is for sorting + std::vector _gfx; ///< These are the actual glyphs + std::vector _renderBatches; + + GeoShader shader; + +}; + +#endif diff --git a/inc/opengl/theshader.frag b/inc/opengl/theshader.frag deleted file mode 100644 index 4f870cb..0000000 --- a/inc/opengl/theshader.frag +++ /dev/null @@ -1,7 +0,0 @@ -#version 320 es -in highp vec3 fColor; -out highp vec4 outColor; -void main() -{ - outColor = vec4(fColor, 1.0); -} \ No newline at end of file diff --git a/inc/opengl/theshader.geom b/inc/opengl/theshader.geom deleted file mode 100644 index a49e6bc..0000000 --- a/inc/opengl/theshader.geom +++ /dev/null @@ -1,24 +0,0 @@ -#version 320 es -layout(points) in; -layout(triangle_strip, max_vertices = 64) out; - -in highp vec3 vColor[]; -in highp float vSides[]; -out highp vec3 fColor; - -const float PI = 3.1415926; - -void main() -{ - fColor = vColor[0]; - - for (float i = 0.0; i <= vSides[0]; i+=1.0) { // Angle between each side in radians - float ang = PI * 2.0 / vSides[0] * i; - // Offset from center of point (0.3 to accomodate for aspect ratio) - vec4 offset = vec4(cos(ang) * 0.3, -sin(ang) * 0.4, 0.0, 0.0); - gl_Position = gl_in[0].gl_Position - offset; - - EmitVertex(); - } - EndPrimitive(); -} \ No newline at end of file diff --git a/inc/opengl/theshader.vert b/inc/opengl/theshader.vert deleted file mode 100644 index b7cfe3b..0000000 --- a/inc/opengl/theshader.vert +++ /dev/null @@ -1,15 +0,0 @@ -#version 320 es -in highp vec2 pos; -in highp vec3 color; -in highp float sides; -uniform mat4 MVP; - -out highp vec3 fColor; -out highp float vSides; - -void main() -{ - gl_Position = MVP * vec4(pos, 0.0, 1.0); - fColor = color; - vSides = sides; -} diff --git a/inc/opengl/transform.hpp b/inc/opengl/transform.hpp new file mode 100644 index 0000000..9d0988c --- /dev/null +++ b/inc/opengl/transform.hpp @@ -0,0 +1,52 @@ +#ifndef transform_h +#define transform_h + +#include +#include + +#include "camera.hpp" + +struct Transform +{ +public: + Transform(const glm::vec3& pos = glm::vec3(), const glm::vec3& rot = glm::vec3(), const glm::vec3& scale = glm::vec3(1.0f, 1.0f, 1.0f)) + { + this->pos = pos; + this->rot = rot; + this->scale = scale; + } + + inline glm::mat4 GetModel() const + { + glm::mat4 posMat = glm::translate(pos); + glm::mat4 scaleMat = glm::scale(scale); + glm::mat4 rotX = glm::rotate(rot.x, glm::vec3(1.0, 0.0, 0.0)); + glm::mat4 rotY = glm::rotate(rot.y, glm::vec3(0.0, 1.0, 0.0)); + glm::mat4 rotZ = glm::rotate(rot.z, glm::vec3(0.0, 0.0, 1.0)); + glm::mat4 rotMat = rotX * rotY * rotZ; + + return posMat * rotMat * scaleMat; + } + + inline glm::mat4 GetMVP(const Camera& camera) const + { + glm::mat4 VP = camera.GetViewProjection(); + glm::mat4 M = GetModel(); + + return VP * M; + } + + inline glm::vec3* GetPos() { return &pos; } + inline glm::vec3* GetRot() { return &rot; } + inline glm::vec3* GetScale() { return &scale; } + + inline void SetPos(glm::vec3& pos) { this->pos = pos; } + inline void SetRot(glm::vec3& rot) { this->rot = rot; } + inline void SetScale(glm::vec3& scale) { this->scale = scale; } +private: + glm::vec3 pos; + glm::vec3 rot; + glm::vec3 scale; +}; + +#endif -- cgit v1.2.3