Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Addition of Chapter 3 Part 1: Model Loading #76

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
29 changes: 11 additions & 18 deletions Common/Model.cs
Expand Up @@ -4,14 +4,15 @@
using Assimp;
using AssimpMesh = Assimp.Mesh;
using System.Runtime.CompilerServices;
using System.Diagnostics;

namespace LearnOpenTK.Common
{
public static class Extensions
{
public static Vector3 ConvertAssimpVector3(this Vector3D AssimpVector)
{
// Reinterpret the assimp vector into a OpenTK vector.
// Reinterpret the assimp vector into an OpenTK vector.
return Unsafe.As<Vector3D, Vector3>(ref AssimpVector);
}

Expand All @@ -31,33 +32,18 @@ public class Model
// Assimp supports many common file formats including .fbx, .obj, .blend and many others
// Check out http://assimp.sourceforge.net/main_features_formats.html for a complete list.
public Model(string path)
{
LoadModel(path);
}

// Draws the model, and thus all its meshes
public void Draw()
{
for (int i = 0; i < meshes.Count; i++)
meshes[i].Draw();
}

// loads a model with supported ASSIMP extensions from file and stores the resulting meshes in the meshes list.

private void LoadModel(string path)
{
// Create a new importer
AssimpContext importer = new AssimpContext();

// We can define a logging callback function that receives messages during the ImportFile method and print them to the console.
// We can define a logging callback function that receives messages during the ImportFile method and print them to the debug console.
// These give information about which step is happening in the import such as:
// "Info, T18696: Found a matching importer for this file format: Autodesk FBX Importer."
// or it can give you important error information such as:
// "Error, T18696: FBX: no material assigned to mesh, setting default material"
// Note that in order to see the messages, you must temporarily change your project to be a console application or log the messages another way
LogStream logstream = new LogStream((String msg, String userData) =>
{
Console.WriteLine(msg);
Debug.WriteLine(msg);
});
logstream.Attach();
NogginBops marked this conversation as resolved.
Show resolved Hide resolved

Expand Down Expand Up @@ -88,6 +74,13 @@ private void LoadModel(string path)
// is now contained within our list of processed meshes
importer.Dispose();
NogginBops marked this conversation as resolved.
Show resolved Hide resolved
}

// Draws the model, and thus all its meshes
public void Draw()
{
for (int i = 0; i < meshes.Count; i++)
meshes[i].Draw();
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would actually like to see this logic moved into the chapter code itself. This type of abstraction using objects with a Draw method isn't an abstraction that works in the long term for graphics. It becomes really inefficient, so I think that we should avoid making the suggestion that this is a good way to structure rendering code.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This means both the mesh and the model drawing code.

And I know the other abstractions like texture and shaders do this type of things too with their Use methods, but I'm not happy with those either.


// Processes a node in a recursive fashion. Processes each individual mesh located at the node and repeats this process on its children nodes (if any).
private void ProcessNode(Node node, Scene scene, Matrix4 parentTransform)
Expand Down