Add MTL file loading

This commit is contained in:
austin committed 2020-07-01 17:42:55 -07:00
1 parent 21fb0058ad
commit c3c154e4ff
11 files changed
+415 -308

No files matched your search

+2 -2
View File
@@ -18,8 +18,8 @@ struct Camera
rotation[1] = 0.0f;
rotation[2] = 0.0f;
nearClip = 1.0f;
farClip = 1000.0f;
nearClip = 5.0f;
farClip = 600.0f;
}
inline void SetFOV(float fov, int winWidth, int winHeight)
+1 -1
View File
@@ -34,7 +34,7 @@ struct Engine_Buffer
// FUNCTIONS
int Engine_Init(Engine_Buffer &buffer, char *filename);
int Engine_Init(Engine_Buffer &buffer, char *objFilename, char *mtlFilename);
void Engine_Render(Engine_Buffer &buffer, uint32_t input);
void Engine_Shutdown(void);
+47 -31
View File
@@ -4,16 +4,12 @@
#include "engine.h"
#include "point.h"
#include <cstdint>
#include <vector>
// STRUCTURES
struct Material
{
bool smooth;
ColorF32 kAmbient;
ColorF32 kDiffuse;
};
#define VERTEX_LIMIT (20000)
#define FACE_LIMIT (30000)
#define MATERIAL_LIMIT (10)
struct Texture
{
@@ -22,18 +18,37 @@ struct Texture
unsigned int height;
};
struct TextureCoord
struct TextureList
{
Texture data[MATERIAL_LIMIT];
size_t size;
};
struct Material
{
ColorF32 ambient;
ColorF32 diffuse;
ColorF32 specular;
float specularExp;
float opacity;
};
struct MaterialList
{
Material data[MATERIAL_LIMIT];
size_t size;
};
struct UV
{
float u;
float v;
};
struct Face
struct UVList
{
unsigned int vertIndex[3];
unsigned int textureIndex[3];
Vector normal;
ColorF32 color;
UV data[VERTEX_LIMIT];
size_t size;
};
struct Vertex
@@ -43,17 +58,23 @@ struct Vertex
ColorF32 color;
};
struct Mesh_LocalData
struct VertexList
{
std::vector<Vertex> verts;
std::vector<Face> faces;
std::vector<TextureCoord> uvs;
Vertex data[VERTEX_LIMIT];
size_t size;
};
struct Mesh_TransformedData
struct Face
{
std::vector<Vertex> verts;
std::vector<Face> faces;
int vertIndex[3];
int uvIndex[3];
int materialIndex;
};
struct FaceList
{
Face data[FACE_LIMIT];
size_t size;
};
struct Mesh
@@ -74,22 +95,17 @@ struct Mesh
Point position;
float rotation[3];
float scale;
Material material;
Mesh_LocalData local;
Mesh_TransformedData transformed;
};
// PUBLIC FUNCTIONS
void CullBackfaces(
Mesh_LocalData &local, Mesh_TransformedData &transformed,
Point &camPosition);
void ClipAndCull(
VertexList &verts, FaceList &localFaces,
FaceList &transFaces, Point &camPosition);
void RenderMesh(
Engine_Buffer &buffer, Mesh_TransformedData &mesh,
Texture &texture, std::vector<TextureCoord> &uvs);
Engine_Buffer &buffer, FaceList &faces, VertexList &verts,
UVList &uvs, TextureList &textures);
#define GEOMETRY_H
+2 -2
View File
@@ -3,8 +3,8 @@
#include "geometry.h"
int LoadMesh(char *filename, Mesh &mesh);
int LoadTexture(char *filename, Texture &texture);
int ParseOBJ(char *filename, VertexList &verts, FaceList &faces, UVList &uvs);
int ParseMTL(char *filename, MaterialList &materials, TextureList &textures);
#define LOADER_H
+25 -13
View File
@@ -22,36 +22,48 @@ struct Point
// OPERATORS
// v / f
inline Point operator/(Point v, float f)
// p / f
inline Point operator/(Point p, float f)
{
Point result;
float inverse = 1.0f / f;
result.x = v.x * inverse;
result.y = v.y * inverse;
result.z = v.z * inverse;
result.x = p.x * inverse;
result.y = p.y * inverse;
result.z = p.z * inverse;
return result;
}
// v /= f
inline Point &operator/=(Point &v, float f)
inline Point &operator/=(Point &p, float f)
{
v = v / f;
p = p / f;
return v;
return p;
}
// v1 - v2
inline Vector operator-(Point v1, Point v2)
// p1 - p2
inline Vector operator-(Point p1, Point p2)
{
Vector result;
result.x = v1.x - v2.x;
result.y = v1.y - v2.y;
result.z = v1.z - v2.z;
result.x = p1.x - p2.x;
result.y = p1.y - p2.y;
result.z = p1.z - p2.z;
return result;
}
// -p
inline Point operator-(Point p)
{
Point result;
result.x = -p.x;
result.y = -p.y;
result.z = -p.z;
return result;
}