added color attribute and scissor clipping. Added debug callback to OpenGL messages

This commit is contained in:
2017-01-24 20:05:19 +00:00
parent 5268f65777
commit 9d6171c7d8
5 changed files with 86 additions and 26 deletions

View File

@@ -5,8 +5,8 @@ App App::I; // singleton
void App::create()
{
width = 800;
height = 800;
width = 500;
height = 500;
}
void App::update_layout()
@@ -99,18 +99,18 @@ void App::load_layout()
//YGNodeStyleSetPositionType(y_root, YGPositionTypeRelative);
while (child)
{
printf("Element %s: ", child->Name());
auto y_node = YGNodeNew();
stack.emplace(y_node, child);
auto attr = child->FirstAttribute();
std::unique_ptr<Plane> shape;
if (strcmp("plane", child->Name()) == 0)
{
auto shape = std::make_unique<Plane>();
//shape->create<5>(100.f, 100.f);
shape = std::make_unique<Plane>();
shape->create<5>(100.f, 100.f);
shape->y_node = y_node;
shapes_list.push_back(std::move(shape));
YGNodeSetContext(y_node, shape.get());
}
printf("Element %s: ", child->Name());
YGNodeStyleSetWidth(y_node, 0);
YGNodeStyleSetWidthPercent(y_node, 100);
YGNodeStyleSetHeight(y_node, 0);
@@ -222,6 +222,22 @@ void App::load_layout()
}
break;
}
case att::kAttribute::Color:
{
glm::vec4 pad;
int n = sscanf(attr->Value(), "%f %f %f %f", &pad.x, &pad.y, &pad.z, &pad.w);
if (n == 1)
{
if (shape)
shape->color = glm::vec4(pad.x);
}
else
{
if (shape)
shape->color = pad;
}
break;
}
default:
break;
}
@@ -229,6 +245,8 @@ void App::load_layout()
}
int n = YGNodeGetChildCount(y_root);
YGNodeInsertChild(y_root, y_node, n);
if (shape)
shapes_list.push_back(std::move(shape));
child = child->NextSiblingElement();
printf("\n");
}
@@ -281,6 +299,25 @@ void App::init()
" frag = col;"
"}";
static CONSOLE_SCREEN_BUFFER_INFO info;
GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &info);
// colors: http://stackoverflow.com/questions/4053837/colorizing-text-in-the-console-with-c
glDebugMessageCallback([](GLenum source, GLenum type, GLuint id,
GLenum severity, GLsizei length, const GLchar* message, const void* userParam)
{
static std::map<GLenum, int> colors = {
{ GL_DEBUG_SEVERITY_NOTIFICATION, 8 },
{ GL_DEBUG_SEVERITY_LOW, 8 },
{ GL_DEBUG_SEVERITY_MEDIUM, FOREGROUND_GREEN | FOREGROUND_INTENSITY },
{ GL_DEBUG_SEVERITY_HIGH, FOREGROUND_RED | FOREGROUND_INTENSITY },
};
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), colors[severity]);
printf("%.*s\n", length, message);
FlushConsoleInputBuffer(GetStdHandle(STD_OUTPUT_HANDLE));
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), info.wAttributes);
}, nullptr);
glEnable(GL_DEBUG_OUTPUT);
load_layout();
for (auto& s : shapes_list)
@@ -306,10 +343,10 @@ void App::init()
if (!tex.load("data/uvs.jpg"))
printf("error loading image\n");
glEnable(GL_TEXTURE);
glEnable(GL_TEXTURE_2D);
glDisable(GL_DEPTH_TEST);
glPointSize(5);
glLineWidth(4);
glLineWidth(2);
//int n;
//glGetIntegerv(GL_NUM_EXTENSIONS, &n);
@@ -383,15 +420,22 @@ void App::update(float dt)
shader_color.use();
shader_color.u_vec4("col", { .3f, .3f, .3f, 1 });
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
for (auto& s : shapes_list)
{
float w = YGNodeLayoutGetWidth(s->y_node);
float h = YGNodeLayoutGetHeight(s->y_node);
float x = YGNodeLayoutGetLeft(s->y_node);
float y = YGNodeLayoutGetTop(s->y_node);
auto layout = *reinterpret_cast<glm::vec4*>(YGNodeGetContext(s->y_node));
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
auto y_parent = YGNodeGetParent(s->y_node);
if (y_parent && YGNodeGetContext(y_parent))
{
auto box = *reinterpret_cast<glm::vec4*>(YGNodeGetContext(y_parent));
glEnable(GL_SCISSOR_TEST);
glScissor(box.x, height - box.y - 1 - box.w, box.z, box.w);
}
else
{
glDisable(GL_SCISSOR_TEST);
}
//glScissor(10, height - 10 - 1 - 50, 480, 50);
glm::mat4 pivot = glm::translate(glm::vec3(.5f, .5f, 0.f));
glm::mat4 scale = glm::scale(glm::vec3(layout.zw(), 1.f));
@@ -402,6 +446,12 @@ void App::update(float dt)
//shader_uv.u_mat4("mvp", mvp);
//plane.draw_fill();
shader_color.u_vec4("col", s->color);
shader_color.use();
shader_color.u_mat4("mvp", mvp);
plane.draw_fill();
shader_color.u_vec4("col", { 1, 1, 1, 1 });
shader_color.use();
shader_color.u_mat4("mvp", mvp);
plane.draw_stroke();

View File

@@ -62,8 +62,8 @@ bool Shader::create(std::string vertex, std::string fragment)
glBindAttribLocation(ps, 1, "uvs");
glLinkProgram(ps);
glGetShaderiv(ps, GL_LINK_STATUS, &status);
glGetShaderInfoLog(ps, sizeof(infolog), &infolen, infolog);
glGetProgramiv(ps, GL_LINK_STATUS, &status);
glGetProgramInfoLog(ps, sizeof(infolog), &infolen, infolog);
if (infolen > 0)
printf("LINK SHADER:\n%s", infolog);
if (status == 0)

View File

@@ -36,11 +36,13 @@ void Shape::draw_fill() const
{
glBindVertexArray(arrays[0]);
glDrawElements(GL_TRIANGLES, count[0], GL_UNSIGNED_SHORT, ioff[0]);
glBindVertexArray(0);
}
void Shape::draw_stroke() const
{
glBindVertexArray(arrays[1]);
glDrawElements(GL_LINES, count[1], GL_UNSIGNED_SHORT, ioff[1]);
glBindVertexArray(0);
}
bool Rect::create(float w, float h)

View File

@@ -7,7 +7,8 @@ namespace att
Height, MinHeight, MaxHeight,
Divisions, InnerRadius, OuterRadius,
Grow, Shrink, FlexDir, FlexWrap,
Padding, Margin
Padding, Margin,
Color
};
struct AttributeBase
@@ -47,7 +48,8 @@ namespace att
{ "dir", kAttribute::FlexDir },
{ "wrap", kAttribute::FlexWrap },
{ "pad", kAttribute::Padding },
{ "margin", kAttribute::Margin},
{ "margin", kAttribute::Margin },
{ "color", kAttribute::Color },
};
constexpr int map_size = sizeof(map) / sizeof(typemap) - 1;
constexpr bool same(const char* a, const char* b)
@@ -88,6 +90,7 @@ namespace att
DECLARE_ATTRIBUTE(FlexWrap, int);
DECLARE_ATTRIBUTE(Padding, glm::vec4);
DECLARE_ATTRIBUTE(Margin, glm::vec4);
DECLARE_ATTRIBUTE(Color, glm::vec4);
#undef DECLARE_ATTRIBUTE
}
@@ -95,19 +98,24 @@ namespace att
class Shape
{
protected:
GLuint buffers[2];
GLuint arrays[2];
GLuint count[2];
GLvoid* ioff[2];
GLuint buffers[2]{ 0 };
GLuint arrays[2]{ 0 };
GLuint count[2]{ 0 };
GLvoid* ioff[2]{ 0 };
struct vertex_t { glm::vec4 pos; glm::vec2 uvs; };
public:
YGNodeRef y_node;
float x, y;
glm::vec4 color;
att::AttrubutesMap attribs;
bool create_buffers(GLvoid* idx, GLvoid* vertices, int isize, int vsize);
void draw_fill() const;
void draw_stroke() const;
virtual bool create_attrib() { return true; };
~Shape()
{
glDeleteBuffers(2, buffers);
glDeleteVertexArrays(2, arrays);
}
protected:
glm::vec2 quad_mid_point(glm::vec2 a, glm::vec2 b, glm::vec2 c, glm::vec2 d)
{