refactor app and enable gles 3.0

This commit is contained in:
2017-03-04 19:04:08 +00:00
parent 11050fde9c
commit a2a221b17a
7 changed files with 127 additions and 83 deletions

View File

@@ -9,10 +9,10 @@ void App::create()
height = 500;
}
void App::init()
void App::initShaders()
{
static const char* shader_v =
"#version 150\n"
"#version 300 es\n"
"uniform mat4 mvp;"
"in vec4 pos;"
"in vec2 uvs;"
@@ -22,7 +22,7 @@ void App::init()
" gl_Position = mvp * vec4(pos.xyz, 1.f);"
"}";
static const char* shader_f =
"#version 150\n"
"#version 300 es\n"
"uniform sampler2D tex;"
"in vec3 uv;"
"out vec4 frag;"
@@ -31,7 +31,7 @@ void App::init()
" frag = texture(tex, uv.xy);"
"}";
static const char* shader_uv_f =
"#version 150\n"
"#version 300 es\n"
"uniform sampler2D tex;"
"in vec3 uv;"
"out vec4 frag;"
@@ -39,7 +39,7 @@ void App::init()
" frag = vec4(uv.xy,0,1);"
"}";
static const char* shader_atlas_v =
"#version 150\n"
"#version 300 es\n"
"uniform mat4 mvp;"
"uniform vec2 tof;"
"uniform vec2 tsz;"
@@ -51,7 +51,7 @@ void App::init()
" gl_Position = mvp * vec4(pos, 0, 1);"
"}";
static const char* shader_atlas_f =
"#version 150\n"
"#version 300 es\n"
"uniform sampler2D tex;"
"in vec2 uv;"
"out vec4 frag;"
@@ -59,21 +59,21 @@ void App::init()
" frag = texture(tex, uv);"
"}";
static const char* shader_color_v =
"#version 150\n"
"#version 300 es\n"
"uniform mat4 mvp;"
"in vec4 pos;"
"void main(){"
" gl_Position = mvp * pos;"
"}";
static const char* shader_color_f =
"#version 150\n"
"#version 300 es\n"
"uniform vec4 col;"
"out vec4 frag;"
"void main(){"
" frag = col;"
"}";
static const char* shader_font_v =
"#version 150\n"
"#version 300 es\n"
"uniform mat4 mvp;"
"in vec2 pos;"
"in vec2 uvs;"
@@ -83,7 +83,7 @@ void App::init()
" gl_Position = mvp * vec4(pos, 0, 1);"
"}";
static const char* shader_font_f =
"#version 150\n"
"#version 300 es\n"
"uniform sampler2D tex;"
"uniform vec4 col;"
"in vec2 uv;"
@@ -93,28 +93,21 @@ void App::init()
" frag = vec4(col.rgb, a);"
"}";
#ifdef _WIN32
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);
#endif
#if _WIN32
if (!ShaderManager::create(kShader::Texture, shader_v, shader_f))
LOG("Failed to create shader Texture");
if (!ShaderManager::create(kShader::Color, shader_color_v, shader_color_f))
LOG("Failed to create shader Color");
if (!ShaderManager::create(kShader::UVs, shader_v, shader_uv_f))
LOG("Failed to create shader UVs");
if (!ShaderManager::create(kShader::Font, shader_font_v, shader_font_f))
LOG("Failed to create shader Font");
if (!ShaderManager::create(kShader::Atlas, shader_atlas_v, shader_atlas_f))
LOG("Failed to create shader Atlas");
}
void App::initAssets()
{
#if _WIN32
const char* ttf = "C:\\Windows\\Fonts\\arial.ttf";
#else
const char* ttf = "/Library/Fonts/Arial.ttf";
@@ -122,6 +115,14 @@ void App::init()
FontManager::init();
FontManager::load(kFont::Arial_11, ttf, 15);
FontManager::load(kFont::Arial_30, ttf, 30);
sampler.create(GL_NEAREST);
if (!tex.load("data/uvs.jpg"))
LOG("error loading image\n");
}
void App::initLayout()
{
NodeBorder::static_init();
NodeImage::static_init();
NodeIcon::static_init();
@@ -191,24 +192,38 @@ void App::init()
}
};
layout.load("data/layout2.xml");
}
//msgbox->find<NodeButton>("btn-ok")->on_click = [] { exit(0); };
// msgbox->find<NodeButton>("btn-ok")->m_text->m_text = "DO";
// msgbox->find<NodeButton>("btn-ok")->m_text->create();
void App::init()
{
#ifdef _WIN32
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]);
LOG("%.*s\n", length, message);
FlushConsoleInputBuffer(GetStdHandle(STD_OUTPUT_HANDLE));
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), info.wAttributes);
}, nullptr);
glEnable(GL_DEBUG_OUTPUT);
#endif
sampler.create(GL_NEAREST);
ShaderManager::create(kShader::Texture, shader_v, shader_f);
ShaderManager::create(kShader::Color, shader_color_v, shader_color_f);
ShaderManager::create(kShader::UVs, shader_v, shader_uv_f);
ShaderManager::create(kShader::Font, shader_font_v, shader_font_f);
ShaderManager::create(kShader::Atlas, shader_atlas_v, shader_atlas_f);
if (!tex.load("data/uvs.jpg"))
printf("error loading image\n");
initShaders();
initAssets();
initLayout();
glEnable(GL_TEXTURE_2D);
glDisable(GL_DEPTH_TEST);
glPointSize(5);
//glPointSize(5);
glLineWidth(2);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
@@ -219,15 +234,15 @@ void App::init()
//for (int i = 0; i < n; i++)
//{
// const unsigned char* s = glGetStringi(GL_EXTENSIONS, i);
// printf("GL ext %03d: %s\n", i, s);
// LOG("GL ext %03d: %s\n", i, s);
//}
printf("GL version: %s\n", glGetString(GL_VERSION));
printf("GL vendor: %s\n", glGetString(GL_VENDOR));
printf("GL renderer: %s\n", glGetString(GL_RENDERER));
LOG("GL version: %s\n", glGetString(GL_VERSION));
LOG("GL vendor: %s\n", glGetString(GL_VENDOR));
LOG("GL renderer: %s\n", glGetString(GL_RENDERER));
GLfloat width_range[2];
glGetFloatv(GL_LINE_WIDTH_RANGE, width_range);
printf("GL line range: %f - %f\n", width_range[0], width_range[1]);
glGetFloatv(GL_ALIASED_LINE_WIDTH_RANGE, width_range);
LOG("GL line range: %f - %f\n", width_range[0], width_range[1]);
}
void App::update(float dt)
@@ -247,7 +262,7 @@ void App::update(float dt)
n->draw();
}
};
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
//glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
glEnable(GL_SCISSOR_TEST);
if (auto* main = layout[main_id])
main->watch(observer);
@@ -269,15 +284,8 @@ void App::mouse_down(int button, float x, float y)
e.m_type = button ? kEventType::MouseDownR : kEventType::MouseDownL;
e.m_pos = { x, y };
layout[main_id]->on_event(&e);
printf("mouse click %f %f\n", x, y);
// NodeBorder* n = new NodeBorder();
// n->SetPositioning(YGPositionTypeAbsolute);
// n->SetWidth(100);
// n->SetHeight(100);
// n->SetPosition(x, y);
// n->m_color = glm::vec4(.5, .5, .5, .5);
// layout[main_id].add_child(n);
// layout[main_id].update(width, height);
LOG("mouse click %f %f\n", x, y);
if (popup)
{
layout[main_id]->remove_child(popup);