feat: parameterize stroke color and thickness
This commit is contained in:
@@ -1,10 +1,10 @@
|
|||||||
#version 320 es
|
#version 320 es
|
||||||
precision mediump float;
|
precision mediump float;
|
||||||
|
|
||||||
uniform vec4 uColor; // RGBA
|
in vec4 vColor;
|
||||||
|
|
||||||
out vec4 FragColor;
|
out vec4 FragColor;
|
||||||
|
|
||||||
void main() {
|
void main()
|
||||||
FragColor = uColor;
|
{
|
||||||
|
FragColor = vColor;
|
||||||
}
|
}
|
||||||
@@ -2,10 +2,14 @@
|
|||||||
precision mediump float;
|
precision mediump float;
|
||||||
|
|
||||||
layout(location = 0) in vec2 aPos;
|
layout(location = 0) in vec2 aPos;
|
||||||
|
layout (location = 1) in vec4 aColor;
|
||||||
|
|
||||||
uniform mat4 uProjection;
|
uniform mat4 uProjection;
|
||||||
|
|
||||||
|
out vec4 vColor;
|
||||||
|
|
||||||
void main()
|
void main()
|
||||||
{
|
{
|
||||||
gl_Position = uProjection * vec4(aPos, 0.0, 1.0);
|
gl_Position = uProjection * vec4(aPos, 0.0, 1.0);
|
||||||
|
vColor = aColor;
|
||||||
}
|
}
|
||||||
@@ -4,6 +4,7 @@
|
|||||||
#include "src/core/primitives.h"
|
#include "src/core/primitives.h"
|
||||||
#include "src/core/raster.h"
|
#include "src/core/raster.h"
|
||||||
#include "src/ui/vektorcanvas.h"
|
#include "src/ui/vektorcanvas.h"
|
||||||
|
#include "src/util/color.h"
|
||||||
|
|
||||||
typedef struct button_tool_set_data {
|
typedef struct button_tool_set_data {
|
||||||
GtkRevealer* revealer;
|
GtkRevealer* revealer;
|
||||||
@@ -19,7 +20,7 @@ static void appstate_set_tool(GtkButton* button, gpointer user_data) {
|
|||||||
gtk_revealer_set_reveal_child(data->revealer, FALSE);
|
gtk_revealer_set_reveal_child(data->revealer, FALSE);
|
||||||
|
|
||||||
// setting tool also resets selected primitive
|
// setting tool also resets selected primitive
|
||||||
data->state->selectedPrimitive = NULL;
|
data->state->selectedShape = NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
static void appstate_reveal_subtools(GtkButton* button, gpointer user_data) {
|
static void appstate_reveal_subtools(GtkButton* button, gpointer user_data) {
|
||||||
@@ -57,32 +58,32 @@ void vektor_appstate_canvas_click(VektorAppState* state, double x, double y) {
|
|||||||
begin_click_dispatch:
|
begin_click_dispatch:
|
||||||
if (state->selectedTool == VektorLineTool) {
|
if (state->selectedTool == VektorLineTool) {
|
||||||
// create new polyline primitive if none is selected
|
// create new polyline primitive if none is selected
|
||||||
if (state->selectedPrimitive == NULL) {
|
if (state->selectedShape == NULL) {
|
||||||
|
|
||||||
VektorPolyline* line = vektor_polyline_new();
|
VektorPolyline* line = vektor_polyline_new();
|
||||||
VektorPrimitive linePrimitive =
|
VektorPrimitive linePrimitive =
|
||||||
(VektorPrimitive){.kind = VEKTOR_POLYLINE, .polyline = line};
|
(VektorPrimitive){.kind = VEKTOR_POLYLINE, .polyline = line};
|
||||||
vektor_primitivebuffer_add_primitive(state->primitiveBuffer,
|
VektorStyle style =
|
||||||
linePrimitive);
|
(VektorStyle){.stroke_color = (VektorColor){255, 0, 0, 1},
|
||||||
|
.stroke_width = 0.01};
|
||||||
|
VektorShape shape = (VektorShape){
|
||||||
|
.primitive = linePrimitive, .z_index = 0, .style = style};
|
||||||
|
vektor_shapebuffer_add_shape(state->shapeBuffer, shape);
|
||||||
|
|
||||||
state->selectedPrimitive =
|
state->selectedShape =
|
||||||
&(state->primitiveBuffer
|
&(state->shapeBuffer->shapes[state->shapeBuffer->count - 1]);
|
||||||
->primitives[state->primitiveBuffer->count - 1]);
|
|
||||||
|
|
||||||
} else if (state->selectedPrimitive->kind != VEKTOR_POLYLINE) {
|
} else if (state->selectedShape->primitive.kind != VEKTOR_POLYLINE) {
|
||||||
// selecting a tool resets the selection, so this condition
|
// selecting a tool resets the selection, so this condition
|
||||||
// should not happen
|
// should not happen
|
||||||
g_warning("Invalid selected primitive; polyline expected");
|
g_warning("Invalid selected primitive; polyline expected");
|
||||||
state->selectedPrimitive = NULL;
|
state->selectedShape = NULL;
|
||||||
goto begin_click_dispatch; // retry
|
goto begin_click_dispatch; // retry
|
||||||
}
|
}
|
||||||
|
|
||||||
vektor_polyline_add_point(state->selectedPrimitive->polyline, pos);
|
vektor_polyline_add_point(state->selectedShape->primitive.polyline,
|
||||||
|
pos);
|
||||||
}
|
}
|
||||||
|
|
||||||
vektor_framebuffer_rasterize(state->frameBuffer, state->primitiveBuffer);
|
|
||||||
vektor_canvas_drawfrom(state->frameBuffer, state->canvas);
|
|
||||||
vektor_canvas_update(state->canvas);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void vektor_appstate_new(VektorWidgetState* wstate, VektorAppState* stateOut) {
|
void vektor_appstate_new(VektorWidgetState* wstate, VektorAppState* stateOut) {
|
||||||
@@ -92,12 +93,10 @@ void vektor_appstate_new(VektorWidgetState* wstate, VektorAppState* stateOut) {
|
|||||||
data_linetool->revealer = wstate->workspaceRevealerShapes;
|
data_linetool->revealer = wstate->workspaceRevealerShapes;
|
||||||
|
|
||||||
// populate appstate
|
// populate appstate
|
||||||
stateOut->primitiveBuffer = malloc(sizeof(VektorPrimitiveBuffer));
|
stateOut->shapeBuffer = malloc(sizeof(VektorShapeBuffer));
|
||||||
*stateOut->primitiveBuffer = (VektorPrimitiveBuffer){0};
|
*stateOut->shapeBuffer = (VektorShapeBuffer){0};
|
||||||
stateOut->frameBuffer = malloc(sizeof(VektorFramebuffer));
|
|
||||||
*stateOut->frameBuffer = vektor_framebuffer_new(400, 400);
|
|
||||||
stateOut->canvas = malloc(sizeof(VektorCanvas));
|
stateOut->canvas = malloc(sizeof(VektorCanvas));
|
||||||
vektor_canvas_init(wstate, stateOut->canvas, stateOut->primitiveBuffer);
|
vektor_canvas_init(wstate, stateOut->canvas, stateOut->shapeBuffer);
|
||||||
|
|
||||||
// link all the buttons
|
// link all the buttons
|
||||||
g_signal_connect(G_OBJECT(wstate->workspaceButtonLinetool), "clicked",
|
g_signal_connect(G_OBJECT(wstate->workspaceButtonLinetool), "clicked",
|
||||||
|
|||||||
@@ -10,12 +10,10 @@ typedef enum VektorAppTool { VektorLineTool } VektorAppTool;
|
|||||||
|
|
||||||
typedef struct VektorAppState {
|
typedef struct VektorAppState {
|
||||||
VektorAppTool selectedTool;
|
VektorAppTool selectedTool;
|
||||||
VektorPrimitive* selectedPrimitive;
|
VektorShape* selectedShape;
|
||||||
|
|
||||||
// Logic space
|
// Logic space
|
||||||
VektorPrimitiveBuffer* primitiveBuffer;
|
VektorShapeBuffer* shapeBuffer;
|
||||||
// Pixel space
|
|
||||||
VektorFramebuffer* frameBuffer;
|
|
||||||
// View space
|
// View space
|
||||||
VektorCanvas* canvas;
|
VektorCanvas* canvas;
|
||||||
|
|
||||||
|
|||||||
@@ -46,12 +46,12 @@ void vektor_polygon_free(VektorPolygon* pg) {
|
|||||||
free(pg);
|
free(pg);
|
||||||
}
|
}
|
||||||
|
|
||||||
void vektor_primitivebuffer_add_primitive(VektorPrimitiveBuffer* buffer,
|
void vektor_shapebuffer_add_shape(VektorShapeBuffer* buffer,
|
||||||
VektorPrimitive prim) {
|
VektorShape shape) {
|
||||||
if (buffer->count >= buffer->capacity) {
|
if (buffer->count >= buffer->capacity) {
|
||||||
buffer->capacity = buffer->capacity ? buffer->capacity * 2 : 4;
|
buffer->capacity = buffer->capacity ? buffer->capacity * 2 : 4;
|
||||||
buffer->primitives = realloc(
|
buffer->shapes =
|
||||||
buffer->primitives, sizeof(VektorPrimitive) * buffer->capacity);
|
realloc(buffer->shapes, sizeof(VektorShape) * buffer->capacity);
|
||||||
}
|
}
|
||||||
buffer->primitives[buffer->count++] = prim;
|
buffer->shapes[buffer->count++] = shape;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,15 +1,11 @@
|
|||||||
#ifndef PRIMITIVES_H_
|
#ifndef PRIMITIVES_H_
|
||||||
#define PRIMITIVES_H_
|
#define PRIMITIVES_H_
|
||||||
|
|
||||||
|
#include "src/util/color.h"
|
||||||
#include "stddef.h"
|
#include "stddef.h"
|
||||||
#include "stdlib.h"
|
#include "stdlib.h"
|
||||||
#include "vector.h"
|
#include "vector.h"
|
||||||
|
|
||||||
typedef struct {
|
|
||||||
V2 p1;
|
|
||||||
V2 p2;
|
|
||||||
} VektorLine;
|
|
||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
V2* points;
|
V2* points;
|
||||||
size_t count;
|
size_t count;
|
||||||
@@ -28,7 +24,6 @@ typedef struct {
|
|||||||
} VektorCircle;
|
} VektorCircle;
|
||||||
|
|
||||||
typedef enum {
|
typedef enum {
|
||||||
VEKTOR_LINE,
|
|
||||||
VEKTOR_POLYLINE,
|
VEKTOR_POLYLINE,
|
||||||
VEKTOR_POLYGON,
|
VEKTOR_POLYGON,
|
||||||
VEKTOR_CIRCLE
|
VEKTOR_CIRCLE
|
||||||
@@ -37,7 +32,6 @@ typedef enum {
|
|||||||
typedef struct {
|
typedef struct {
|
||||||
VektorPrimitiveKind kind;
|
VektorPrimitiveKind kind;
|
||||||
union {
|
union {
|
||||||
VektorLine line;
|
|
||||||
VektorPolyline* polyline;
|
VektorPolyline* polyline;
|
||||||
VektorPolygon* polygon;
|
VektorPolygon* polygon;
|
||||||
VektorCircle circle;
|
VektorCircle circle;
|
||||||
@@ -53,12 +47,22 @@ void vektor_polygon_add_point(VektorPolygon* pl, V2 point);
|
|||||||
void vektor_polygon_free(VektorPolygon* pl);
|
void vektor_polygon_free(VektorPolygon* pl);
|
||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
VektorPrimitive* primitives;
|
VektorColor stroke_color;
|
||||||
|
float stroke_width;
|
||||||
|
} VektorStyle;
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
VektorStyle style;
|
||||||
|
int z_index;
|
||||||
|
VektorPrimitive primitive;
|
||||||
|
} VektorShape;
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
VektorShape* shapes;
|
||||||
size_t count;
|
size_t count;
|
||||||
size_t capacity;
|
size_t capacity;
|
||||||
} VektorPrimitiveBuffer;
|
} VektorShapeBuffer;
|
||||||
|
|
||||||
void vektor_primitivebuffer_add_primitive(VektorPrimitiveBuffer* edges,
|
void vektor_shapebuffer_add_shape(VektorShapeBuffer* buffer, VektorShape shape);
|
||||||
VektorPrimitive edge);
|
|
||||||
|
|
||||||
#endif // PRIMITIVES_H_
|
#endif // PRIMITIVES_H_
|
||||||
|
|||||||
@@ -2,6 +2,7 @@
|
|||||||
#include "epoxy/gl.h"
|
#include "epoxy/gl.h"
|
||||||
#include "primitives.h"
|
#include "primitives.h"
|
||||||
#include "stddef.h"
|
#include "stddef.h"
|
||||||
|
#include <stddef.h>
|
||||||
|
|
||||||
void vektor_edgebuffer_add_edge(EdgeBuffer* buffer, Edge edge) {
|
void vektor_edgebuffer_add_edge(EdgeBuffer* buffer, Edge edge) {
|
||||||
if (buffer->count >= buffer->capacity) {
|
if (buffer->count >= buffer->capacity) {
|
||||||
@@ -11,18 +12,15 @@ void vektor_edgebuffer_add_edge(EdgeBuffer* buffer, Edge edge) {
|
|||||||
buffer->edges[buffer->count++] = edge;
|
buffer->edges[buffer->count++] = edge;
|
||||||
}
|
}
|
||||||
|
|
||||||
void vektor_line_flatten(EdgeBuffer* buffer, VektorLine line) {
|
void vektor_polyline_flatten(EdgeBuffer* buffer, VektorPolyline* line,
|
||||||
vektor_edgebuffer_add_edge(buffer, (Edge){line.p1, line.p2, 0});
|
size_t j) {
|
||||||
}
|
|
||||||
|
|
||||||
void vektor_polyline_flatten(EdgeBuffer* buffer, VektorPolyline* line) {
|
|
||||||
for (size_t i = 0; i + 1 < line->count; i++) {
|
for (size_t i = 0; i + 1 < line->count; i++) {
|
||||||
vektor_edgebuffer_add_edge(
|
vektor_edgebuffer_add_edge(
|
||||||
buffer, (Edge){line->points[i], line->points[i + 1], 0});
|
buffer, (Edge){line->points[i], line->points[i + 1], 0, j});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void vektor_polygon_flatten(EdgeBuffer* buffer, VektorPolygon* pg) {
|
void vektor_polygon_flatten(EdgeBuffer* buffer, VektorPolygon* pg, size_t j) {
|
||||||
size_t n = pg->count;
|
size_t n = pg->count;
|
||||||
if (n < 3)
|
if (n < 3)
|
||||||
return;
|
return;
|
||||||
@@ -31,86 +29,22 @@ void vektor_polygon_flatten(EdgeBuffer* buffer, VektorPolygon* pg) {
|
|||||||
V2 p1 = pg->points[i];
|
V2 p1 = pg->points[i];
|
||||||
V2 p2 = pg->points[(i + 1) % n];
|
V2 p2 = pg->points[(i + 1) % n];
|
||||||
int winding = (p1.y < p2.y) ? +1 : -1;
|
int winding = (p1.y < p2.y) ? +1 : -1;
|
||||||
vektor_edgebuffer_add_edge(buffer, (Edge){p1, p2, winding});
|
vektor_edgebuffer_add_edge(buffer, (Edge){p1, p2, winding, j});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
inline VektorFramebuffer vektor_framebuffer_new(unsigned int W,
|
void vektor_rasterize(VertexBuffer* vb, VektorShapeBuffer* shapes) {
|
||||||
unsigned int H) {
|
|
||||||
VektorFramebuffer fb = {
|
|
||||||
.width = W, .height = H, .pixels = calloc(W * H * 4, 1)};
|
|
||||||
return fb;
|
|
||||||
}
|
|
||||||
|
|
||||||
inline void vektor_framebuffer_putpixel(VektorFramebuffer* fb, int x, int y,
|
|
||||||
VektorColor color) {
|
|
||||||
if ((unsigned)x >= fb->width || (unsigned)y >= fb->height)
|
|
||||||
return;
|
|
||||||
|
|
||||||
int i = (y * fb->width + x) * 4;
|
|
||||||
fb->pixels[i + 0] = color.r;
|
|
||||||
fb->pixels[i + 1] = color.g;
|
|
||||||
fb->pixels[i + 2] = color.b;
|
|
||||||
fb->pixels[i + 3] = color.a;
|
|
||||||
}
|
|
||||||
|
|
||||||
void draw_filled_circle(VektorFramebuffer* fb, int cx, int cy, int r,
|
|
||||||
VektorColor color) {
|
|
||||||
for (int y = -r; y <= r; y++) {
|
|
||||||
int dx = (int)sqrt(r * r - y * y);
|
|
||||||
for (int x = -dx; x <= dx; x++) {
|
|
||||||
vektor_framebuffer_putpixel(fb, cx + x, cy + y, color);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void vektor_framebuffer_drawline(VektorFramebuffer* fb, V2 a, V2 b,
|
|
||||||
VektorColor color, double thickness) {
|
|
||||||
int x0 = (int)a.x;
|
|
||||||
int y0 = (int)a.y;
|
|
||||||
int x1 = (int)b.x;
|
|
||||||
int y1 = (int)b.y;
|
|
||||||
|
|
||||||
int dx = abs(x1 - x0);
|
|
||||||
int sx = x0 < x1 ? 1 : -1;
|
|
||||||
int dy = -abs(y1 - y0);
|
|
||||||
int sy = y0 < y1 ? 1 : -1;
|
|
||||||
int err = dx + dy;
|
|
||||||
|
|
||||||
for (;;) {
|
|
||||||
draw_filled_circle(fb, x0, y0, thickness / 2, color);
|
|
||||||
if (x0 == x1 && y0 == y1)
|
|
||||||
break;
|
|
||||||
|
|
||||||
int e2 = 2 * err;
|
|
||||||
if (e2 >= dy) {
|
|
||||||
err += dy;
|
|
||||||
x0 += sx;
|
|
||||||
}
|
|
||||||
if (e2 <= dx) {
|
|
||||||
err += dx;
|
|
||||||
y0 += sy;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void vektor_framebuffer_rasterize(VektorFramebuffer* fb,
|
|
||||||
VektorPrimitiveBuffer* prims) {
|
|
||||||
EdgeBuffer edges = {0};
|
EdgeBuffer edges = {0};
|
||||||
for (size_t i = 0; i < prims->count; i++) {
|
for (size_t i = 0; i < shapes->count; i++) {
|
||||||
VektorPrimitive* p = &prims->primitives[i];
|
VektorPrimitive* p = &shapes->shapes[i].primitive;
|
||||||
|
|
||||||
switch (p->kind) {
|
switch (p->kind) {
|
||||||
case VEKTOR_LINE:
|
|
||||||
vektor_line_flatten(&edges, p->line);
|
|
||||||
break;
|
|
||||||
|
|
||||||
case VEKTOR_POLYLINE:
|
case VEKTOR_POLYLINE:
|
||||||
vektor_polyline_flatten(&edges, p->polyline);
|
vektor_polyline_flatten(&edges, p->polyline, i);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case VEKTOR_POLYGON:
|
case VEKTOR_POLYGON:
|
||||||
vektor_polygon_flatten(&edges, p->polygon);
|
vektor_polygon_flatten(&edges, p->polygon, i);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
default:
|
default:
|
||||||
@@ -119,73 +53,46 @@ void vektor_framebuffer_rasterize(VektorFramebuffer* fb,
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
for (size_t i = 0; i < edges.count; i++) {
|
vektor_edges_to_triangles(vb, &edges, shapes);
|
||||||
vektor_framebuffer_drawline(fb, edges.edges[i].p1, edges.edges[i].p2,
|
|
||||||
vektor_color_solid(255, 0, 255), 4);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
VertexBuffer vektor_rasterize(VektorPrimitiveBuffer* prims) {
|
void vb_add_triangle(VertexBuffer* vb, V2 v0, V2 v1, V2 v2, VektorColor color) {
|
||||||
EdgeBuffer edges = {0};
|
|
||||||
for (size_t i = 0; i < prims->count; i++) {
|
|
||||||
VektorPrimitive* p = &prims->primitives[i];
|
|
||||||
|
|
||||||
switch (p->kind) {
|
|
||||||
case VEKTOR_LINE:
|
|
||||||
vektor_line_flatten(&edges, p->line);
|
|
||||||
break;
|
|
||||||
|
|
||||||
case VEKTOR_POLYLINE:
|
|
||||||
vektor_polyline_flatten(&edges, p->polyline);
|
|
||||||
break;
|
|
||||||
|
|
||||||
case VEKTOR_POLYGON:
|
|
||||||
vektor_polygon_flatten(&edges, p->polygon);
|
|
||||||
break;
|
|
||||||
|
|
||||||
default:
|
|
||||||
// TODO: fill in all primitives
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
VertexBuffer vb = vektor_edges_to_triangles(&edges, 0.1f);
|
|
||||||
return vb;
|
|
||||||
}
|
|
||||||
|
|
||||||
void vb_add_triangle(VertexBuffer* vb, V2 v0, V2 v1, V2 v2) {
|
|
||||||
if (vb->count + 3 >= vb->capacity) {
|
if (vb->count + 3 >= vb->capacity) {
|
||||||
vb->capacity = vb->capacity ? vb->capacity * 2 : 8;
|
vb->capacity = vb->capacity ? vb->capacity * 2 : 8;
|
||||||
vb->vertices = realloc(vb->vertices, sizeof(V2) * vb->capacity);
|
vb->vertices = realloc(vb->vertices, sizeof(Vertex) * vb->capacity);
|
||||||
}
|
}
|
||||||
vb->vertices[vb->count++] = v0;
|
vb->vertices[vb->count++] = (Vertex){v0, color};
|
||||||
vb->vertices[vb->count++] = v1;
|
vb->vertices[vb->count++] = (Vertex){v1, color};
|
||||||
vb->vertices[vb->count++] = v2;
|
vb->vertices[vb->count++] = (Vertex){v2, color};
|
||||||
}
|
}
|
||||||
|
|
||||||
void vektor_edge_to_triangles(VertexBuffer* vb, Edge e, float thickness) {
|
void vektor_edge_to_triangles(VertexBuffer* vb, Edge e,
|
||||||
|
VektorShapeBuffer* shape_buffer) {
|
||||||
float dx = e.p2.x - e.p1.x;
|
float dx = e.p2.x - e.p1.x;
|
||||||
float dy = e.p2.y - e.p1.y;
|
float dy = e.p2.y - e.p1.y;
|
||||||
float len = sqrtf(dx * dx + dy * dy);
|
float len = sqrtf(dx * dx + dy * dy);
|
||||||
if (len == 0)
|
if (len == 0)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
float px = -dy / len * (thickness / 2);
|
float px =
|
||||||
float py = dx / len * (thickness / 2);
|
-dy / len * (shape_buffer->shapes[e.shape_id].style.stroke_width / 2);
|
||||||
|
float py =
|
||||||
|
dx / len * (shape_buffer->shapes[e.shape_id].style.stroke_width / 2);
|
||||||
|
|
||||||
V2 v0 = {e.p1.x + px, e.p1.y + py};
|
V2 v0 = {e.p1.x + px, e.p1.y + py};
|
||||||
V2 v1 = {e.p1.x - px, e.p1.y - py};
|
V2 v1 = {e.p1.x - px, e.p1.y - py};
|
||||||
V2 v2 = {e.p2.x + px, e.p2.y + py};
|
V2 v2 = {e.p2.x + px, e.p2.y + py};
|
||||||
V2 v3 = {e.p2.x - px, e.p2.y - py};
|
V2 v3 = {e.p2.x - px, e.p2.y - py};
|
||||||
|
|
||||||
vb_add_triangle(vb, v0, v1, v2);
|
vb_add_triangle(vb, v0, v1, v2,
|
||||||
vb_add_triangle(vb, v2, v1, v3);
|
shape_buffer->shapes[e.shape_id].style.stroke_color);
|
||||||
|
vb_add_triangle(vb, v2, v1, v3,
|
||||||
|
shape_buffer->shapes[e.shape_id].style.stroke_color);
|
||||||
}
|
}
|
||||||
|
|
||||||
VertexBuffer vektor_edges_to_triangles(EdgeBuffer* edges, float thickness) {
|
void vektor_edges_to_triangles(VertexBuffer* vb, EdgeBuffer* edges,
|
||||||
VertexBuffer vb = {0};
|
VektorShapeBuffer* shape_buffer) {
|
||||||
for (size_t i = 0; i < edges->count; i++) {
|
for (size_t i = 0; i < edges->count; i++) {
|
||||||
vektor_edge_to_triangles(&vb, edges->edges[i], thickness);
|
vektor_edge_to_triangles(vb, edges->edges[i], shape_buffer);
|
||||||
}
|
}
|
||||||
return vb;
|
|
||||||
}
|
}
|
||||||
@@ -3,14 +3,16 @@
|
|||||||
|
|
||||||
#include "primitives.h"
|
#include "primitives.h"
|
||||||
|
|
||||||
#include "src/util/color.h"
|
#include "../util/color.h"
|
||||||
#include "stddef.h"
|
#include "stddef.h"
|
||||||
#include "vector.h"
|
#include "vector.h"
|
||||||
|
#include <stddef.h>
|
||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
V2 p1;
|
V2 p1;
|
||||||
V2 p2;
|
V2 p2;
|
||||||
int winding;
|
int winding;
|
||||||
|
size_t shape_id;
|
||||||
} Edge;
|
} Edge;
|
||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
@@ -21,37 +23,25 @@ typedef struct {
|
|||||||
|
|
||||||
void vektor_edgebuffer_add_edge(EdgeBuffer* edges, Edge edge);
|
void vektor_edgebuffer_add_edge(EdgeBuffer* edges, Edge edge);
|
||||||
|
|
||||||
void vektor_line_flatten(EdgeBuffer* edges, VektorLine line);
|
void vektor_polyline_flatten(EdgeBuffer* edges, VektorPolyline* line, size_t i);
|
||||||
void vektor_polyline_flatten(EdgeBuffer* edges, VektorPolyline* line);
|
void vektor_polygon_flatten(EdgeBuffer* buffer, VektorPolygon* line, size_t i);
|
||||||
void vektor_polygon_flatten(EdgeBuffer* buffer, VektorPolygon* line);
|
|
||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
unsigned int width;
|
V2 coords;
|
||||||
unsigned int height;
|
VektorColor color;
|
||||||
unsigned char* pixels; // Flat RGBA8 array
|
} Vertex;
|
||||||
} VektorFramebuffer;
|
|
||||||
|
|
||||||
VektorFramebuffer vektor_framebuffer_new(unsigned int width,
|
|
||||||
unsigned int height);
|
|
||||||
|
|
||||||
void vektor_framebuffer_putpixel(VektorFramebuffer* fb, int x, int y,
|
|
||||||
VektorColor color);
|
|
||||||
|
|
||||||
void vektor_framebuffer_drawline(VektorFramebuffer* fb, V2 a, V2 b,
|
|
||||||
VektorColor color, double thickness);
|
|
||||||
|
|
||||||
void vektor_framebuffer_rasterize(VektorFramebuffer* fb,
|
|
||||||
VektorPrimitiveBuffer* primitives);
|
|
||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
V2* vertices;
|
Vertex* vertices;
|
||||||
size_t count;
|
size_t count;
|
||||||
size_t capacity;
|
size_t capacity;
|
||||||
} VertexBuffer;
|
} VertexBuffer;
|
||||||
|
|
||||||
void vb_add_triangle(VertexBuffer* vb, V2 v0, V2 v1, V2 v2);
|
void vb_add_triangle(VertexBuffer* vb, V2 v0, V2 v1, V2 v2, VektorColor color);
|
||||||
void vektor_edge_to_triangles(VertexBuffer* vb, Edge e, float thickness);
|
void vektor_edge_to_triangles(VertexBuffer* vb, Edge e,
|
||||||
VertexBuffer vektor_edges_to_triangles(EdgeBuffer* edges, float thickness);
|
VektorShapeBuffer* shape_buffer);
|
||||||
VertexBuffer vektor_rasterize(VektorPrimitiveBuffer* prims);
|
void vektor_edges_to_triangles(VertexBuffer* vb, EdgeBuffer* edges,
|
||||||
|
VektorShapeBuffer* shape_buffer);
|
||||||
|
void vektor_rasterize(VertexBuffer* vb, VektorShapeBuffer* shapes);
|
||||||
|
|
||||||
#endif // RASTER_H_
|
#endif // RASTER_H_
|
||||||
|
|||||||
@@ -29,7 +29,6 @@ char* read_file(const char* path) {
|
|||||||
|
|
||||||
static GLuint shader_program;
|
static GLuint shader_program;
|
||||||
static GLuint vao;
|
static GLuint vao;
|
||||||
// VektorPrimitiveBuffer prims = {0};
|
|
||||||
VertexBuffer vb;
|
VertexBuffer vb;
|
||||||
|
|
||||||
static GLuint compile_shader(GLenum type, const char* src) {
|
static GLuint compile_shader(GLenum type, const char* src) {
|
||||||
@@ -78,24 +77,6 @@ static void init_shader(void) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
static void init_geometry(void) {
|
static void init_geometry(void) {
|
||||||
// V2 vs[3] = {(V2){-0.5, -0.5}, (V2){0.5, -0.5}, (V2){0.0, 0.5}};
|
|
||||||
// VertexBuffer vb =
|
|
||||||
// (VertexBuffer){.count = 3, .capacity = 3, .vertices = &vs[0]};
|
|
||||||
|
|
||||||
// VektorPolygon* triangle = vektor_polygon_new();
|
|
||||||
// vektor_polygon_add_point(triangle, (V2){-0.5f, -0.5f}); // bottom-left
|
|
||||||
// vektor_polygon_add_point(triangle, (V2){0.5f, -0.5f}); // bottom-right
|
|
||||||
// vektor_polygon_add_point(triangle, (V2){0.0f, 0.5f}); // top-center
|
|
||||||
|
|
||||||
// vektor_primitivebuffer_add_primitive(
|
|
||||||
// &prims, (VektorPrimitive){.kind = VEKTOR_POLYGON, .polygon =
|
|
||||||
// triangle});
|
|
||||||
|
|
||||||
// for (size_t i = 0; i < vb.count; i++) {
|
|
||||||
// printf("Vertex %zu: x=%f, y=%f\n", i, vb.vertices[i].x,
|
|
||||||
// vb.vertices[i].y);
|
|
||||||
// }
|
|
||||||
|
|
||||||
GLuint vbo;
|
GLuint vbo;
|
||||||
|
|
||||||
glGenVertexArrays(1, &vao);
|
glGenVertexArrays(1, &vao);
|
||||||
@@ -105,30 +86,33 @@ static void init_geometry(void) {
|
|||||||
glBindBuffer(GL_ARRAY_BUFFER, vbo);
|
glBindBuffer(GL_ARRAY_BUFFER, vbo);
|
||||||
|
|
||||||
glEnableVertexAttribArray(0);
|
glEnableVertexAttribArray(0);
|
||||||
glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, sizeof(V2), (void*)0);
|
glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, sizeof(Vertex),
|
||||||
|
(void*)offsetof(Vertex, coords));
|
||||||
|
|
||||||
|
glEnableVertexAttribArray(1);
|
||||||
|
glVertexAttribPointer(1, 4, GL_UNSIGNED_BYTE, GL_TRUE, sizeof(Vertex),
|
||||||
|
(void*)offsetof(Vertex, color));
|
||||||
|
|
||||||
glBindVertexArray(0);
|
glBindVertexArray(0);
|
||||||
}
|
}
|
||||||
|
|
||||||
static gboolean render(GtkGLArea* area, GdkGLContext* context,
|
static gboolean render(GtkGLArea* area, GdkGLContext* context,
|
||||||
VektorPrimitiveBuffer* prims) {
|
VektorShapeBuffer* prims) {
|
||||||
vb = vektor_rasterize(prims);
|
vektor_rasterize(&vb, prims);
|
||||||
|
|
||||||
for (size_t i = 0; i < vb.count; i++) {
|
for (size_t i = 0; i < vb.count; i++) {
|
||||||
printf("Vertex %zu: x=%f, y=%f\n", i, vb.vertices[i].x,
|
printf("Vertex %zu: x=%f, y=%f\n", i, vb.vertices[i].coords.x,
|
||||||
vb.vertices[i].y);
|
vb.vertices[i].coords.y);
|
||||||
}
|
}
|
||||||
|
|
||||||
glBufferData(GL_ARRAY_BUFFER, vb.count * sizeof(V2), vb.vertices,
|
glBufferData(GL_ARRAY_BUFFER, vb.count * sizeof(Vertex), vb.vertices,
|
||||||
GL_STATIC_DRAW);
|
GL_STATIC_DRAW);
|
||||||
|
|
||||||
glUseProgram(shader_program);
|
glUseProgram(shader_program);
|
||||||
|
|
||||||
GLuint uProjectionLoc = glGetUniformLocation(shader_program, "uProjection");
|
GLuint uProjectionLoc = glGetUniformLocation(shader_program, "uProjection");
|
||||||
GLuint uColorLoc = glGetUniformLocation(shader_program, "uColor");
|
|
||||||
float projectionMatrix[16] = {1, 0, 0, 0, 0, 1, 0, 0,
|
float projectionMatrix[16] = {1, 0, 0, 0, 0, 1, 0, 0,
|
||||||
0, 0, 1, 0, 0, 0, 0, 1};
|
0, 0, 1, 0, 0, 0, 0, 1};
|
||||||
glUniformMatrix4fv(uProjectionLoc, 1, GL_FALSE, projectionMatrix);
|
glUniformMatrix4fv(uProjectionLoc, 1, GL_FALSE, projectionMatrix);
|
||||||
glUniform4f(uColorLoc, 1.0, 0.0, 1.0, 1.0); // magenta
|
|
||||||
|
|
||||||
glBindVertexArray(vao);
|
glBindVertexArray(vao);
|
||||||
glDisable(GL_CULL_FACE);
|
glDisable(GL_CULL_FACE);
|
||||||
@@ -176,7 +160,7 @@ static void realize(GtkGLArea* area, gpointer user_data) {
|
|||||||
gtk_gl_area_make_current(area);
|
gtk_gl_area_make_current(area);
|
||||||
|
|
||||||
if (gtk_gl_area_get_error(area) != NULL)
|
if (gtk_gl_area_get_error(area) != NULL)
|
||||||
return; // context creation failed
|
return;
|
||||||
|
|
||||||
glEnable(GL_DEBUG_OUTPUT);
|
glEnable(GL_DEBUG_OUTPUT);
|
||||||
dump_gl_info(area);
|
dump_gl_info(area);
|
||||||
@@ -185,7 +169,7 @@ static void realize(GtkGLArea* area, gpointer user_data) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void vektor_canvas_init(VektorWidgetState* state, VektorCanvas* canvasOut,
|
void vektor_canvas_init(VektorWidgetState* state, VektorCanvas* canvasOut,
|
||||||
VektorPrimitiveBuffer* prims) {
|
VektorShapeBuffer* prims) {
|
||||||
canvasOut->canvasWidget = state->workspaceCanvas;
|
canvasOut->canvasWidget = state->workspaceCanvas;
|
||||||
canvasOut->width = VKTR_CANVAS_WIDTH;
|
canvasOut->width = VKTR_CANVAS_WIDTH;
|
||||||
canvasOut->height = VKTR_CANVAS_HEIGHT;
|
canvasOut->height = VKTR_CANVAS_HEIGHT;
|
||||||
@@ -201,49 +185,4 @@ void vektor_canvas_init(VektorWidgetState* state, VektorCanvas* canvasOut,
|
|||||||
NULL);
|
NULL);
|
||||||
g_signal_connect(canvasOut->canvasWidget, "render", G_CALLBACK(render),
|
g_signal_connect(canvasOut->canvasWidget, "render", G_CALLBACK(render),
|
||||||
prims);
|
prims);
|
||||||
// gtk_picture_set_paintable(canvasOut->canvasWidget,
|
|
||||||
// GDK_PAINTABLE(canvasOut->canvasTexture));
|
|
||||||
// gtk_picture_set_content_fit(GTK_PICTURE(canvasOut->canvasWidget),
|
|
||||||
// GTK_CONTENT_FIT_CONTAIN);
|
|
||||||
// g_object_unref(bytes);
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Generate new texture based on canvasPixels*/
|
|
||||||
void vektor_canvas_update(VektorCanvas* canvas) {
|
|
||||||
g_bytes_unref(canvas->canvasPixelBytes);
|
|
||||||
canvas->canvasPixelBytes =
|
|
||||||
g_bytes_new(canvas->canvasPixels, VKTR_CANVAS_SIZE);
|
|
||||||
|
|
||||||
g_object_unref(canvas->canvasTexture);
|
|
||||||
canvas->canvasTexture = gdk_memory_texture_new(
|
|
||||||
canvas->width, canvas->height, GDK_MEMORY_R8G8B8A8,
|
|
||||||
canvas->canvasPixelBytes, canvas->width * 4);
|
|
||||||
|
|
||||||
// gtk_picture_set_paintable(canvas->canvasWidget,
|
|
||||||
// GDK_PAINTABLE(canvas->canvasTexture));
|
|
||||||
}
|
|
||||||
|
|
||||||
void vektor_canvas_fill(VektorCanvas* canvas, VektorColor color) {
|
|
||||||
for (int x = 0; x < VKTR_CANVAS_WIDTH; x++) {
|
|
||||||
for (int y = 0; y < VKTR_CANVAS_HEIGHT; y++) {
|
|
||||||
int i = (y * VKTR_CANVAS_WIDTH + x) * 4;
|
|
||||||
canvas->canvasPixels[i + 0] = color.r;
|
|
||||||
canvas->canvasPixels[i + 1] = color.g;
|
|
||||||
canvas->canvasPixels[i + 2] = color.b;
|
|
||||||
canvas->canvasPixels[i + 3] = color.a;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void vektor_canvas_drawfrom(VektorFramebuffer* fb, VektorCanvas* target) {
|
|
||||||
for (int x = 0; x < fb->width; x++) {
|
|
||||||
for (int y = 0; y < fb->height; y++) {
|
|
||||||
|
|
||||||
int i = (y * fb->width + x) * 4;
|
|
||||||
target->canvasPixels[i + 0] = (guchar)fb->pixels[i + 0];
|
|
||||||
target->canvasPixels[i + 1] = (guchar)fb->pixels[i + 1];
|
|
||||||
target->canvasPixels[i + 2] = (guchar)fb->pixels[i + 2];
|
|
||||||
target->canvasPixels[i + 3] = (guchar)fb->pixels[i + 3];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
@@ -19,9 +19,9 @@ typedef struct VektorCanvas {
|
|||||||
} VektorCanvas;
|
} VektorCanvas;
|
||||||
|
|
||||||
void vektor_canvas_init(VektorWidgetState* state, VektorCanvas* canvasOut,
|
void vektor_canvas_init(VektorWidgetState* state, VektorCanvas* canvasOut,
|
||||||
VektorPrimitiveBuffer* prims);
|
VektorShapeBuffer* shapes);
|
||||||
void vektor_canvas_update(VektorCanvas* canvas);
|
// void vektor_canvas_update(VektorCanvas* canvas);
|
||||||
void vektor_canvas_fill(VektorCanvas* canvas, VektorColor color);
|
// void vektor_canvas_fill(VektorCanvas* canvas, VektorColor color);
|
||||||
void vektor_canvas_drawfrom(VektorFramebuffer* fb, VektorCanvas* canvas);
|
// void vektor_canvas_drawfrom(VektorFramebuffer* fb, VektorCanvas* canvas);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
Reference in New Issue
Block a user