obj
This commit is contained in:
parent
65954d2dcf
commit
8a38007e35
|
@ -2,19 +2,9 @@
|
||||||
|
|
||||||
#include <Orange/Core/Types.h>
|
#include <Orange/Core/Types.h>
|
||||||
#include <Orange/Core/Result.h>
|
#include <Orange/Core/Result.h>
|
||||||
|
#include <Orange/Core/Span.h>
|
||||||
|
|
||||||
namespace orange::fs
|
namespace orange::fs
|
||||||
{
|
{
|
||||||
enum class FileSystemErrorCode : ErrorCodeBaseType
|
Result<Buffer> OpenFileIntoBuffer(const char* path, bool text = false);
|
||||||
{
|
|
||||||
Invalid = -1,
|
|
||||||
Failed = -2,
|
|
||||||
|
|
||||||
Success = 0,
|
|
||||||
};
|
|
||||||
|
|
||||||
template <typename T>
|
|
||||||
using FileResult = Result<T, FileSystemErrorCode, FileSystemErrorCode::Success, FileSystemErrorCode::Invalid>;
|
|
||||||
|
|
||||||
FileResult<Buffer> OpenFileIntoBuffer(const char *path);
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,96 @@
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include "Orange/Core/SmallVector.h"
|
||||||
|
#include <Orange/Core/Result.h>
|
||||||
|
#include <Orange/Core/Span.h>
|
||||||
|
|
||||||
|
#include <charconv>
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
|
namespace orange::stream
|
||||||
|
{
|
||||||
|
template <typename T>
|
||||||
|
Result<T> Parse(const char*& first, const char* end)
|
||||||
|
{
|
||||||
|
T obj;
|
||||||
|
auto result = std::from_chars(first, end, obj);
|
||||||
|
if (result.ec == std::errc{})
|
||||||
|
{
|
||||||
|
first = result.ptr;
|
||||||
|
return Result<T>::Success(obj);
|
||||||
|
}
|
||||||
|
|
||||||
|
return Result<T>::Error(BasicErrorCode::NotFound);
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename T>
|
||||||
|
Result<T> Parse(const char*& first, size_t length)
|
||||||
|
{
|
||||||
|
return Parse<T>(first, first + length);
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename T>
|
||||||
|
Result<T> Parse(const char*& first)
|
||||||
|
{
|
||||||
|
return Parse<T>(first, first + strlen(first));
|
||||||
|
}
|
||||||
|
|
||||||
|
inline bool CharMatches(char character, StringView delims)
|
||||||
|
{
|
||||||
|
for (auto delim : delims)
|
||||||
|
{
|
||||||
|
if (character == delim)
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
inline size_t Consume(const char*& first, StringView delims)
|
||||||
|
{
|
||||||
|
size_t count = 0;
|
||||||
|
while (CharMatches(*first, delims))
|
||||||
|
{
|
||||||
|
first++;
|
||||||
|
count++;
|
||||||
|
}
|
||||||
|
|
||||||
|
return count;
|
||||||
|
}
|
||||||
|
|
||||||
|
inline size_t ConsumeSpace(const char*& first)
|
||||||
|
{
|
||||||
|
return Consume(first, " \t");
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename OutArray = SmallVector<char, 1>>
|
||||||
|
size_t ReadOrAdvance(const char*& first, StringView delims, size_t advancement = 0, OutArray* array = nullptr)
|
||||||
|
{
|
||||||
|
size_t count = 0;
|
||||||
|
while (!CharMatches(*first, delims) && *first != '\0')
|
||||||
|
{
|
||||||
|
if (array)
|
||||||
|
array->PushBack(*first);
|
||||||
|
first++;
|
||||||
|
count++;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (*first == '\0')
|
||||||
|
return count;
|
||||||
|
|
||||||
|
first += advancement;
|
||||||
|
|
||||||
|
return count;
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename OutArray = SmallVector<char, 1>>
|
||||||
|
size_t ReadString(const char*& first, StringView delims, OutArray& array)
|
||||||
|
{
|
||||||
|
return ReadOrAdvance(first, delims, 0, &array);
|
||||||
|
}
|
||||||
|
|
||||||
|
inline size_t AdvancePast(const char*& first, StringView delims)
|
||||||
|
{
|
||||||
|
return ReadOrAdvance(first, delims, 1);
|
||||||
|
}
|
||||||
|
}
|
|
@ -32,6 +32,17 @@ namespace orange
|
||||||
delete[] u.m_ptr;
|
delete[] u.m_ptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
template <typename J>
|
||||||
|
bool operator == (const Span<J>& x)
|
||||||
|
{
|
||||||
|
return Span<T>(*this) == x;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool operator == (const char *x)
|
||||||
|
{
|
||||||
|
return Span<T>(*this) == StringView(x);
|
||||||
|
}
|
||||||
|
|
||||||
void Reserve(size_t n)
|
void Reserve(size_t n)
|
||||||
{
|
{
|
||||||
n = PickCapacity(n);
|
n = PickCapacity(n);
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include <Orange/Core/Types.h>
|
#include <Orange/Core/Types.h>
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
namespace orange
|
namespace orange
|
||||||
{
|
{
|
||||||
|
@ -23,6 +24,21 @@ namespace orange
|
||||||
T& operator [] (size_t idx) { return data[idx]; }
|
T& operator [] (size_t idx) { return data[idx]; }
|
||||||
const T& operator [] (size_t idx) const { return data[idx]; }
|
const T& operator [] (size_t idx) const { return data[idx]; }
|
||||||
|
|
||||||
|
template <typename J>
|
||||||
|
bool operator == (const Span<J>& other) const
|
||||||
|
{
|
||||||
|
if (other.size != size)
|
||||||
|
return false;
|
||||||
|
|
||||||
|
for (size_t i = 0; i < size; i++)
|
||||||
|
{
|
||||||
|
if (data[i] != other.data[i])
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
bool IsEmpty() const { return size == 0; }
|
bool IsEmpty() const { return size == 0; }
|
||||||
|
|
||||||
T* begin() { return &data[0]; }
|
T* begin() { return &data[0]; }
|
||||||
|
@ -42,7 +58,11 @@ namespace orange
|
||||||
};
|
};
|
||||||
|
|
||||||
using BufferView = Span<uint8_t>;
|
using BufferView = Span<uint8_t>;
|
||||||
using StringView = Span<char>;
|
struct StringView : public Span<const char>
|
||||||
|
{
|
||||||
|
StringView(const char* str)
|
||||||
|
: Span<const char>{str, strlen(str)} {}
|
||||||
|
};
|
||||||
|
|
||||||
struct Buffer : public BufferView
|
struct Buffer : public BufferView
|
||||||
{
|
{
|
||||||
|
|
|
@ -1,12 +1,86 @@
|
||||||
|
#include "Orange/Core/SmallVector.h"
|
||||||
|
#include "Orange/Core/Span.h"
|
||||||
#include <Orange/Core/Result.h>
|
#include <Orange/Core/Result.h>
|
||||||
|
#include <Orange/Core/FileSystem.h>
|
||||||
|
#include <Orange/Core/Parse.h>
|
||||||
|
|
||||||
#include <Orange/Render/Window.h>
|
#include <Orange/Render/Window.h>
|
||||||
#include <Orange/Render/RenderContext.h>
|
#include <Orange/Render/RenderContext.h>
|
||||||
#include <Orange/Render/Swapchain.h>
|
#include <Orange/Render/Swapchain.h>
|
||||||
#include <vulkan/vulkan_core.h>
|
|
||||||
|
|
||||||
using namespace orange;
|
using namespace orange;
|
||||||
|
|
||||||
|
Result<VoidResult> ParseOBJ(BufferView buffer)
|
||||||
|
{
|
||||||
|
const char* obj = (const char *)buffer.data;
|
||||||
|
do
|
||||||
|
{
|
||||||
|
SmallVector<char, 8> element;
|
||||||
|
stream::ReadString(obj, " #\n", element);
|
||||||
|
|
||||||
|
if (element == "v" || element == "vn" || element == "vt")
|
||||||
|
{
|
||||||
|
float vtx[3]{};
|
||||||
|
for (int i = 0; i < 3; i++)
|
||||||
|
{
|
||||||
|
stream::ConsumeSpace(obj);
|
||||||
|
auto r_float = stream::Parse<float>(obj);
|
||||||
|
if (!r_float) return Result<VoidResult>::Error();
|
||||||
|
vtx[i] = *r_float;
|
||||||
|
}
|
||||||
|
|
||||||
|
element.PushBack('\0');
|
||||||
|
log::info("Found: %s %f %f %f", element.Data(), vtx[0], vtx[1], vtx[2]);
|
||||||
|
}
|
||||||
|
else if (element == "g" || element == "o")
|
||||||
|
{
|
||||||
|
stream::ConsumeSpace(obj);
|
||||||
|
SmallVector<char, 32> name;
|
||||||
|
stream::ReadString(obj, " #\n", name);
|
||||||
|
|
||||||
|
name.PushBack('\0');
|
||||||
|
if (element == "g")
|
||||||
|
log::info("Group name: %s", name.Data());
|
||||||
|
else
|
||||||
|
log::info("Object name: %s", name.Data());
|
||||||
|
}
|
||||||
|
else if (element == "f")
|
||||||
|
{
|
||||||
|
int32_t indices[3][3]{};
|
||||||
|
for (int i = 0; i < 3; i++)
|
||||||
|
{
|
||||||
|
stream::ConsumeSpace(obj);
|
||||||
|
|
||||||
|
for (int j = 0; j < 3; j++)
|
||||||
|
{
|
||||||
|
indices[i][j] = -1;
|
||||||
|
|
||||||
|
if (j == 0 || stream::Consume(obj, "/"))
|
||||||
|
{
|
||||||
|
auto r_int = stream::Parse<uint32_t>(obj);
|
||||||
|
if (!r_int) continue;
|
||||||
|
|
||||||
|
indices[i][j] = *r_int;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
log::info("Found: f %d/%d/%d %d/%d/%d %d/%d/%d",
|
||||||
|
indices[0][0], indices[0][1], indices[0][2],
|
||||||
|
indices[1][0], indices[1][1], indices[1][2],
|
||||||
|
indices[2][0], indices[2][1], indices[2][2]);
|
||||||
|
}
|
||||||
|
else if (element.Size())
|
||||||
|
{
|
||||||
|
element.PushBack('\0');
|
||||||
|
log::info("Unknown element: %s", element.Data());
|
||||||
|
}
|
||||||
|
|
||||||
|
stream::AdvancePast(obj, "\n");
|
||||||
|
} while (*obj != '\0');
|
||||||
|
|
||||||
|
return Result<VoidResult>::Success();
|
||||||
|
}
|
||||||
|
|
||||||
int main(int argc, char** argv)
|
int main(int argc, char** argv)
|
||||||
{
|
{
|
||||||
(void)argc; (void)argv;
|
(void)argc; (void)argv;
|
||||||
|
@ -27,6 +101,14 @@ int main(int argc, char** argv)
|
||||||
if (!r_swapchain)
|
if (!r_swapchain)
|
||||||
return 1;
|
return 1;
|
||||||
|
|
||||||
|
auto r_objData = fs::OpenFileIntoBuffer("/home/joshua/chair.obj", true);
|
||||||
|
if (!r_objData)
|
||||||
|
return 1;
|
||||||
|
|
||||||
|
ParseOBJ(*r_objData);
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
|
||||||
while (r_window->Update())
|
while (r_window->Update())
|
||||||
{
|
{
|
||||||
r_renderContext->BeginCommandBuffer(r_swapchain->CommandBuffer());
|
r_renderContext->BeginCommandBuffer(r_swapchain->CommandBuffer());
|
||||||
|
|
|
@ -0,0 +1,23 @@
|
||||||
|
#include <Orange/Core/FileSystem.h>
|
||||||
|
|
||||||
|
#include <stdio.h>
|
||||||
|
|
||||||
|
namespace orange::fs
|
||||||
|
{
|
||||||
|
Result<Buffer> OpenFileIntoBuffer(const char* path, bool text)
|
||||||
|
{
|
||||||
|
FILE* file = fopen(path, text ? "r" : "rb");
|
||||||
|
if (!file)
|
||||||
|
Result<Buffer>::Error(BasicErrorCode::NotFound);
|
||||||
|
fseek(file, 0, SEEK_END);
|
||||||
|
size_t size = size_t(ftell(file));
|
||||||
|
fseek(file, 0, SEEK_SET);
|
||||||
|
|
||||||
|
uint8_t* data = new uint8_t[size];
|
||||||
|
fread(data, size, 1, file);
|
||||||
|
fclose(file);
|
||||||
|
if (text) data[size++] = '\0';
|
||||||
|
|
||||||
|
return Result<Buffer>::Success(data, size);
|
||||||
|
}
|
||||||
|
}
|
|
@ -18,10 +18,11 @@ namespace orange::log
|
||||||
stbsp_vsnprintf(buf, sizeof(buf), fmt, va);
|
stbsp_vsnprintf(buf, sizeof(buf), fmt, va);
|
||||||
va_end(va);
|
va_end(va);
|
||||||
|
|
||||||
if (level <= LogLevels::Error)
|
//if (level <= LogLevels::Error)
|
||||||
fputs(buf, stderr);
|
// fputs(buf, stderr);
|
||||||
else
|
//else
|
||||||
fputs(buf, stdout);
|
// fputs(buf, stdout);
|
||||||
|
puts(buf);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,4 +1,5 @@
|
||||||
orange_src = [
|
orange_src = [
|
||||||
|
'Core/Filesystem.cpp',
|
||||||
'Core/Log.cpp',
|
'Core/Log.cpp',
|
||||||
|
|
||||||
'Render/RenderContext_Init.cpp',
|
'Render/RenderContext_Init.cpp',
|
||||||
|
|
Loading…
Reference in New Issue