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:
@ -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);
|
||||
}
|
@ -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;
|
||||
|
||||
}
|
@ -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();
|
||||
|
||||
}
|
||||
|
40
BarinkEngine/graphics/Texture.cpp
Normal file
40
BarinkEngine/graphics/Texture.cpp
Normal 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);
|
||||
}
|
@ -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);
|
||||
}
|
||||
|
||||
|
@ -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));
|
||||
}
|
@ -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);
|
||||
}
|
Reference in New Issue
Block a user