Adding textures capabilities

* Added Texures to the two sample cubes
* Modified mesh structure
	- mesh now contains indices and vertices
	- Vertices contain vertices and uv's (later on they will also
	contain normals)

Solution definitely not perfect and needs improvement.
This commit is contained in:
Nigel Barink 2022-06-05 01:44:54 +02:00
parent d019155d10
commit 16b61986a1
17 changed files with 8412 additions and 272 deletions

View File

@ -2,9 +2,11 @@
#include "glm/glm.hpp"
#include "graphics/Shader.h"
#include "graphics/Window.h"
#include "graphics/Texture.h"
#include "graphics/Camera.h"
#include "graphics/Renderable.h"
#include "Graphics/Material.h"
#include "spdlog/spdlog.h"
#include "Input/InputManager.h"

View File

@ -1,13 +1,20 @@
#pragma once
#include <vector>
#include <glm/glm.hpp>
namespace BarinkEngine{
class Mesh {
public:
std::vector<glm::vec3> vertices;
std::vector<unsigned int > elements;
std::vector<glm::vec2> uv;
};
#pragma once
#include <vector>
#include <glm/glm.hpp>
namespace BarinkEngine{
struct Vertex {
glm::vec3 vertices;
glm::vec2 uv;
};
class Mesh {
public:
std::vector<Vertex> vertices;
std::vector<unsigned int > elements;
};
}

View File

@ -3,6 +3,7 @@
#include "Mesh.h"
#include "Buffer.h"
#include "Material.h"
#include "Texture.h"
#include "VertexArray.h"
#include "Scene.h"
@ -14,10 +15,13 @@ public:
*/
Buffer vertexBuffer;
Buffer elementBuffer;
//Buffer uv;
VertexArray VAO;
GLuint UV_id;
Material* material;
Texture* texture;
Shader* shader;

View File

@ -10,7 +10,7 @@
class Shader {
private:
int id;
char* readFile (const char* filePath);
public:
Shader(const std::string vertexShaderPath, const std::string fragmentShaderPath);
@ -22,5 +22,6 @@ class Shader {
void setUniformFloat(std::string uniformName, float value)const;
void setUniformInt(std::string uniformName, int value) const ;
int id;
};

View File

@ -0,0 +1,18 @@
#pragma once
#include <spdlog/spdlog.h>
#include <string>
class Texture {
public:
Texture(const std::string texturePath);
void Bind();
void Unbind();
private:
unsigned int Id;
};

File diff suppressed because it is too large Load Diff

View File

@ -1,47 +1,47 @@
#include "Graphics/Buffer.h"
int Buffer::getBufferID() {
return id;
}
void Buffer::createBuffer() {
glGenBuffers(1, (GLuint*) &id);
}
void Buffer::setBufferData(void* data, size_t dataSize, bool elementBuffer = false ) {
if (elementBuffer) {
glBufferData(GL_ELEMENT_ARRAY_BUFFER, dataSize, data, GL_STATIC_DRAW);
}
else {
glBufferData(GL_ARRAY_BUFFER, dataSize, data, GL_STATIC_DRAW);
}
}
void Buffer::Bind(bool elementBuffer = false ) {
if (elementBuffer) {
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, id);
}
else {
glBindBuffer(GL_ARRAY_BUFFER, id);
}
}
void Buffer::Unbind(bool elementBuffer = false) {
if (elementBuffer) {
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
}
else {
glBindBuffer(GL_ARRAY_BUFFER, 0);
}
}
void Buffer::Delete() {
glDeleteBuffers(1, (GLuint*) &id);
#include "Graphics/Buffer.h"
int Buffer::getBufferID() {
return id;
}
void Buffer::createBuffer() {
glGenBuffers(1, (GLuint*) &id);
}
void Buffer::setBufferData(void* data, size_t dataSize, bool elementBuffer = false ) {
if (elementBuffer) {
glBufferData(GL_ELEMENT_ARRAY_BUFFER, dataSize, data, GL_STATIC_DRAW);
}
else {
glBufferData(GL_ARRAY_BUFFER, dataSize, data, GL_STATIC_DRAW);
}
}
void Buffer::Bind(bool elementBuffer = false ) {
if (elementBuffer) {
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, id);
}
else {
glBindBuffer(GL_ARRAY_BUFFER, id);
}
}
void Buffer::Unbind(bool elementBuffer = false) {
if (elementBuffer) {
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
}
else {
glBindBuffer(GL_ARRAY_BUFFER, 0);
}
}
void Buffer::Delete() {
glDeleteBuffers(1, (GLuint*) &id);
}

View File

@ -1,115 +1,133 @@
#include "AssetManager/ModelImporter.h"
void ModelImporter::ImportFBX(std::string path)
{
//spdlog::warn("ImportFBX not implemented!");
}
void ModelImporter::ImportBlend(std::string path)
{
//spdlog::warn("ImportBlend not implemented!");
}
void ModelImporter::ImportGLTF(std::string path)
{
//spdlog::warn("ImportGLTF not implemented!");
}
void ModelImporter::ImportOBJ(std::string path)
{
//spdlog::warn("ImportOBJ not implemented!");
}
void ModelImporter::Import(std::string path)
{
//spdlog::warn("Import not implemented!");
}
std::vector<BarinkEngine::Mesh> ModelImporter::Test() {
/*
spdlog::info("====== Tiny GLTF ======");
tinygltf::Model loadedModel;
tinygltf::TinyGLTF loader;
std::string error;
std::string warn;
bool ret = loader.LoadASCIIFromFile(&loadedModel, &error, &warn, "./Build/SandboxApplication/Debug/sponza.gltf");
if (!warn.empty())
spdlog::warn("TinyGLTF Warning: {}", warn);
if (!error.empty())
spdlog::error("TinyGLTF Error: {}", error);
if (!ret) {
spdlog::error("TinyGLTF Error: Failed to parse glTF");
exit(-1);
}
spdlog::info("Meshes in model: {}", loadedModel.meshes.size());
spdlog::info("Primitives in mesh: {}", loadedModel.meshes[0].primitives.size());
*/
//spdlog::info("======= Assimp ======");
Assimp::Importer importer;
const aiScene* scene = importer.ReadFile("build/SandboxApplication/Debug/Cube.obj", aiProcess_Triangulate | aiProcess_FlipUVs);
aiNode* currentNode = scene->mRootNode;
return processNode(currentNode, scene);
}
std::vector<BarinkEngine::Mesh> ModelImporter::processNode(aiNode* node, const aiScene* scene) {
std::vector<BarinkEngine::Mesh> meshes;
for (unsigned int i = 0; i < node->mNumMeshes; i++) {
aiMesh* mesh = scene->mMeshes[node->mMeshes[i]];
meshes.push_back(processMesh(mesh, scene));
}
for (unsigned int i = 0; i < node->mNumChildren; i++) {
auto m2 = processNode(node->mChildren[i], scene);
for(auto m : m2) {
meshes.push_back(m);
}
}
return meshes;
}
BarinkEngine::Mesh ModelImporter::processMesh(aiMesh* mesh, const aiScene* scene) {
std::vector<glm::vec3> vertices ;
std::vector<unsigned int> indices;
// Process vertices
for (unsigned int i = 0; i < mesh->mNumVertices; i++) {
glm::vec3 vector;
vector.x = mesh->mVertices[i].x;
vector.y = mesh->mVertices[i].y;
vector.z = mesh->mVertices[i].z;
vertices.push_back(vector);
}
// Process Indices
for (unsigned int i = 0; i < mesh->mNumFaces; i++) {
aiFace face = mesh->mFaces[i];
if (face.mNumIndices < 3)
continue;
for (unsigned int j = 0; j < face.mNumIndices; j++) {
indices.push_back(face.mIndices[j]);
}
}
BarinkEngine::Mesh result;
result.vertices = vertices;
result.elements = indices;
return result;
#include "AssetManager/ModelImporter.h"
void ModelImporter::ImportFBX(std::string path)
{
//spdlog::warn("ImportFBX not implemented!");
}
void ModelImporter::ImportBlend(std::string path)
{
//spdlog::warn("ImportBlend not implemented!");
}
void ModelImporter::ImportGLTF(std::string path)
{
//spdlog::warn("ImportGLTF not implemented!");
}
void ModelImporter::ImportOBJ(std::string path)
{
//spdlog::warn("ImportOBJ not implemented!");
}
void ModelImporter::Import(std::string path)
{
//spdlog::warn("Import not implemented!");
}
std::vector<BarinkEngine::Mesh> ModelImporter::Test() {
/*
spdlog::info("====== Tiny GLTF ======");
tinygltf::Model loadedModel;
tinygltf::TinyGLTF loader;
std::string error;
std::string warn;
bool ret = loader.LoadASCIIFromFile(&loadedModel, &error, &warn, "./Build/SandboxApplication/Debug/sponza.gltf");
if (!warn.empty())
spdlog::warn("TinyGLTF Warning: {}", warn);
if (!error.empty())
spdlog::error("TinyGLTF Error: {}", error);
if (!ret) {
spdlog::error("TinyGLTF Error: Failed to parse glTF");
exit(-1);
}
spdlog::info("Meshes in model: {}", loadedModel.meshes.size());
spdlog::info("Primitives in mesh: {}", loadedModel.meshes[0].primitives.size());
*/
//spdlog::info("======= Assimp ======");
Assimp::Importer importer;
const aiScene* scene = importer.ReadFile("build/SandboxApplication/Debug/Cube.obj", aiProcess_Triangulate | aiProcess_FlipUVs);
aiNode* currentNode = scene->mRootNode;
return processNode(currentNode, scene);
}
std::vector<BarinkEngine::Mesh> ModelImporter::processNode(aiNode* node, const aiScene* scene) {
std::vector<BarinkEngine::Mesh> meshes;
for (unsigned int i = 0; i < node->mNumMeshes; i++) {
aiMesh* mesh = scene->mMeshes[node->mMeshes[i]];
meshes.push_back(processMesh(mesh, scene));
}
for (unsigned int i = 0; i < node->mNumChildren; i++) {
auto m2 = processNode(node->mChildren[i], scene);
for(auto m : m2) {
meshes.push_back(m);
}
}
return meshes;
}
BarinkEngine::Mesh ModelImporter::processMesh(aiMesh* mesh, const aiScene* scene) {
std::vector<unsigned int> indices;
std::vector<BarinkEngine::Vertex> vertices;
// Process vertices
for (unsigned int i = 0; i < mesh->mNumVertices; i++) {
BarinkEngine::Vertex v{};
glm::vec3 vector;
vector.x = mesh->mVertices[i].x;
vector.y = mesh->mVertices[i].y;
vector.z = mesh->mVertices[i].z;
v.vertices = vector;
if (mesh->mTextureCoords[0]) {
glm::vec2 texCoord;
texCoord.x = mesh->mTextureCoords[0][i].x;
texCoord.y = mesh->mTextureCoords[0][i].y;
v.uv = texCoord;
}
vertices.push_back(v);
}
// Process Indices
for (unsigned int i = 0; i < mesh->mNumFaces; i++) {
aiFace face = mesh->mFaces[i];
if (face.mNumIndices < 3)
continue;
for (unsigned int j = 0; j < face.mNumIndices; j++) {
indices.push_back(face.mIndices[j]);
}
}
BarinkEngine::Mesh result;
result.vertices = vertices;
result.elements = indices;
return result;
}

View File

@ -1,55 +1,62 @@
#include "Graphics/Renderable.h"
#include "AssetManager/ModelImporter.h"
#include "PerfCounter.h"
Renderable* Renderable::Load()
{
return new Renderable();
}
Renderable::Renderable()
{
meshes = ModelImporter::Test();
transform.Scale = glm::vec3(1.0f);
transform.Rotation = glm::vec3(0.0f, 90.0f, 0.0f);
transform.Position = glm::vec3(0.0f, 0.0f, -10.0f);
VAO.Create();
VAO.Bind();
vertexBuffer.createBuffer();
vertexBuffer.Bind(false);
vertexBuffer.setBufferData(&meshes[0].vertices[0], meshes[0].vertices.size() * sizeof(glm::vec3), false);
elementBuffer.createBuffer();
elementBuffer.Bind(true);
elementBuffer.setBufferData(&meshes[0].elements[0], meshes[0].elements.size() * sizeof(unsigned int), true);
VAO.AttachAttribute(0, 3, 0);
vertexBuffer.Unbind(false);
VAO.Unbind();
}
Renderable::~Renderable()
{
}
void Renderable::Draw()
{
VAO.Bind();
elementBuffer.Bind(true);
ES->verts = meshes[0].vertices.size();
ES->DC++;
glDrawElements(GL_TRIANGLES, static_cast<unsigned int>(meshes[0].elements.size()), GL_UNSIGNED_INT, NULL);
VAO.Unbind();
}
#include "Graphics/Renderable.h"
#include "AssetManager/ModelImporter.h"
#include "PerfCounter.h"
Renderable* Renderable::Load()
{
return new Renderable();
}
Renderable::Renderable()
{
meshes = ModelImporter::Test();
transform.Scale = glm::vec3(1.0f);
transform.Rotation = glm::vec3(0.0f, 0.0f, 0.0f);
transform.Position = glm::vec3(0.0f, 0.0f, 0.0f);
VAO.Create();
VAO.Bind();
vertexBuffer.createBuffer();
vertexBuffer.Bind(false);
vertexBuffer.setBufferData(&meshes[0].vertices[0], meshes[0].vertices.size() * sizeof(BarinkEngine::Vertex), false);
elementBuffer.createBuffer();
elementBuffer.Bind(true);
elementBuffer.setBufferData(&meshes[0].elements[0], meshes[0].elements.size() * sizeof(unsigned int), true);
VAO.AttachAttribute(0, 3, sizeof(BarinkEngine::Vertex));
glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, sizeof(BarinkEngine::Vertex),(void*) offsetof(BarinkEngine::Vertex, vertices));
glEnableVertexAttribArray(1);
vertexBuffer.Unbind(false);
VAO.Unbind();
}
Renderable::~Renderable()
{
glDeleteBuffers(1, &UV_id);
}
void Renderable::Draw()
{
VAO.Bind();
elementBuffer.Bind(true);
glActiveTexture(GL_TEXTURE0);
glUniform1i(glGetUniformLocation(shader->id, "Texture"), GL_TEXTURE0);
texture->Bind();
ES->verts = meshes[0].vertices.size();
ES->DC++;
glDrawElements(GL_TRIANGLES, static_cast<unsigned int>(meshes[0].elements.size()), GL_UNSIGNED_INT, NULL);
VAO.Unbind();
}

View File

@ -0,0 +1,40 @@
#include "../Include/Graphics/Texture.h"
#include <glad/glad.h>
#include <GLFW/glfw3.h>
#define STB_IMAGE_IMPLEMENTATION
#include "Graphics/stb_image.h"
#include <iostream>
Texture::Texture(const std::string texturePath) {
int width, height, channels;
unsigned char* data = stbi_load(texturePath.c_str(), &width, &height, &channels, 0);
std::cout << channels << std::endl;
if (data) {
glGenTextures(1, &Id);
glBindTexture(GL_TEXTURE_2D, Id);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, data);
glGenerateMipmap(GL_TEXTURE_2D);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
}
else {
spdlog::error("Failed to load image (%s)", texturePath );
}
stbi_image_free(data);
}
void Texture::Bind() {
glBindTexture(GL_TEXTURE_2D, Id);
}
void Texture::Unbind() {
glBindTexture(GL_TEXTURE_2D, 0);
}

