[EDIT] renaming the static_shader

This commit is contained in:
Menno
2021-05-21 08:43:32 +02:00
parent 9e9d50da9e
commit e2f6bd720d
10 changed files with 149 additions and 135 deletions

View File

@@ -15,7 +15,7 @@ namespace render_engine
/*
This function will load the projectionMatrix into the shader
*/
void Init(shaders::StaticShader& shader)
void Init(shaders::EntityShader& shader)
{
// Faces which are not facing the camera are not rendered
glEnable(GL_CULL_FACE);
@@ -43,7 +43,7 @@ namespace render_engine
/*
This function will Render a Model on the screen.
*/
void Render(entities::Entity& entity, shaders::StaticShader& shader)
void Render(entities::Entity& entity, shaders::EntityShader& shader)
{
const models::TexturedModel model = entity.GetModel();
const models::RawModel raw_model = model.raw_model;

View File

@@ -1,7 +1,7 @@
#pragma once
#include "../entities/entity.h"
#include "../shaders/static_shader.h"
#include "../shaders/entity_shader.h"
namespace render_engine
{
@@ -10,7 +10,7 @@ namespace render_engine
/*
Call this function when starting the program
*/
void Init(shaders::StaticShader& shader);
void Init(shaders::EntityShader& shader);
/*
Call this function before rendering.
@@ -20,6 +20,6 @@ namespace render_engine
/*
Call this function when wanting to Render a mesh to the screen.
*/
void Render(entities::Entity& entity, shaders::StaticShader& shader);
void Render(entities::Entity& entity, shaders::EntityShader& shader);
}
}

View File

@@ -6,124 +6,128 @@
#include "loader.h"
#include "obj_loader.h"
static void Split(const std::string& s, char delim, std::vector<std::string>& elems)
namespace render_engine
{
std::stringstream ss;
ss.str(s);
std::string item;
while (getline(ss, item, delim)) {
elems.push_back(item);
}
}
static std::vector<std::string> Split(const std::string& s, char delim)
{
std::vector<std::string> elems;
Split(s, delim, elems);
return elems;
}
static void ProcessVertex(const std::vector<std::string>& vertex_data,
const std::vector<glm::vec3>& normals,
const std::vector<glm::vec2>& textures,
std::vector<GLuint>& indices,
std::vector<GLfloat>& texture_array,
std::vector<GLfloat>& normal_array)
{
GLuint current_vertex_pointer = std::stoi(vertex_data.at(0)) - 1;
indices.push_back(current_vertex_pointer);
glm::vec2 current_texture = textures.at(std::stoi(vertex_data.at(1)) - 1);
texture_array[(current_vertex_pointer * 2) % texture_array.size()] = current_texture.x;
texture_array[(current_vertex_pointer * 2 + 1) % texture_array.size()] = 1 - current_texture.y;
glm::vec3 current_norm = normals.at(std::stoi(vertex_data.at(2)) - 1);
normal_array[current_vertex_pointer * 3] = current_norm.x;
normal_array[current_vertex_pointer * 3 + 1] = current_norm.y;
normal_array[current_vertex_pointer * 3 + 2] = current_norm.z;
}
models::RawModel LoadObjModel(std::string file_name)
{
std::ifstream inFile (file_name);
if ( !inFile.is_open() )
static void Split(const std::string& s, char delim, std::vector<std::string>& elems)
{
throw std::runtime_error ( "Could not open model file " + file_name + ".obj!" );
std::stringstream ss;
ss.str(s);
std::string item;
while (getline(ss, item, delim)) {
elems.push_back(item);
}
}
std::vector<glm::vec3> vertices;
std::vector<glm::vec3> normals;
std::vector<glm::vec2> textures;
std::vector<GLuint> indices;
std::vector<GLfloat> vertex_array;
std::vector<GLfloat> normal_array;
std::vector<GLfloat> texture_array;
std::string line;
try
static std::vector<std::string> Split(const std::string& s, char delim)
{
while (std::getline(inFile, line))
std::vector<std::string> elems;
Split(s, delim, elems);
return elems;
}
static void ProcessVertex(const std::vector<std::string>& vertex_data,
const std::vector<glm::vec3>& normals,
const std::vector<glm::vec2>& textures,
std::vector<GLuint>& indices,
std::vector<GLfloat>& texture_array,
std::vector<GLfloat>& normal_array)
{
GLuint current_vertex_pointer = std::stoi(vertex_data.at(0)) - 1;
indices.push_back(current_vertex_pointer);
glm::vec2 current_texture = textures.at(std::stoi(vertex_data.at(1)) - 1);
texture_array[(current_vertex_pointer * 2) % texture_array.size()] = current_texture.x;
texture_array[(current_vertex_pointer * 2 + 1) % texture_array.size()] = 1 - current_texture.y;
glm::vec3 current_norm = normals.at(std::stoi(vertex_data.at(2)) - 1);
normal_array[current_vertex_pointer * 3] = current_norm.x;
normal_array[current_vertex_pointer * 3 + 1] = current_norm.y;
normal_array[current_vertex_pointer * 3 + 2] = current_norm.z;
}
models::RawModel LoadObjModel(std::string file_name)
{
std::ifstream inFile(file_name);
if (!inFile.is_open())
{
std::vector<std::string> split_line = Split(line, ' ');
if (split_line.at(0) == "v")
throw std::runtime_error("Could not open model file " + file_name + ".obj!");
}
std::vector<glm::vec3> vertices;
std::vector<glm::vec3> normals;
std::vector<glm::vec2> textures;
std::vector<GLuint> indices;
std::vector<GLfloat> vertex_array;
std::vector<GLfloat> normal_array;
std::vector<GLfloat> texture_array;
std::string line;
try
{
while (std::getline(inFile, line))
{
glm::vec3 vertex;
vertex.x = std::stof(split_line.at(1));
vertex.y = std::stof(split_line.at(2));
vertex.z = std::stof(split_line.at(3));
vertices.push_back(vertex);
std::vector<std::string> split_line = Split(line, ' ');
if (split_line.at(0) == "v")
{
glm::vec3 vertex;
vertex.x = std::stof(split_line.at(1));
vertex.y = std::stof(split_line.at(2));
vertex.z = std::stof(split_line.at(3));
vertices.push_back(vertex);
}
else if (split_line.at(0) == "vt")
{
glm::vec2 texture;
texture.x = std::stof(split_line.at(1));
texture.y = std::stof(split_line.at(2));
textures.push_back(texture);
}
else if (split_line.at(0) == "vn")
{
glm::vec3 normal;
normal.x = std::stof(split_line.at(1));
normal.y = std::stof(split_line.at(2));
normal.z = std::stof(split_line.at(3));
normals.push_back(normal);
}
else if (split_line.at(0) == "f")
{
normal_array = std::vector<GLfloat>(vertices.size() * 3);
texture_array = std::vector<GLfloat>(textures.size() * 2);
break;
}
}
else if (split_line.at(0) == "vt")
while (true)
{
glm::vec2 texture;
texture.x = std::stof(split_line.at(1));
texture.y = std::stof(split_line.at(2));
textures.push_back(texture);
}
else if (split_line.at(0) == "vn")
{
glm::vec3 normal;
normal.x = std::stof(split_line.at(1));
normal.y = std::stof(split_line.at(2));
normal.z = std::stof(split_line.at(3));
normals.push_back(normal);
}
else if (split_line.at(0) == "f")
{
normal_array = std::vector<GLfloat>(vertices.size() * 3);
texture_array = std::vector<GLfloat>(textures.size() * 2);
break;
std::vector<std::string> split = Split(line, ' ');
std::vector<std::string> vertex1 = Split(split.at(1), '/');
std::vector<std::string> vertex2 = Split(split.at(2), '/');
std::vector<std::string> vertex3 = Split(split.at(3), '/');
ProcessVertex(vertex1, normals, textures, indices, texture_array, normal_array);
ProcessVertex(vertex2, normals, textures, indices, texture_array, normal_array);
ProcessVertex(vertex3, normals, textures, indices, texture_array, normal_array);
if (!std::getline(inFile, line))
{
break;
}
}
}
while (true)
catch (const std::exception& e)
{
std::vector<std::string> split = Split(line, ' ');
std::vector<std::string> vertex1 = Split(split.at(1), '/');
std::vector<std::string> vertex2 = Split(split.at(2), '/');
std::vector<std::string> vertex3 = Split(split.at(3), '/');
ProcessVertex(vertex1, normals, textures, indices, texture_array, normal_array);
ProcessVertex(vertex2, normals, textures, indices, texture_array, normal_array);
ProcessVertex(vertex3, normals, textures, indices, texture_array, normal_array);
if (!std::getline(inFile, line))
{
break;
}
// Always go in here
}
} catch (const std::exception& e)
{
// Always go in here
}
inFile.close();
vertex_array = std::vector<GLfloat>( vertices.size() * 3 );
int p = 0;
for ( auto& vertex : vertices )
{
vertex_array[p++] = vertex.x;
vertex_array[p++] = vertex.y;
vertex_array[p++] = vertex.z;
}
inFile.close();
return render_engine::loader::LoadToVAO( vertex_array, texture_array, normal_array, indices);
vertex_array = std::vector<GLfloat>(vertices.size() * 3);
int p = 0;
for (auto& vertex : vertices)
{
vertex_array[p++] = vertex.x;
vertex_array[p++] = vertex.y;
vertex_array[p++] = vertex.z;
}
return render_engine::loader::LoadToVAO(vertex_array, texture_array, normal_array, indices);
}
}

View File

@@ -3,4 +3,10 @@
#include <string>
#include "../models/model.h"
models::RawModel LoadObjModel(std::string file_name);
namespace render_engine
{
/*
* This function retrieves an .obj file, loads it into the VBO and returns a RawModel
*/
models::RawModel LoadObjModel(std::string file_name);
}