Model loading and texturing
This commit is contained in:
parent
66bf7776c7
commit
70a2e0352a
@ -37,9 +37,9 @@ target_link_libraries(couch couchlua)
|
|||||||
add_subdirectory(thirdparty)
|
add_subdirectory(thirdparty)
|
||||||
|
|
||||||
add_subdirectory(shaders)
|
add_subdirectory(shaders)
|
||||||
|
add_dependencies(couch shader_headers)
|
||||||
target_include_directories(couch
|
target_include_directories(couch
|
||||||
PUBLIC "${CMAKE_CURRENT_BINARY_DIR}/shaders")
|
PUBLIC "${CMAKE_CURRENT_BINARY_DIR}/shaders/")
|
||||||
|
|
||||||
|
|
||||||
if(WIN32)
|
if(WIN32)
|
||||||
target_link_libraries(couch glfw3dll)
|
target_link_libraries(couch glfw3dll)
|
||||||
|
@ -5,7 +5,9 @@ Mesh::Mesh() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
Mesh::~Mesh() {
|
Mesh::~Mesh() {
|
||||||
delete material;
|
if (material) {
|
||||||
|
delete material;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Mesh::Mesh(VertexList vertices, IndexList indices) {
|
Mesh::Mesh(VertexList vertices, IndexList indices) {
|
||||||
@ -38,8 +40,26 @@ void Mesh::SetupMesh() {
|
|||||||
glBindVertexArray(0);
|
glBindVertexArray(0);
|
||||||
}
|
}
|
||||||
|
|
||||||
Mesh Mesh::FromFile(const char *filename) {
|
Mesh* Mesh::FromFile(const char *filename) {
|
||||||
return Mesh();
|
// 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() {
|
void Mesh::Draw() {
|
||||||
|
10
core/Mesh.h
10
core/Mesh.h
@ -2,8 +2,16 @@
|
|||||||
#define MESH_H
|
#define MESH_H
|
||||||
|
|
||||||
#include <list>
|
#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 "types.h"
|
||||||
|
#include "Util.h"
|
||||||
#include "Spatial.h"
|
#include "Spatial.h"
|
||||||
#include "Vertex.h"
|
#include "Vertex.h"
|
||||||
#include "Index.h"
|
#include "Index.h"
|
||||||
@ -17,7 +25,7 @@ public:
|
|||||||
Mesh();
|
Mesh();
|
||||||
~Mesh();
|
~Mesh();
|
||||||
Mesh(VertexList vertices, IndexList indices);
|
Mesh(VertexList vertices, IndexList indices);
|
||||||
static Mesh FromFile(const char *filename);
|
static Mesh *FromFile(const char *filename);
|
||||||
virtual bool IsDrawable() const {return true;}
|
virtual bool IsDrawable() const {return true;}
|
||||||
virtual void Draw();
|
virtual void Draw();
|
||||||
virtual void SetupMesh();
|
virtual void SetupMesh();
|
||||||
|
18
core/Util.cpp
Normal file
18
core/Util.cpp
Normal 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
15
core/Util.h
Normal 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
BIN
demo/cube.glb
Normal file
Binary file not shown.
@ -40,6 +40,12 @@ function init()
|
|||||||
couch.Node.GetRoot().children:Append(ball1)
|
couch.Node.GetRoot().children:Append(ball1)
|
||||||
|
|
||||||
ball1.transform:Translate(0.0, 3.0, 0.0)
|
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
|
end
|
||||||
|
|
||||||
function update(delta)
|
function update(delta)
|
||||||
|
@ -1,14 +1,16 @@
|
|||||||
project(Couch)
|
project(Couch)
|
||||||
|
|
||||||
file(GLOB shaders *.vert *.frag)
|
macro(add_shader shaderfile)
|
||||||
list(
|
add_custom_command(
|
||||||
TRANSFORM shaders
|
OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/${shaderfile}.h
|
||||||
APPEND .h
|
COMMAND perl "${CMAKE_CURRENT_SOURCE_DIR}/makeheaders.pl" ${CMAKE_CURRENT_SOURCE_DIR}/${shaderfile} ${CMAKE_CURRENT_BINARY_DIR}/${shaderfile}.h
|
||||||
OUTPUT_VARIABLE shader_header_files)
|
DEPENDS ${shaderfile} makeheaders.pl
|
||||||
|
)
|
||||||
|
endmacro()
|
||||||
|
|
||||||
|
add_shader(flat.vert)
|
||||||
|
add_shader(flat.frag)
|
||||||
|
|
||||||
add_custom_target(shader_headers
|
add_custom_target(shader_headers
|
||||||
BYPRODUCTS ${shader_header_files}
|
DEPENDS flat.vert.h flat.frag.h
|
||||||
COMMAND perl "${CMAKE_CURRENT_SOURCE_DIR}/makeheaders.pl"
|
COMMENT "Generated shaders headers.")
|
||||||
DEPENDS ${shaders} ${CMAKE_CURRENT_SOURCE_DIR}/makeheaders.pl)
|
|
||||||
|
|
||||||
add_dependencies(couch shader_headers)
|
|
||||||
|
@ -1,33 +1,27 @@
|
|||||||
use strict;
|
use strict;
|
||||||
use warnings;
|
use warnings;
|
||||||
|
use File::Basename;
|
||||||
|
|
||||||
sub convert {
|
my ($filename, $outfilename) = @ARGV;
|
||||||
my $filename = $1;
|
|
||||||
|
|
||||||
my $headerguard = "$filename.h";
|
my $headerguard = basename $outfilename;
|
||||||
$headerguard =~ tr/a-z./A-Z_/;
|
$headerguard =~ tr/a-z./A-Z_/;
|
||||||
|
|
||||||
my $constname = "$filename";
|
my $constname = basename "$filename";
|
||||||
$constname =~ tr/./_/;
|
$constname =~ tr/./_/;
|
||||||
|
|
||||||
open my $fin, '<', $filename;
|
open(my $fin, '<', $filename) or die("Couldn't find $filename");
|
||||||
open my $fout, '>', "$filename.h";
|
open my $fout, '>', $outfilename;
|
||||||
|
|
||||||
print $fout "#ifndef $headerguard \n#define $headerguard\nconst char * $constname = \n";
|
print $fout "#ifndef $headerguard \n#define $headerguard\nconst char * $constname = \n";
|
||||||
|
|
||||||
while(my $line = <$fin>) {
|
while(my $line = <$fin>) {
|
||||||
$line =~ s/\n/\\n/;
|
$line =~ s/\n/\\n/;
|
||||||
print $fout "\"$line\"\n";
|
print $fout "\"$line\"\n";
|
||||||
}
|
|
||||||
|
|
||||||
print $fout ";\n#endif // $headerguard\n";
|
|
||||||
close $fin;
|
|
||||||
close $fout;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
opendir DIR, ".";
|
print $fout ";\n#endif // $headerguard\n";
|
||||||
while (readdir DIR) {
|
close $fin;
|
||||||
if (/(.*\.(frag|vert))/) {
|
close $fout;
|
||||||
convert;
|
print "Generated $outfilename\n";
|
||||||
}
|
|
||||||
}
|
|
||||||
|
10
thirdparty/CMakeLists.txt
vendored
10
thirdparty/CMakeLists.txt
vendored
@ -1,7 +1,15 @@
|
|||||||
project(Couch)
|
project(Couch)
|
||||||
|
|
||||||
|
|
||||||
|
## STB (image)
|
||||||
target_include_directories(couch
|
target_include_directories(couch
|
||||||
PUBLIC stb)
|
PUBLIC stb)
|
||||||
|
|
||||||
target_include_directories(couchlua
|
target_include_directories(couchlua
|
||||||
PUBLIC stb)
|
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)
|
||||||
|
Loading…
Reference in New Issue
Block a user