Skip to content

Instantly share code, notes, and snippets.

@ShawnMcGrath
Created September 26, 2016 18:14
Show Gist options
  • Save ShawnMcGrath/8cc4f079da36284a9a8e16c8af8c96c5 to your computer and use it in GitHub Desktop.
Save ShawnMcGrath/8cc4f079da36284a9a8e16c8af8c96c5 to your computer and use it in GitHub Desktop.
#define MG_PLATFORM_IMPLEMENTATION
#define MG_TOOL_IMPLEMENTATION
#define MGGL_IMPLEMENTATION
#define MG_PLATFORM_OPENGL
#define MG_PLATFORM_OPENGL_DEBUG
#define MGGL_DEBUG
#define MG_PLATFORM_LOOP gameLoop
#define MG_PLATFORM_INIT initGame
#define MG_PLATFORM_CONFIG configGame
#include "mg_tool.h"
#include "mg_platform.h"
#include "mg_gl.h"
#pragma comment(lib, "OpenGL32.lib")
mgPlatform *gp;
void configGame(mgPlatformConfig *cfg, const mgPlatformInfo *info) {
cfg->window_width = 1600;
cfg->window_height = 900;
}
int initGame(struct mgPlatform *platform, const char *argv) {
gp = platform;
mggl_bsInit(platform->window_width, platform->window_height);
return 0;
}
mgVec2 bezier_pts[4];
int editing_pt_id;
int editing_line_pt_id;
r32 length[2] = { 100.f, 100.f };
r32 length_dist_fact = 0.5f;
struct Line {
mgVec2 pts[2];
};
Line lines[2] = {
{ 100.f, 100.f, 300.f, 250.f },
{ 1000.f, 100.f, 800.f, 450.f },
};
int gameLoop(r32 dt) {
{ //input
mg_fei(bezier_pts) {
if (gp->input_state.key_state[mgKey_1 + i] & mgButtonState_Press) {
editing_pt_id = i;
editing_line_pt_id = -1;
}
}
if (gp->input_state.key_state['A'] & mgButtonState_Press) {
editing_pt_id = -1;
editing_line_pt_id = 0;
}
if (gp->input_state.key_state['S'] & mgButtonState_Press) {
editing_pt_id = -1;
editing_line_pt_id = 1;
}
if (gp->input_state.key_state['D'] & mgButtonState_Press) {
editing_pt_id = -1;
editing_line_pt_id = 2;
}
if (gp->input_state.key_state['F'] & mgButtonState_Press) {
editing_pt_id = -1;
editing_line_pt_id = 3;
}
if (editing_pt_id >= 0 && editing_pt_id < MG_ARR_LEN(bezier_pts)) {
if (gp->input_state.mouse_buttons[0] & mgButtonState_Down) {
bezier_pts[editing_pt_id] = gp->input_state.mouse_pos;
}
} else if (editing_line_pt_id != -1) {
mgVec2 *line_pts = (mgVec2 *)lines;
if (gp->input_state.mouse_buttons[0] & mgButtonState_Down) {
line_pts[editing_line_pt_id] = gp->input_state.mouse_pos;
}
}
//int len_id = (gp->input_state.key_state[mgKey_ShiftLeft] & mgButtonState_Down) ? 1 : 0;
//length[len_id] += (r32)gp->input_state.mouse_wheel * 10.f;
length_dist_fact += (r32)gp->input_state.mouse_wheel * 0.025f;
}
r32 len_between_end_points = mg_vec2Len(lines[0].pts[1] - lines[1].pts[1]);
mgVec2 v0 = mg_vec2GetUnit(lines[0].pts[0] - lines[0].pts[1]);
mgVec2 v1 = mg_vec2GetUnit(lines[1].pts[0] - lines[1].pts[1]);
r32 dp = mg_vec2Dot(v0, v1);
len_between_end_points /= mg_smootherStep(0.f, 1.f, mg_abs32(dp));
bezier_pts[0] = lines[0].pts[1];
// bezier_pts[1] = lines[0].pts[1] - v0 * length[0];
// bezier_pts[2] = lines[1].pts[1] - v1 * length[1];
bezier_pts[1] = lines[0].pts[1] - v0 * len_between_end_points * length_dist_fact;
bezier_pts[2] = lines[1].pts[1] - v1 * len_between_end_points * length_dist_fact;
bezier_pts[3] = lines[1].pts[1];
mggl_bsSetRes(gp->window_width, gp->window_height);
mggl_bsSetFSViewport();
mggl_clearScreen(mg_col_black);
glUseProgram(mggl_state->ortho_col_shader.program);
glUniformMatrix4fv(mggl_state->ortho_col_shader.getUniform("ortho_mat"), 1, GL_FALSE, mggl_state->ortho_mat);
mggl_uniformFloatColour(mggl_state->ortho_col_shader.getUniform("in_colour"), mg_col_white);
{ //draw curve
int num_subdivisions = 512;
glBegin(GL_LINE_STRIP);
mg_fiz(num_subdivisions) {
r32 t = (r32)i / (r32)(num_subdivisions - 1);
mgVec2 p = mg_bezierCubic2Df(t, bezier_pts);
glVertex2fv((r32 *)&p);
}
glEnd();
}
{ //draw tangent handles
mggl_uniformFloatColour(mggl_state->ortho_col_shader.getUniform("in_colour"), mg_col_yellow);
glBegin(GL_LINES);
mg_fei(bezier_pts) {
glVertex2fv((r32 *)(bezier_pts + i));
}
glEnd();
}
{ //draw lines
mggl_uniformFloatColour(mggl_state->ortho_col_shader.getUniform("in_colour"), mg_col_red);
glBegin(GL_LINES);
mgVec2 *line_pts = (mgVec2 *)lines;
mg_fiz(4) {
glVertex2fv((r32 *)(line_pts + i));
}
glEnd();
}
{ //ui
int font_size = 24;
int font_spacing = (int)((r32)font_size * 1.25f);
mgglFont *font = mggl_getFont("Arial", font_size);
int top = gp->window_height - font_spacing;
int x = 10;
int y = top;
mggl_drawString(font, x, y, mg_col_white, "Editing curve point: %d", editing_pt_id); y -= font_spacing;
mggl_drawString(font, x, y, mg_col_white, "Editing line point: %d", editing_line_pt_id); y -= font_spacing;
mggl_drawString(font, x, y, mg_col_white, "dot product: %f", dp); y -= font_spacing;
mggl_drawString(font, x, y, mg_col_white, "smooth dot product: %f", mg_smootherStep(0.f, 1.f, dp)); y -= font_spacing;
/*mg_fei(bezier_pts) {
mggl_drawString(font, bezier_pts[i].x, bezier_pts[i].y, mg_col_white, "%d (%3.2f, %3.2f)", i + 1, bezier_pts[i].x, bezier_pts[i].y);
}*/
const char *fuck = "ASDF";
mgVec2 *line_pts = (mgVec2 *)lines;
mg_fei(fuck) {
mggl_drawString(font, line_pts[i].x, line_pts[i].y, mg_col_white, "%c (%3.2f, %3.2f)", fuck[i], line_pts[i].x, line_pts[i].y);
}
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment