2 Commits 16d2bd6466 ... bae169977b

Author SHA1 Message Date
  Nicolás A. Ortega Froysa bae169977b Should be able to load models now. 5 years ago
  Nicolás A. Ortega Froysa e2d9fe9189 Using assimp instead for model importing. 5 years ago

+ 8 - 3
CMakeLists.txt

@@ -31,18 +31,22 @@ message(STATUS "Build Type: ${CMAKE_BUILD_TYPE}")
 # prefer newer versions of OpenGL
 set(OpenGL_GL_PREFERENCE "GLVND")
 find_package(OpenGL REQUIRED)
-find_package(GLUT REQUIRED)
 find_package(GLEW REQUIRED)
+find_package(GLUT REQUIRED)
 find_package(PkgConfig REQUIRED)
+pkg_search_module(ASSIMP REQUIRED assimp)
 pkg_search_module(SDL2 REQUIRED sdl2)
 
 include_directories(
-	SYSTEM "include/"
+	SYSTEM ${ASSIMP_INCLUDE_DIRS}
 	SYSTEM ${SDL2_INCLUDE_DIRS})
 
 set(SRCS
+	"src/AssetManager.cpp"
 	"src/Logger.cpp"
 	"src/Main.cpp"
+	"src/Model.cpp"
+	"src/Shader.cpp"
 	"src/System.cpp")
 
 # Define C++ compiler flags
