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

@@ -33,10 +33,12 @@ add_library(
../engine/shader.cpp
../engine/shape.cpp
../engine/layout.cpp
../engine/app.cpp
)
target_include_directories(native-lib PRIVATE
${ANDROID_NDK}/sources/android/native_app_glue
../engine
../libs/glm
../libs/tinyxml2
../libs/yoga

View File

@@ -22,15 +22,8 @@
#include <errno.h>
#include <cassert>
#include <EGL/egl.h>
#include <GLES3/gl3.h>
#include <android/sensor.h>
#include <android/log.h>
#include <android_native_app_glue.h>
#define LOGI(...) ((void)__android_log_print(ANDROID_LOG_INFO, "native-activity", __VA_ARGS__))
#define LOGW(...) ((void)__android_log_print(ANDROID_LOG_WARN, "native-activity", __VA_ARGS__))
#include "pch.h"
#include "app.h"
/**
* Our saved state data.
@@ -60,6 +53,8 @@ struct engine {
struct saved_state state;
};
Plane plane;
/**
* Initialize an EGL context for the current display.
*/
@@ -120,10 +115,16 @@ static int engine_init_display(struct engine* engine) {
* ANativeWindow buffers to match, using EGL_NATIVE_VISUAL_ID. */
eglGetConfigAttrib(display, config, EGL_NATIVE_VISUAL_ID, &format);
surface = eglCreateWindowSurface(display, config, engine->app->window, NULL);
context = eglCreateContext(display, config, NULL, NULL);
const EGLint attribs_test[] = {
EGL_CONTEXT_CLIENT_VERSION, 3,
EGL_NONE
};
context = eglCreateContext(display, config, EGL_NO_CONTEXT, attribs_test);
if (eglMakeCurrent(display, surface, surface, context) == EGL_FALSE) {
LOGW("Unable to eglMakeCurrent");
LOG("Unable to eglMakeCurrent");
return -1;
}
@@ -141,13 +142,23 @@ static int engine_init_display(struct engine* engine) {
auto opengl_info = {GL_VENDOR, GL_RENDERER, GL_VERSION, GL_EXTENSIONS};
for (auto name : opengl_info) {
auto info = glGetString(name);
LOGI("OpenGL Info: %s", info);
LOG("OpenGL Info: %s", info);
}
// Initialize GL state.
//glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_FASTEST);
glEnable(GL_CULL_FACE);
//glEnable(GL_CULL_FACE);
//glShadeModel(GL_SMOOTH);
glDisable(GL_DEPTH_TEST);
//glEnableClientState(GL_VERTEX_ARRAY);
App::I.width = w;
App::I.height = h;
App::I.initShaders();
if (!plane.create<1>(1,1))
LOG("Failed to create the plane mesh");
LOG("All ready");
return 0;
}
@@ -166,6 +177,13 @@ static void engine_draw_frame(struct engine* engine) {
((float)engine->state.y)/engine->height, 1);
glClear(GL_COLOR_BUFFER_BIT);
glm::mat4 mvp = glm::ortho<float>(-1, 1, -1, 1, -1, 1);
//App::I.update(now - _prevTime);
ShaderManager::use(kShader::Color);
ShaderManager::u_mat4(kShaderUniform::MVP, mvp);
ShaderManager::u_vec4(kShaderUniform::Col, glm::vec4(1, 0, 0, 1));
plane.draw_fill();
eglSwapBuffers(engine->display, engine->surface);
}
@@ -283,6 +301,8 @@ void android_main(struct android_app* state) {
engine.state = *(struct saved_state*)state->savedState;
}
//App::I.create();
// loop waiting for stuff to do.
while (1) {
@@ -308,9 +328,9 @@ void android_main(struct android_app* state) {
ASensorEvent event;
while (ASensorEventQueue_getEvents(engine.sensorEventQueue,
&event, 1) > 0) {
LOGI("accelerometer: x=%f y=%f z=%f",
event.acceleration.x, event.acceleration.y,
event.acceleration.z);
// LOGI("accelerometer: x=%f y=%f z=%f",
// event.acceleration.x, event.acceleration.y,
// event.acceleration.z);
}
}
}

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);

View File

@@ -23,6 +23,9 @@ public:
float width;
float height;
void init();
void initShaders();
void initAssets();
void initLayout();
void create();
void update(float dt);
void resize(float w, float h);

View File

@@ -483,10 +483,10 @@ bool LayoutManager::load(const char* path)
auto id_str = current->Attribute("id");
if (!id_str)
{
printf("Layout node without id\n");
LOG("Layout node without id\n");
return false;
}
printf("Parsing layout: %s\n", id_str);
LOG("Parsing layout: %s\n", id_str);
uint16_t id = const_hash(id_str);
auto p = m_layouts.find(id);
if (p == m_layouts.end())
@@ -513,7 +513,7 @@ bool LayoutManager::load(const char* path)
}
else
{
printf("Layout id \"%s\" duplicated\n", id_str);
LOG("Layout id \"%s\" duplicated\n", id_str);
}
current = current->NextSiblingElement("layout");
}

View File

@@ -354,9 +354,9 @@ int main()
if (glewInit() != GLEW_OK)
return 0;
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));
// If supported create a 3.1 context
if (wglewIsSupported("WGL_ARB_create_context"))

View File

@@ -3,9 +3,18 @@
#ifdef __APPLE__
#include <OpenGL/gl3.h>
#include <sys/stat.h>
#define LOG printf
#elif __ANDROID__
#include <EGL/egl.h>
#include <GLES3/gl3.h>
#include <sys/stat.h>
#include <android/sensor.h>
#include <android/log.h>
#include <android_native_app_glue.h>
#define LOG(...) ((void)__android_log_print(ANDROID_LOG_INFO, "native-engine", __VA_ARGS__))
#elif _WIN32
#define _USE_MATH_DEFINES
#define _CRT_SECURE_NO_WARNINGS
@@ -13,6 +22,8 @@
#include <gl\glew.h>
#include <gl\wglew.h>
#include <gl\GL.h>
#define LOG printf
#endif
#ifdef __cplusplus