Files
panopainter/engine/app.cpp

174 lines
4.8 KiB
C++

#include "pch.h"
#include "app.hpp"
App App::I; // singleton
void App::create()
{
width = 500;
height = 500;
}
void App::init()
{
static const char* shader_v =
"#version 150\n"
"uniform mat4 mvp;"
"in vec4 pos;"
"in vec2 uvs;"
"out vec3 uv;"
"void main(){"
" uv = vec3(uvs, pos.w);"
" gl_Position = mvp * vec4(pos.xyz, 1.f);"
"}";
static const char* shader_f =
"#version 150\n"
"uniform sampler2D tex;"
"in vec3 uv;"
"out vec4 frag;"
"void main(){"
//" frag = texture(tex, uv.xy/uv.z);"
" frag = texture(tex, uv.xy);"
"}";
static const char* shader_uv_f =
"#version 150\n"
"uniform sampler2D tex;"
"in vec3 uv;"
"out vec4 frag;"
"void main(){"
" frag = vec4(uv.xy,0,1);"
"}";
static const char* shader_color_v =
"#version 150\n"
"uniform mat4 mvp;"
"in vec4 pos;"
"void main(){"
" gl_Position = mvp * pos;"
"}";
static const char* shader_color_f =
"#version 150\n"
"uniform vec4 col;"
"out vec4 frag;"
"void main(){"
" frag = col;"
"}";
#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
layout.load("data/layout.xml");
layout.update(width, height);
sampler.create();
shader.create(shader_v, shader_f);
shader_color.create(shader_color_v, shader_color_f);
shader_uv.create(shader_v, shader_uv_f);
plane.create<5>(1, 1);
if (!tex.load("data/uvs.jpg"))
printf("error loading image\n");
glEnable(GL_TEXTURE_2D);
glDisable(GL_DEPTH_TEST);
glPointSize(5);
glLineWidth(2);
//int n;
//glGetIntegerv(GL_NUM_EXTENSIONS, &n);
//for (int i = 0; i < n; i++)
//{
// const unsigned char* s = glGetStringi(GL_EXTENSIONS, i);
// printf("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));
GLfloat width_range[2];
glGetFloatv(GL_LINE_WIDTH_RANGE, width_range);
printf("GL line range: %f - %f\n", width_range[0], width_range[1]);
}
void App::update(float dt)
{
glm::mat4 proj = glm::ortho(0.f, width, height, 0.f, -1.f, 1.f);
glClearColor(.1f, .1f, .1f, 1.f);
glViewport(0, 0, (GLsizei)width, (GLsizei)height);
glClear(GL_COLOR_BUFFER_BIT);
layout.reload();
glActiveTexture(GL_TEXTURE0);
tex.bind();
sampler.bind(0);
shader.use();
shader.u_int("tex", 0);
shader_color.use();
shader_color.u_vec4("col", { .3f, .3f, .3f, 1 });
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
glEnable(GL_SCISSOR_TEST);
for (auto& n : layout)
{
glm::mat4 pivot = glm::translate(glm::vec3(.5f, .5f, 0.f));
glm::mat4 scale = glm::scale(glm::vec3(n.m_size, 1.f));
glm::mat4 pos = glm::translate(glm::vec3(n.m_pos, 0));
auto mvp = proj * pos * scale * pivot;
auto box = n.m_clip;
glScissor(box.x, height - box.y - box.w, box.z, box.w);
shader_color.u_vec4("col", n.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();
}
glDisable(GL_SCISSOR_TEST);
tex.unbind();
sampler.unbind();
}
void App::resize(float w, float h)
{
width = w;
height = h;
layout.update(width, height);
}
void App::mouse_down(int button, float x, float y)
{
printf("mouse click %f %f\n", x, y);
}
void App::mouse_move(float x, float y)
{
}
void App::mouse_up(int button, float x, float y)
{
}