Merge pull request #527 from MihailRis/vcm-format
new simple text 3d models format
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
#include "assetload_funcs.hpp"
|
||||
|
||||
#include <array>
|
||||
#include <filesystem>
|
||||
#include <iostream>
|
||||
#include <stdexcept>
|
||||
@@ -10,6 +11,7 @@
|
||||
#include "coders/imageio.hpp"
|
||||
#include "coders/json.hpp"
|
||||
#include "coders/obj.hpp"
|
||||
#include "coders/vcm.hpp"
|
||||
#include "coders/vec3.hpp"
|
||||
#include "constants.hpp"
|
||||
#include "debug/Logger.hpp"
|
||||
@@ -300,7 +302,8 @@ assetload::postfunc assetload::sound(
|
||||
|
||||
static void request_textures(AssetsLoader* loader, const model::Model& model) {
|
||||
for (auto& mesh : model.meshes) {
|
||||
if (mesh.texture.find('$') == std::string::npos) {
|
||||
if (mesh.texture.find('$') == std::string::npos &&
|
||||
mesh.texture.find(':') == std::string::npos) {
|
||||
auto filename = TEXTURES_FOLDER + "/" + mesh.texture;
|
||||
loader->add(
|
||||
AssetType::TEXTURE, filename, mesh.texture, nullptr
|
||||
@@ -337,9 +340,40 @@ assetload::postfunc assetload::model(
|
||||
};
|
||||
}
|
||||
path = paths.find(file + ".obj");
|
||||
if (io::exists(path)) {
|
||||
auto text = io::read_string(path);
|
||||
try {
|
||||
auto model = obj::parse(path.string(), text).release();
|
||||
return [=](Assets* assets) {
|
||||
request_textures(loader, *model);
|
||||
assets->store(std::unique_ptr<model::Model>(model), name);
|
||||
};
|
||||
} catch (const parsing_error& err) {
|
||||
std::cerr << err.errorLog() << std::endl;
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
std::array<std::string, 2> extensions {
|
||||
".xml",
|
||||
".vcm"
|
||||
};
|
||||
|
||||
path = "";
|
||||
for (const auto& ext : extensions) {
|
||||
auto newPath = paths.find(file + ext);
|
||||
if (io::exists(newPath)) {
|
||||
path = std::move(newPath);
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (path.empty()) {
|
||||
throw std::runtime_error("could not to find model " + util::quote(file));
|
||||
}
|
||||
|
||||
auto text = io::read_string(path);
|
||||
try {
|
||||
auto model = obj::parse(path.string(), text).release();
|
||||
auto model = vcm::parse(path.string(), text).release();
|
||||
return [=](Assets* assets) {
|
||||
request_textures(loader, *model);
|
||||
assets->store(std::unique_ptr<model::Model>(model), name);
|
||||
|
||||
@@ -9,7 +9,11 @@ util::TextureRegion util::get_texture_region(
|
||||
) {
|
||||
size_t sep = name.find(':');
|
||||
if (sep == std::string::npos) {
|
||||
return {assets.get<Texture>(name), UVRegion(0,0,1,1)};
|
||||
auto texture = assets.get<Texture>(name);
|
||||
if (texture == nullptr && !fallback.empty()) {
|
||||
return util::get_texture_region(assets, fallback, "");
|
||||
}
|
||||
return {texture, UVRegion(0,0,1,1)};
|
||||
} else {
|
||||
auto atlas = assets.get<Atlas>(name.substr(0, sep));
|
||||
if (atlas) {
|
||||
|
||||
@@ -0,0 +1,161 @@
|
||||
#include "vcm.hpp"
|
||||
|
||||
#include <iostream>
|
||||
|
||||
#include "xml.hpp"
|
||||
#include "util/stringutil.hpp"
|
||||
#include "graphics/commons/Model.hpp"
|
||||
#include "io/io.hpp"
|
||||
|
||||
using namespace vcm;
|
||||
using namespace xml;
|
||||
|
||||
static const std::unordered_map<std::string, int> side_indices {
|
||||
{"north", 0},
|
||||
{"south", 1},
|
||||
{"top", 2},
|
||||
{"bottom", 3},
|
||||
{"west", 4},
|
||||
{"east", 5},
|
||||
};
|
||||
|
||||
static void perform_rect(const xmlelement& root, model::Model& model) {
|
||||
auto from = root.attr("from").asVec3();
|
||||
auto right = root.attr("right").asVec3();
|
||||
auto up = root.attr("up").asVec3();
|
||||
|
||||
right *= -1;
|
||||
from -= right;
|
||||
|
||||
UVRegion region {};
|
||||
if (root.has("region")) {
|
||||
region.set(root.attr("region").asVec4());
|
||||
} else {
|
||||
region.scale(glm::length(right), glm::length(up));
|
||||
}
|
||||
if (root.has("region-scale")) {
|
||||
region.scale(root.attr("region-scale").asVec2());
|
||||
}
|
||||
|
||||
auto flip = root.attr("flip", "").getText();
|
||||
if (flip == "h") {
|
||||
std::swap(region.u1, region.u2);
|
||||
right *= -1;
|
||||
from -= right;
|
||||
} else if (flip == "v") {
|
||||
std::swap(region.v1, region.v2);
|
||||
up *= -1;
|
||||
from -= up;
|
||||
}
|
||||
std::string texture = root.attr("texture", "$0").getText();
|
||||
auto& mesh = model.addMesh(texture);
|
||||
|
||||
auto normal = glm::cross(glm::normalize(right), glm::normalize(up));
|
||||
mesh.addRect(
|
||||
from + right * 0.5f + up * 0.5f,
|
||||
right * 0.5f,
|
||||
up * 0.5f,
|
||||
normal,
|
||||
region
|
||||
);
|
||||
}
|
||||
|
||||
static void perform_box(const xmlelement& root, model::Model& model) {
|
||||
auto from = root.attr("from").asVec3();
|
||||
auto to = root.attr("to").asVec3();
|
||||
|
||||
UVRegion regions[6] {};
|
||||
regions[0].scale(to.x - from.x, to.y - from.y);
|
||||
regions[1].scale(from.x - to.x, to.y - from.y);
|
||||
regions[2].scale(to.x - from.x, to.z - from.z);
|
||||
regions[3].scale(from.x - to.x, to.z - from.z);
|
||||
regions[4].scale(to.z - from.z, to.y - from.y);
|
||||
regions[5].scale(from.z - to.z, to.y - from.y);
|
||||
|
||||
auto center = (from + to) * 0.5f;
|
||||
auto halfsize = (to - from) * 0.5f;
|
||||
|
||||
std::string texfaces[6] {"$0","$1","$2","$3","$4","$5"};
|
||||
|
||||
for (const auto& elem : root.getElements()) {
|
||||
if (elem->getTag() == "part") {
|
||||
// todo: replace by expression parsing
|
||||
auto tags = util::split(elem->attr("tags").getText(), ',');
|
||||
for (auto& tag : tags) {
|
||||
util::trim(tag);
|
||||
const auto& found = side_indices.find(tag);
|
||||
if (found == side_indices.end()) {
|
||||
continue;
|
||||
}
|
||||
int idx = found->second;
|
||||
if (elem->has("texture")) {
|
||||
texfaces[idx] = elem->attr("texture").getText();
|
||||
}
|
||||
if (elem->has("region")) {
|
||||
regions[idx].set(elem->attr("region").asVec4());
|
||||
}
|
||||
if (elem->has("region-scale")) {
|
||||
regions[idx].scale(elem->attr("region-scale").asVec2());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
bool deleted[6] {};
|
||||
if (root.has("delete")) {
|
||||
// todo: replace by expression parsing
|
||||
auto names = util::split(root.attr("delete").getText(), ',');
|
||||
for (auto& name : names) {
|
||||
util::trim(name);
|
||||
const auto& found = side_indices.find(name);
|
||||
if (found != side_indices.end()) {
|
||||
deleted[found->second] = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for (int i = 0; i < 6; i++) {
|
||||
if (deleted[i]) {
|
||||
continue;
|
||||
}
|
||||
bool enabled[6] {};
|
||||
enabled[i] = true;
|
||||
auto& mesh = model.addMesh(texfaces[i]);
|
||||
mesh.addBox(center, halfsize, regions, enabled);
|
||||
}
|
||||
}
|
||||
|
||||
static std::unique_ptr<model::Model> load_model(const xmlelement& root) {
|
||||
model::Model model;
|
||||
|
||||
for (const auto& elem : root.getElements()) {
|
||||
auto tag = elem->getTag();
|
||||
|
||||
if (tag == "rect") {
|
||||
perform_rect(*elem, model);
|
||||
} else if (tag == "box") {
|
||||
perform_box(*elem, model);
|
||||
}
|
||||
}
|
||||
|
||||
return std::make_unique<model::Model>(std::move(model));
|
||||
}
|
||||
|
||||
std::unique_ptr<model::Model> vcm::parse(
|
||||
std::string_view file, std::string_view src
|
||||
) {
|
||||
try {
|
||||
auto doc = io::path(std::string(file)).extension() == ".xml"
|
||||
? xml::parse(file, src) : xml::parse_vcm(file, src, "model");
|
||||
const auto& root = *doc->getRoot();
|
||||
if (root.getTag() != "model") {
|
||||
throw std::runtime_error(
|
||||
"'model' tag expected as root, got '" + root.getTag() + "'"
|
||||
);
|
||||
}
|
||||
std::cout << xml::stringify(*doc) << std::endl;
|
||||
return load_model(root);
|
||||
} catch (const parsing_error& err) {
|
||||
throw std::runtime_error(err.errorLog());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
#pragma once
|
||||
|
||||
#include <memory>
|
||||
#include <string>
|
||||
|
||||
namespace model {
|
||||
struct Model;
|
||||
}
|
||||
|
||||
namespace vcm {
|
||||
std::unique_ptr<model::Model> parse(std::string_view file, std::string_view src);
|
||||
}
|
||||
@@ -355,6 +355,83 @@ std::unique_ptr<Document> xml::parse(
|
||||
return parser.parse();
|
||||
}
|
||||
|
||||
namespace {
|
||||
class VcmParser : public BasicParser<char> {
|
||||
public:
|
||||
VcmParser(std::string_view filename, std::string_view source)
|
||||
: BasicParser(filename, source) {
|
||||
document = std::make_unique<Document>("1.0", "UTF-8");
|
||||
hashComment = true;
|
||||
}
|
||||
|
||||
std::string parseValue() {
|
||||
char c = peek();
|
||||
if (c == '"' || c == '\'') {
|
||||
nextChar();
|
||||
return parseString(c);
|
||||
}
|
||||
if (c == '(') {
|
||||
nextChar();
|
||||
// TODO: replace with array parsing after moving to dv::value's
|
||||
std::string value = std::string(readUntil(')'));
|
||||
expect(')');
|
||||
return value;
|
||||
}
|
||||
return std::string(readUntilWhitespace());
|
||||
}
|
||||
|
||||
void parseSubElements(Node& node) {
|
||||
skipWhitespace();
|
||||
while (hasNext()) {
|
||||
if (peek() != '@') {
|
||||
throw error("unexpected character in element");
|
||||
}
|
||||
nextChar();
|
||||
auto subnodePtr = std::make_unique<Node>(parseName());
|
||||
auto subnode = subnodePtr.get();
|
||||
node.add(std::move(subnodePtr));
|
||||
|
||||
skipWhitespace();
|
||||
while (hasNext() && peek() != '@' && peek() != '{' && peek() != '}') {
|
||||
std::string attrname = parseName();
|
||||
skipWhitespace();
|
||||
std::string value = parseValue();
|
||||
subnode->set(attrname, value);
|
||||
skipWhitespace();
|
||||
}
|
||||
if (!hasNext()) {
|
||||
break;
|
||||
}
|
||||
char c = peek();
|
||||
if (c == '{') {
|
||||
nextChar();
|
||||
parseSubElements(*subnode);
|
||||
expect('}');
|
||||
skipWhitespace();
|
||||
} else if (c == '}') {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
std::unique_ptr<Document> parse(const std::string& rootTag) {
|
||||
auto root = std::make_unique<Node>(rootTag);
|
||||
parseSubElements(*root);
|
||||
document->setRoot(std::move(root));
|
||||
return std::move(document);
|
||||
}
|
||||
private:
|
||||
std::unique_ptr<Document> document;
|
||||
};
|
||||
}
|
||||
|
||||
std::unique_ptr<Document> xml::parse_vcm(
|
||||
std::string_view filename, std::string_view source, std::string_view tag
|
||||
) {
|
||||
VcmParser parser(filename, source);
|
||||
return parser.parse(std::string(tag));
|
||||
}
|
||||
|
||||
inline void newline(
|
||||
std::stringstream& ss, bool nice, const std::string& indentStr, int indent
|
||||
) {
|
||||
|
||||
@@ -126,5 +126,9 @@ namespace xml {
|
||||
std::string_view filename, std::string_view source
|
||||
);
|
||||
|
||||
std::unique_ptr<Document> parse_vcm(
|
||||
std::string_view filename, std::string_view source, std::string_view tag
|
||||
);
|
||||
|
||||
using xmlelement = Node;
|
||||
}
|
||||
|
||||
@@ -37,17 +37,15 @@ void ContentGfxCache::refresh(const Block& def, const Atlas& atlas) {
|
||||
}
|
||||
if (def.model.type == BlockModelType::CUSTOM) {
|
||||
auto model = assets.require<model::Model>(def.model.name);
|
||||
// temporary dirty fix tbh
|
||||
if (def.model.name.find(':') == std::string::npos) {
|
||||
for (auto& mesh : model.meshes) {
|
||||
size_t pos = mesh.texture.find(':');
|
||||
if (pos == std::string::npos) {
|
||||
continue;
|
||||
}
|
||||
if (auto region = atlas.getIf(mesh.texture.substr(pos+1))) {
|
||||
for (auto& vertex : mesh.vertices) {
|
||||
vertex.uv = region->apply(vertex.uv);
|
||||
}
|
||||
|
||||
for (auto& mesh : model.meshes) {
|
||||
size_t pos = mesh.texture.find(':');
|
||||
if (pos == std::string::npos) {
|
||||
continue;
|
||||
}
|
||||
if (auto region = atlas.getIf(mesh.texture.substr(pos+1))) {
|
||||
for (auto& vertex : mesh.vertices) {
|
||||
vertex.uv = region->apply(vertex.uv);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -39,6 +39,22 @@ void Mesh::addPlane(
|
||||
vertices.push_back({pos-right+up, {uv.u1, uv.v2}, norm});
|
||||
}
|
||||
|
||||
void Mesh::addRect(
|
||||
const glm::vec3& pos,
|
||||
const glm::vec3& right,
|
||||
const glm::vec3& up,
|
||||
const glm::vec3& norm,
|
||||
const UVRegion& uv
|
||||
) {
|
||||
vertices.push_back({pos-right-up, {uv.u1, uv.v1}, norm});
|
||||
vertices.push_back({pos+right-up, {uv.u2, uv.v1}, norm});
|
||||
vertices.push_back({pos+right+up, {uv.u2, uv.v2}, norm});
|
||||
|
||||
vertices.push_back({pos-right-up, {uv.u1, uv.v1}, norm});
|
||||
vertices.push_back({pos+right+up, {uv.u2, uv.v2}, norm});
|
||||
vertices.push_back({pos-right+up, {uv.u1, uv.v2}, norm});
|
||||
}
|
||||
|
||||
void Mesh::addBox(const glm::vec3& pos, const glm::vec3& size) {
|
||||
addPlane(pos+Z*size, X*size, Y*size, Z);
|
||||
addPlane(pos-Z*size, -X*size, Y*size, -Z);
|
||||
@@ -51,16 +67,23 @@ void Mesh::addBox(const glm::vec3& pos, const glm::vec3& size) {
|
||||
}
|
||||
|
||||
void Mesh::addBox(
|
||||
const glm::vec3& pos, const glm::vec3& size, const UVRegion (&uvs)[6]
|
||||
const glm::vec3& pos,
|
||||
const glm::vec3& size,
|
||||
const UVRegion (&uvs)[6],
|
||||
const bool enabledSides[6]
|
||||
) {
|
||||
addPlane(pos+Z*size, X*size, Y*size, Z, uvs[0]);
|
||||
addPlane(pos-Z*size, -X*size, Y*size, -Z, uvs[1]);
|
||||
|
||||
addPlane(pos+Y*size, X*size, -Z*size, Y, uvs[2]);
|
||||
addPlane(pos-Y*size, X*size, Z*size, -Y, uvs[3]);
|
||||
|
||||
addPlane(pos+X*size, -Z*size, Y*size, X, uvs[4]);
|
||||
addPlane(pos-X*size, Z*size, Y*size, -X, uvs[5]);
|
||||
if (enabledSides[0]) // north
|
||||
addPlane(pos+Z*size, X*size, Y*size, Z, uvs[0]);
|
||||
if (enabledSides[1]) // south
|
||||
addPlane(pos-Z*size, -X*size, Y*size, -Z, uvs[1]);
|
||||
if (enabledSides[2]) // top
|
||||
addPlane(pos+Y*size, X*size, -Z*size, Y, uvs[2] * glm::vec2(-1));
|
||||
if (enabledSides[3]) // bottom
|
||||
addPlane(pos-Y*size, X*size, Z*size, -Y, uvs[3] * glm::vec2(-1, 1));
|
||||
if (enabledSides[4]) // west
|
||||
addPlane(pos+X*size, -Z*size, Y*size, X, uvs[4]);
|
||||
if (enabledSides[5]) // east
|
||||
addPlane(pos-X*size, Z*size, Y*size, -X, uvs[5] * glm::vec2(-1, 1));
|
||||
}
|
||||
|
||||
void Mesh::scale(const glm::vec3& size) {
|
||||
|
||||
@@ -31,11 +31,19 @@ namespace model {
|
||||
const glm::vec3& norm,
|
||||
const UVRegion& region
|
||||
);
|
||||
void addRect(
|
||||
const glm::vec3& pos,
|
||||
const glm::vec3& right,
|
||||
const glm::vec3& up,
|
||||
const glm::vec3& norm,
|
||||
const UVRegion& region
|
||||
);
|
||||
void addBox(const glm::vec3& pos, const glm::vec3& size);
|
||||
void addBox(
|
||||
const glm::vec3& pos,
|
||||
const glm::vec3& size,
|
||||
const UVRegion (&texfaces)[6]
|
||||
const UVRegion (&texfaces)[6],
|
||||
const bool enabledSides[6]
|
||||
);
|
||||
void scale(const glm::vec3& size);
|
||||
};
|
||||
@@ -47,6 +55,11 @@ namespace model {
|
||||
/// @param texture texture name
|
||||
/// @return writeable Mesh
|
||||
Mesh& addMesh(const std::string& texture) {
|
||||
for (auto& mesh : meshes) {
|
||||
if (mesh.texture == texture) {
|
||||
return mesh;
|
||||
}
|
||||
}
|
||||
meshes.push_back({texture, {}});
|
||||
return meshes[meshes.size()-1];
|
||||
}
|
||||
|
||||
@@ -6,6 +6,11 @@
|
||||
#include "typedefs.hpp"
|
||||
#include "maths/UVRegion.hpp"
|
||||
|
||||
namespace {
|
||||
const glm::vec3 SUN_VECTOR(0.528265f, 0.833149f, -0.163704f);
|
||||
const float DIRECTIONAL_LIGHT_FACTOR = 0.3f;
|
||||
}
|
||||
|
||||
Batch3D::Batch3D(size_t capacity)
|
||||
: capacity(capacity) {
|
||||
|
||||
@@ -31,20 +36,28 @@ void Batch3D::begin(){
|
||||
}
|
||||
|
||||
void Batch3D::vertex(
|
||||
float x, float y, float z, float u, float v,
|
||||
float r, float g, float b, float a
|
||||
float x,
|
||||
float y,
|
||||
float z,
|
||||
float u,
|
||||
float v,
|
||||
float r,
|
||||
float g,
|
||||
float b,
|
||||
float a
|
||||
) {
|
||||
buffer[index].position = {x, y, z};
|
||||
buffer[index].uv = {u, v};
|
||||
buffer[index].uv = {
|
||||
u * region.getWidth() + region.u1, v * region.getHeight() + region.v1};
|
||||
buffer[index].color = {r, g, b, a};
|
||||
index++;
|
||||
}
|
||||
void Batch3D::vertex(
|
||||
glm::vec3 coord, float u, float v,
|
||||
float r, float g, float b, float a
|
||||
glm::vec3 coord, float u, float v, float r, float g, float b, float a
|
||||
) {
|
||||
buffer[index].position = coord;
|
||||
buffer[index].uv = {u, v};
|
||||
buffer[index].uv = {
|
||||
u * region.getWidth() + region.u1, v * region.getHeight() + region.v1};
|
||||
buffer[index].color = {r, g, b, a};
|
||||
index++;
|
||||
}
|
||||
@@ -54,7 +67,9 @@ void Batch3D::vertex(
|
||||
float r, float g, float b, float a
|
||||
) {
|
||||
buffer[index].position = point;
|
||||
buffer[index].uv = uvpoint;
|
||||
buffer[index].uv = {
|
||||
uvpoint.x * region.getWidth() + region.u1,
|
||||
uvpoint.y * region.getHeight() + region.v1};
|
||||
buffer[index].color = {r, g, b, a};
|
||||
index++;
|
||||
}
|
||||
@@ -86,14 +101,18 @@ void Batch3D::face(
|
||||
}
|
||||
|
||||
void Batch3D::texture(const Texture* new_texture){
|
||||
if (currentTexture == new_texture)
|
||||
if (currentTexture == new_texture) {
|
||||
return;
|
||||
}
|
||||
flush();
|
||||
currentTexture = new_texture;
|
||||
if (new_texture == nullptr)
|
||||
if (new_texture == nullptr) {
|
||||
blank->bind();
|
||||
else
|
||||
region = blank->getUVRegion();
|
||||
} else {
|
||||
new_texture->bind();
|
||||
region = currentTexture->getUVRegion();
|
||||
}
|
||||
}
|
||||
|
||||
void Batch3D::sprite(
|
||||
@@ -240,6 +259,16 @@ void Batch3D::blockCube(
|
||||
cube((1.0f - size) * -0.5f, size, texfaces, tint, shading);
|
||||
}
|
||||
|
||||
void Batch3D::setRegion(UVRegion region) {
|
||||
this->region = region;
|
||||
}
|
||||
|
||||
void Batch3D::vertex(const glm::vec3& pos, const glm::vec2& uv, const glm::vec3& norm) {
|
||||
float d = glm::dot(glm::normalize(norm), SUN_VECTOR);
|
||||
d = (1.0f - DIRECTIONAL_LIGHT_FACTOR) + d * DIRECTIONAL_LIGHT_FACTOR;
|
||||
vertex(pos, uv, glm::vec4(d, d, d, 1.0f));
|
||||
}
|
||||
|
||||
void Batch3D::vertex(
|
||||
const glm::vec3& coord, const glm::vec2& uv, const glm::vec4& tint
|
||||
) {
|
||||
|
||||
@@ -3,6 +3,7 @@
|
||||
#include "typedefs.hpp"
|
||||
#include "commons.hpp"
|
||||
#include "MeshData.hpp"
|
||||
#include "maths/UVRegion.hpp"
|
||||
|
||||
#include <memory>
|
||||
#include <cstdlib>
|
||||
@@ -32,8 +33,8 @@ class Batch3D : public Flushable {
|
||||
std::unique_ptr<Texture> blank;
|
||||
size_t index;
|
||||
glm::vec4 tint {1.0f};
|
||||
|
||||
const Texture* currentTexture;
|
||||
UVRegion region {0.0f, 0.0f, 1.0f, 1.0f};
|
||||
|
||||
void vertex(
|
||||
float x, float y, float z,
|
||||
@@ -102,6 +103,8 @@ public:
|
||||
const glm::vec4& tint,
|
||||
bool shading = true
|
||||
);
|
||||
void setRegion(UVRegion region);
|
||||
void vertex(const glm::vec3& pos, const glm::vec2& uv, const glm::vec3& norm);
|
||||
void vertex(const glm::vec3& pos, const glm::vec2& uv, const glm::vec4& tint);
|
||||
void point(const glm::vec3& pos, const glm::vec4& tint);
|
||||
void flush() override;
|
||||
|
||||
@@ -34,7 +34,7 @@ public:
|
||||
Batch2D* getBatch2D() const;
|
||||
|
||||
const glm::uvec2& getViewport() const;
|
||||
DrawContext sub(Flushable* flushable=nullptr) const;
|
||||
[[nodiscard]] DrawContext sub(Flushable* flushable=nullptr) const;
|
||||
|
||||
void setViewport(const glm::uvec2& viewport);
|
||||
void setFramebuffer(Framebuffer* fbo);
|
||||
|
||||
@@ -16,6 +16,7 @@
|
||||
namespace fs = std::filesystem;
|
||||
|
||||
GLSLExtension* Shader::preprocessor = new GLSLExtension();
|
||||
Shader* Shader::used = nullptr;
|
||||
|
||||
Shader::Shader(uint id) : id(id){
|
||||
}
|
||||
@@ -25,6 +26,7 @@ Shader::~Shader(){
|
||||
}
|
||||
|
||||
void Shader::use(){
|
||||
used = this;
|
||||
glUseProgram(id);
|
||||
}
|
||||
|
||||
@@ -131,3 +133,7 @@ std::unique_ptr<Shader> Shader::create(
|
||||
}
|
||||
return std::make_unique<Shader>(id);
|
||||
}
|
||||
|
||||
Shader& Shader::getUsed() {
|
||||
return *used;
|
||||
}
|
||||
|
||||
@@ -10,6 +10,7 @@
|
||||
class GLSLExtension;
|
||||
|
||||
class Shader {
|
||||
static Shader* used;
|
||||
uint id;
|
||||
std::unordered_map<std::string, uint> uniformLocations;
|
||||
|
||||
@@ -43,4 +44,6 @@ public:
|
||||
const std::string& vertexSource,
|
||||
const std::string& fragmentSource
|
||||
);
|
||||
|
||||
static Shader& getUsed();
|
||||
};
|
||||
|
||||
@@ -50,16 +50,31 @@ static inline UVRegion get_region_for(
|
||||
|
||||
void ModelsGenerator::prepare(Content& content, Assets& assets) {
|
||||
for (auto& [name, def] : content.blocks.getDefs()) {
|
||||
if (def->model.type == BlockModelType::CUSTOM && def->model.name.empty()) {
|
||||
assets.store(
|
||||
std::make_unique<model::Model>(
|
||||
loadCustomBlockModel(
|
||||
def->model.customRaw, assets, !def->shadeless
|
||||
)
|
||||
),
|
||||
name + ".model"
|
||||
);
|
||||
def->model.name = def->name + ".model";
|
||||
if (def->model.type == BlockModelType::CUSTOM) {
|
||||
if (def->model.name.empty()) {
|
||||
assets.store(
|
||||
std::make_unique<model::Model>(
|
||||
loadCustomBlockModel(
|
||||
def->model.customRaw, assets, !def->shadeless
|
||||
)
|
||||
),
|
||||
name + ".model"
|
||||
);
|
||||
def->model.name = def->name + ".model";
|
||||
} else {
|
||||
auto srcModel = assets.get<model::Model>(def->model.name);
|
||||
if (srcModel) {
|
||||
auto model = std::make_unique<model::Model>(*srcModel);
|
||||
for (auto& mesh : model->meshes) {
|
||||
if (mesh.texture.length() && mesh.texture[0] == '$') {
|
||||
int index = std::stoll(mesh.texture.substr(1));
|
||||
mesh.texture = "blocks:" + def->textureFaces[index];
|
||||
}
|
||||
}
|
||||
def->model.name = name + ".model";
|
||||
assets.store(std::move(model), def->model.name);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
for (auto& [name, def] : content.items.getDefs()) {
|
||||
@@ -83,7 +98,7 @@ model::Model ModelsGenerator::fromCustom(
|
||||
for (size_t i = 0; i < modelBoxes.size(); i++) {
|
||||
auto& mesh = model.addMesh("blocks:");
|
||||
mesh.lighting = lighting;
|
||||
const UVRegion boxtexfaces[6] = {
|
||||
UVRegion boxtexfaces[6] = {
|
||||
get_region_for(modelTextures[i * 6 + 5], assets),
|
||||
get_region_for(modelTextures[i * 6 + 4], assets),
|
||||
get_region_for(modelTextures[i * 6 + 3], assets),
|
||||
@@ -91,8 +106,15 @@ model::Model ModelsGenerator::fromCustom(
|
||||
get_region_for(modelTextures[i * 6 + 1], assets),
|
||||
get_region_for(modelTextures[i * 6 + 0], assets)
|
||||
};
|
||||
boxtexfaces[2].scale(glm::vec2(-1));
|
||||
boxtexfaces[5].scale(glm::vec2(-1, 1));
|
||||
|
||||
bool enabled[6] {1,1,1,1,1,1};
|
||||
mesh.addBox(
|
||||
modelBoxes[i].center(), modelBoxes[i].size() * 0.5f, boxtexfaces
|
||||
modelBoxes[i].center(),
|
||||
modelBoxes[i].size() * 0.5f,
|
||||
boxtexfaces,
|
||||
enabled
|
||||
);
|
||||
}
|
||||
for (size_t i = 0; i < points.size() / 4; i++) {
|
||||
|
||||
@@ -36,6 +36,7 @@ void InlineFrame::setDocument(const std::shared_ptr<UiDocument>& document) {
|
||||
}
|
||||
|
||||
void InlineFrame::act(float delta) {
|
||||
Container::act(delta);
|
||||
if (document || src.empty()) {
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,152 @@
|
||||
#include <array>
|
||||
#include <glm/ext.hpp>
|
||||
#include "ModelViewer.hpp"
|
||||
|
||||
#include "assets/Assets.hpp"
|
||||
#include "assets/assets_util.hpp"
|
||||
#include "graphics/commons/Model.hpp"
|
||||
#include "graphics/core/Batch2D.hpp"
|
||||
#include "graphics/core/Batch3D.hpp"
|
||||
#include "graphics/core/Shader.hpp"
|
||||
#include "graphics/core/Framebuffer.hpp"
|
||||
#include "graphics/core/DrawContext.hpp"
|
||||
#include "window/Window.hpp"
|
||||
#include "../GUI.hpp"
|
||||
|
||||
// TODO: remove
|
||||
#include <GL/glew.h>
|
||||
|
||||
using namespace gui;
|
||||
|
||||
ModelViewer::ModelViewer(
|
||||
GUI& gui, const glm::vec2& size, const std::string& modelName
|
||||
)
|
||||
: Container(gui, size),
|
||||
modelName(modelName),
|
||||
camera(),
|
||||
batch(std::make_unique<Batch3D>(1024)),
|
||||
fbo(std::make_unique<Framebuffer>(size.x, size.y)) {
|
||||
camera.perspective = true;
|
||||
camera.position = glm::vec3(2, 2, 2);
|
||||
}
|
||||
|
||||
ModelViewer::~ModelViewer() = default;
|
||||
|
||||
void ModelViewer::setModel(const std::string& modelName) {
|
||||
this->modelName = modelName;
|
||||
}
|
||||
|
||||
const std::string& ModelViewer::getModel() const {
|
||||
return modelName;
|
||||
}
|
||||
|
||||
Camera& ModelViewer::getCamera() {
|
||||
return camera;
|
||||
}
|
||||
|
||||
const Camera& ModelViewer::getCamera() const {
|
||||
return camera;
|
||||
}
|
||||
|
||||
void ModelViewer::act(float delta) {
|
||||
Container::act(delta);
|
||||
|
||||
auto& input = gui.getInput();
|
||||
|
||||
if (!grabbing && hover && input.jclicked(Mousecode::BUTTON_3)) {
|
||||
grabbing = true;
|
||||
}
|
||||
if (grabbing && input.clicked(Mousecode::BUTTON_3)) {
|
||||
auto cursor = input.getCursor();
|
||||
if (input.pressed(Keycode::LEFT_SHIFT)) {
|
||||
center -= camera.right * (cursor.delta.x / size.x) * distance;
|
||||
center += camera.up * (cursor.delta.y / size.y) * distance;
|
||||
} else {
|
||||
rotation.x -= cursor.delta.x / size.x * glm::two_pi<float>();
|
||||
rotation.y -= cursor.delta.y / size.y * glm::two_pi<float>();
|
||||
rotation.y = glm::max(
|
||||
-glm::half_pi<float>(), glm::min(glm::half_pi<float>(), rotation.y)
|
||||
);
|
||||
}
|
||||
} else if (grabbing) {
|
||||
grabbing = false;
|
||||
}
|
||||
if (hover) {
|
||||
distance *= 1.0f - 0.2f * input.getScroll();
|
||||
}
|
||||
camera.rotation = glm::mat4(1.0f);
|
||||
camera.rotate(rotation.y, rotation.x, rotation.z);
|
||||
camera.position = center - camera.front * distance;
|
||||
}
|
||||
|
||||
void ModelViewer::draw(const DrawContext& pctx, const Assets& assets) {
|
||||
camera.setAspectRatio(size.x / size.y);
|
||||
camera.updateVectors();
|
||||
|
||||
auto model = assets.get<model::Model>(modelName);
|
||||
if (model == nullptr) {
|
||||
return;
|
||||
}
|
||||
auto& prevShader = Shader::getUsed();
|
||||
|
||||
fbo->resize(size.x, size.y);
|
||||
{
|
||||
glDisable(GL_SCISSOR_TEST);
|
||||
auto ctx = pctx.sub();
|
||||
ctx.setFramebuffer(fbo.get());
|
||||
ctx.setViewport({size.x, size.y});
|
||||
ctx.setDepthTest(true);
|
||||
ctx.setCullFace(true);
|
||||
display::clear();
|
||||
|
||||
auto& ui3dShader = assets.require<Shader>("ui3d");
|
||||
ui3dShader.use();
|
||||
ui3dShader.uniformMatrix("u_apply", glm::mat4(1.0f));
|
||||
ui3dShader.uniformMatrix("u_projview", camera.getProjView());
|
||||
batch->begin();
|
||||
for (const auto& mesh : model->meshes) {
|
||||
util::TextureRegion region;
|
||||
if (!mesh.texture.empty() && mesh.texture[0] == '$') {
|
||||
// todo: refactor
|
||||
static std::array<std::string, 6> faces {
|
||||
"blocks:dbg_north",
|
||||
"blocks:dbg_south",
|
||||
"blocks:dbg_top",
|
||||
"blocks:dbg_bottom",
|
||||
"blocks:dbg_west",
|
||||
"blocks:dbg_east",
|
||||
};
|
||||
region = util::get_texture_region(
|
||||
assets,
|
||||
faces.at(mesh.texture.at(1) - '0'),
|
||||
"blocks:notfound"
|
||||
);
|
||||
} else {
|
||||
region = util::get_texture_region(assets, mesh.texture, "blocks:notfound");
|
||||
}
|
||||
batch->texture(region.texture);
|
||||
batch->setRegion(region.region);
|
||||
for (const auto& vertex : mesh.vertices) {
|
||||
batch->vertex(vertex.coord, vertex.uv, vertex.normal);
|
||||
}
|
||||
}
|
||||
batch->flush();
|
||||
glEnable(GL_SCISSOR_TEST);
|
||||
}
|
||||
|
||||
auto pos = calcPos();
|
||||
prevShader.use();
|
||||
auto& batch2d = *pctx.getBatch2D();
|
||||
batch2d.texture(fbo->getTexture());
|
||||
batch2d.rect(pos.x, pos.y, size.x, size.y, 0.0f, 0.0f, 0.0f, UVRegion {}, false, true, glm::vec4{1.0f});
|
||||
|
||||
Container::draw(pctx, assets);
|
||||
}
|
||||
|
||||
void ModelViewer::setCenter(const glm::vec3& center) {
|
||||
this->center = center;
|
||||
}
|
||||
|
||||
void ModelViewer::setRotation(const glm::vec3& euler) {
|
||||
this->rotation = euler;
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
#pragma once
|
||||
|
||||
#include <memory>
|
||||
#include <string>
|
||||
#include "Container.hpp"
|
||||
#include "window/Camera.hpp"
|
||||
|
||||
class Batch3D;
|
||||
class Framebuffer;
|
||||
|
||||
namespace gui {
|
||||
class ModelViewer : public Container {
|
||||
private:
|
||||
std::string modelName;
|
||||
Camera camera;
|
||||
std::unique_ptr<Batch3D> batch;
|
||||
std::unique_ptr<Framebuffer> fbo;
|
||||
|
||||
glm::vec3 rotation {};
|
||||
glm::vec3 center {};
|
||||
float distance = 4.0f;
|
||||
bool grabbing = false;
|
||||
public:
|
||||
ModelViewer(GUI& gui, const glm::vec2& size, const std::string& modelName);
|
||||
|
||||
~ModelViewer();
|
||||
|
||||
void setModel(const std::string& modelName);
|
||||
const std::string& getModel() const;
|
||||
|
||||
Camera& getCamera();
|
||||
const Camera& getCamera() const;
|
||||
|
||||
void act(float delta) override;
|
||||
void draw(const DrawContext& pctx, const Assets& assets) override;
|
||||
|
||||
void setRotation(const glm::vec3& euler);
|
||||
void setCenter(const glm::vec3& center);
|
||||
};
|
||||
}
|
||||
@@ -1,5 +1,7 @@
|
||||
#include "SplitBox.hpp"
|
||||
|
||||
#include "util/stack_vector.hpp"
|
||||
|
||||
using namespace gui;
|
||||
|
||||
SplitBox::SplitBox(GUI& gui, const glm::vec2& size, float splitPos, Orientation orientation)
|
||||
@@ -28,18 +30,31 @@ void SplitBox::mouseMove(int x, int y) {
|
||||
void SplitBox::refresh() {
|
||||
Container::refresh();
|
||||
|
||||
if (nodes.empty()) {
|
||||
util::stack_vector<UINode*, 2> visibleNodes;
|
||||
|
||||
for (const auto& node : nodes) {
|
||||
if (!node->isVisible()) {
|
||||
continue;
|
||||
}
|
||||
visibleNodes.push_back(node.get());
|
||||
if (visibleNodes.full()) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (visibleNodes.empty()) {
|
||||
return;
|
||||
}
|
||||
|
||||
glm::vec2 size = getSize();
|
||||
if (nodes.size() == 1) {
|
||||
auto node = nodes.at(0);
|
||||
if (visibleNodes.size() == 1) {
|
||||
auto node = visibleNodes.at(0);
|
||||
node->setPos(glm::vec2());
|
||||
node->setSize(size);
|
||||
return;
|
||||
}
|
||||
auto nodeA = nodes.at(0);
|
||||
auto nodeB = nodes.at(1);
|
||||
auto nodeA = visibleNodes.at(0);
|
||||
auto nodeB = visibleNodes.at(1);
|
||||
|
||||
float sepRadius = interval / 2.0f;
|
||||
|
||||
|
||||
@@ -19,6 +19,7 @@
|
||||
#include "elements/Panel.hpp"
|
||||
#include "elements/TextBox.hpp"
|
||||
#include "elements/TrackBar.hpp"
|
||||
#include "elements/ModelViewer.hpp"
|
||||
#include "engine/Engine.hpp"
|
||||
#include "frontend/locale.hpp"
|
||||
#include "frontend/menu.hpp"
|
||||
@@ -368,6 +369,23 @@ static std::shared_ptr<UINode> read_split_box(
|
||||
return splitBox;
|
||||
}
|
||||
|
||||
static std::shared_ptr<UINode> read_model_viewer(
|
||||
UiXmlReader& reader, const xml::xmlelement& element
|
||||
) {
|
||||
auto model = element.attr("src", "").getText();
|
||||
auto viewer = std::make_shared<ModelViewer>(
|
||||
reader.getGUI(), glm::vec2(), model
|
||||
);
|
||||
read_container_impl(reader, element, *viewer);
|
||||
if (element.has("center")) {
|
||||
viewer->setCenter(element.attr("center").asVec3());
|
||||
}
|
||||
if (element.has("cam-rotation")) {
|
||||
viewer->setRotation(glm::radians(element.attr("cam-rotation").asVec3()));
|
||||
}
|
||||
return viewer;
|
||||
}
|
||||
|
||||
static std::shared_ptr<UINode> read_panel(
|
||||
UiXmlReader& reader, const xml::xmlelement& element
|
||||
) {
|
||||
@@ -785,6 +803,7 @@ UiXmlReader::UiXmlReader(gui::GUI& gui, scriptenv&& env) : gui(gui), env(std::mo
|
||||
add("trackbar", read_track_bar);
|
||||
add("container", read_container);
|
||||
add("bindbox", read_input_bind_box);
|
||||
add("modelviewer", read_model_viewer);
|
||||
add("inventory", read_inventory);
|
||||
}
|
||||
|
||||
|
||||
@@ -192,6 +192,9 @@ std::string EnginePaths::createWriteableDevice(const std::string& name) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (name == "core") {
|
||||
folder = "res:";
|
||||
}
|
||||
if (folder.emptyOrInvalid()) {
|
||||
throw std::runtime_error("pack not found");
|
||||
}
|
||||
|
||||
@@ -15,6 +15,7 @@
|
||||
|
||||
// Libraries
|
||||
extern const luaL_Reg applib[];
|
||||
extern const luaL_Reg assetslib[];
|
||||
extern const luaL_Reg audiolib[];
|
||||
extern const luaL_Reg base64lib[];
|
||||
extern const luaL_Reg bjsonlib[];
|
||||
|
||||
@@ -0,0 +1,66 @@
|
||||
#include "api_lua.hpp"
|
||||
|
||||
#include "assets/Assets.hpp"
|
||||
#include "coders/png.hpp"
|
||||
#include "coders/vcm.hpp"
|
||||
#include "debug/Logger.hpp"
|
||||
#include "engine/Engine.hpp"
|
||||
#include "graphics/commons/Model.hpp"
|
||||
#include "graphics/core/Texture.hpp"
|
||||
#include "util/Buffer.hpp"
|
||||
|
||||
using namespace scripting;
|
||||
|
||||
static void load_texture(
|
||||
const ubyte* bytes, size_t size, const std::string& destname
|
||||
) {
|
||||
try {
|
||||
engine->getAssets()->store(png::load_texture(bytes, size), destname);
|
||||
} catch (const std::runtime_error& err) {
|
||||
debug::Logger logger("lua.assetslib");
|
||||
logger.error() << err.what();
|
||||
}
|
||||
}
|
||||
|
||||
static int l_load_texture(lua::State* L) {
|
||||
if (lua::istable(L, 1)) {
|
||||
lua::pushvalue(L, 1);
|
||||
size_t size = lua::objlen(L, 1);
|
||||
util::Buffer<ubyte> buffer(size);
|
||||
for (size_t i = 0; i < size; i++) {
|
||||
lua::rawgeti(L, i + 1);
|
||||
buffer[i] = lua::tointeger(L, -1);
|
||||
lua::pop(L);
|
||||
}
|
||||
lua::pop(L);
|
||||
load_texture(buffer.data(), buffer.size(), lua::require_string(L, 2));
|
||||
} else {
|
||||
auto string = lua::bytearray_as_string(L, 1);
|
||||
load_texture(
|
||||
reinterpret_cast<const ubyte*>(string.data()),
|
||||
string.size(),
|
||||
lua::require_string(L, 2)
|
||||
);
|
||||
lua::pop(L);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int l_parse_model(lua::State* L) {
|
||||
auto format = lua::require_lstring(L, 1);
|
||||
auto string = lua::require_lstring(L, 2);
|
||||
auto name = lua::require_string(L, 3);
|
||||
|
||||
if (format == "xml" || format == "vcm") {
|
||||
engine->getAssets()->store(vcm::parse(name, string), name);
|
||||
} else {
|
||||
throw std::runtime_error("unknown format " + util::quote(std::string(format)));
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
const luaL_Reg assetslib[] = {
|
||||
{"load_texture", lua::wrap<l_load_texture>},
|
||||
{"parse_model", lua::wrap<l_parse_model>},
|
||||
{NULL, NULL}
|
||||
};
|
||||
@@ -1,13 +1,12 @@
|
||||
#include <memory>
|
||||
#include <vector>
|
||||
#include <sstream>
|
||||
|
||||
#include "api_lua.hpp"
|
||||
#include "coders/png.hpp"
|
||||
#include "constants.hpp"
|
||||
#include "assets/Assets.hpp"
|
||||
#include "content/Content.hpp"
|
||||
#include "content/ContentControl.hpp"
|
||||
#include "debug/Logger.hpp"
|
||||
#include "engine/Engine.hpp"
|
||||
#include "io/engine_paths.hpp"
|
||||
#include "io/io.hpp"
|
||||
@@ -225,41 +224,6 @@ static int l_get_setting_info(lua::State* L) {
|
||||
throw std::runtime_error("unsupported setting type");
|
||||
}
|
||||
|
||||
static void load_texture(
|
||||
const ubyte* bytes, size_t size, const std::string& destname
|
||||
) {
|
||||
try {
|
||||
engine->getAssets()->store(png::load_texture(bytes, size), destname);
|
||||
} catch (const std::runtime_error& err) {
|
||||
debug::Logger logger("lua.corelib");
|
||||
logger.error() << err.what();
|
||||
}
|
||||
}
|
||||
|
||||
static int l_load_texture(lua::State* L) {
|
||||
if (lua::istable(L, 1)) {
|
||||
lua::pushvalue(L, 1);
|
||||
size_t size = lua::objlen(L, 1);
|
||||
util::Buffer<ubyte> buffer(size);
|
||||
for (size_t i = 0; i < size; i++) {
|
||||
lua::rawgeti(L, i + 1);
|
||||
buffer[i] = lua::tointeger(L, -1);
|
||||
lua::pop(L);
|
||||
}
|
||||
lua::pop(L);
|
||||
load_texture(buffer.data(), buffer.size(), lua::require_string(L, 2));
|
||||
} else {
|
||||
auto string = lua::bytearray_as_string(L, 1);
|
||||
load_texture(
|
||||
reinterpret_cast<const ubyte*>(string.data()),
|
||||
string.size(),
|
||||
lua::require_string(L, 2)
|
||||
);
|
||||
lua::pop(L);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int l_open_folder(lua::State* L) {
|
||||
platform::open_folder(io::resolve(lua::require_string(L, 1)));
|
||||
return 0;
|
||||
@@ -322,6 +286,5 @@ const luaL_Reg corelib[] = {
|
||||
{"open_folder", lua::wrap<l_open_folder>},
|
||||
{"quit", lua::wrap<l_quit>},
|
||||
{"capture_output", lua::wrap<l_capture_output>},
|
||||
{"__load_texture", lua::wrap<l_load_texture>},
|
||||
{NULL, NULL}
|
||||
};
|
||||
|
||||
@@ -14,6 +14,7 @@
|
||||
#include "graphics/ui/elements/TextBox.hpp"
|
||||
#include "graphics/ui/elements/TrackBar.hpp"
|
||||
#include "graphics/ui/elements/InlineFrame.hpp"
|
||||
#include "graphics/ui/elements/ModelViewer.hpp"
|
||||
#include "graphics/ui/gui_util.hpp"
|
||||
#include "graphics/ui/markdown.hpp"
|
||||
#include "graphics/core/Font.hpp"
|
||||
@@ -114,6 +115,16 @@ static int l_node_destruct(lua::State* L) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int l_container_refresh(lua::State* L) {
|
||||
auto docnode = get_document_node(L);
|
||||
auto node = dynamic_cast<Container*>(docnode.node.get());
|
||||
if (node == nullptr) {
|
||||
return 0;
|
||||
}
|
||||
node->refresh();
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int l_node_reposition(lua::State* L) {
|
||||
auto docnode = get_document_node(L);
|
||||
docnode.node->reposition();
|
||||
@@ -341,6 +352,8 @@ static int p_get_src(UINode* node, lua::State* L) {
|
||||
return lua::pushstring(L, image->getTexture());
|
||||
} else if (auto iframe = dynamic_cast<InlineFrame*>(node)) {
|
||||
return lua::pushstring(L, iframe->getSrc());
|
||||
} else if (auto modelviewer = dynamic_cast<ModelViewer*>(node)) {
|
||||
return lua::pushstring(L, modelviewer->getModel());
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
@@ -408,6 +421,13 @@ static int p_get_destruct(UINode*, lua::State* L) {
|
||||
return lua::pushcfunction(L, lua::wrap<l_node_destruct>);
|
||||
}
|
||||
|
||||
static int p_refresh(UINode* node, lua::State* L) {
|
||||
if (dynamic_cast<Container*>(node)) {
|
||||
return lua::pushcfunction(L, lua::wrap<l_container_refresh>);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int p_get_reposition(UINode*, lua::State* L) {
|
||||
return lua::pushcfunction(L, lua::wrap<l_node_reposition>);
|
||||
}
|
||||
@@ -490,6 +510,12 @@ static int p_get_scroll(UINode* node, lua::State* L) {
|
||||
}
|
||||
|
||||
static int l_gui_getattr(lua::State* L) {
|
||||
if (!lua::isstring(L, 1)) {
|
||||
throw std::runtime_error("document name is not a string");
|
||||
}
|
||||
if (!lua::isstring(L, 2)) {
|
||||
throw std::runtime_error("element id is not a string");
|
||||
}
|
||||
auto docname = lua::require_string(L, 1);
|
||||
auto element = lua::require_string(L, 2);
|
||||
if (lua::isnumber(L, 3)) {
|
||||
@@ -507,6 +533,9 @@ static int l_gui_getattr(lua::State* L) {
|
||||
const auto& id = request_node_id(DocumentNode {docnode.document, node});
|
||||
return push_document_node(L, id);
|
||||
}
|
||||
if (!lua::isstring(L, 3)) {
|
||||
throw std::runtime_error("attribute name is not a string");
|
||||
}
|
||||
auto attr = lua::require_string(L, 3);
|
||||
|
||||
static const std::unordered_map<
|
||||
@@ -530,6 +559,7 @@ static int l_gui_getattr(lua::State* L) {
|
||||
{"moveInto", p_move_into},
|
||||
{"add", p_get_add},
|
||||
{"destruct", p_get_destruct},
|
||||
{"refresh", p_refresh},
|
||||
{"reposition", p_get_reposition},
|
||||
{"clear", p_get_clear},
|
||||
{"setInterval", p_set_interval},
|
||||
@@ -666,6 +696,8 @@ static void p_set_src(UINode* node, lua::State* L, int idx) {
|
||||
image->setTexture(lua::require_string(L, idx));
|
||||
} else if (auto iframe = dynamic_cast<InlineFrame*>(node)) {
|
||||
iframe->setSrc(lua::require_string(L, idx));
|
||||
} else if (auto modelviewer = dynamic_cast<ModelViewer*>(node)) {
|
||||
modelviewer->setModel(lua::require_string(L, idx));
|
||||
}
|
||||
}
|
||||
static void p_set_region(UINode* node, lua::State* L, int idx) {
|
||||
@@ -755,6 +787,15 @@ static int p_set_scroll(UINode* node, lua::State* L, int idx) {
|
||||
}
|
||||
|
||||
static int l_gui_setattr(lua::State* L) {
|
||||
if (!lua::isstring(L, 1)) {
|
||||
throw std::runtime_error("document name is not a string");
|
||||
}
|
||||
if (!lua::isstring(L, 2)) {
|
||||
throw std::runtime_error("element id is not a string");
|
||||
}
|
||||
if (!lua::isstring(L, 3)) {
|
||||
throw std::runtime_error("attribute name is not a string");
|
||||
}
|
||||
auto docname = lua::require_string(L, 1);
|
||||
auto element = lua::require_string(L, 2);
|
||||
auto attr = lua::require_string(L, 3);
|
||||
|
||||
@@ -64,6 +64,7 @@ static void create_libs(State* L, StateType stateType) {
|
||||
openlib(L, "__vc_app", applib);
|
||||
}
|
||||
if (stateType == StateType::BASE || stateType == StateType::SCRIPT) {
|
||||
openlib(L, "assets", assetslib);
|
||||
openlib(L, "audio", audiolib);
|
||||
openlib(L, "console", consolelib);
|
||||
openlib(L, "core", corelib);
|
||||
|
||||
+20
-2
@@ -2,6 +2,7 @@
|
||||
|
||||
#include <cmath>
|
||||
#include <glm/vec2.hpp>
|
||||
#include <glm/vec4.hpp>
|
||||
|
||||
struct UVRegion {
|
||||
float u1;
|
||||
@@ -42,8 +43,8 @@ struct UVRegion {
|
||||
}
|
||||
|
||||
void scale(float x, float y) {
|
||||
float w = getWidth();
|
||||
float h = getHeight();
|
||||
float w = u2 - u1;
|
||||
float h = v2 - v1;
|
||||
float cx = (u1 + u2) * 0.5f;
|
||||
float cy = (v1 + v2) * 0.5f;
|
||||
u1 = cx - w * 0.5f * x;
|
||||
@@ -51,4 +52,21 @@ struct UVRegion {
|
||||
u2 = cx + w * 0.5f * x;
|
||||
v2 = cy + h * 0.5f * y;
|
||||
}
|
||||
|
||||
void scale(const glm::vec2& vec) {
|
||||
scale(vec.x, vec.y);
|
||||
}
|
||||
|
||||
void set(const glm::vec4& vec) {
|
||||
u1 = vec.x;
|
||||
v1 = vec.y;
|
||||
u2 = vec.z;
|
||||
v2 = vec.w;
|
||||
}
|
||||
|
||||
UVRegion operator*(const glm::vec2& scale) const {
|
||||
auto copy = UVRegion(*this);
|
||||
copy.scale(scale);
|
||||
return copy;
|
||||
}
|
||||
};
|
||||
|
||||
@@ -0,0 +1,99 @@
|
||||
#pragma once
|
||||
|
||||
namespace util {
|
||||
template<typename T, int capacity>
|
||||
class stack_vector {
|
||||
public:
|
||||
stack_vector() : size_(0) {}
|
||||
|
||||
stack_vector(const stack_vector<T, capacity>& other)
|
||||
: size_(other.size_) {
|
||||
for (int i = 0; i < size_; ++i) {
|
||||
new (&data_[i]) T(data_[i]);
|
||||
}
|
||||
}
|
||||
|
||||
stack_vector(stack_vector<T, capacity>&& other) noexcept
|
||||
: size_(other.size_) {
|
||||
for (int i = 0; i < size_; ++i) {
|
||||
new (&data_[i]) T(std::move(other.data_[i]));
|
||||
}
|
||||
other.size_ = 0;
|
||||
}
|
||||
|
||||
stack_vector(const std::initializer_list<T>& init) : size_(0) {
|
||||
static_assert(
|
||||
init.size() <= capacity,
|
||||
"initializer list exceeds stack vector capacity"
|
||||
);
|
||||
for (const auto& value : init) {
|
||||
new (&data_[size_++]) T(value);
|
||||
}
|
||||
}
|
||||
|
||||
~stack_vector() {
|
||||
clear();
|
||||
}
|
||||
|
||||
void push_back(const T& value) {
|
||||
if (size_ < capacity) {
|
||||
data_[size_++] = value;
|
||||
} else {
|
||||
throw std::overflow_error("stack vector capacity exceeded");
|
||||
}
|
||||
}
|
||||
|
||||
void pop_back() {
|
||||
if (size_ > 0) {
|
||||
data_[size_ - 1].~T();
|
||||
--size_;
|
||||
} else {
|
||||
throw std::underflow_error("stack vector is empty");
|
||||
}
|
||||
}
|
||||
|
||||
void clear() {
|
||||
for (int i = 0; i < size_; ++i) {
|
||||
data_[i].~T();
|
||||
}
|
||||
size_ = 0;
|
||||
}
|
||||
|
||||
T& operator[](int index) {
|
||||
return data_[index];
|
||||
}
|
||||
|
||||
const T& operator[](int index) const {
|
||||
return data_[index];
|
||||
}
|
||||
|
||||
T& at(int index) {
|
||||
if (index < 0 || index >= size_) {
|
||||
throw std::out_of_range("index out of range");
|
||||
}
|
||||
return data_[index];
|
||||
}
|
||||
|
||||
const T& at(int index) const {
|
||||
if (index < 0 || index >= size_) {
|
||||
throw std::out_of_range("index out of range");
|
||||
}
|
||||
return data_[index];
|
||||
}
|
||||
|
||||
int size() const {
|
||||
return size_;
|
||||
}
|
||||
|
||||
bool empty() const {
|
||||
return size_ == 0;
|
||||
}
|
||||
|
||||
bool full() const {
|
||||
return size_ == capacity;
|
||||
}
|
||||
private:
|
||||
T data_[capacity];
|
||||
int size_;
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user