Meshes can display textures

This commit is contained in:
Julian Nießner
2018-05-06 17:35:07 +02:00
parent aa9b1a1949
commit 7d616b6ce0
7 changed files with 74 additions and 25 deletions

View File

@@ -1,8 +1,13 @@
#version 300 es #version 300 es
precision mediump float; precision mediump float;
in vec2 TexCoord;
out vec4 FragColor; out vec4 FragColor;
uniform sampler2D theTexture;
void main() { 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);
} }

View File

@@ -1,7 +1,11 @@
#version 300 es #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() { 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;
} }

View File

@@ -13,12 +13,20 @@ DarkRP2D::~DarkRP2D() {
void DarkRP2D::create () { void DarkRP2D::create () {
std::cout << "Hello World!" << std::endl; std::cout << "Hello World!" << std::endl;
assets = new Assets(); assets = new Assets();
assets->loadTexture("bucket.png",false,"bucket"); assets->loadTexture("police_officer.png",true,"police_officer");
assets->loadTexture("drop.png",false,"drop");
assets->loadShader("vert.shader","frag.shader",nullptr,"defaultShader"); assets->loadShader("vert.shader","frag.shader",nullptr,"defaultShader");
assets->loadShader("bitmapvert.shader","bitmapfrag.shader",nullptr,"bitmapShader"); assets->loadShader("bitmapvert.shader","bitmapfrag.shader",nullptr,"bitmapShader");
obj = new Mesh(); std::vector<Vertex2D> 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<unsigned int> indices({
0,1,2,0
});
obj = new Mesh(vertices,indices,assets->getTexture("police_officer"));
inputHandler = new InputHandler(); inputHandler = new InputHandler();
font = new BitmapFont("arial.ttf"); font = new BitmapFont("arial.ttf");

View File

@@ -1,7 +1,7 @@
#ifndef REASSEMBLY2_H #ifndef DarkRP2D_H
#define REASSEMBLY2_H #define DarkRP2D_H
class Reassembly2; class DarkRP2D;
#include <SDL.h> #include <SDL.h>
#include <vector> #include <vector>
#include "Assets.h" #include "Assets.h"

View File

@@ -3,35 +3,51 @@
#include <GL/glew.h> #include <GL/glew.h>
#include <iostream> #include <iostream>
Mesh::Mesh() {
Mesh::Mesh(std::vector<Vertex2D> vertices, std::vector<unsigned int> 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 //Create VBO
glGenBuffers(1, &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); glBindBuffer(GL_ARRAY_BUFFER,VBO);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices),vertices, GL_STATIC_DRAW); glBufferData(GL_ARRAY_BUFFER, vertices.size() * sizeof(Vertex2D), &vertices[0], GL_STATIC_DRAW);
//Create VAO //Create EBO
glGenBuffers(1, &VAO); glGenBuffers(1, &EBO);
glBindVertexArray(VAO); 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 //Set attriPointer and enable
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), (void *) 0);
glEnableVertexAttribArray(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 //Cleanup
glBindBuffer(GL_ARRAY_BUFFER,0);
glBindVertexArray(0); glBindVertexArray(0);
} }
Mesh::~Mesh() { Mesh::~Mesh() {
std::cout << "Deleting Mesh" << std::endl; std::cout << "Deleting Mesh" << std::endl;
glDeleteBuffers(1,&VBO); glDeleteBuffers(1,&VBO);
glDeleteBuffers(1,&EBO);
glDeleteBuffers(1,&VAO); glDeleteBuffers(1,&VAO);
} }
void Mesh::draw() { void Mesh::draw() {
//Bind Texture
texture.Bind();
//draw Mesh
glBindVertexArray(this->VAO); glBindVertexArray(this->VAO);
glDrawArrays(GL_TRIANGLES, 0, 3); glDrawElements(GL_TRIANGLES, indices.size(), GL_UNSIGNED_INT, 0);
glBindVertexArray(0);
} }

View File

@@ -1,12 +1,28 @@
#ifndef MESH_H #ifndef MESH_H
#define MESH_H #define MESH_H
#include "Texture.h"
#include <glm/glm.hpp>
#include <vector>
struct Vertex2D {
glm::vec2 position;
glm::vec2 texCoord;
};
class Mesh { class Mesh {
private: private:
unsigned int VBO, VAO; unsigned int VBO, VAO, EBO;
void setupMesh();
public: public:
Mesh(); /* Mesh Data */
std::vector<Vertex2D> vertices;
std::vector<unsigned int> indices;
Texture2D texture;
Mesh(std::vector<Vertex2D> vertices, std::vector<unsigned int> indices,Texture2D textures);
~Mesh(); ~Mesh();
void draw(); void draw();

View File

@@ -4,7 +4,7 @@
Texture2D::Texture2D() 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); glGenTextures(1, &this->ID);
} }