2 Commits

9 changed files with 272 additions and 15 deletions

3
.gitmodules vendored
View File

@@ -6,3 +6,6 @@
path = 3rdparty/libfort
url = https://git.shimatta.de/3rd-party/libfort
branch = develop
[submodule "3rdparty/poly2tri"]
path = 3rdparty/poly2tri
url = https://git.shimatta.de/3rd-party/poly2tri.git

1
3rdparty/poly2tri vendored Submodule

Submodule 3rdparty/poly2tri added at 3380f5c805

View File

@@ -2,7 +2,7 @@ if (POLICY CMP0048)
cmake_policy(SET CMP0048 NEW)
endif (POLICY CMP0048)
project(gds-render LANGUAGES C)
project(gds-render LANGUAGES C CXX)
if(CMAKE_INSTALL_PREFIX_INITIALIZED_TO_DEFAULT)
set(CMAKE_INSTALL_PREFIX "/usr/" CACHE PATH "..." FORCE)
@@ -75,8 +75,6 @@ SET_SOURCE_FILES_PROPERTIES(${SOURCE_GENERATED} PROPERTIES GENERATED 1)
add_subdirectory(test)
add_compile_options(-Wall -Wextra -Wold-style-declaration -Wuninitialized -Wmaybe-uninitialized -Wunused-parameter)
add_subdirectory(resources)
add_subdirectory(doxygen)
add_subdirectory(translations)
@@ -91,6 +89,8 @@ if(OPENGL_PREVIEW)
add_subdirectory(preview-rendering)
endif(OPENGL_PREVIEW)
add_compile_options(-Wall -Wextra -Wold-style-declaration -Wuninitialized -Wmaybe-uninitialized -Wunused-parameter)
link_directories(${GLIB_LINK_DIRS} ${GTK3_LINK_DIRS} ${CAIRO_LINK_DIRS})
add_definitions(${GLIB2_CFLAGS_OTHER})

View File

@@ -8,8 +8,11 @@ pkg_check_modules(GTK3 REQUIRED gtk+-3.0)
aux_source_directory("src" SRCS)
add_subdirectory(poly2tri-wrapper)
add_library(${PROJECT_NAME} STATIC ${SRCS})
target_include_directories(${PROJECT_NAME} PUBLIC "include")
target_include_directories(${PROJECT_NAME} PRIVATE ${EPOXY_INCLUDE_DIRS} ${GLIB_INCLUDE_DIRS} ${GTK3_INCLUDE_DIRS})
target_link_directories(${PROJECT_NAME} PUBLIC ${EPOXY_LINK_DIRS} ${GLIB_LINK_DIRS} ${GTK3_LINK_DIRS})
target_link_libraries(${PROJECT_NAME} ${GLIB_LDFLAGS} ${EPOXY_LDFLAGS} ${GTK3_LDFLAGS})
target_link_libraries(${PROJECT_NAME} ${GLIB_LDFLAGS} ${EPOXY_LDFLAGS} ${GTK3_LDFLAGS} poly2tri-wrapper)

View File

@@ -0,0 +1,11 @@
project(poly2tri-wrapper LANGUAGES CXX)
cmake_minimum_required(VERSION 3.12)
add_subdirectory(../../3rdparty/poly2tri poly2tri)
set(CMAKE_CXX_STANDARD 14)
aux_source_directory("src" WRAPPER_SOURCES)
add_library(${PROJECT_NAME} STATIC ${WRAPPER_SOURCES})
target_link_libraries(${PROJECT_NAME} poly2tri)
target_include_directories(${PROJECT_NAME} PUBLIC "include")

View File

@@ -0,0 +1,16 @@
#ifndef _POLY2TRI_WRAPPER_H_
#define _POLY2TRI_WRAPPER_H_
#ifdef __cplusplus
extern "C" {
#endif
#include <gds-render/gds-utils/gds-types.h>
int tesselator_triangulate_polygon(const struct gds_graphics *gfx, double scale, float **output_vertices, size_t *vertex_count);
#ifdef __cplusplus
} /* extern "C" */
#endif
#endif /* _POLY2TRI_WRAPPER_H_ */

View File

