From 93fb12eb249f7b71f032fb2dc9b7d953b275b242 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mario=20H=C3=BCttel?= Date: Sat, 27 Jun 2020 22:08:02 +0200 Subject: [PATCH] Add graphics class and add example triangle --- include/shimatta-opengl-colored-triangle.h | 33 ++++++ include/shimatta-opengl-graphics.h | 46 ++++++++ include/shimatta-opengl-program.h | 5 + src/shimatta-opengl-colored-triangle.c | 120 +++++++++++++++++++++ src/shimatta-opengl-graphics.c | 101 +++++++++++++++++ src/shimatta-opengl-program.c | 4 +- 6 files changed, 308 insertions(+), 1 deletion(-) create mode 100644 include/shimatta-opengl-colored-triangle.h create mode 100644 include/shimatta-opengl-graphics.h create mode 100644 src/shimatta-opengl-colored-triangle.c create mode 100644 src/shimatta-opengl-graphics.c diff --git a/include/shimatta-opengl-colored-triangle.h b/include/shimatta-opengl-colored-triangle.h new file mode 100644 index 0000000..597435b --- /dev/null +++ b/include/shimatta-opengl-colored-triangle.h @@ -0,0 +1,33 @@ +/* + * This file is part of Shimatta OpenGL. + * +* Shimatta OpenGL is free software: you can redistribute it and/or modify +* it under the terms of the GNU General Public License version 2 as +* published by the Free Software Foundation. +* +* Shimatta OpenGL is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU General Public License for more details. +* +* You should have received a copy of the GNU General Public License +* along with GtkGraphView. If not, see . +*/ + +#ifndef _SHIMATTA_OPENGL_COLORED_TRIANGLE_H_ +#define _SHIMATTA_OPENGL_COLORED_TRIANGLE_H_ + +#include +#include + +G_BEGIN_DECLS + +#define SHIMATTA_TYPE_OPENGL_COLORED_TRIANGLE (shimatta_opengl_colored_triangle_get_type()) + +G_DECLARE_FINAL_TYPE(ShimattaOpenglColoredTriangle, shimatta_opengl_colored_triangle, SHIMATTA, OPENGL_COLORED_TRIANGLE, ShimattaOpenglGraphics); + +ShimattaOpenglGraphics *shimatta_opengl_colored_triangle_new(ShimattaOpenglProgram *prog, const vec3 color, const vec3* vertecies); + +G_END_DECLS + +#endif /* _SHIMATTA_OPENGL_COLORED_TRIANGLE_H_ */ diff --git a/include/shimatta-opengl-graphics.h b/include/shimatta-opengl-graphics.h new file mode 100644 index 0000000..8f07073 --- /dev/null +++ b/include/shimatta-opengl-graphics.h @@ -0,0 +1,46 @@ +/* + * This file is part of Shimatta OpenGL. + * +* Shimatta OpenGL is free software: you can redistribute it and/or modify +* it under the terms of the GNU General Public License version 2 as +* published by the Free Software Foundation. +* +* Shimatta OpenGL is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU General Public License for more details. +* +* You should have received a copy of the GNU General Public License +* along with GtkGraphView. If not, see . +*/ + +#ifndef _SHIMATTA_OPENGL_GRAPHICS_H_ +#define _SHIMATTA_OPENGL_GRAPHICS_H_ + +#include +#include +#include + +G_BEGIN_DECLS + +#define SHIMATTA_TYPE_OPENGL_GRAPHICS (shimatta_opengl_graphics_get_type()) + +G_DECLARE_DERIVABLE_TYPE(ShimattaOpenglGraphics, shimatta_opengl_graphics, SHIMATTA, OPENGL_GRAPHICS, GObject); + +struct _ShimattaOpenglGraphicsClass { + GObjectClass super_class; + + int (*realize)(ShimattaOpenglGraphics *graphics); + void (*draw)(ShimattaOpenglGraphics *graphics); + void (*draw_with_model_matrix)(ShimattaOpenglGraphics *graphics, mat4 model_matrix, mat4 normal_matrix); +}; + +int shimatta_opengl_graphics_realize(ShimattaOpenglGraphics *graphics); + +void shimatta_opengl_graphics_draw(ShimattaOpenglGraphics *graphics); + +void shimatta_opengl_graphics_draw_with_model_matrix(ShimattaOpenglGraphics *graphics, mat4 model_matrix, mat4 normal_matrix); + +G_END_DECLS + +#endif /* _SHIMATTA_OPENGL_GRAPHICS_H_ */ diff --git a/include/shimatta-opengl-program.h b/include/shimatta-opengl-program.h index 8d7cb40..42dce8f 100644 --- a/include/shimatta-opengl-program.h +++ b/include/shimatta-opengl-program.h @@ -14,6 +14,9 @@ * along with GtkGraphView. If not, see . */ +#ifndef _SHIMATTA_OPENGL_PROGRAM_H_ +#define _SHIMATTA_OPENGL_PROGRAM_H_ + #include #include #include @@ -106,3 +109,5 @@ void shimatta_opengl_program_set_uniform_mat3(ShimattaOpenglProgram *program, co void shimatta_opengl_program_set_uniform_mat4(ShimattaOpenglProgram *program, const char *uniform, mat4 value); G_END_DECLS + +#endif /* _SHIMATTA_OPENGL_PROGRAM_H_ */ diff --git a/src/shimatta-opengl-colored-triangle.c b/src/shimatta-opengl-colored-triangle.c new file mode 100644 index 0000000..7660c62 --- /dev/null +++ b/src/shimatta-opengl-colored-triangle.c @@ -0,0 +1,120 @@ +/* + * This file is part of Shimatta OpenGL. + * +* Shimatta OpenGL is free software: you can redistribute it and/or modify +* it under the terms of the GNU General Public License version 2 as +* published by the Free Software Foundation. +* +* Shimatta OpenGL is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU General Public License for more details. +* +* You should have received a copy of the GNU General Public License +* along with GtkGraphView. If not, see . +*/ + +#include +#include +#include + +struct _ShimattaOpenglColoredTriangle { + ShimattaOpenglGraphics super; +}; + +typedef struct { + vec3 color; + vec3 vertecies[3]; + GLuint vao; + GLuint vbo; + ShimattaOpenglProgram *prog; +} ShimattaOpenglColoredTrianglePrivate; + +G_DEFINE_TYPE_WITH_PRIVATE(ShimattaOpenglColoredTriangle, shimatta_opengl_colored_triangle, SHIMATTA_TYPE_OPENGL_GRAPHICS); + +static int realize(ShimattaOpenglGraphics *self) +{ + ShimattaOpenglColoredTrianglePrivate *priv; + + priv = shimatta_opengl_colored_triangle_get_instance_private(SHIMATTA_OPENGL_COLORED_TRIANGLE(self)); + + glGenVertexArrays(1, &priv->vao); + glBindVertexArray(priv->vao); + glGenBuffers(1, &priv->vbo); + glBindBuffer(GL_ARRAY_BUFFER, priv->vbo); + glBufferData(GL_ARRAY_BUFFER, sizeof(vec3)*3, priv->vertecies, GL_STATIC_DRAW); + glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3*sizeof(float), (void *)0); + glEnableVertexAttribArray(0); + + return 0; +} + +static void draw(ShimattaOpenglGraphics *self) +{ + ShimattaOpenglColoredTrianglePrivate *priv; + + priv = shimatta_opengl_colored_triangle_get_instance_private(SHIMATTA_OPENGL_COLORED_TRIANGLE(self)); + + shimatta_opengl_program_use(priv->prog); + shimatta_opengl_program_set_uniform_vec3(priv->prog, "color", priv->color); + glPolygonMode(GL_FRONT_AND_BACK, GL_FILL); + glBindVertexArray(priv->vao); + glDrawArrays(GL_TRIANGLES, 0, 3); +} + +static void draw_with_model_matrix(ShimattaOpenglGraphics *self, mat4 m, mat4 n) +{ + (void)m; + (void)n; + draw(self); +} + +void shimatta_opengl_colored_triangle_class_init(ShimattaOpenglColoredTriangleClass *klass) +{ + ShimattaOpenglGraphicsClass *gclass; + gclass = SHIMATTA_OPENGL_GRAPHICS_CLASS(klass); + + gclass->draw = draw; + gclass->realize = realize; + gclass->draw_with_model_matrix = draw_with_model_matrix; +} + +void shimatta_opengl_colored_triangle_init(ShimattaOpenglColoredTriangle *self) +{ + ShimattaOpenglColoredTrianglePrivate *priv; + + priv = shimatta_opengl_colored_triangle_get_instance_private(self); + priv->color[0] = 0.0f; + priv->color[1] = 0.0f; + priv->color[2] = 0.0f; +} + +ShimattaOpenglGraphics *shimatta_opengl_colored_triangle_new(ShimattaOpenglProgram *prog, const vec3 color, const vec3* vertecies) +{ + ShimattaOpenglGraphics *ret; + ShimattaOpenglColoredTrianglePrivate *priv; + + ret = g_object_new(SHIMATTA_TYPE_OPENGL_COLORED_TRIANGLE, NULL); + + priv = shimatta_opengl_colored_triangle_get_instance_private(SHIMATTA_OPENGL_COLORED_TRIANGLE(ret)); + priv->prog = prog; + g_object_ref(prog); + + priv->vertecies[0][0] = vertecies[0][0]; + priv->vertecies[0][1] = vertecies[0][1]; + priv->vertecies[0][2] = vertecies[0][2]; + + priv->vertecies[1][0] = vertecies[1][0]; + priv->vertecies[1][1] = vertecies[1][1]; + priv->vertecies[1][2] = vertecies[1][2]; + + priv->vertecies[2][0] = vertecies[2][0]; + priv->vertecies[2][1] = vertecies[2][1]; + priv->vertecies[2][2] = vertecies[2][2]; + + priv->color[0] = color[0]; + priv->color[1] = color[1]; + priv->color[2] = color[2]; + + return ret; +} diff --git a/src/shimatta-opengl-graphics.c b/src/shimatta-opengl-graphics.c new file mode 100644 index 0000000..62d3eca --- /dev/null +++ b/src/shimatta-opengl-graphics.c @@ -0,0 +1,101 @@ +/* + * This file is part of Shimatta OpenGL. + * +* Shimatta OpenGL is free software: you can redistribute it and/or modify +* it under the terms of the GNU General Public License version 2 as +* published by the Free Software Foundation. +* +* Shimatta OpenGL is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU General Public License for more details. +* +* You should have received a copy of the GNU General Public License +* along with GtkGraphView. If not, see . +*/ + +#include + +typedef struct { + gpointer dummy[12]; +} ShimattaOpenglGraphicsPrivate; + +G_DEFINE_TYPE_WITH_PRIVATE(ShimattaOpenglGraphics, shimatta_opengl_graphics, G_TYPE_OBJECT); + +static void shimatta_opengl_graphics_dispose(GObject *self) +{ + (void)self; +} + +static void draw_dummy(ShimattaOpenglGraphics *graphics) +{ + (void)graphics; + + g_warning("Object does not implement draw function"); +} + +static void draw_dummy_with_matrix(ShimattaOpenglGraphics *graphics, mat4 model_matrix, mat4 normal_matrix) +{ + (void)graphics; + (void)model_matrix; + (void)normal_matrix; + + g_warning("Object does not implement draw_with_model_matrix function"); +} + +static int realize_dummy(ShimattaOpenglGraphics *graphics) +{ + (void)graphics; + + g_warning("Object does not implement relize function"); + return -1; +} + +static void shimatta_opengl_graphics_class_init(ShimattaOpenglGraphicsClass *klass) +{ + (void)klass; + GObjectClass *oclass; + + oclass = G_OBJECT_CLASS(klass); + oclass->dispose = shimatta_opengl_graphics_dispose; + + klass->draw = draw_dummy; + klass->draw_with_model_matrix = draw_dummy_with_matrix; + klass->realize = realize_dummy; + + return; +} + +static void shimatta_opengl_graphics_init(ShimattaOpenglGraphics *self) +{ + (void)self; + + return; +} + +int shimatta_opengl_graphics_realize(ShimattaOpenglGraphics *graphics) +{ + ShimattaOpenglGraphicsClass *klass; + + g_return_val_if_fail(SHIMATTA_IS_OPENGL_GRAPHICS(graphics), -1001); + klass = SHIMATTA_OPENGL_GRAPHICS_GET_CLASS(graphics); + return klass->realize(graphics); +} + +void shimatta_opengl_graphics_draw(ShimattaOpenglGraphics *graphics) +{ + ShimattaOpenglGraphicsClass *klass; + + g_return_if_fail(SHIMATTA_IS_OPENGL_GRAPHICS(graphics)); + klass = SHIMATTA_OPENGL_GRAPHICS_GET_CLASS(graphics); + klass->draw(graphics); +} + +void shimatta_opengl_graphics_draw_with_model_matrix(ShimattaOpenglGraphics *graphics, mat4 model_matrix, mat4 normal_matrix) +{ + ShimattaOpenglGraphicsClass *klass; + + g_return_if_fail(SHIMATTA_IS_OPENGL_GRAPHICS(graphics)); + klass = SHIMATTA_OPENGL_GRAPHICS_GET_CLASS(graphics); + klass->draw_with_model_matrix(graphics, model_matrix, normal_matrix); +} diff --git a/src/shimatta-opengl-program.c b/src/shimatta-opengl-program.c index 0c26ba7..1a4b299 100644 --- a/src/shimatta-opengl-program.c +++ b/src/shimatta-opengl-program.c @@ -229,8 +229,10 @@ int shimatta_opengl_program_compile(ShimattaOpenglProgram *program, char *error_ g_return_val_if_fail(SHIMATTA_IS_OPENGL_PROGRAM(program), -1001); - if (error_text && error_text_size > 0) + if (error_text && error_text_size > 0) { use_error = TRUE; + error_text[0] = 0; + } priv = shimatta_opengl_program_get_instance_private(program);