diff --git a/shaders/frag.shader b/shaders/frag.shader index 249afd3..0ae3840 100644 --- a/shaders/frag.shader +++ b/shaders/frag.shader @@ -1,8 +1,13 @@ #version 300 es precision mediump float; +in vec2 TexCoord; + out vec4 FragColor; +uniform sampler2D theTexture; + void main() { - FragColor = vec4(1.f, 0.5f, 0.2f, 1.0f); + //FragColor = vec4(1.f, 0.5f, 0.2f, 1.0f); + FragColor = texture(theTexture, TexCoord); } diff --git a/shaders/vert.shader b/shaders/vert.shader index 884a645..717d805 100644 --- a/shaders/vert.shader +++ b/shaders/vert.shader @@ -1,7 +1,11 @@ #version 300 es -layout (location = 0) in vec3 aPos; +layout (location = 0) in vec2 aPos; +layout (location = 1) in vec2 texCoord; + +out vec2 TexCoord; void main() { - gl_Position = vec4(aPos.x,aPos.y,aPos.z,1.0); + gl_Position = vec4(aPos.x,aPos.y,0.,1.0); + TexCoord = texCoord; } diff --git a/src/DarkRP2D.cpp b/src/DarkRP2D.cpp index 8300ee4..8aee7fa 100644 --- a/src/DarkRP2D.cpp +++ b/src/DarkRP2D.cpp @@ -13,12 +13,20 @@ DarkRP2D::~DarkRP2D() { void DarkRP2D::create () { std::cout << "Hello World!" << std::endl; assets = new Assets(); - assets->loadTexture("bucket.png",false,"bucket"); - assets->loadTexture("drop.png",false,"drop"); + assets->loadTexture("police_officer.png",true,"police_officer"); assets->loadShader("vert.shader","frag.shader",nullptr,"defaultShader"); assets->loadShader("bitmapvert.shader","bitmapfrag.shader",nullptr,"bitmapShader"); - obj = new Mesh(); + std::vector vertices({ + Vertex2D{glm::vec2(-0.5f,-0.5f), glm::vec2(0.0f,0.0f)}, + Vertex2D{glm::vec2(0.5f,-0.5f), glm::vec2(1.0f,0.0f)}, + Vertex2D{glm::vec2(0.0f,0.5f), glm::vec2(0.5f,1.0f)} + }); + std::vector indices({ + 0,1,2,0 + }); + + obj = new Mesh(vertices,indices,assets->getTexture("police_officer")); inputHandler = new InputHandler(); font = new BitmapFont("arial.ttf"); diff --git a/src/DarkRP2D.h b/src/DarkRP2D.h index 49b862c..71dfedb 100644 --- a/src/DarkRP2D.h +++ b/src/DarkRP2D.h @@ -1,7 +1,7 @@ -#ifndef REASSEMBLY2_H -#define REASSEMBLY2_H +#ifndef DarkRP2D_H +#define DarkRP2D_H -class Reassembly2; +class DarkRP2D; #include #include #include "Assets.h" diff --git a/src/Mesh.cpp b/src/Mesh.cpp index 3101278..d9ba3f4 100644 --- a/src/Mesh.cpp +++ b/src/Mesh.cpp @@ -3,35 +3,51 @@ #include #include -Mesh::Mesh() { + +Mesh::Mesh(std::vector vertices, std::vector indices, Texture2D texture) { + this->texture = texture; + this->vertices = vertices; + this->indices = indices; + + setupMesh(); +} + +void Mesh::setupMesh() { + //Create VAO & Bind + glGenVertexArrays(1, &VAO); + glBindVertexArray(VAO); //Create VBO glGenBuffers(1, &VBO); - float vertices[] = { - -0.5f, -0.5f, 0.0f, - 0.5f, -0.5f, 0.0f, - 0.0f, 0.5f, 0.0f - }; glBindBuffer(GL_ARRAY_BUFFER,VBO); - glBufferData(GL_ARRAY_BUFFER, sizeof(vertices),vertices, GL_STATIC_DRAW); - //Create VAO - glGenBuffers(1, &VAO); - glBindVertexArray(VAO); + glBufferData(GL_ARRAY_BUFFER, vertices.size() * sizeof(Vertex2D), &vertices[0], GL_STATIC_DRAW); + //Create EBO + glGenBuffers(1, &EBO); + glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO); + glBufferData(GL_ELEMENT_ARRAY_BUFFER, indices.size() * sizeof(unsigned int), &indices[0], GL_STATIC_DRAW); + //Set attriPointer and enable - glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), (void *) 0); glEnableVertexAttribArray(0); + glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, sizeof(Vertex2D), (void *) 0); + + glEnableVertexAttribArray(1); + glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, sizeof(Vertex2D), (void *)offsetof(Vertex2D, texCoord)); //Cleanup - glBindBuffer(GL_ARRAY_BUFFER,0); glBindVertexArray(0); } Mesh::~Mesh() { std::cout << "Deleting Mesh" << std::endl; glDeleteBuffers(1,&VBO); + glDeleteBuffers(1,&EBO); glDeleteBuffers(1,&VAO); } void Mesh::draw() { + //Bind Texture + texture.Bind(); + //draw Mesh glBindVertexArray(this->VAO); - glDrawArrays(GL_TRIANGLES, 0, 3); + glDrawElements(GL_TRIANGLES, indices.size(), GL_UNSIGNED_INT, 0); + glBindVertexArray(0); } diff --git a/src/Mesh.h b/src/Mesh.h index b561c27..b0918f7 100644 --- a/src/Mesh.h +++ b/src/Mesh.h @@ -1,12 +1,28 @@ #ifndef MESH_H #define MESH_H +#include "Texture.h" +#include +#include + +struct Vertex2D { + glm::vec2 position; + glm::vec2 texCoord; +}; + class Mesh { private: - unsigned int VBO, VAO; + unsigned int VBO, VAO, EBO; + + void setupMesh(); public: - Mesh(); + /* Mesh Data */ + std::vector vertices; + std::vector indices; + Texture2D texture; + + Mesh(std::vector vertices, std::vector indices,Texture2D textures); ~Mesh(); void draw(); diff --git a/src/Texture.cpp b/src/Texture.cpp index 1b0480b..0240def 100644 --- a/src/Texture.cpp +++ b/src/Texture.cpp @@ -4,7 +4,7 @@ Texture2D::Texture2D() - : Width(0), Height(0), Internal_Format(GL_RGB), Image_Format(GL_RGB), Wrap_S(GL_REPEAT), Wrap_T(GL_REPEAT), Filter_Min(GL_LINEAR), Filter_Max(GL_LINEAR) + : Width(0), Height(0), Internal_Format(GL_RGB), Image_Format(GL_RGB), Wrap_S(GL_REPEAT), Wrap_T(GL_REPEAT), Filter_Min(GL_NEAREST), Filter_Max(GL_NEAREST) { glGenTextures(1, &this->ID); }