Model loading and texturing

This commit is contained in:
Dane Johnson 2021-01-18 18:25:47 -06:00
parent 66bf7776c7
commit 70a2e0352a
10 changed files with 113 additions and 42 deletions

View File

@ -37,9 +37,9 @@ target_link_libraries(couch couchlua)
add_subdirectory(thirdparty)
add_subdirectory(shaders)
add_dependencies(couch shader_headers)
target_include_directories(couch
PUBLIC "${CMAKE_CURRENT_BINARY_DIR}/shaders")
PUBLIC "${CMAKE_CURRENT_BINARY_DIR}/shaders/")
if(WIN32)
target_link_libraries(couch glfw3dll)

View File

@ -5,7 +5,9 @@ Mesh::Mesh() {
}
Mesh::~Mesh() {
delete material;
if (material) {
delete material;
}
}
Mesh::Mesh(VertexList vertices, IndexList indices) {
@ -38,8 +40,26 @@ void Mesh::SetupMesh() {
glBindVertexArray(0);
}
Mesh Mesh::FromFile(const char *filename) {
return Mesh();
Mesh* Mesh::FromFile(const char *filename) {
// HOCUS: https://assimp-docs.readthedocs.io/en/latest/usage/use_the_lib.html
Assimp::Importer importer;
const aiScene* scene = importer.ReadFile(
filename, // Read the file
aiProcess_Triangulate | // We only do triangles
aiProcess_GenNormals | // We want normals precalculated
aiProcess_GenUVCoords // We want UV mappings precalculated
);
if (!scene) {
std::cerr << importer.GetErrorString() << std::endl;
exit(1);
}
aiNode *root = scene->mRootNode;
aiMesh *mesh_to_import = scene->mMeshes[root->mMeshes[0]];
return Util::aiMesh2Mesh(mesh_to_import);
}
void Mesh::Draw() {

View File

@ -2,8 +2,16 @@
#define MESH_H
#include <list>
#include <iostream>
#include <stdlib.h>
// Thirdpart includes
#include <assimp/Importer.hpp>
#include <assimp/scene.h>
#include <assimp/postprocess.h>
#include "types.h"
#include "Util.h"
#include "Spatial.h"
#include "Vertex.h"
#include "Index.h"
@ -17,7 +25,7 @@ public:
Mesh();
~Mesh();
Mesh(VertexList vertices, IndexList indices);
static Mesh FromFile(const char *filename);
static Mesh *FromFile(const char *filename);
virtual bool IsDrawable() const {return true;}
virtual void Draw();
virtual void SetupMesh();

18
core/Util.cpp Normal file
View File

@ -0,0 +1,18 @@
#include "Util.h"
Mesh *Util::aiMesh2Mesh(aiMesh *aimesh){
Mesh *mymesh = new Mesh();
for (int i = 0; i < aimesh->mNumVertices; i++) {
aiVector3D aiPosition = aimesh->mVertices[i];
aiVector3D aiUV = aimesh->mTextureCoords[0][i]; // TODO get ALL texture coords
Vertex vertex(aiPosition.x, aiPosition.y, aiPosition.z, aiUV.x, aiUV.y);
mymesh->vertices.push_back(vertex);
}
for (int i = 0; i < aimesh->mNumFaces; i++) {
// We're importing triangulated meshes, so each face is three indices
unsigned int *face = aimesh->mFaces[i].mIndices;
Index index(face[0], face[1], face[2]);
mymesh->indices.push_back(index);
}
return mymesh;
}

15
core/Util.h Normal file
View File

@ -0,0 +1,15 @@
#ifndef UTIL_H
#define UTIL_H
// Thirdparty includes
#include <assimp/mesh.h>
// Gahhhhhh mutual inclusion!
#include "Mesh.h"
class Mesh;
namespace Util {
Mesh *aiMesh2Mesh(aiMesh *mesh);
}
#endif /* UTIL_H */

BIN
demo/cube.glb Normal file

Binary file not shown.

View File

@ -40,6 +40,12 @@ function init()
couch.Node.GetRoot().children:Append(ball1)
ball1.transform:Translate(0.0, 3.0, 0.0)
cube = couch.Mesh.FromFile("cube.glb")
cube:SetupMesh();
cube.material.tex = couch.Texture.FromFile("container.png")
cube.material.usesTex = true
couch.Node.GetRoot().children:Append(cube)
end
function update(delta)

View File

@ -1,14 +1,16 @@
project(Couch)
file(GLOB shaders *.vert *.frag)
list(
TRANSFORM shaders
APPEND .h
OUTPUT_VARIABLE shader_header_files)
macro(add_shader shaderfile)
add_custom_command(
OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/${shaderfile}.h
COMMAND perl "${CMAKE_CURRENT_SOURCE_DIR}/makeheaders.pl" ${CMAKE_CURRENT_SOURCE_DIR}/${shaderfile} ${CMAKE_CURRENT_BINARY_DIR}/${shaderfile}.h
DEPENDS ${shaderfile} makeheaders.pl
)
endmacro()
add_shader(flat.vert)
add_shader(flat.frag)
add_custom_target(shader_headers
BYPRODUCTS ${shader_header_files}
COMMAND perl "${CMAKE_CURRENT_SOURCE_DIR}/makeheaders.pl"
DEPENDS ${shaders} ${CMAKE_CURRENT_SOURCE_DIR}/makeheaders.pl)
add_dependencies(couch shader_headers)
DEPENDS flat.vert.h flat.frag.h
COMMENT "Generated shaders headers.")

View File

@ -1,33 +1,27 @@
use strict;
use warnings;
use File::Basename;
sub convert {
my $filename = $1;
my $headerguard = "$filename.h";
$headerguard =~ tr/a-z./A-Z_/;
my $constname = "$filename";
$constname =~ tr/./_/;
my ($filename, $outfilename) = @ARGV;
open my $fin, '<', $filename;
open my $fout, '>', "$filename.h";
print $fout "#ifndef $headerguard \n#define $headerguard\nconst char * $constname = \n";
my $headerguard = basename $outfilename;
$headerguard =~ tr/a-z./A-Z_/;
while(my $line = <$fin>) {
$line =~ s/\n/\\n/;
print $fout "\"$line\"\n";
}
my $constname = basename "$filename";
$constname =~ tr/./_/;
print $fout ";\n#endif // $headerguard\n";
close $fin;
close $fout;
open(my $fin, '<', $filename) or die("Couldn't find $filename");
open my $fout, '>', $outfilename;
print $fout "#ifndef $headerguard \n#define $headerguard\nconst char * $constname = \n";
while(my $line = <$fin>) {
$line =~ s/\n/\\n/;
print $fout "\"$line\"\n";
}
opendir DIR, ".";
while (readdir DIR) {
if (/(.*\.(frag|vert))/) {
convert;
}
}
print $fout ";\n#endif // $headerguard\n";
close $fin;
close $fout;
print "Generated $outfilename\n";

View File

@ -1,7 +1,15 @@
project(Couch)
## STB (image)
target_include_directories(couch
PUBLIC stb)
target_include_directories(couchlua
PUBLIC stb)
## ASSIMP
set(ASSIMP_BUILD_ALL_IMPORTERS_BY_DEFAULT FALSE CACHE BOOL "Turn off all assimp importers")
set(ASSIMP_BUILD_GLTF_IMPORTER ON CACHE BOOL "Turn on gltf importer")
add_subdirectory(assimp)
target_link_libraries(couch assimp::assimp)
target_link_libraries(couchlua assimp::assimp)