fix equirectangular shader for higher precision, add polygon draw mode
This commit is contained in:
@@ -68,3 +68,9 @@ android {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
task copyFiles(type: Copy) {
|
||||
from '../data/'
|
||||
into 'src/main/assets/data'
|
||||
}
|
||||
preBuild.dependsOn(copyFiles)
|
||||
|
||||
@@ -362,7 +362,8 @@
|
||||
<button id="btn-erase" width="60" height="100%" margin="0 0 0 0" text="Erase"/>
|
||||
<button id="btn-line" width="50" height="100%" margin="0 0 0 0" text="Line"/>
|
||||
<button id="btn-cam" width="70" height="100%" margin="0 0 0 0" text="Camera"/>
|
||||
<button id="btn-normal" width="70" height="100%" margin="0 0 0 0" text="Normal"/>
|
||||
<button id="btn-grid" width="50" height="100%" margin="0 0 0 0" text="Grid"/>
|
||||
<button id="btn-fill" width="50" height="100%" margin="0 0 0 0" text="Fill"/>
|
||||
<!--
|
||||
<button-custom id="btn-layer" width="50" height="100%" margin="0 5 0 0" thickness="1" border-color="0 0 0 1" pad="6" align="center" justify="center">
|
||||
<icon width="100%" height="100%" icon="disk"/>
|
||||
|
||||
@@ -88,6 +88,7 @@ void App::initShaders()
|
||||
"in vec4 pos;"
|
||||
"void main(){"
|
||||
" gl_Position = mvp * pos;"
|
||||
" gl_PointSize = 15.0;"
|
||||
"}";
|
||||
static const char* shader_color_f =
|
||||
SHADER_VERSION
|
||||
@@ -299,14 +300,14 @@ void App::initShaders()
|
||||
static const char* shader_equirect_f =
|
||||
SHADER_VERSION
|
||||
"uniform samplerCube tex;\n"
|
||||
"in mediump vec2 uv;\n"
|
||||
"in highp vec2 uv;\n"
|
||||
"out mediump vec4 frag;\n"
|
||||
"void main(){\n"
|
||||
" mediump float anglex = uv.x;\n"
|
||||
" mediump float angley = uv.y;\n"
|
||||
" mediump float sx = sin(anglex);\n"
|
||||
" mediump float cx = cos(anglex);\n"
|
||||
" mediump vec3 dir = vec3(0.0, 0.0, 0.0);\n"
|
||||
" highp float anglex = uv.x;\n"
|
||||
" highp float angley = uv.y;\n"
|
||||
" highp float sx = sin(anglex);\n"
|
||||
" highp float cx = cos(anglex);\n"
|
||||
" highp vec3 dir = vec3(0.0, 0.0, 0.0);\n"
|
||||
" dir.x = sin(angley) * cx;\n"
|
||||
" dir.y = cos(angley);\n"
|
||||
" dir.z = sin(angley) * sx;\n"
|
||||
@@ -472,7 +473,8 @@ void App::initLayout()
|
||||
layout[main_id]->find<NodeButton>("btn-erase")->set_color(color_button_normal);
|
||||
layout[main_id]->find<NodeButton>("btn-line")->set_color(color_button_normal);
|
||||
layout[main_id]->find<NodeButton>("btn-cam")->set_color(color_button_normal);
|
||||
layout[main_id]->find<NodeButton>("btn-normal")->set_color(color_button_normal);
|
||||
layout[main_id]->find<NodeButton>("btn-grid")->set_color(color_button_normal);
|
||||
layout[main_id]->find<NodeButton>("btn-fill")->set_color(color_button_normal);
|
||||
Canvas::set_mode(Canvas::kCanvasMode::Draw);
|
||||
};
|
||||
layout[main_id]->find<NodeButton>("btn-pen")->set_color(color_button_hlight);
|
||||
@@ -485,7 +487,8 @@ void App::initLayout()
|
||||
layout[main_id]->find<NodeButton>("btn-erase")->set_color(color_button_hlight);
|
||||
layout[main_id]->find<NodeButton>("btn-line")->set_color(color_button_normal);
|
||||
layout[main_id]->find<NodeButton>("btn-cam")->set_color(color_button_normal);
|
||||
layout[main_id]->find<NodeButton>("btn-normal")->set_color(color_button_normal);
|
||||
layout[main_id]->find<NodeButton>("btn-grid")->set_color(color_button_normal);
|
||||
layout[main_id]->find<NodeButton>("btn-fill")->set_color(color_button_normal);
|
||||
Canvas::set_mode(Canvas::kCanvasMode::Erase);
|
||||
};
|
||||
}
|
||||
@@ -496,7 +499,8 @@ void App::initLayout()
|
||||
layout[main_id]->find<NodeButton>("btn-erase")->set_color(color_button_normal);
|
||||
layout[main_id]->find<NodeButton>("btn-line")->set_color(color_button_hlight);
|
||||
layout[main_id]->find<NodeButton>("btn-cam")->set_color(color_button_normal);
|
||||
layout[main_id]->find<NodeButton>("btn-normal")->set_color(color_button_normal);
|
||||
layout[main_id]->find<NodeButton>("btn-grid")->set_color(color_button_normal);
|
||||
layout[main_id]->find<NodeButton>("btn-fill")->set_color(color_button_normal);
|
||||
Canvas::set_mode(Canvas::kCanvasMode::Line);
|
||||
};
|
||||
}
|
||||
@@ -507,19 +511,33 @@ void App::initLayout()
|
||||
layout[main_id]->find<NodeButton>("btn-erase")->set_color(color_button_normal);
|
||||
layout[main_id]->find<NodeButton>("btn-line")->set_color(color_button_normal);
|
||||
layout[main_id]->find<NodeButton>("btn-cam")->set_color(color_button_hlight);
|
||||
layout[main_id]->find<NodeButton>("btn-normal")->set_color(color_button_normal);
|
||||
layout[main_id]->find<NodeButton>("btn-grid")->set_color(color_button_normal);
|
||||
layout[main_id]->find<NodeButton>("btn-fill")->set_color(color_button_normal);
|
||||
Canvas::set_mode(Canvas::kCanvasMode::Camera);
|
||||
};
|
||||
}
|
||||
if (auto* button = layout[main_id]->find<NodeButton>("btn-normal"))
|
||||
if (auto* button = layout[main_id]->find<NodeButton>("btn-grid"))
|
||||
{
|
||||
button->on_click = [this](Node*) {
|
||||
layout[main_id]->find<NodeButton>("btn-pen")->set_color(color_button_normal);
|
||||
layout[main_id]->find<NodeButton>("btn-erase")->set_color(color_button_normal);
|
||||
layout[main_id]->find<NodeButton>("btn-line")->set_color(color_button_normal);
|
||||
layout[main_id]->find<NodeButton>("btn-cam")->set_color(color_button_normal);
|
||||
layout[main_id]->find<NodeButton>("btn-normal")->set_color(color_button_hlight);
|
||||
Canvas::set_mode(Canvas::kCanvasMode::Normal);
|
||||
layout[main_id]->find<NodeButton>("btn-grid")->set_color(color_button_hlight);
|
||||
layout[main_id]->find<NodeButton>("btn-fill")->set_color(color_button_normal);
|
||||
Canvas::set_mode(Canvas::kCanvasMode::Grid);
|
||||
};
|
||||
}
|
||||
if (auto* button = layout[main_id]->find<NodeButton>("btn-fill"))
|
||||
{
|
||||
button->on_click = [this](Node*) {
|
||||
layout[main_id]->find<NodeButton>("btn-pen")->set_color(color_button_normal);
|
||||
layout[main_id]->find<NodeButton>("btn-erase")->set_color(color_button_normal);
|
||||
layout[main_id]->find<NodeButton>("btn-line")->set_color(color_button_normal);
|
||||
layout[main_id]->find<NodeButton>("btn-cam")->set_color(color_button_normal);
|
||||
layout[main_id]->find<NodeButton>("btn-grid")->set_color(color_button_normal);
|
||||
layout[main_id]->find<NodeButton>("btn-fill")->set_color(color_button_hlight);
|
||||
Canvas::set_mode(Canvas::kCanvasMode::Fill);
|
||||
};
|
||||
}
|
||||
if (auto* button = layout[main_id]->find<NodeButtonCustom>("btn-bucket"))
|
||||
@@ -742,9 +760,10 @@ void App::init()
|
||||
|
||||
zoom = ceilf(width / 1000.f);
|
||||
|
||||
//glEnable(GL_TEXTURE_2D);
|
||||
glDisable(GL_DEPTH_TEST);
|
||||
|
||||
#if defined(_WIN32) || defined(__OSX__)
|
||||
glEnable(GL_PROGRAM_POINT_SIZE);
|
||||
#endif
|
||||
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
|
||||
glBlendEquation(GL_FUNC_ADD);
|
||||
|
||||
|
||||
@@ -9,6 +9,7 @@ std::vector<CanvasMode*> ui::Canvas::modes[] = {
|
||||
{ new CanvasModeLine, new CanvasModeBasicCamera },
|
||||
{ new CanvasModeCamera, new CanvasModeBasicCamera },
|
||||
{ new CanvasModeNormal, new CanvasModeBasicCamera },
|
||||
{ new CanvasModeFill, new CanvasModeBasicCamera },
|
||||
};
|
||||
glm::vec3 ui::Canvas::m_plane_origin[6] = {
|
||||
{ 0, 0,-1}, // front
|
||||
@@ -564,10 +565,11 @@ void ui::Canvas::save(std::string data_path)
|
||||
// static char name[128];
|
||||
// sprintf(name, "%s/Face%d.png", data_path.c_str(), i);
|
||||
// LOG("writing %s", name);
|
||||
// //int ret = stbi_write_png(name, m_tmp[i].getWidth(), m_tmp[i].getHeight(), 4, data.get(), m_tmp[i].stride());
|
||||
// int ret = stbi_write_png(name, m_tmp[i].getWidth(), m_tmp[i].getHeight(), 4, data.get(), m_tmp[i].stride());
|
||||
// }
|
||||
|
||||
glViewport(0, 0, m_latlong.getWidth(), m_latlong.getHeight());
|
||||
glActiveTexture(GL_TEXTURE0);
|
||||
m_latlong.bindFramebuffer();
|
||||
ui::ShaderManager::use(kShader::Equirect);
|
||||
ui::ShaderManager::u_mat4(kShaderUniform::MVP, glm::ortho(-.5f, .5f, -.5f, .5f, -1.f, 1.f));
|
||||
|
||||
@@ -71,12 +71,15 @@ public:
|
||||
glm::vec3 m_cam_pos;
|
||||
float m_cam_fov = 85;
|
||||
|
||||
enum class kCanvasMode { Draw, Erase, Line, Camera, Normal, COUNT };
|
||||
enum class kCanvasMode { Draw, Erase, Line, Camera, Grid, Fill, COUNT };
|
||||
kCanvasMode m_state{ kCanvasMode::Draw };
|
||||
static std::vector<CanvasMode*> modes[];
|
||||
std::vector<CanvasMode*>* m_mode;
|
||||
std::vector<CanvasMode*>* m_mode = nullptr;
|
||||
static void set_mode(kCanvasMode mode)
|
||||
{
|
||||
if (I->m_mode)
|
||||
for (auto& m : *I->m_mode)
|
||||
m->leave();
|
||||
I->m_mode = &modes[(int)mode];
|
||||
I->m_state = mode;
|
||||
}
|
||||
|
||||
@@ -84,6 +84,7 @@ void CanvasModePen::on_MouseEvent(MouseEvent* me, glm::vec2& loc)
|
||||
canvas->stroke_update(loc, 1.f);
|
||||
break;
|
||||
case kEventType::MouseCancel:
|
||||
canvas->stroke_end();
|
||||
m_dragging = false;
|
||||
node->mouse_release();
|
||||
break;
|
||||
@@ -167,6 +168,7 @@ void CanvasModeCamera::on_MouseEvent(MouseEvent* me, glm::vec2& loc)
|
||||
case kEventType::MouseUpL:
|
||||
m_dragging = false;
|
||||
node->mouse_release();
|
||||
canvas->m_cam_pos.xy = m_pos_start;
|
||||
break;
|
||||
case kEventType::MouseMove:
|
||||
if (m_dragging)
|
||||
@@ -193,6 +195,7 @@ void CanvasModeNormal::on_MouseEvent(MouseEvent* me, glm::vec2& loc)
|
||||
glm::vec3 ro, rd, hit_o, hit_d;
|
||||
if (canvas->point_trace(loc, ro, rd, hit_o, hit_d, m_plane_id))
|
||||
{
|
||||
m_lines.push_back({ hit_o, hit_d });
|
||||
origin = hit_o;
|
||||
dir = hit_d;
|
||||
m_dragging = true;
|
||||
@@ -202,13 +205,14 @@ void CanvasModeNormal::on_MouseEvent(MouseEvent* me, glm::vec2& loc)
|
||||
case kEventType::MouseUpL:
|
||||
node->mouse_release();
|
||||
m_dragging = false;
|
||||
commit();
|
||||
//commit();
|
||||
break;
|
||||
case kEventType::MouseMove:
|
||||
{
|
||||
glm::vec3 ro, rd, hit_o, hit_d;
|
||||
if (m_dragging && canvas->point_trace_plane(loc, ro, rd, hit_o, hit_d, m_plane_id))
|
||||
{
|
||||
m_lines.back() = { hit_o, hit_d };
|
||||
origin = hit_o;
|
||||
dir = hit_d;
|
||||
m_dragging = true;
|
||||
@@ -226,8 +230,11 @@ void CanvasModeNormal::on_MouseEvent(MouseEvent* me, glm::vec2& loc)
|
||||
|
||||
void CanvasModeNormal::on_Draw(const glm::mat4& ortho, const glm::mat4& proj, const glm::mat4& camera)
|
||||
{
|
||||
if (m_dragging)
|
||||
//if (m_dragging)
|
||||
for (auto l : m_lines)
|
||||
{
|
||||
auto origin = l.o;
|
||||
auto dir = l.d;
|
||||
ui::ShaderManager::use(ui::kShader::Color);
|
||||
ui::ShaderManager::u_mat4(ui::kShaderUniform::MVP, proj * camera);
|
||||
ui::ShaderManager::u_vec4(ui::kShaderUniform::Col, {1, 0, 0, 1});
|
||||
@@ -258,3 +265,102 @@ void CanvasModeNormal::commit()
|
||||
};
|
||||
canvas->draw_objects(std::bind(drawer, std::placeholders::_1, std::placeholders::_2));
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////
|
||||
|
||||
void CanvasModeFill::init()
|
||||
{
|
||||
m_shape.create();
|
||||
}
|
||||
|
||||
void CanvasModeFill::leave()
|
||||
{
|
||||
auto drawer = [this](const glm::mat4& camera, const glm::mat4& proj) {
|
||||
ui::ShaderManager::use(ui::kShader::Color);
|
||||
ui::ShaderManager::u_mat4(ui::kShaderUniform::MVP, proj * camera);
|
||||
ui::ShaderManager::u_vec4(ui::kShaderUniform::Col, { node->m_brush.m_tip_color.rgb(), node->m_brush.m_tip_opacity });
|
||||
m_shape.draw_fill();
|
||||
};
|
||||
canvas->draw_objects(std::bind(drawer, std::placeholders::_1, std::placeholders::_2));
|
||||
m_points.clear();
|
||||
}
|
||||
|
||||
void CanvasModeFill::on_MouseEvent(MouseEvent* me, glm::vec2& loc)
|
||||
{
|
||||
switch (me->m_type)
|
||||
{
|
||||
case kEventType::MouseDownR:
|
||||
{
|
||||
auto drawer = [this](const glm::mat4& camera, const glm::mat4& proj) {
|
||||
ui::ShaderManager::use(ui::kShader::Color);
|
||||
ui::ShaderManager::u_mat4(ui::kShaderUniform::MVP, proj * camera);
|
||||
ui::ShaderManager::u_vec4(ui::kShaderUniform::Col, { node->m_brush.m_tip_color.rgb(), node->m_brush.m_tip_opacity });
|
||||
m_shape.draw_fill();
|
||||
};
|
||||
canvas->draw_objects(std::bind(drawer, std::placeholders::_1, std::placeholders::_2));
|
||||
m_points.clear();
|
||||
break;
|
||||
}
|
||||
case kEventType::MouseDownL:
|
||||
{
|
||||
node->mouse_capture();
|
||||
m_dragging = true;
|
||||
glm::vec3 ro, rd, hit_o, hit_d;
|
||||
int plane_id;
|
||||
if (canvas->point_trace(loc, ro, rd, hit_o, hit_d, plane_id))
|
||||
{
|
||||
ui::Shape::vertex_t v;
|
||||
v.pos = glm::vec4(hit_o, 1);
|
||||
v.uvs = glm::vec2(0);
|
||||
if (m_points.size() < 3)
|
||||
{
|
||||
m_points.push_back(v);
|
||||
}
|
||||
else
|
||||
{
|
||||
auto last = m_points.back();
|
||||
m_points.push_back(m_points[0]);
|
||||
m_points.push_back(last);
|
||||
m_points.push_back(v);
|
||||
}
|
||||
m_shape.update_vertices(m_points.data(), m_points.size());
|
||||
}
|
||||
break;
|
||||
}
|
||||
case kEventType::MouseUpL:
|
||||
node->mouse_release();
|
||||
m_dragging = false;
|
||||
break;
|
||||
case kEventType::MouseMove:
|
||||
{
|
||||
glm::vec3 ro, rd, hit_o, hit_d;
|
||||
int plane_id;
|
||||
if (m_dragging && canvas->point_trace(loc, ro, rd, hit_o, hit_d, plane_id))
|
||||
{
|
||||
ui::Shape::vertex_t v;
|
||||
v.pos = glm::vec4(hit_o, 1);
|
||||
v.uvs = glm::vec2(0);
|
||||
m_points.back() = v;
|
||||
m_shape.update_vertices(m_points.data(), m_points.size());
|
||||
}
|
||||
break;
|
||||
}
|
||||
case kEventType::MouseCancel:
|
||||
m_dragging = false;
|
||||
node->mouse_release();
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
void CanvasModeFill::on_Draw(const glm::mat4& ortho, const glm::mat4& proj, const glm::mat4& camera)
|
||||
{
|
||||
if (!m_points.empty())
|
||||
{
|
||||
ui::ShaderManager::use(ui::kShader::Color);
|
||||
ui::ShaderManager::u_mat4(ui::kShaderUniform::MVP, proj * camera);
|
||||
ui::ShaderManager::u_vec4(ui::kShaderUniform::Col, { node->m_brush.m_tip_color.rgb(), node->m_brush.m_tip_opacity });
|
||||
m_shape.draw_fill();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -16,6 +16,7 @@ public:
|
||||
virtual void on_GestureEvent(GestureEvent* ge) {}
|
||||
virtual void on_Draw(const glm::mat4& ortho, const glm::mat4& proj, const glm::mat4& camera) {}
|
||||
virtual void init() {}
|
||||
virtual void leave() {}
|
||||
};
|
||||
|
||||
class CanvasModeBasicCamera : public CanvasMode
|
||||
@@ -34,8 +35,6 @@ public:
|
||||
class CanvasModePen : public CanvasMode
|
||||
{
|
||||
bool m_dragging = false;
|
||||
bool m_draggingR = false;
|
||||
glm::vec2 m_dragR_start;
|
||||
glm::vec2 m_pan_start;
|
||||
float m_camera_fov;
|
||||
float m_zoom_canvas = 1.f;
|
||||
@@ -63,6 +62,8 @@ class CanvasModeNormal : public CanvasMode
|
||||
glm::vec3 dir;
|
||||
int m_plane_id;
|
||||
bool m_dragging = false;
|
||||
struct ray_t { glm::vec3 o, d; };
|
||||
std::vector<ray_t> m_lines;
|
||||
public:
|
||||
virtual void on_MouseEvent(MouseEvent* me, glm::vec2& loc) override;
|
||||
virtual void on_Draw(const glm::mat4& ortho, const glm::mat4& proj, const glm::mat4& camera) override;
|
||||
@@ -75,7 +76,18 @@ class CanvasModeCamera : public CanvasMode
|
||||
bool m_dragging = false;
|
||||
glm::vec2 m_drag_start;
|
||||
glm::vec2 m_pos_start;
|
||||
ui::Plane m_face_plane;
|
||||
public:
|
||||
virtual void on_MouseEvent(MouseEvent* me, glm::vec2& loc) override;
|
||||
};
|
||||
|
||||
class CanvasModeFill : public CanvasMode
|
||||
{
|
||||
ui::DynamicShape m_shape;
|
||||
bool m_dragging = false;
|
||||
std::vector<ui::Shape::vertex_t> m_points;
|
||||
public:
|
||||
virtual void on_Draw(const glm::mat4& ortho, const glm::mat4& proj, const glm::mat4& camera) override;
|
||||
virtual void on_MouseEvent(MouseEvent* me, glm::vec2& loc) override;
|
||||
virtual void init() override;
|
||||
virtual void leave() override;
|
||||
};
|
||||
|
||||
@@ -1980,7 +1980,7 @@ public:
|
||||
m_mouse_ignore = false;
|
||||
m_canvas = std::make_unique<ui::Canvas>();
|
||||
m_canvas->create(1024, 1024);
|
||||
m_sampler.create();
|
||||
m_sampler.create(GL_NEAREST);
|
||||
m_face_plane.create<1>(2, 2);
|
||||
m_line.create();
|
||||
CanvasMode::node = this;
|
||||
@@ -1993,7 +1993,7 @@ public:
|
||||
{
|
||||
Node::restore_context();
|
||||
m_canvas->create(1024, 1024);
|
||||
m_sampler.create();
|
||||
m_sampler.create(GL_NEAREST);
|
||||
m_face_plane.create<1>(2, 2);
|
||||
m_canvas->snapshot_restore();
|
||||
CanvasMode::node = this;
|
||||
@@ -2080,6 +2080,11 @@ public:
|
||||
for (auto& mode : *m_canvas->m_mode)
|
||||
mode->on_Draw(ortho_proj, proj, camera);
|
||||
|
||||
// keep drawing the grids
|
||||
if (m_canvas->m_state != ui::Canvas::kCanvasMode::Grid)
|
||||
for (auto& mode : ui::Canvas::modes[(int)ui::Canvas::kCanvasMode::Grid])
|
||||
mode->on_Draw(ortho_proj, proj, camera);
|
||||
|
||||
//ui::ShaderManager::use(kShader::Equirect);
|
||||
//ui::ShaderManager::u_mat4(kShaderUniform::MVP, glm::scale(glm::vec3(.5, .5, 1)));
|
||||
//ui::ShaderManager::u_int(kShaderUniform::Tex, 0);
|
||||
|
||||
@@ -17,6 +17,7 @@
|
||||
// define something for iphone
|
||||
#else
|
||||
#define TARGET_OS_OSX 1
|
||||
#define __OSX__ 1
|
||||
#include <OpenGL/gl3.h>
|
||||
#include <OpenGL/gl3ext.h>
|
||||
#define SHADER_VERSION "#version 150\n"
|
||||
|
||||
@@ -6,6 +6,8 @@ using namespace ui;
|
||||
|
||||
bool Shape::create_buffers(GLvoid* idx, GLvoid* vertices, int isize, int vsize)
|
||||
{
|
||||
use_idx = true;
|
||||
|
||||
glGenBuffers(2, buffers);
|
||||
if (!(buffers[0] && buffers[1]))
|
||||
return false;
|
||||
@@ -35,20 +37,66 @@ bool Shape::create_buffers(GLvoid* idx, GLvoid* vertices, int isize, int vsize)
|
||||
#endif
|
||||
return true;
|
||||
}
|
||||
bool Shape::create_buffers(GLvoid* vertices, int vsize)
|
||||
{
|
||||
use_idx = false;
|
||||
|
||||
glGenBuffers(1, buffers);
|
||||
if (!buffers[0])
|
||||
return false;
|
||||
|
||||
if (vsize)
|
||||
{
|
||||
glBindBuffer(GL_ARRAY_BUFFER, buffers[0]);
|
||||
glBufferData(GL_ARRAY_BUFFER, vsize, vertices, GL_STATIC_DRAW);
|
||||
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
|
||||
glBindBuffer(GL_ARRAY_BUFFER, 0);
|
||||
}
|
||||
|
||||
#if USE_VBO
|
||||
glGenVertexArrays(2, arrays);
|
||||
if (!(arrays[0] && arrays[1]))
|
||||
return false;
|
||||
for (int i = 0; i < 2; i++)
|
||||
{
|
||||
glBindVertexArray(arrays[i]);
|
||||
glEnableVertexAttribArray(0);
|
||||
glEnableVertexAttribArray(1);
|
||||
glBindBuffer(GL_ARRAY_BUFFER, buffers[0]);
|
||||
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));
|
||||
}
|
||||
glBindVertexArray(0);
|
||||
#endif
|
||||
return true;
|
||||
}
|
||||
void Shape::draw_fill() const
|
||||
{
|
||||
GLenum type = GL_TRIANGLES;
|
||||
if (count[0] == 1)
|
||||
type = GL_POINTS;
|
||||
if (count[0] == 2)
|
||||
type = GL_LINES;
|
||||
#if USE_VBO
|
||||
glBindVertexArray(arrays[0]);
|
||||
glDrawElements(GL_TRIANGLES, count[0], GL_UNSIGNED_SHORT, ioff[0]);
|
||||
if (use_idx)
|
||||
glDrawElements(type, count[0], GL_UNSIGNED_SHORT, ioff[0]);
|
||||
else
|
||||
glDrawArrays(type, 0, count[0]);
|
||||
glBindVertexArray(0);
|
||||
#else
|
||||
glEnableVertexAttribArray(0);
|
||||
glEnableVertexAttribArray(1);
|
||||
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, buffers[1]);
|
||||
glBindBuffer(GL_ARRAY_BUFFER, buffers[0]);
|
||||
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]);
|
||||
if (use_idx)
|
||||
{
|
||||
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, buffers[1]);
|
||||
glDrawElements(type, count[0], GL_UNSIGNED_SHORT, ioff[0]);
|
||||
}
|
||||
else
|
||||
glDrawArrays(type, 0, count[0]);
|
||||
glDisableVertexAttribArray(0);
|
||||
glDisableVertexAttribArray(1);
|
||||
glBindBuffer(GL_ARRAY_BUFFER, 0);
|
||||
@@ -57,9 +105,15 @@ void Shape::draw_fill() const
|
||||
}
|
||||
void Shape::draw_stroke() const
|
||||
{
|
||||
GLenum type = GL_LINES;
|
||||
if (count[1] == 1)
|
||||
type = GL_POINTS;
|
||||
#if USE_VBO
|
||||
glBindVertexArray(arrays[1]);
|
||||
glDrawElements(GL_LINES, count[1], GL_UNSIGNED_SHORT, ioff[1]);
|
||||
if (use_idx)
|
||||
glDrawElements(type, count[1], GL_UNSIGNED_SHORT, ioff[1]);
|
||||
else
|
||||
glDrawArrays(type, 0, count[1]);
|
||||
glBindVertexArray(0);
|
||||
#else
|
||||
glEnableVertexAttribArray(0);
|
||||
@@ -68,7 +122,13 @@ void Shape::draw_stroke() const
|
||||
glBindBuffer(GL_ARRAY_BUFFER, buffers[0]);
|
||||
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]);
|
||||
if (use_idx)
|
||||
{
|
||||
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, buffers[1]);
|
||||
glDrawElements(type, count[1], GL_UNSIGNED_SHORT, ioff[1]);
|
||||
}
|
||||
else
|
||||
glDrawArrays(type, 0, count[1]);
|
||||
glDisableVertexAttribArray(0);
|
||||
glDisableVertexAttribArray(1);
|
||||
glBindBuffer(GL_ARRAY_BUFFER, 0);
|
||||
@@ -381,17 +441,6 @@ void Slice9::create_impl(float w, float h, float r, float tr, GLushort *idx, Sha
|
||||
*idx++ = 12; // D
|
||||
*idx++ = 0; // A
|
||||
}
|
||||
void ui::LineSegment::create_impl(GLushort* idx, vertex_t* vertices)
|
||||
{
|
||||
count[0] = 2;
|
||||
count[1] = 2;
|
||||
ioff[0] = (GLvoid*)0;
|
||||
ioff[1] = (GLvoid*)0;
|
||||
vertices[0] = { { 0, 0, 0, 1 }, { 0, 0 } }; // A
|
||||
vertices[1] = { { 0, 0, 0, 1 }, { 0, 1 } }; // B
|
||||
idx[0] = 0;
|
||||
idx[1] = 1;
|
||||
}
|
||||
void ui::LineSegment::update_vertices(const glm::vec4 data[2])
|
||||
{
|
||||
static vertex_t vertices[2];
|
||||
@@ -399,10 +448,13 @@ void ui::LineSegment::update_vertices(const glm::vec4 data[2])
|
||||
vertices[1] = { data[1], { 0, 1 } }; // B
|
||||
glBindBuffer(GL_ARRAY_BUFFER, buffers[0]);
|
||||
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
|
||||
static GLushort idx[4] { 0, 1 };
|
||||
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, buffers[1]);
|
||||
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(idx), idx, GL_STATIC_DRAW);
|
||||
|
||||
glBindBuffer(GL_ARRAY_BUFFER, 0);
|
||||
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
|
||||
}
|
||||
void ui::DynamicShape::update_vertices(vertex_t* vertices, int vcount)
|
||||
{
|
||||
count[0] = vcount;
|
||||
count[1] = vcount;
|
||||
glBindBuffer(GL_ARRAY_BUFFER, buffers[0]);
|
||||
glBufferData(GL_ARRAY_BUFFER, sizeof(vertex_t) * vcount, vertices, GL_STATIC_DRAW);
|
||||
glBindBuffer(GL_ARRAY_BUFFER, 0);
|
||||
}
|
||||
@@ -9,9 +9,11 @@ protected:
|
||||
GLuint arrays[2]{ 0 };
|
||||
GLuint count[2]{ 0 };
|
||||
GLvoid* ioff[2]{ 0 };
|
||||
struct vertex_t { glm::vec4 pos; glm::vec2 uvs; };
|
||||
bool use_idx = true;
|
||||
public:
|
||||
struct vertex_t { glm::vec4 pos; glm::vec2 uvs; };
|
||||
bool create_buffers(GLvoid* idx, GLvoid* vertices, int isize, int vsize);
|
||||
bool create_buffers(GLvoid* vertices, int vsize);
|
||||
void draw_fill() const;
|
||||
void draw_stroke() const;
|
||||
virtual bool create_attrib() { return true; };
|
||||
@@ -49,18 +51,35 @@ protected:
|
||||
|
||||
class LineSegment : public Shape
|
||||
{
|
||||
void create_impl(GLushort* idx, vertex_t* vertices);
|
||||
public:
|
||||
bool create()
|
||||
{
|
||||
static GLushort idx[2];
|
||||
static vertex_t vertices[2];
|
||||
create_impl(idx, vertices);
|
||||
return create_buffers(idx, vertices, sizeof(idx), sizeof(vertices));
|
||||
count[0] = 2;
|
||||
count[1] = 2;
|
||||
ioff[0] = (GLvoid*)0;
|
||||
ioff[1] = (GLvoid*)0;
|
||||
vertices[0] = { { 0, 0, 0, 1 },{ 0, 0 } }; // A
|
||||
vertices[1] = { { 0, 0, 0, 1 },{ 0, 1 } }; // B
|
||||
return create_buffers(vertices, sizeof(vertices));
|
||||
}
|
||||
void update_vertices(const glm::vec4 data[2]);
|
||||
};
|
||||
|
||||
class DynamicShape : public Shape
|
||||
{
|
||||
public:
|
||||
bool create(vertex_t* vertices = nullptr, int vcount = 0)
|
||||
{
|
||||
count[0] = vcount;
|
||||
count[1] = vcount;
|
||||
ioff[0] = (GLvoid*)0;
|
||||
ioff[1] = (GLvoid*)0;
|
||||
return create_buffers(vertices, sizeof(vertex_t) * vcount);
|
||||
}
|
||||
void update_vertices(vertex_t* vertices, int vcount);
|
||||
};
|
||||
|
||||
class Plane : public Shape
|
||||
{
|
||||
void create_impl(float w, float h, int div, GLushort* idx, vertex_t* vertices);
|
||||
|
||||
Reference in New Issue
Block a user