Merge branch 'model3dview' into update-gfx-pipeline
This commit is contained in:
@@ -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) {
|
||||
|
||||
+2
-2
@@ -14,8 +14,8 @@ static const std::unordered_map<std::string, int> side_indices {
|
||||
{"south", 1},
|
||||
{"top", 2},
|
||||
{"bottom", 3},
|
||||
{"east", 4},
|
||||
{"west", 5},
|
||||
{"west", 4},
|
||||
{"east", 5},
|
||||
};
|
||||
|
||||
static void perform_rect(const xmlelement& root, model::Model& model) {
|
||||
|
||||
@@ -77,13 +77,13 @@ void Mesh::addBox(
|
||||
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]);
|
||||
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]);
|
||||
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]);
|
||||
addPlane(pos-X*size, Z*size, Y*size, -X, uvs[5] * glm::vec2(-1, 1));
|
||||
}
|
||||
|
||||
void Mesh::scale(const glm::vec3& size) {
|
||||
|
||||
@@ -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) {
|
||||
buffer = std::make_unique<Batch3DVertex[]>(capacity);
|
||||
@@ -29,20 +34,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++;
|
||||
}
|
||||
@@ -52,7 +65,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++;
|
||||
}
|
||||
@@ -84,14 +99,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(
|
||||
@@ -238,6 +257,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(Bindable* 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();
|
||||
};
|
||||
|
||||
@@ -98,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),
|
||||
@@ -106,6 +106,9 @@ 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(),
|
||||
|
||||
@@ -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,151 @@
|
||||
#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);
|
||||
};
|
||||
}
|
||||
@@ -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("model", "").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);
|
||||
}
|
||||
|
||||
|
||||
@@ -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") {
|
||||
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}
|
||||
};
|
||||
@@ -2,12 +2,10 @@
|
||||
#include <vector>
|
||||
|
||||
#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 +223,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 +285,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}
|
||||
};
|
||||
|
||||
@@ -490,6 +490,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 +513,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<
|
||||
@@ -755,6 +764,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);
|
||||
|
||||
@@ -43,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;
|
||||
@@ -63,4 +63,10 @@ struct UVRegion {
|
||||
u2 = vec.z;
|
||||
v2 = vec.w;
|
||||
}
|
||||
|
||||
UVRegion operator*(const glm::vec2& scale) const {
|
||||
auto copy = UVRegion(*this);
|
||||
copy.scale(scale);
|
||||
return copy;
|
||||
}
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user