121 lines
3.6 KiB
C
121 lines
3.6 KiB
C
/*
|
|
* 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 <https://www.gnu.org/licenses/>.
|
|
*/
|
|
|
|
#include <shimatta-opengl-colored-triangle.h>
|
|
#include <shimatta-opengl-program.h>
|
|
#include <epoxy/gl.h>
|
|
|
|
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;
|
|
}
|