@@ -0,0 +1,132 @@
#include <iostream>
#include <poly2tri/poly2tri.h>
#include <poly2tri-wrapper/poly2tri-wrapper.h>
//#include <gds-render/gds-utils/gds-types.h>
#include <vector>
class Hole {
private:
std::vector <p2t::Point *> hole_vertices;
public:
const std::vector <p2t::Point *> &get_vertices()
{
return hole_vertices;
}
void add_vertex(const p2t::Point &point)
{
auto pt = new p2t::Point(point);
hole_vertices.push_back(pt);
}
Hole(std::vector<p2t::Point *>::iterator start, std::vector<p2t::Point *>::iterator end)
{
hole_vertices = std::vector<p2t::Point *>(start, end);
}
};
static std::vector<Hole> resolve_holes_in_polyline(std::vector<p2t::Point *> &polyline)
{
bool found = false;
std::vector<Hole> holes;
std::vector<std::pair<
std::vector<p2t::Point *>::iterator,
std::vector<p2t::Point *>::iterator>
> points_to_remove;
for (auto start_it = polyline.begin(); start_it != polyline.end(); start_it++) {
for (auto check_it = std::next(start_it); check_it != polyline.end(); check_it++) {
/* Check if points are equal */
if (**start_it == **check_it) {
if (**std::prev(check_it) == **std::next(start_it)) {
/* Found a hole. Everything between start_it and check_it can be removed */
auto hole = Hole(std::next(start_it)+1, check_it);
holes.push_back(hole);
auto tbd = std::make_pair(start_it, check_it);
points_to_remove.push_back(tbd);
found = true;
break;
}
}
}
if (found)
break;
}
/* Remove form polyline */
for (auto it_pair_it = points_to_remove.begin(); it_pair_it != points_to_remove.end(); it_pair_it++) {
auto it_pair = *it_pair_it;
delete *it_pair.first;
delete *std::next(it_pair.first);
polyline.erase(it_pair.first, it_pair.second);
}
if (found) {
auto new_holes = resolve_holes_in_polyline(polyline);
holes.insert(holes.end(), new_holes.begin(), new_holes.end());
}
return holes;
}
int tesselator_triangulate_polygon(const struct gds_graphics *gfx, double scale, float** output_vertices, size_t *vertex_count)
{
GList *vertex_iter;
std::vector<p2t::Point*> polyline;
if (gfx->gfx_type != GRAPHIC_POLYGON)
return -1;
for (vertex_iter = gfx->vertices; vertex_iter; vertex_iter = g_list_next(vertex_iter)) {
struct gds_point *gds_pt = (struct gds_point *)vertex_iter->data;
auto pt = new p2t::Point((double)gds_pt->x/scale, (double)gds_pt->y/scale);
polyline.push_back(pt);
}
auto holes = resolve_holes_in_polyline(polyline);
auto my_cdt = p2t::CDT(polyline);
for (auto hole_it = holes.begin(); hole_it != holes.end(); hole_it++) {
my_cdt.AddHole((*hole_it).get_vertices());
}
my_cdt.Triangulate();
auto triangles = my_cdt.GetTriangles();
/* Free the triangles */
for (auto it = triangles.begin(); it != triangles.end(); it++) {
auto triangle = *it;
std::cout << "Triangle: " <<
triangle->GetPoint(0)->x << "|" << triangle->GetPoint(0)->y << ","
<< triangle->GetPoint(1)->x << "|" << triangle->GetPoint(1)->x << ","
<< triangle->GetPoint(2)->x << "|" << triangle->GetPoint(2)->y
<< std::endl;
}
/* Get the amount of triangles */
auto count = triangles.size();
*output_vertices = (float *)malloc(2*sizeof(float)*3*count);
*vertex_count = count * 3;
/* Free the points in the vector */
for (unsigned int i = 0; i < count; i++) {
for (int j = 0; j < 3; j++) {
auto pt = triangles[i]->GetPoint(j);
unsigned int idx = i * 6 + j * 2;
(*output_vertices)[idx] = (float)pt->x;
(*output_vertices)[idx + 1] = (float)pt->y;
}
}
for (auto it = polyline.begin(); it != polyline.end(); it++)
delete *it;
return 0;
}

View File

