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:
parent
d019155d10
commit
16b61986a1
@ -2,9 +2,11 @@
|
|||||||
#include "glm/glm.hpp"
|
#include "glm/glm.hpp"
|
||||||
#include "graphics/Shader.h"
|
#include "graphics/Shader.h"
|
||||||
#include "graphics/Window.h"
|
#include "graphics/Window.h"
|
||||||
|
#include "graphics/Texture.h"
|
||||||
#include "graphics/Camera.h"
|
#include "graphics/Camera.h"
|
||||||
#include "graphics/Renderable.h"
|
#include "graphics/Renderable.h"
|
||||||
#include "Graphics/Material.h"
|
#include "Graphics/Material.h"
|
||||||
|
|
||||||
#include "spdlog/spdlog.h"
|
#include "spdlog/spdlog.h"
|
||||||
|
|
||||||
#include "Input/InputManager.h"
|
#include "Input/InputManager.h"
|
||||||
|
@ -4,10 +4,17 @@
|
|||||||
|
|
||||||
namespace BarinkEngine{
|
namespace BarinkEngine{
|
||||||
|
|
||||||
|
|
||||||
|
struct Vertex {
|
||||||
|
glm::vec3 vertices;
|
||||||
|
glm::vec2 uv;
|
||||||
|
};
|
||||||
|
|
||||||
class Mesh {
|
class Mesh {
|
||||||
public:
|
public:
|
||||||
std::vector<glm::vec3> vertices;
|
std::vector<Vertex> vertices;
|
||||||
std::vector<unsigned int > elements;
|
std::vector<unsigned int > elements;
|
||||||
std::vector<glm::vec2> uv;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
}
|
}
|
@ -3,6 +3,7 @@
|
|||||||
#include "Mesh.h"
|
#include "Mesh.h"
|
||||||
#include "Buffer.h"
|
#include "Buffer.h"
|
||||||
#include "Material.h"
|
#include "Material.h"
|
||||||
|
#include "Texture.h"
|
||||||
#include "VertexArray.h"
|
#include "VertexArray.h"
|
||||||
#include "Scene.h"
|
#include "Scene.h"
|
||||||
|
|
||||||
@ -14,10 +15,13 @@ public:
|
|||||||
*/
|
*/
|
||||||
Buffer vertexBuffer;
|
Buffer vertexBuffer;
|
||||||
Buffer elementBuffer;
|
Buffer elementBuffer;
|
||||||
|
//Buffer uv;
|
||||||
VertexArray VAO;
|
VertexArray VAO;
|
||||||
|
|
||||||
|
|
||||||
|
GLuint UV_id;
|
||||||
Material* material;
|
Material* material;
|
||||||
|
Texture* texture;
|
||||||
|
|
||||||
|
|
||||||
Shader* shader;
|
Shader* shader;
|
||||||
|
@ -10,7 +10,7 @@
|
|||||||
|
|
||||||
class Shader {
|
class Shader {
|
||||||
private:
|
private:
|
||||||
int id;
|
|
||||||
char* readFile (const char* filePath);
|
char* readFile (const char* filePath);
|
||||||
public:
|
public:
|
||||||
Shader(const std::string vertexShaderPath, const std::string fragmentShaderPath);
|
Shader(const std::string vertexShaderPath, const std::string fragmentShaderPath);
|
||||||
@ -22,5 +22,6 @@ class Shader {
|
|||||||
void setUniformFloat(std::string uniformName, float value)const;
|
void setUniformFloat(std::string uniformName, float value)const;
|
||||||
void setUniformInt(std::string uniformName, int value) const ;
|
void setUniformInt(std::string uniformName, int value) const ;
|
||||||
|
|
||||||
|
int id;
|
||||||
|
|
||||||
};
|
};
|
18
BarinkEngine/Include/Graphics/Texture.h
Normal file
18
BarinkEngine/Include/Graphics/Texture.h
Normal 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;
|
||||||
|
|
||||||
|
};
|
8007
BarinkEngine/Include/Graphics/stb_image.h
Normal file
8007
BarinkEngine/Include/Graphics/stb_image.h
Normal file
File diff suppressed because it is too large
Load Diff
@ -82,16 +82,31 @@ std::vector<BarinkEngine::Mesh> ModelImporter::processNode(aiNode* node, const a
|
|||||||
}
|
}
|
||||||
|
|
||||||
BarinkEngine::Mesh ModelImporter::processMesh(aiMesh* mesh, const aiScene* scene) {
|
BarinkEngine::Mesh ModelImporter::processMesh(aiMesh* mesh, const aiScene* scene) {
|
||||||
std::vector<glm::vec3> vertices ;
|
|
||||||
std::vector<unsigned int> indices;
|
std::vector<unsigned int> indices;
|
||||||
|
std::vector<BarinkEngine::Vertex> vertices;
|
||||||
|
|
||||||
// Process vertices
|
// Process vertices
|
||||||
for (unsigned int i = 0; i < mesh->mNumVertices; i++) {
|
for (unsigned int i = 0; i < mesh->mNumVertices; i++) {
|
||||||
|
BarinkEngine::Vertex v{};
|
||||||
glm::vec3 vector;
|
glm::vec3 vector;
|
||||||
vector.x = mesh->mVertices[i].x;
|
vector.x = mesh->mVertices[i].x;
|
||||||
vector.y = mesh->mVertices[i].y;
|
vector.y = mesh->mVertices[i].y;
|
||||||
vector.z = mesh->mVertices[i].z;
|
vector.z = mesh->mVertices[i].z;
|
||||||
vertices.push_back(vector);
|
|
||||||
|
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
|
// Process Indices
|
||||||
@ -104,12 +119,15 @@ BarinkEngine::Mesh ModelImporter::processMesh(aiMesh* mesh, const aiScene* scene
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
BarinkEngine::Mesh result;
|
BarinkEngine::Mesh result;
|
||||||
|
|
||||||
|
|
||||||
result.vertices = vertices;
|
result.vertices = vertices;
|
||||||
result.elements = indices;
|
result.elements = indices;
|
||||||
|
|
||||||
|
|
||||||
return result;
|
return result;
|
||||||
|
|
||||||
}
|
}
|
@ -10,13 +10,11 @@ Renderable* Renderable::Load()
|
|||||||
|
|
||||||
Renderable::Renderable()
|
Renderable::Renderable()
|
||||||
{
|
{
|
||||||
|
|
||||||
meshes = ModelImporter::Test();
|
meshes = ModelImporter::Test();
|
||||||
|
|
||||||
transform.Scale = glm::vec3(1.0f);
|
transform.Scale = glm::vec3(1.0f);
|
||||||
transform.Rotation = glm::vec3(0.0f, 90.0f, 0.0f);
|
transform.Rotation = glm::vec3(0.0f, 0.0f, 0.0f);
|
||||||
transform.Position = glm::vec3(0.0f, 0.0f, -10.0f);
|
transform.Position = glm::vec3(0.0f, 0.0f, 0.0f);
|
||||||
|
|
||||||
|
|
||||||
VAO.Create();
|
VAO.Create();
|
||||||
VAO.Bind();
|
VAO.Bind();
|
||||||
@ -24,29 +22,38 @@ Renderable::Renderable()
|
|||||||
|
|
||||||
vertexBuffer.createBuffer();
|
vertexBuffer.createBuffer();
|
||||||
vertexBuffer.Bind(false);
|
vertexBuffer.Bind(false);
|
||||||
vertexBuffer.setBufferData(&meshes[0].vertices[0], meshes[0].vertices.size() * sizeof(glm::vec3), false);
|
vertexBuffer.setBufferData(&meshes[0].vertices[0], meshes[0].vertices.size() * sizeof(BarinkEngine::Vertex), false);
|
||||||
|
|
||||||
|
|
||||||
elementBuffer.createBuffer();
|
elementBuffer.createBuffer();
|
||||||
elementBuffer.Bind(true);
|
elementBuffer.Bind(true);
|
||||||
elementBuffer.setBufferData(&meshes[0].elements[0], meshes[0].elements.size() * sizeof(unsigned int), true);
|
elementBuffer.setBufferData(&meshes[0].elements[0], meshes[0].elements.size() * sizeof(unsigned int), true);
|
||||||
|
|
||||||
VAO.AttachAttribute(0, 3, 0);
|
VAO.AttachAttribute(0, 3, sizeof(BarinkEngine::Vertex));
|
||||||
|
|
||||||
vertexBuffer.Unbind(false);
|
glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, sizeof(BarinkEngine::Vertex),(void*) offsetof(BarinkEngine::Vertex, vertices));
|
||||||
VAO.Unbind();
|
glEnableVertexAttribArray(1);
|
||||||
|
|
||||||
|
vertexBuffer.Unbind(false);
|
||||||
|
|
||||||
|
VAO.Unbind();
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Renderable::~Renderable()
|
Renderable::~Renderable()
|
||||||
{
|
{
|
||||||
|
glDeleteBuffers(1, &UV_id);
|
||||||
}
|
}
|
||||||
|
|
||||||
void Renderable::Draw()
|
void Renderable::Draw()
|
||||||
{
|
{
|
||||||
VAO.Bind();
|
VAO.Bind();
|
||||||
elementBuffer.Bind(true);
|
elementBuffer.Bind(true);
|
||||||
|
|
||||||
|
glActiveTexture(GL_TEXTURE0);
|
||||||
|
glUniform1i(glGetUniformLocation(shader->id, "Texture"), GL_TEXTURE0);
|
||||||
|
texture->Bind();
|
||||||
|
|
||||||
ES->verts = meshes[0].vertices.size();
|
ES->verts = meshes[0].vertices.size();
|
||||||
ES->DC++;
|
ES->DC++;
|
||||||
glDrawElements(GL_TRIANGLES, static_cast<unsigned int>(meshes[0].elements.size()), GL_UNSIGNED_INT, NULL);
|
glDrawElements(GL_TRIANGLES, static_cast<unsigned int>(meshes[0].elements.size()), GL_UNSIGNED_INT, NULL);
|
||||||
|
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);
|
||||||
|
}
|
@ -2,8 +2,13 @@
|
|||||||
|
|
||||||
out vec4 FragColor;
|
out vec4 FragColor;
|
||||||
|
|
||||||
uniform vec3 MatColour;
|
uniform vec3 Color;
|
||||||
|
|
||||||
|
in vec2 TexCoord;
|
||||||
|
|
||||||
|
uniform sampler2D Texture;
|
||||||
|
|
||||||
|
|
||||||
void main(){
|
void main(){
|
||||||
FragColor = vec4(MatColour, 1.0f);
|
FragColor = mix ( texture(Texture, TexCoord), vec4(Color, 1.0f));
|
||||||
}
|
}
|
@ -1,10 +1,15 @@
|
|||||||
#version 440 core
|
#version 440 core
|
||||||
in layout(location=0) vec3 aPos;
|
in layout(location=0) vec3 aPos;
|
||||||
|
in layout(location=1) vec2 uv;
|
||||||
|
|
||||||
uniform mat4 M;
|
uniform mat4 M;
|
||||||
uniform mat4 V;
|
uniform mat4 V;
|
||||||
uniform mat4 P;
|
uniform mat4 P;
|
||||||
|
|
||||||
|
out vec2 TexCoord;
|
||||||
|
|
||||||
|
|
||||||
void main() {
|
void main() {
|
||||||
|
TexCoord = uv;
|
||||||
gl_Position = P * V * M * vec4(aPos.x, aPos.y, aPos.z, 1.0);
|
gl_Position = P * V * M * vec4(aPos.x, aPos.y, aPos.z, 1.0);
|
||||||
}
|
}
|
@ -16,7 +16,7 @@ project "BarinkEngine"
|
|||||||
"../libs/assimp/include",
|
"../libs/assimp/include",
|
||||||
"../libs/glad/include",
|
"../libs/glad/include",
|
||||||
"../libs/glfw/include",
|
"../libs/glfw/include",
|
||||||
"../libs/tinygltf",
|
-- "../libs/tinygltf",
|
||||||
"../libs/glew/include",
|
"../libs/glew/include",
|
||||||
"../libs/glm",
|
"../libs/glm",
|
||||||
"../libs/ImGui",
|
"../libs/ImGui",
|
||||||
|
@ -33,3 +33,9 @@ void transformWindow(Transform& transform, std::string PanelName) {
|
|||||||
ImGui::End();
|
ImGui::End();
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void materialWindow(Material& material, std::string PanelName) {
|
||||||
|
ImGui::Begin(PanelName.c_str());
|
||||||
|
ImGui::ColorPicker3("Color:", &material.Color[0]);
|
||||||
|
ImGui::End();
|
||||||
|
}
|
@ -5,3 +5,4 @@
|
|||||||
void CameraTool(Camera* camera);
|
void CameraTool(Camera* camera);
|
||||||
void ScriptingTool(char* code);
|
void ScriptingTool(char* code);
|
||||||
void transformWindow(Transform& transform, std::string PanelName);
|
void transformWindow(Transform& transform, std::string PanelName);
|
||||||
|
void materialWindow(Material& material, std::string PanelName);
|
@ -7,11 +7,15 @@
|
|||||||
* Define globals
|
* Define globals
|
||||||
*/
|
*/
|
||||||
Camera* cam;
|
Camera* cam;
|
||||||
Renderable* Cube;
|
|
||||||
Renderable* Cube2;
|
|
||||||
|
|
||||||
Shader* shader;
|
Shader* shader;
|
||||||
|
|
||||||
|
Renderable* Cube;
|
||||||
Material* matCube;
|
Material* matCube;
|
||||||
|
Texture* textureCube;
|
||||||
|
|
||||||
|
Renderable* Cube2;
|
||||||
Material* matCube2;
|
Material* matCube2;
|
||||||
|
|
||||||
char* code = new char[254];
|
char* code = new char[254];
|
||||||
@ -54,6 +58,8 @@ void Start() {
|
|||||||
|
|
||||||
shader = new Shader(vertexShaderSource, fragmentShaderSource);
|
shader = new Shader(vertexShaderSource, fragmentShaderSource);
|
||||||
|
|
||||||
|
textureCube = new Texture("build/SandboxApplication/Debug/die.jpg");
|
||||||
|
|
||||||
matCube = new Material(*shader);
|
matCube = new Material(*shader);
|
||||||
matCube->Color = glm::vec3(1.0, 0.0, 0.0);
|
matCube->Color = glm::vec3(1.0, 0.0, 0.0);
|
||||||
|
|
||||||
@ -67,7 +73,17 @@ void Start() {
|
|||||||
Cube2 = Renderable::Load();
|
Cube2 = Renderable::Load();
|
||||||
Cube->addChild(*Cube2);
|
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);
|
memset(code, '\0', 254);
|
||||||
|
|
||||||
@ -98,6 +114,9 @@ void ImmediateGraphicsDraw() {
|
|||||||
|
|
||||||
transformWindow(Cube2->transform, "Transform (Cube2)");
|
transformWindow(Cube2->transform, "Transform (Cube2)");
|
||||||
|
|
||||||
|
materialWindow(*matCube, "Material Cube");
|
||||||
|
materialWindow(*matCube2, "Material Cube2");
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user