@@ -66,6 +70,7 @@ add_executable(${TARGET_NAME}
 target_link_libraries(${TARGET_NAME}
 	OpenGL::OpenGL
 	OpenGL::GLU
-	${GLUT_LIBRARIES}
+	${ASSIMP_LIBRARIES}
 	${GLEW_LIBRARIES}
+	${GLUT_LIBRARIES}
 	${SDL2_LIBRARIES})

+ 7 - 0
assets/shaders/fragment_shader.glsl

@@ -0,0 +1,7 @@
+#version 330 core
+in vec3 frag_col;
+out vec3 col;
+
+void main() {
+	col = frag_col;
+}

+ 9 - 0
assets/shaders/vertex_shader.glsl

@@ -0,0 +1,9 @@
+#version 330 core
+layout(location = 0) in vec3 vert_pos;
+uniform vec3 col;
+/*uniform mat4 mvp;*/
+out vec3 frag_col;
+void main() {
+	gl_Position = /*mvp * */vec4(vert_pos, 1);
+	frag_col = col;
+}

File diff suppressed because it is too large
+ 0 - 2550
include/tiny_obj_loader.h


+ 126 - 1
src/AssetManager.cpp

@@ -18,7 +18,132 @@
 
 #include "AssetManager.hpp"
 
-void AssetManager::loadOBJ(const std::string &path,
+#include "System.hpp"
+
+#include <vector>
+#include <stdio.h>
+
+//#define TINYOBJLOADER_IMPLEMENTATION
+//#include <tiny_obj_loader.h>
+#include <glm/glm.hpp>
+#include <assimp/Importer.hpp>
+#include <assimp/scene.h>
+#include <assimp/postprocess.h>
+
+/*void AssetManager::loadOBJ(const std::string &path,
 		const std::string &name)
 {
+	std::vector<unsigned int> vertexIndices,
+		uvIndices, normalIndices;
+	std::vector<glm::vec3> temp_vertices;
+	std::vector<glm::vec2> temp_uvs;
+	std::vector<glm::vec3> temp_normals;
+
+	FILE *file = fopen(path.c_str(), "r");
+	if(not file)
+	{
+		System::logger->writeError("Failed to open file.");
+		return;
+	}
+
+	while(true)
+	{
+		char lineHeader[128];
+		int res = fscanf(file, "%s", lineHeader);
+		if(res == EOF)
+			break;
+
+		if(strcmp(lineHeader, "v") == 0)
+		{
+			glm::vec3 vertex;
+			fscanf(file, "%f %f %f\n",
+					&vertex.x, &vertex.y, &vertex.z);
+			temp_vertices.push_back(vertex);
+		}
+		else if(strcmp(lineHeader, "vt") == 0)
+		{
+			glm::vec2 uv;
+			fscanf(file, "%f %f\n", &uv.x, &uv.y);
+			temp_uvs.push_back(uv);
+		}
+		else if(strcmp(lineHeader, "vn") == 0)
+		{
+			glm::vec3 normal;
+			fscanf(file, "%f %f %f\n",
+					&normal.x, &normal.y, &normal.z);
+			temp_normals.push_back(normal);
+		}
+		else if(strcmp(lineHeader, "f") == 0)
+		{
+			std::string v1, v2, v3;
+			unsigned int vIndex[3], uvIndex[3], normIndex[3];
+			int matches = fscanf(file,
+					"%d/%d/%d %d/%d/%d %d/%d/%d\n",
+					&vIndex[0], &uvIndex[0], &normIndex[0],
+					&vIndex[1], &uvIndex[1], &normIndex[1],
+					&vIndex[2], &uvIndex[2], &normIndex[2]);
+			if(matches not_eq 9)
+			{
+				System::logger->writeError(
+						"OBJ file is too complicated.");
+				return;
+			}
+
+			for(auto &i : vIndex)
+				vertexIndices.push_back(i);
+			for(auto &i : uvIndex)
+				uvIndices.push_back(i);
+			for(auto &i : normIndex)
+				normalIndices.push_back(i);
+		}
+	}
+}*/
+
+void AssetManager::loadModel(const std::string &path,
+		const std::string &name)
+{
+	Assimp::Importer importer;
+	const aiScene *scene = importer.ReadFile(path,
+			aiProcess_Triangulate);
+	if(not scene)
+	{
+		System::logger->writeError(importer.GetErrorString());
+		return;
+	}
+	const aiMesh *mesh = scene->mMeshes[0];
+
+	std::vector<struct Vertex> vertices;
+	std::vector<unsigned int> indices;
+
+	vertices.reserve(mesh->mNumVertices);
+	for(unsigned int i = 0; i < mesh->mNumVertices; ++i)
+	{
+		aiVector3D pos = mesh->mVertices[i];
+		aiVector3D norm = mesh->mNormals[i];
+
+		vertices.push_back({
+				glm::vec3(pos.x, pos.y, pos.z),
+				glm::vec3(norm.x, norm.y, norm.z)});
+	}
+
+	indices.reserve(3 * mesh->mNumFaces);
+	for(unsigned int i = 0; i < mesh->mNumFaces; ++i)
+	{
+		for(unsigned int j = 0; j < 3; ++j)
+			indices.push_back(mesh->mFaces[i].mIndices[j]);
+	}
+
+	models.insert({ name,
+			std::make_shared<Model>(
+					vertices, indices)});
+}
+
+std::shared_ptr<Model> AssetManager::getModel(
+		const std::string &name)
+{
+	return models.at(name);
+}
+
+void AssetManager::unloadModel(const std::string &name) {
+	models.erase(name);
 }

+ 10 - 5
src/AssetManager.hpp

@@ -18,17 +18,22 @@
 
 #pragma once
 
+#include "Model.hpp"
+#include <map>
+#include <memory>
 #include <string>
 
 class AssetManager {
 public:
 	AssetManager();
 
-	void loadOBJ(const std::string &path,
+	/*void loadOBJ(const std::string &path,
+			const std::string &name);*/
+	void loadModel(const std::string &path,
 			const std::string &name);
-	void loadSound(const std::string &path,
-			const std::string &name);
-	void unloadOBJ(const std::string &name);
-	void unloadSound(const std::string &name);
+	std::shared_ptr<Model> getModel(const std::string &name);
+	void unloadModel(const std::string &name);
+	//void unloadOBJ(const std::string &name);
 private:
+	std::map<std::string, std::shared_ptr<Model>> models;
 };

+ 60 - 0
src/Model.cpp

@@ -0,0 +1,60 @@
+/*
+ * Copyright (C) 2018 Ortega Froysa, Nicolás <nortega@themusicinnoise.net>
+ * Author: Ortega Froysa, Nicolás <nortega@themusicinnoise.net>
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as
+ * published by the Free Software Foundation, either version 3 of the
+ * License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include "Model.hpp"
+
+#include <GL/glew.h>
+#include <GL/gl.h>
+
+Model::Model(const std::vector<struct Vertex> &vertices,
+		const std::vector<unsigned int> &indices) :
+	vertices(vertices), indices(indices)
+{
+	glGenVertexArrays(1, &vao);
+	glGenBuffers(1, &vbo);
+	glGenBuffers(1, &ebo);
+
+	glBindVertexArray(vao);
+	glBindBuffer(GL_ARRAY_BUFFER, vbo);
+	glBufferData(GL_ARRAY_BUFFER,
+			vertices.size() * sizeof(struct Vertex),
+			&vertices[0], GL_STATIC_DRAW);
+	glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ebo);
+	glBufferData(GL_ELEMENT_ARRAY_BUFFER,
+			indices.size() * sizeof(unsigned int),
+			&indices[0], GL_STATIC_DRAW);
+
+	glEnableVertexAttribArray(0);
+	glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE,
+			sizeof(struct Vertex), nullptr);
+	glEnableVertexAttribArray(1);
+	glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE,
+			sizeof(struct Vertex),
+			(void*)offsetof(struct Vertex, normal));
+	glBindVertexArray(0);
+}
+
+void Model::draw(const Shader &shader) {
+	glUniform3f(
+			glGetUniformLocation(shader.getId(), "col"),
+			color.r, color.g, color.b);
+	glBindVertexArray(vao);
+	glDrawElements(GL_TRIANGLES, indices.size(),
+			GL_UNSIGNED_INT, 0);
+	glBindVertexArray(0);
+}

+ 52 - 0
src/Model.hpp

@@ -0,0 +1,52 @@
+/*
+ * Copyright (C) 2018 Ortega Froysa, Nicolás <nortega@themusicinnoise.net>
+ * Author: Ortega Froysa, Nicolás <nortega@themusicinnoise.net>
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as
+ * published by the Free Software Foundation, either version 3 of the
+ * License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#pragma once
+
+#include "Shader.hpp"
+
+#include <vector>
+#include <glm/glm.hpp>
+
+struct Color {
+	float r;
+	float g;
+	float b;
+};
+
+struct Vertex {
+	glm::vec3 position;
+	glm::vec3 normal;
+};
+
+class Model {
+public:
+	Model(const std::vector<struct Vertex> &vertices,
+			const std::vector<unsigned int> &indices);
+	void draw(const Shader &shader);
+
+	inline void setColor(float r, float g, float b) {
+		color = { r, g, b };
+	}
+
+private:
+	struct Color color;
+	std::vector<struct Vertex> vertices;
+	std::vector<unsigned int> indices;
+	unsigned int vao, vbo, ebo;
+};

+ 110 - 0
src/Shader.cpp

@@ -0,0 +1,110 @@
+/*
+ * Copyright (C) 2018 Ortega Froysa, Nicolás <nortega@themusicinnoise.net>
+ * Author: Ortega Froysa, Nicolás <nortega@themusicinnoise.net>
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as
+ * published by the Free Software Foundation, either version 3 of the
+ * License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include "Shader.hpp"
+#include "System.hpp"
+
+#include <fstream>
+#include <sstream>
+
+Shader::Shader(const std::string &vShaderPath,
+		const std::string &fShaderPath)
+{
+	// Load the code from the files.
+	std::string vShaderCode, fShaderCode;
+	std::ifstream vShaderFile, fShaderFile;
+	vShaderFile.exceptions(std::ifstream::failbit bitor
+			std::ifstream::badbit);
+	fShaderFile.exceptions(std::ifstream::failbit bitor
+			std::ifstream::badbit);
+	try
+	{
+		vShaderFile.open(vShaderPath);
+		fShaderFile.open(fShaderPath);
+		std::stringstream vShaderStream, fShaderStream;
+		vShaderStream << vShaderFile.rdbuf();
+		fShaderStream << fShaderFile.rdbuf();
+		vShaderFile.close();
+		fShaderFile.close();
+		vShaderCode = vShaderStream.str();
+		fShaderCode = fShaderStream.str();
+	}
+	catch(const std::exception &e)
+	{
+		System::logger->writeError(
+				std::string("Failed to read from file: ") +
+				e.what());
+	}
+
+	// Compile the shaders
+	unsigned int vertexId, fragmentId;
+	int res;
+	char infoLog[512];
+
+	vertexId = glCreateShader(GL_VERTEX_SHADER);
+	{
+		const char *vShaderCodeChar = vShaderCode.c_str();
+		glShaderSource(vertexId, 1,
+				&vShaderCodeChar, nullptr);
+	}
+	glCompileShader(vertexId);
+	glGetShaderiv(vertexId, GL_COMPILE_STATUS, &res);
+	if(not res)
+	{
+		glGetShaderInfoLog(vertexId, 512, nullptr, infoLog);
+		System::logger->writeError(
+				std::string(
+					"Vertex shader compilation failed: ") +
+				infoLog);
+	}
+	fragmentId = glCreateShader(GL_FRAGMENT_SHADER);
+	{
+		const char *fShaderCodeChar = fShaderCode.c_str();
+		glShaderSource(fragmentId, 1,
+				&fShaderCodeChar, nullptr);
+	}
+	glCompileShader(fragmentId);
+	glGetShaderiv(fragmentId, GL_COMPILE_STATUS, &res);
+	if(not res)
+	{
+		glGetShaderInfoLog(fragmentId, 512, nullptr, infoLog);
+		System::logger->writeError(
+				std::string(
+					"Fragment shader compilation failed: ") +
+				infoLog);
+	}
+
+	// Link the program
+	id = glCreateProgram();
+	glAttachShader(id, vertexId);
+	glAttachShader(id, fragmentId);
+	glLinkProgram(id);
+
+	glGetProgramiv(id, GL_LINK_STATUS, &res);
+	if(not res)
+	{
+		glGetProgramInfoLog(id, 512, nullptr, infoLog);
+		System::logger->writeError(
+				std::string("Failed to link program: ") +
+				infoLog);
+	}
+
+	// Delete the shaders
+	glDeleteShader(vertexId);
+	glDeleteShader(fragmentId);
+}

+ 0 - 0
src/Shader.hpp


Some files were not shown because too many files changed in this diff