minor changes and added namespace to avoid conflicts when integrating with PanoPainter

This commit is contained in:
2017-03-15 08:17:22 +00:00
parent ee6d352fc6
commit c34d1a1f44
16 changed files with 112 additions and 39 deletions

View File

@@ -1,6 +1,8 @@
#include "pch.h"
#include "app.h"
using namespace ui;
App App::I; // singleton
#ifdef __APPLE__
@@ -18,6 +20,13 @@ void App::create()
height = 500;
}
void App::clear()
{
glClearColor(.1f, .1f, .1f, 1.f);
glViewport(0, 0, (GLsizei)width, (GLsizei)height);
glClear(GL_COLOR_BUFFER_BIT);
}
void App::initShaders()
{
static const char* shader_v =
@@ -143,6 +152,7 @@ void App::initLayout()
LOG("initializing layout updating after load");
layout[main_id]->update(width, height, zoom);
LOG("initializing layout components");
sidebar = layout[main_id]->find<NodeBorder>("sidebar");
if (auto* button = layout[main_id]->find<NodeButton>("btn-close"))
{
button->on_click = [] { exit(0); };
@@ -264,11 +274,11 @@ void App::init()
void App::update(float dt)
{
glClearColor(.1f, .1f, .1f, 1.f);
glViewport(0, 0, (GLsizei)width, (GLsizei)height);
glClear(GL_COLOR_BUFFER_BIT);
//glClearColor(.1f, .1f, .1f, 1.f);
//glViewport(0, 0, (GLsizei)width, (GLsizei)height);
//glClear(GL_COLOR_BUFFER_BIT);
//layout.reload();
layout.reload();
if (auto* main = layout[main_id])
main->update(width, height, zoom);
@@ -295,7 +305,7 @@ void App::resize(float w, float h)
width = w;
height = h;
if (auto* main = layout[main_id])
main->update(w, h, zoom);
main->update(w , h, zoom);
}
void App::mouse_down(int button, float x, float y)

View File

@@ -8,6 +8,8 @@
class App
{
public:
static App I;
Sampler sampler;
Texture2D tex;
LayoutManager layout;
@@ -17,9 +19,8 @@ class App
NodePopupMenu* menu_file = nullptr;
NodePopupMenu* menu_edit = nullptr;
NodePopupMenu* menu_layers = nullptr;
NodeBorder* sidebar = nullptr;
const uint16_t main_id = const_hash("main");
public:
static App I;
float width;
float height;
#ifdef __ANDROID__
@@ -32,6 +33,7 @@ public:
void initAssets();
void initLayout();
void create();
void clear();
void update(float dt);
void resize(float w, float h);
void mouse_down(int button, float x, float y);

View File

@@ -51,7 +51,8 @@ void Asset::close()
#ifdef __ANDROID__
AAsset_close(m_asset);
#else
fclose(m_fp);
if (m_fp)
fclose(m_fp);
if (m_data)
delete m_data;
m_data = nullptr;

View File

@@ -123,6 +123,8 @@ void TextMesh::draw()
glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, sizeof(glm::vec4), (GLvoid*)0);
glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, sizeof(glm::vec4), (GLvoid*)(sizeof(float) * 2));
glDrawElements(GL_TRIANGLES, font_array_count, GL_UNSIGNED_SHORT, 0);
glDisableVertexAttribArray(0);
glDisableVertexAttribArray(1);
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
#endif // USE_VBO

View File

@@ -1,6 +1,7 @@
#pragma once
#include "texture.h"
#include "util.h"
#include <stb/stb_truetype.h>
enum class kFont : uint16_t
{

View File

@@ -4,9 +4,11 @@
#include <stb/stb_image.h>
using namespace ui;
bool Image::load(std::string filename)
{
//stbi_set_flip_vertically_on_load(true);
stbi_set_flip_vertically_on_load(false);
Asset file;
if (!(file.open(filename.c_str()) && file.read_all()))
{

View File

@@ -1,5 +1,7 @@
#pragma once
namespace ui {
class Image
{
std::unique_ptr<uint8_t[]> m_data;
@@ -11,3 +13,5 @@ public:
const uint8_t* data() const { return m_data.get(); }
int size() const { return width * height * comp; }
};
}

View File

@@ -3,6 +3,8 @@
#include "util.h"
#include "asset.h"
using namespace ui;
Plane NodeBorder::m_plane;
Plane NodeImage::m_plane;
Sampler NodeImage::m_sampler;
@@ -137,8 +139,9 @@ void Node::update_internal(const glm::vec2& origin, const glm::mat4& proj)
glm::mat4 pivot = glm::translate(glm::vec3(.5f, .5f, 0.f));
glm::mat4 scale = glm::scale(glm::vec3(m_size, 1.f));
glm::mat4 prescale = glm::scale(glm::vec3(m_scale, 1.f));
glm::mat4 pos = glm::translate(glm::vec3(m_pos, 0));
m_mvp = proj * pos * scale * pivot;
m_mvp = proj * pos * scale * pivot * prescale;
m_proj = proj;
for (int i = 0; i < m_children.size(); i++)

View File

@@ -4,6 +4,8 @@
#include "shader.h"
#include "font.h"
#include "asset.h"
#include <tinyxml2.h>
#include <yoga/Yoga.h>
enum class kAttribute : uint16_t
{
@@ -106,7 +108,11 @@ public:
std::function<void()> on_loaded;
bool load(const char* path);
bool reload();
class Node* operator[](uint16_t id) { return m_layouts[id].get(); }
class Node* operator[](uint16_t id)
{
auto i = m_layouts.find(id);
return i == m_layouts.end() ? nullptr : i->second.get();
}
//Node& operator[](const char* ids) { return m_layouts[const_hash(ids)]; }
};
@@ -128,6 +134,7 @@ public:
bool m_destroyed = false;
float m_zoom = 1.f;
glm::vec2 m_scale{ 1.f };
glm::vec2 m_pos;
glm::vec2 m_size;
glm::vec4 m_clip;
@@ -195,6 +202,7 @@ public:
void SetJustify(YGJustify value) { YGNodeStyleSetJustifyContent(y_node, value); }
void SetAlign(YGAlign value) { YGNodeStyleSetAlignItems(y_node, value); }
void SetPositioning(YGPositionType value) { YGNodeStyleSetPositionType(y_node, value); }
void SetAspectRatio(float ar) { YGNodeStyleSetAspectRatio(y_node, ar); }
glm::vec4 rect_intersection(glm::vec4 a, glm::vec4 b)
{
@@ -287,7 +295,7 @@ public:
class NodeBorder : public Node
{
public:
static Plane m_plane;
static ui::Plane m_plane;
glm::vec4 m_color{ 0, 0, 0, 1 };
glm::vec4 m_border_color{ 1, 1, 1, 1 };
float m_thinkness{ 0 };
@@ -338,19 +346,20 @@ public:
}
virtual void draw() override
{
ShaderManager::use(kShader::Color);
ShaderManager::u_mat4(kShaderUniform::MVP, m_mvp);
using namespace ui;
ui::ShaderManager::use(kShader::Color);
ui::ShaderManager::u_mat4(kShaderUniform::MVP, m_mvp);
if (m_color.a != 1.f)
glEnable(GL_BLEND);
ShaderManager::u_vec4(kShaderUniform::Col, m_color);
ui::ShaderManager::u_vec4(kShaderUniform::Col, m_color);
m_plane.draw_fill();
if (m_thinkness > 0)
{
glLineWidth(m_thinkness);
ShaderManager::u_vec4(kShaderUniform::Col, m_border_color);
ui::ShaderManager::u_vec4(kShaderUniform::Col, m_border_color);
m_plane.draw_stroke();
}
@@ -417,12 +426,13 @@ public:
}
virtual void draw() override
{
using namespace ui;
glm::mat4 pos = glm::translate(glm::vec3(glm::floor(m_pos), 0));
m_mvp = m_proj * pos;
ShaderManager::use(kShader::Font);
ShaderManager::u_int(kShaderUniform::Tex, 0);
ShaderManager::u_mat4(kShaderUniform::MVP, m_mvp);
ShaderManager::u_vec4(kShaderUniform::Col, m_color);
ui::ShaderManager::use(kShader::Font);
ui::ShaderManager::u_int(kShaderUniform::Tex, 0);
ui::ShaderManager::u_mat4(kShaderUniform::MVP, m_mvp);
ui::ShaderManager::u_vec4(kShaderUniform::Col, m_color);
glEnable(GL_BLEND);
m_text_mesh.draw();
glDisable(GL_BLEND);
@@ -432,9 +442,9 @@ public:
class NodeImage : public Node
{
public:
static Plane m_plane;
static ui::Plane m_plane;
static Sampler m_sampler;
bool m_use_atlas;
bool m_use_atlas = false;
glm::vec4 m_region;
glm::vec2 m_off;
glm::vec2 m_sz;
@@ -459,7 +469,7 @@ public:
}
virtual void create() override
{
if (TextureManager::load(m_path.c_str()))
if (!m_path.empty() && TextureManager::load(m_path.c_str()))
{
auto tex_sz = TextureManager::get(m_tex_id).size();
m_off = m_region.xy / tex_sz;
@@ -492,21 +502,22 @@ public:
}
virtual void draw() override
{
using namespace ui;
TextureManager::get(m_tex_id).bind();
m_sampler.bind(0);
glEnable(GL_BLEND);
if (m_use_atlas)
{
ShaderManager::use(kShader::Atlas);
ShaderManager::u_vec2(kShaderUniform::Tof, m_off);
ShaderManager::u_vec2(kShaderUniform::Tsz, m_sz);
ui::ShaderManager::use(kShader::Atlas);
ui::ShaderManager::u_vec2(kShaderUniform::Tof, m_off);
ui::ShaderManager::u_vec2(kShaderUniform::Tsz, m_sz);
}
else
{
ShaderManager::use(kShader::Texture);
ui::ShaderManager::use(kShader::Texture);
}
ShaderManager::u_int(kShaderUniform::Tex, 0);
ShaderManager::u_mat4(kShaderUniform::MVP, m_mvp);
ui::ShaderManager::u_int(kShaderUniform::Tex, 0);
ui::ShaderManager::u_mat4(kShaderUniform::MVP, m_mvp);
m_plane.draw_fill();
m_sampler.unbind();
TextureManager::get(m_tex_id).unbind();
@@ -787,7 +798,7 @@ public:
class NodeViewport : public Node
{
public:
std::unique_ptr<Plane> m_faces;
std::unique_ptr<ui::Plane> m_faces;
std::unique_ptr<Sampler> m_sampler;
uint16_t m_tex_id;
glm::vec2 drag_start;
@@ -798,7 +809,8 @@ public:
virtual void draw() override
{
glm::mat4 cam = glm::lookAt(glm::vec3(sinf(angle)*10, 0, -10), glm::vec3(0, 0, 0), glm::vec3(0, -1, 0));
using namespace ui;
glm::mat4 cam = glm::lookAt(glm::vec3(sinf(angle) * 10, 0, -10), glm::vec3(0, 0, 0), glm::vec3(0, -1, 0));
glm::mat4 proj = glm::perspective<float>(glm::radians(45.f), m_clip.z / m_clip.w, .1f, 100);
GLint vp[4];
@@ -814,9 +826,9 @@ public:
TextureManager::get(m_tex_id).bind();
m_sampler->bind(0);
glEnable(GL_BLEND);
ShaderManager::use(kShader::Texture);
ShaderManager::u_int(kShaderUniform::Tex, 0);
ShaderManager::u_mat4(kShaderUniform::MVP, proj * cam);
ui::ShaderManager::use(kShader::Texture);
ui::ShaderManager::u_int(kShaderUniform::Tex, 0);
ui::ShaderManager::u_mat4(kShaderUniform::MVP, proj * cam);
m_faces->draw_fill();
m_sampler->unbind();
TextureManager::get(m_tex_id).unbind();
@@ -831,7 +843,7 @@ public:
}
virtual void create() override
{
m_faces = std::make_unique<Plane>();
m_faces = std::make_unique<ui::Plane>();
m_faces->create<1>(10, 10);
m_sampler = std::make_unique<Sampler>();
m_sampler->create();

View File

@@ -427,6 +427,7 @@ int main()
float dt = (float)(t1 - t0) / 1000.0f;
if (dt > 1.0f / 60.0f)
{
App::I.clear();
App::I.update((float)(t1 - t0) / 1000.0f);
t0 = t1;
SwapBuffers(hDC);
@@ -452,6 +453,7 @@ LRESULT CALLBACK WndProc(HWND hWnd, UINT msg, WPARAM wp, LPARAM lp)
break;
case WM_SIZE:
App::I.resize((float)LOWORD(lp), (float)HIWORD(lp));
App::I.clear();
App::I.update(0.f);
SwapBuffers(hDC);
break;

View File

@@ -1,6 +1,8 @@
#include "pch.h"
#include "shader.h"
using namespace ui;
std::map<kShader, Shader> ShaderManager::m_shaders;
Shader* ShaderManager::m_current;

View File

@@ -1,6 +1,8 @@
#pragma once
#include "util.h"
namespace ui {
enum class kShaderUniform : uint16_t
{
MVP = const_hash("mvp"),
@@ -45,3 +47,5 @@ public:
static void u_mat4(kShaderUniform id, const glm::mat4& m);
static void u_int(kShaderUniform id, int i);
};
}

View File

@@ -1,6 +1,8 @@
#include "pch.h"
#include "shape.h"
using namespace ui;
bool Shape::create_buffers(GLvoid* idx, GLvoid* vertices, int isize, int vsize)
{
glGenBuffers(2, buffers);
@@ -46,6 +48,8 @@ void Shape::draw_fill() const
glVertexAttribPointer(0, 4, GL_FLOAT, GL_FALSE, sizeof(vertex_t), (GLvoid*)0);
glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, sizeof(vertex_t), (GLvoid*)offsetof(vertex_t, uvs));
glDrawElements(GL_TRIANGLES, count[0], GL_UNSIGNED_SHORT, ioff[0]);
glDisableVertexAttribArray(0);
glDisableVertexAttribArray(1);
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
#endif // USE_VBO
@@ -64,6 +68,10 @@ void Shape::draw_stroke() const
glVertexAttribPointer(0, 4, GL_FLOAT, GL_FALSE, sizeof(vertex_t), (GLvoid*)0);
glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, sizeof(vertex_t), (GLvoid*)offsetof(vertex_t, uvs));
glDrawElements(GL_LINES, count[1], GL_UNSIGNED_SHORT, ioff[1]);
glDisableVertexAttribArray(0);
glDisableVertexAttribArray(1);
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
#endif // USE_VBO
}

View File

@@ -1,5 +1,7 @@
#pragma once
namespace ui {
class Shape
{
protected:
@@ -149,3 +151,5 @@ public:
return create_buffers(idx, vertices, sizeof(idx), sizeof(vertices));
}
};
}

View File

@@ -1,6 +1,5 @@
#include "pch.h"
#include "texture.h"
#include "image.h"
#include "util.h"
std::map<uint16_t, Texture2D> TextureManager::m_textures;
@@ -15,6 +14,11 @@ bool TextureManager::load(const char* path)
return true;
}
void TextureManager::assign(uint16_t id, GLuint tex, int w/* = -1*/, int h/* = -1*/, GLuint format/* = GL_RGBA*/)
{
m_textures[id].assign(tex, w, h, format);
}
Texture2D& TextureManager::get(uint16_t id)
{
return m_textures[id];
@@ -31,14 +35,23 @@ bool Texture2D::create(int width, int height, GLint format, const uint8_t* data)
unbind();
return true;
}
bool Texture2D::create(const Image& img)
bool Texture2D::create(const ui::Image& img)
{
static GLint formats[] = { 0, 0, GL_RGB, GL_RGBA };
return create(img.width, img.height, formats[img.comp - 1], img.data());
}
void Texture2D::assign(GLuint tex, int w/* = -1*/, int h/* = -1*/, GLuint format/* = GL_RGBA*/)
{
m_tex = tex;
m_width = w;
m_height = h;
m_format = format;
}
bool Texture2D::load(std::string filename)
{
Image img;
ui::Image img;
if (!img.load(filename))
return false;
return create(img);

View File

@@ -1,4 +1,5 @@
#pragma once
#include "image.h"
class Texture2D
{
@@ -8,7 +9,8 @@ class Texture2D
GLint m_format;
public:
bool create(int width, int height, GLint format = GL_RGBA, const uint8_t* data = nullptr);
bool create(const class Image& img);
bool create(const ui::Image& img);
void assign(GLuint tex, int w = -1, int h = -1, GLuint format = GL_RGBA);
bool load(std::string filename);
void destroy() { glDeleteTextures(1, &m_tex); }
void bind() const { glBindTexture(GL_TEXTURE_2D, m_tex); }
@@ -35,5 +37,6 @@ class TextureManager
public:
static std::map<uint16_t, Texture2D> m_textures;
static bool load(const char* path);
static void assign(uint16_t id, GLuint tex, int w = -1, int h = -1, GLuint format = GL_RGBA);
static Texture2D& get(uint16_t id);
};