add src/coders/obj
This commit is contained in:
@@ -163,6 +163,28 @@ void BasicParser::goBack(size_t count) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void BasicParser::reset() {
|
||||||
|
pos = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
char BasicParser::peekInLine() {
|
||||||
|
while (hasNext()) {
|
||||||
|
char next = source[pos];
|
||||||
|
if (next == '\n') {
|
||||||
|
return next;
|
||||||
|
}
|
||||||
|
if (is_whitespace(next)) {
|
||||||
|
pos++;
|
||||||
|
} else {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (pos >= source.length()) {
|
||||||
|
throw error("unexpected end");
|
||||||
|
}
|
||||||
|
return source[pos];
|
||||||
|
}
|
||||||
|
|
||||||
char BasicParser::peek() {
|
char BasicParser::peek() {
|
||||||
skipWhitespace();
|
skipWhitespace();
|
||||||
if (pos >= source.length()) {
|
if (pos >= source.length()) {
|
||||||
@@ -226,6 +248,19 @@ int64_t BasicParser::parseSimpleInt(int base) {
|
|||||||
return value;
|
return value;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
dynamic::Value BasicParser::parseNumber() {
|
||||||
|
switch (peek()) {
|
||||||
|
case '-':
|
||||||
|
skip(1);
|
||||||
|
return parseNumber(-1);
|
||||||
|
case '+':
|
||||||
|
skip(1);
|
||||||
|
return parseNumber(1);
|
||||||
|
default:
|
||||||
|
return parseNumber(1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
dynamic::Value BasicParser::parseNumber(int sign) {
|
dynamic::Value BasicParser::parseNumber(int sign) {
|
||||||
char c = peek();
|
char c = peek();
|
||||||
int base = 10;
|
int base = 10;
|
||||||
|
|||||||
@@ -87,9 +87,11 @@ protected:
|
|||||||
bool isNext(const std::string& substring);
|
bool isNext(const std::string& substring);
|
||||||
void expectNewLine();
|
void expectNewLine();
|
||||||
void goBack(size_t count=1);
|
void goBack(size_t count=1);
|
||||||
|
void reset();
|
||||||
|
|
||||||
int64_t parseSimpleInt(int base);
|
int64_t parseSimpleInt(int base);
|
||||||
dynamic::Value parseNumber(int sign);
|
dynamic::Value parseNumber(int sign);
|
||||||
|
dynamic::Value parseNumber();
|
||||||
std::string parseString(char chr, bool closeRequired=true);
|
std::string parseString(char chr, bool closeRequired=true);
|
||||||
|
|
||||||
parsing_error error(const std::string& message);
|
parsing_error error(const std::string& message);
|
||||||
@@ -99,6 +101,7 @@ public:
|
|||||||
std::string parseName();
|
std::string parseName();
|
||||||
bool hasNext();
|
bool hasNext();
|
||||||
char peek();
|
char peek();
|
||||||
|
char peekInLine();
|
||||||
char peekNoJump();
|
char peekNoJump();
|
||||||
char nextChar();
|
char nextChar();
|
||||||
|
|
||||||
|
|||||||
+2
-6
@@ -209,9 +209,8 @@ std::unique_ptr<List> Parser::parseList() {
|
|||||||
|
|
||||||
Value Parser::parseValue() {
|
Value Parser::parseValue() {
|
||||||
char next = peek();
|
char next = peek();
|
||||||
if (next == '-' || next == '+') {
|
if (next == '-' || next == '+' || is_digit(next)) {
|
||||||
pos++;
|
return parseNumber();
|
||||||
return parseNumber(next == '-' ? -1 : 1);
|
|
||||||
}
|
}
|
||||||
if (is_identifier_start(next)) {
|
if (is_identifier_start(next)) {
|
||||||
std::string literal = parseName();
|
std::string literal = parseName();
|
||||||
@@ -232,9 +231,6 @@ Value Parser::parseValue() {
|
|||||||
if (next == '[') {
|
if (next == '[') {
|
||||||
return List_sptr(parseList().release());
|
return List_sptr(parseList().release());
|
||||||
}
|
}
|
||||||
if (is_digit(next)) {
|
|
||||||
return parseNumber(1);
|
|
||||||
}
|
|
||||||
if (next == '"' || next == '\'') {
|
if (next == '"' || next == '\'') {
|
||||||
pos++;
|
pos++;
|
||||||
return parseString(next);
|
return parseString(next);
|
||||||
|
|||||||
@@ -0,0 +1,124 @@
|
|||||||
|
#include "obj.hpp"
|
||||||
|
|
||||||
|
#include "commons.hpp"
|
||||||
|
#include "../graphics/core/Model.hpp"
|
||||||
|
|
||||||
|
using namespace model;
|
||||||
|
|
||||||
|
class ObjParser : BasicParser {
|
||||||
|
std::vector<glm::vec3> coords {{0, 0, 0}};
|
||||||
|
std::vector<glm::vec2> uvs {{0, 0}};
|
||||||
|
std::vector<glm::vec3> normals {{0, 1, 0}};
|
||||||
|
|
||||||
|
void parseFace(Mesh& mesh) {
|
||||||
|
std::vector<Vertex> vertices;
|
||||||
|
while (hasNext()) {
|
||||||
|
auto c = peekInLine();
|
||||||
|
if (c == '\n') {
|
||||||
|
break;
|
||||||
|
} else {
|
||||||
|
uint indices[3] {};
|
||||||
|
uint i = 0;
|
||||||
|
do {
|
||||||
|
char next = peekInLine();
|
||||||
|
if (is_digit(next)) {
|
||||||
|
indices[i] = parseSimpleInt(10);
|
||||||
|
if (peekInLine() == '/') {
|
||||||
|
pos++;
|
||||||
|
}
|
||||||
|
} else if (next == '/') {
|
||||||
|
pos++;
|
||||||
|
} else {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
} while (peekInLine() != '\n' && ++i < 3);
|
||||||
|
|
||||||
|
vertices.push_back(Vertex {
|
||||||
|
coords[indices[0]], uvs[indices[1]], normals[indices[2]]
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (peekInLine() != '\n' && hasNext()) {
|
||||||
|
skipLine();
|
||||||
|
}
|
||||||
|
if (vertices.size() >= 3) {
|
||||||
|
for (size_t j = 0; j < vertices.size() - 2; j++) {
|
||||||
|
mesh.vertices.push_back(vertices[0]);
|
||||||
|
for (size_t i = 1; i < 3; i++) {
|
||||||
|
mesh.vertices.push_back(vertices[i + j]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
public:
|
||||||
|
ObjParser(const std::string_view file, const std::string_view src) : BasicParser(file, src) {
|
||||||
|
}
|
||||||
|
|
||||||
|
std::unique_ptr<Model> parse() {
|
||||||
|
// first iteration - collecting vertex data
|
||||||
|
while (hasNext()) {
|
||||||
|
if (peek() == '#') {
|
||||||
|
skipLine();
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
auto cmd = parseName();
|
||||||
|
if (cmd == "v") {
|
||||||
|
float x = dynamic::as_number(parseNumber());
|
||||||
|
float y = dynamic::as_number(parseNumber());
|
||||||
|
float z = dynamic::as_number(parseNumber());
|
||||||
|
coords.emplace_back(x, y, z);
|
||||||
|
} else if (cmd == "vt") {
|
||||||
|
float u = dynamic::as_number(parseNumber());
|
||||||
|
float v = dynamic::as_number(parseNumber());
|
||||||
|
uvs.emplace_back(u, v);
|
||||||
|
} else if (cmd == "vn") {
|
||||||
|
float x = dynamic::as_number(parseNumber());
|
||||||
|
float y = dynamic::as_number(parseNumber());
|
||||||
|
float z = dynamic::as_number(parseNumber());
|
||||||
|
normals.emplace_back(x, y, z);
|
||||||
|
} else {
|
||||||
|
skipLine();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// second iteration - building meshes
|
||||||
|
reset();
|
||||||
|
|
||||||
|
auto model = std::make_unique<Model>();
|
||||||
|
std::string texture;
|
||||||
|
while (hasNext()) {
|
||||||
|
if (peek() != '#' && parseName() == "usemtl") {
|
||||||
|
skipWhitespace();
|
||||||
|
texture = readUntil('\n');
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
skipLine();
|
||||||
|
}
|
||||||
|
do {
|
||||||
|
Mesh* mesh = &model->addMesh(texture);
|
||||||
|
while (hasNext()) {
|
||||||
|
if (peek() == '#') {
|
||||||
|
skipLine();
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
auto cmd = parseName();
|
||||||
|
if (cmd == "usemtl") {
|
||||||
|
skipWhitespace();
|
||||||
|
texture = readUntil('\n');
|
||||||
|
mesh = &model->addMesh(texture);
|
||||||
|
break;
|
||||||
|
} else if (cmd == "f") {
|
||||||
|
parseFace(*mesh);
|
||||||
|
}
|
||||||
|
skipLine();
|
||||||
|
}
|
||||||
|
} while(hasNext());
|
||||||
|
model->clean();
|
||||||
|
return model;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
std::unique_ptr<Model> obj::parse(
|
||||||
|
const std::string_view file, const std::string_view src
|
||||||
|
) {
|
||||||
|
return ObjParser(file, src).parse();
|
||||||
|
}
|
||||||
@@ -0,0 +1,19 @@
|
|||||||
|
#ifndef CODERS_OBJ_HPP_
|
||||||
|
#define CODERS_OBJ_HPP_
|
||||||
|
|
||||||
|
#include <string>
|
||||||
|
#include <memory>
|
||||||
|
|
||||||
|
/// Wavefont OBJ files parser
|
||||||
|
|
||||||
|
namespace model {
|
||||||
|
struct Model;
|
||||||
|
}
|
||||||
|
|
||||||
|
namespace obj {
|
||||||
|
std::unique_ptr<model::Model> parse(
|
||||||
|
const std::string_view file, const std::string_view src
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif // CODERS_OBJ_HPP_
|
||||||
@@ -3,6 +3,7 @@
|
|||||||
|
|
||||||
#include "../typedefs.hpp"
|
#include "../typedefs.hpp"
|
||||||
|
|
||||||
|
#include <cmath>
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
#include <memory>
|
#include <memory>
|
||||||
@@ -47,6 +48,15 @@ namespace dynamic {
|
|||||||
std::holds_alternative<integer_t>(value);
|
std::holds_alternative<integer_t>(value);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
inline number_t as_number(const Value& value) {
|
||||||
|
if (auto num = std::get_if<number_t>(&value)) {
|
||||||
|
return *num;
|
||||||
|
} else if (auto num = std::get_if<integer_t>(&value)) {
|
||||||
|
return *num;
|
||||||
|
}
|
||||||
|
return NAN;
|
||||||
|
}
|
||||||
|
|
||||||
class List {
|
class List {
|
||||||
public:
|
public:
|
||||||
std::vector<Value> values;
|
std::vector<Value> values;
|
||||||
|
|||||||
@@ -1,5 +1,7 @@
|
|||||||
#include "Model.hpp"
|
#include "Model.hpp"
|
||||||
|
|
||||||
|
#include <algorithm>
|
||||||
|
|
||||||
using namespace model;
|
using namespace model;
|
||||||
|
|
||||||
inline constexpr glm::vec3 X(1, 0, 0);
|
inline constexpr glm::vec3 X(1, 0, 0);
|
||||||
@@ -26,3 +28,14 @@ void Mesh::addBox(glm::vec3 pos, glm::vec3 size) {
|
|||||||
addPlane(pos+X*size, -Z*size, Y*size, X);
|
addPlane(pos+X*size, -Z*size, Y*size, X);
|
||||||
addPlane(pos-X*size, Z*size, Y*size, -X);
|
addPlane(pos-X*size, Z*size, Y*size, -X);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void Model::clean() {
|
||||||
|
meshes.erase(
|
||||||
|
std::remove_if(meshes.begin(), meshes.end(),
|
||||||
|
[](const Mesh& mesh){
|
||||||
|
return mesh.vertices.empty();
|
||||||
|
}),
|
||||||
|
meshes.end()
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|||||||
@@ -23,10 +23,15 @@ namespace model {
|
|||||||
struct Model {
|
struct Model {
|
||||||
std::vector<Mesh> meshes;
|
std::vector<Mesh> meshes;
|
||||||
|
|
||||||
|
/// @brief Add mesh to the model
|
||||||
|
/// @param texture texture name
|
||||||
|
/// @return writeable Mesh
|
||||||
Mesh& addMesh(const std::string& texture) {
|
Mesh& addMesh(const std::string& texture) {
|
||||||
meshes.push_back({texture, {}});
|
meshes.push_back({texture, {}});
|
||||||
return meshes[meshes.size()-1];
|
return meshes[meshes.size()-1];
|
||||||
}
|
}
|
||||||
|
/// @brief Remove all empty meshes
|
||||||
|
void clean();
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -66,13 +66,19 @@ void ModelBatch::draw(const model::Model& model) {
|
|||||||
} else {
|
} else {
|
||||||
blank->bind();
|
blank->bind();
|
||||||
}
|
}
|
||||||
for (const auto& vert : mesh.vertices) {
|
for (size_t i = 0; i < mesh.vertices.size() / 3; i++) {
|
||||||
auto norm = rotation * vert.normal;
|
if (index + VERTEX_SIZE * 3 > capacity) {
|
||||||
float d = glm::dot(norm, SUN_VECTOR);
|
flush();
|
||||||
d = 0.8f + d * 0.2f;
|
}
|
||||||
|
for (size_t j = 0; j < 3; j++) {
|
||||||
auto color = lights * d;
|
const auto& vert = mesh.vertices[i * 3 + j];
|
||||||
vertex(vert.coord, vert.uv, color);
|
auto norm = rotation * vert.normal;
|
||||||
|
float d = glm::dot(norm, SUN_VECTOR);
|
||||||
|
d = 0.8f + d * 0.2f;
|
||||||
|
|
||||||
|
auto color = lights * d;
|
||||||
|
vertex(vert.coord, vert.uv, color);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
flush();
|
flush();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -7,10 +7,12 @@
|
|||||||
#include "../../assets/Assets.hpp"
|
#include "../../assets/Assets.hpp"
|
||||||
#include "../../content/Content.hpp"
|
#include "../../content/Content.hpp"
|
||||||
#include "../../engine.hpp"
|
#include "../../engine.hpp"
|
||||||
|
#include "../../coders/obj.hpp"
|
||||||
#include "../../frontend/LevelFrontend.hpp"
|
#include "../../frontend/LevelFrontend.hpp"
|
||||||
#include "../../items/Inventory.hpp"
|
#include "../../items/Inventory.hpp"
|
||||||
#include "../../items/ItemDef.hpp"
|
#include "../../items/ItemDef.hpp"
|
||||||
#include "../../items/ItemStack.hpp"
|
#include "../../items/ItemStack.hpp"
|
||||||
|
#include "../../files/files.hpp"
|
||||||
#include "../../logic/PlayerController.hpp"
|
#include "../../logic/PlayerController.hpp"
|
||||||
#include "../../maths/FrustumCulling.hpp"
|
#include "../../maths/FrustumCulling.hpp"
|
||||||
#include "../../maths/voxmaths.hpp"
|
#include "../../maths/voxmaths.hpp"
|
||||||
@@ -70,6 +72,10 @@ WorldRenderer::WorldRenderer(Engine* engine, LevelFrontend* frontend, Player* pl
|
|||||||
settings.graphics.skyboxResolution.get(),
|
settings.graphics.skyboxResolution.get(),
|
||||||
assets->getShader("skybox_gen")
|
assets->getShader("skybox_gen")
|
||||||
);
|
);
|
||||||
|
|
||||||
|
auto name = "dingus.obj";
|
||||||
|
auto text = files::read_string(fs::path(name));
|
||||||
|
model = obj::parse(name, text);
|
||||||
}
|
}
|
||||||
|
|
||||||
WorldRenderer::~WorldRenderer() {
|
WorldRenderer::~WorldRenderer() {
|
||||||
@@ -194,23 +200,13 @@ void WorldRenderer::renderLevel(
|
|||||||
|
|
||||||
drawChunks(level->chunks.get(), camera, shader);
|
drawChunks(level->chunks.get(), camera, shader);
|
||||||
|
|
||||||
model::Model model {};
|
|
||||||
auto& mesh = model.addMesh("gui/warning");
|
|
||||||
mesh.addBox({}, glm::vec3(0.3f));
|
|
||||||
mesh.addBox({}, glm::vec3(0.6f));
|
|
||||||
|
|
||||||
auto& mesh2 = model.addMesh("gui/error");
|
|
||||||
mesh2.addBox({}, glm::vec3(0.7f));
|
|
||||||
mesh2.addBox({}, glm::vec3(0.9f));
|
|
||||||
|
|
||||||
float timer = static_cast<float>(Window::time());
|
float timer = static_cast<float>(Window::time());
|
||||||
assets->getTexture("gui/menubg")->bind();
|
|
||||||
shader->uniformMatrix("u_model", glm::mat4(1.0f));
|
shader->uniformMatrix("u_model", glm::mat4(1.0f));
|
||||||
modelBatch->translate({0, 86, 0});
|
modelBatch->translate({0, 85, 0});
|
||||||
modelBatch->scale(glm::vec3(glm::sin(timer*6)+1));
|
//modelBatch->scale(glm::vec3(glm::sin(timer*6)+10));
|
||||||
modelBatch->rotate(glm::vec3(1, 0, 0), timer);
|
modelBatch->rotate(glm::vec3(0, 1, 0), timer*6);
|
||||||
modelBatch->draw(model);
|
modelBatch->draw(*model);
|
||||||
modelBatch->popMatrix();
|
//modelBatch->popMatrix();
|
||||||
modelBatch->popMatrix();
|
modelBatch->popMatrix();
|
||||||
modelBatch->popMatrix();
|
modelBatch->popMatrix();
|
||||||
|
|
||||||
|
|||||||
@@ -26,6 +26,10 @@ class DrawContext;
|
|||||||
class ModelBatch;
|
class ModelBatch;
|
||||||
struct EngineSettings;
|
struct EngineSettings;
|
||||||
|
|
||||||
|
namespace model {
|
||||||
|
struct Model;
|
||||||
|
}
|
||||||
|
|
||||||
class WorldRenderer {
|
class WorldRenderer {
|
||||||
Engine* engine;
|
Engine* engine;
|
||||||
Level* level;
|
Level* level;
|
||||||
@@ -36,6 +40,8 @@ class WorldRenderer {
|
|||||||
std::unique_ptr<Skybox> skybox;
|
std::unique_ptr<Skybox> skybox;
|
||||||
std::unique_ptr<Batch3D> batch3d;
|
std::unique_ptr<Batch3D> batch3d;
|
||||||
std::unique_ptr<ModelBatch> modelBatch;
|
std::unique_ptr<ModelBatch> modelBatch;
|
||||||
|
|
||||||
|
std::unique_ptr<model::Model> model;
|
||||||
bool drawChunk(size_t index, Camera* camera, Shader* shader, bool culling);
|
bool drawChunk(size_t index, Camera* camera, Shader* shader, bool culling);
|
||||||
void drawChunks(Chunks* chunks, Camera* camera, Shader* shader);
|
void drawChunks(Chunks* chunks, Camera* camera, Shader* shader);
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user