@@ -1,4 +1,5 @@
#include <preview-rendering/preview-rendering-gui.h>
#include <poly2tri-wrapper/poly2tri-wrapper.h>
#include <epoxy/gl.h>
struct _OpenGlPreviewGui {
@@ -7,8 +8,10 @@ struct _OpenGlPreviewGui {
GtkGLArea *gl_area;
unsigned int polygon_shader_prog;
unsigned int vao;
unsigned int vao_vertex_count;
unsigned int vbo;
float color[4];
gboolean line;
};
G_DEFINE_TYPE(OpenGlPreviewGui, opengl_preview_gui, G_TYPE_OBJECT)
@@ -47,12 +50,12 @@ static gboolean gl_area_render(GtkGLArea *area, GdkGLContext *context, gpointer
glClearColor (0, 0, 0, 1.0);
glClear (GL_COLOR_BUFFER_BIT);
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
glPolygonMode(GL_FRONT_AND_BACK, gui->line ? GL_LINE : GL_FILL);
glUseProgram(gui->polygon_shader_prog);
glBindVertexArray(gui->vao);
loc = glGetUniformLocation(gui->polygon_shader_prog, "const_color");
glUniform4fv(loc, 1, gui->color);
glDrawArrays(GL_TRIANGLES, 0, 3);
glDrawArrays(GL_TRIANGLES, 0, gui->vao_vertex_count);
glFlush();
@@ -78,6 +81,7 @@ static int compile_shader_from_resource(GLenum shader_type, const char *res_name
g_bytes_unref(res_bytes);
if (shader)
*shader = sh;
return success;
}
@@ -90,6 +94,9 @@ static gboolean gl_area_realize(GtkGLArea *area, gpointer user)
unsigned int vao;
OpenGlPreviewGui *gui;
int success;
struct gds_graphics gfx;
struct gds_point points[20];
size_t vertex_count;
gui = GDS_RENDER_OPENGL_PREVIEW_GUI(user);
@@ -120,30 +127,111 @@ static gboolean gl_area_realize(GtkGLArea *area, gpointer user)
glDeleteShader(fs);
float vertices[] = {
-0.5f, -0.5f,
0.5f, -0.5f,
0.0f, 0.5f,
};
gui->polygon_shader_prog = prog;
gfx.vertices = NULL;
points[0].x = -100;
points[0].y = -100;
points[1].x = -100;
points[1].y = +100;
points[2].x = +0;
points[2].y = +100;
points[3].x = +0;
points[3].y = 20;
points[4].x = -20;
points[4].y = 20;
points[5].x = -20;
points[5].y = -20;
points[6].x = 20;
points[6].y = -20;
points[7].x = 20;
points[7].y = 20;
points[8].x = 0;
points[8].y = 20;
points[9].x = 0;
points[9].y = 100;
points[10].x = 100;
points[10].y = 100;
points[11].x = 100;
points[11].y = 5;
points[12].x = 60;
points[12].y = 5;
points[13].x = 60;
points[13].y = 20;
points[14].x = 50;
points[14].y = 20;
points[15].x = 50;
points[15].y = 0;
points[16].x = 60;
points[16].y = 0;
points[17].x = 60;
points[17].y = 5;
points[18].x = 100;
points[18].y = 5;
points[19].x = 100;
points[19].y = -100;
gfx.gfx_type = GRAPHIC_POLYGON;
for (int i = 0; i < 20; i++) {
gfx.vertices = g_list_append(gfx.vertices, &points[i]);
}
float *vertices = NULL;
tesselator_triangulate_polygon(&gfx, 180.0, &vertices, &vertex_count);
gui->vao_vertex_count = vertex_count;
glGenVertexArrays(1, &vao);
glBindVertexArray(vao);
glGenBuffers(1, &vbo);
glBindBuffer(GL_ARRAY_BUFFER, vbo);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
glBufferData(GL_ARRAY_BUFFER, vertex_count*2*sizeof(float), vertices, GL_STATIC_DRAW);
glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 2*sizeof(float), (void *)0);
glEnableVertexAttribArray(0);
free(vertices);
gui->polygon_shader_prog = prog;
gui->vao = vao;
gui->vbo = vbo;
g_list_free(gfx.vertices);
return TRUE;
}
static void on_clicked(GtkWidget *sender, gboolean *p)
{
*p = !*p;
}
static void opengl_preview_gui_init(OpenGlPreviewGui *self)
{
GtkBuilder *builder;
@@ -153,6 +241,9 @@ static void opengl_preview_gui_init(OpenGlPreviewGui *self)
self->gl_area = GTK_GL_AREA(gtk_builder_get_object(builder, "gl-area"));
g_signal_connect(self->gl_area, "render", G_CALLBACK(gl_area_render), self);
g_signal_connect(self->gl_area, "realize", G_CALLBACK(gl_area_realize), self);
g_signal_connect(gtk_builder_get_object(builder, "test-button"), "clicked", G_CALLBACK(on_clicked), &self->line);
self->line = FALSE;
g_object_ref(self->gl_area);
}

View File

@@ -12,7 +12,7 @@
<property name="can-focus">False</property>
<property name="baseline-position">bottom</property>
<child>
<object class="GtkButton">
<object class="GtkButton" id="test-button">
<property name="label" translatable="yes">button</property>
<property name="visible">True</property>
<property name="can-focus">True</property>