View File

@ -1,25 +1,25 @@
#include "Graphics/VertexArray.h"
#include <glad/glad.h>
void VertexArray::Create(){
glGenVertexArrays(1, &id);
}
void VertexArray::Bind(){
glBindVertexArray(id);
}
void VertexArray::Unbind(){
glBindVertexArray(0);
}
void VertexArray::Delete() {
glDeleteVertexArrays(1, &id);
}
void VertexArray::AttachAttribute(unsigned int index , int size, int stride ){
glVertexAttribPointer(index, size, GL_FLOAT, GL_FALSE, stride, 0);
glEnableVertexAttribArray(0);
}
#include "Graphics/VertexArray.h"
#include <glad/glad.h>
void VertexArray::Create(){
glGenVertexArrays(1, &id);
}
void VertexArray::Bind(){
glBindVertexArray(id);
}
void VertexArray::Unbind(){
glBindVertexArray(0);
}
void VertexArray::Delete() {
glDeleteVertexArrays(1, &id);
}
void VertexArray::AttachAttribute(unsigned int index , int size, int stride ){
glVertexAttribPointer(index, size, GL_FLOAT, GL_FALSE, stride, 0);
glEnableVertexAttribArray(0);
}

View File

@ -1,9 +1,14 @@
#version 440 core
out vec4 FragColor;
uniform vec3 MatColour;
void main(){
FragColor = vec4(MatColour, 1.0f);
#version 440 core
out vec4 FragColor;
uniform vec3 Color;
in vec2 TexCoord;
uniform sampler2D Texture;
void main(){
FragColor = mix ( texture(Texture, TexCoord), vec4(Color, 1.0f));
}

View File

@ -1,10 +1,15 @@
#version 440 core
in layout(location=0) vec3 aPos;
in layout(location=1) vec2 uv;
uniform mat4 M;
uniform mat4 V;
uniform mat4 P;
out vec2 TexCoord;
void main() {
TexCoord = uv;
gl_Position = P * V * M * vec4(aPos.x, aPos.y, aPos.z, 1.0);
}

View File

@ -16,7 +16,7 @@ project "BarinkEngine"
"../libs/assimp/include",
"../libs/glad/include",
"../libs/glfw/include",
"../libs/tinygltf",
-- "../libs/tinygltf",
"../libs/glew/include",
"../libs/glm",
"../libs/ImGui",

View File

@ -32,4 +32,10 @@ void transformWindow(Transform& transform, std::string PanelName) {
ImGui::InputFloat3("Scale:", (float*)&transform.Scale[0]);
ImGui::End();
}
void materialWindow(Material& material, std::string PanelName) {
ImGui::Begin(PanelName.c_str());
ImGui::ColorPicker3("Color:", &material.Color[0]);
ImGui::End();
}

View File

@ -1,7 +1,8 @@
#pragma once
#include "imgui.h"
#include <BarinkEngine.h>
void CameraTool(Camera* camera);
void ScriptingTool(char* code);
void transformWindow(Transform& transform, std::string PanelName);
#pragma once
#include "imgui.h"
#include <BarinkEngine.h>
void CameraTool(Camera* camera);
void ScriptingTool(char* code);
void transformWindow(Transform& transform, std::string PanelName);
void materialWindow(Material& material, std::string PanelName);

View File

@ -7,11 +7,15 @@
* Define globals
*/
Camera* cam;
Renderable* Cube;
Renderable* Cube2;
Shader* shader;
Renderable* Cube;
Material* matCube;
Texture* textureCube;
Renderable* Cube2;
Material* matCube2;
char* code = new char[254];
@ -54,6 +58,8 @@ void Start() {
shader = new Shader(vertexShaderSource, fragmentShaderSource);
textureCube = new Texture("build/SandboxApplication/Debug/die.jpg");
matCube = new Material(*shader);
matCube->Color = glm::vec3(1.0, 0.0, 0.0);
@ -67,7 +73,17 @@ void Start() {
Cube2 = Renderable::Load();
Cube->addChild(*Cube2);
cam = new Camera(glm::vec3(0.0f, 1.5f, -10.0f), glm::vec3(0.0f, 0.0f, 0.0f), 90.0f);
Cube->shader = shader;
Cube2->shader = shader;
Cube->texture = textureCube;
Cube2->texture = textureCube;
Cube2->transform.Position = glm::vec3(-9.0f, 0.0f, 0.0f);
Cube->transform.Position = glm::vec3(-8.0f, 0.0f, -2.0f);
cam = new Camera(glm::vec3(0.0f, 1.5f, 0.0f), glm::vec3(0.0f, 0.0f, 0.0f), 90.0f);
memset(code, '\0', 254);
@ -98,6 +114,9 @@ void ImmediateGraphicsDraw() {
transformWindow(Cube2->transform, "Transform (Cube2)");
materialWindow(*matCube, "Material Cube");
materialWindow(*matCube2, "Material Cube2");
}