Compare commits

..

3 Commits

8 changed files with 8094 additions and 2 deletions

View File

@@ -9,8 +9,9 @@ pkg_check_modules(EPOXY REQUIRED epoxy)
add_compile_options(-Wall -Wextra -Wold-style-declaration -Wuninitialized -Wmaybe-uninitialized -Wunused-parameter)
aux_source_directory("src" SRC_DIR)
aux_source_directory("stb" STB_DIR)
add_library(${PROJECT_NAME} SHARED ${SRC_DIR})
add_library(${PROJECT_NAME} SHARED ${SRC_DIR} ${STB_DIR})
target_include_directories(${PROJECT_NAME} PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/include)
target_include_directories(${PROJECT_NAME} PRIVATE ${EPOXY_INCLUDE_DIRS})
target_link_libraries(${PROJECT_NAME} PUBLIC ${GLIB_LDFLAGS} ${EPOXY_LDFLAGS} cglm)

View File

@@ -0,0 +1,53 @@
#ifndef _SHIMATTA_OPENGL_TEXTURE_H_
#define _SHIMATTA_OPENGL_TEXTURE_H_
#include <glib.h>
#include <glib-object.h>
#include <epoxy/gl.h>
G_BEGIN_DECLS
#define SHIMATTA_TYPE_OPENGL_TEXTURE (shimatta_opengl_texture_get_type())
G_DECLARE_DERIVABLE_TYPE(ShimattaOpenglTexture, shimatta_opengl_texture, SHIMATTA, OPENGL_TEXTURE, GObject);
struct _ShimattaOpenglTextureClass {
GObjectClass super;
void (*use)(ShimattaOpenglTexture *texture);
int (*use_in_slot)(ShimattaOpenglTexture *texture, unsigned int slot);
};
void shimatta_opengl_texture_use(ShimattaOpenglTexture *texture);
int shimatta_opengl_texture_use_in_slot(ShimattaOpenglTexture *texture, unsigned int slot);
ShimattaOpenglTexture *shimatta_opengl_texture_new(void);
int shimatta_opengl_texture_set_data(ShimattaOpenglTexture *texture, unsigned int width, unsigned int height,
GLenum format, void *buffer);
int shimatta_opengl_texture_set_data_from_image(ShimattaOpenglTexture *texture, const char *path);
void shimatta_opengl_texture_set_wrap_s(ShimattaOpenglTexture *self, int wrap_s);
void shimatta_opengl_texture_set_wrap_t(ShimattaOpenglTexture *self, int wrap_t);
void shimatta_opengl_texture_set_min_filter(ShimattaOpenglTexture *self, int min_filter);
void shimatta_opengl_texture_set_mag_filter(ShimattaOpenglTexture *self, int mag_filter);
void shimatta_opengl_texture_set_gen_mipmap(ShimattaOpenglTexture *self, gboolean gen_mipmap);
int shimatta_opengl_texture_get_wrap_s(ShimattaOpenglTexture *self);
int shimatta_opengl_texture_get_wrap_t(ShimattaOpenglTexture *self);
int shimatta_opengl_texture_get_mag_filter(ShimattaOpenglTexture *self);
int shimatta_opengl_texture_get_min_filter(ShimattaOpenglTexture *self);
gboolean shimatta_opengl_texture_get_gen_mipmap(ShimattaOpenglTexture *self);
G_END_DECLS
#endif /* _SHIMATTA_OPENGL_TEXTURE_H_ */

7656
include/stb/stb_image.h Normal file

File diff suppressed because it is too large Load Diff

View File

@@ -69,14 +69,41 @@ static void draw_with_model_matrix(ShimattaOpenglGraphics *self, mat4 m, mat4 n)
draw(self);
}
static void shimatta_opengl_colored_triangle_dispose(GObject *obj)
{
ShimattaOpenglColoredTriangle *self;
ShimattaOpenglColoredTrianglePrivate *priv;
self = SHIMATTA_OPENGL_COLORED_TRIANGLE(obj);
priv = shimatta_opengl_colored_triangle_get_instance_private(self);
if (priv->vao) {
glDeleteVertexArrays(1, &priv->vao);
priv->vao = 0;
}
if (priv->vbo) {
glDeleteBuffers(1, &priv->vbo);
priv->vbo = 0;
}
g_clear_object(&priv->prog);
G_OBJECT_CLASS(shimatta_opengl_colored_triangle_parent_class)->dispose(obj);
}
void shimatta_opengl_colored_triangle_class_init(ShimattaOpenglColoredTriangleClass *klass)
{
ShimattaOpenglGraphicsClass *gclass;
GObjectClass *oclass;
gclass = SHIMATTA_OPENGL_GRAPHICS_CLASS(klass);
oclass = G_OBJECT_CLASS(klass);
gclass->draw = draw;
gclass->realize = realize;
gclass->draw_with_model_matrix = draw_with_model_matrix;
oclass->dispose = shimatta_opengl_colored_triangle_dispose;
}
void shimatta_opengl_colored_triangle_init(ShimattaOpenglColoredTriangle *self)

View File

@@ -24,7 +24,7 @@ G_DEFINE_TYPE_WITH_PRIVATE(ShimattaOpenglGraphics, shimatta_opengl_graphics, G_T
static void shimatta_opengl_graphics_dispose(GObject *self)
{
(void)self;
G_OBJECT_CLASS(shimatta_opengl_graphics_parent_class)->dispose(self);
}
static void draw_dummy(ShimattaOpenglGraphics *graphics)

View File

@@ -64,6 +64,8 @@ static void shimatta_opengl_program_dispose(GObject *self)
if (priv->geometry_file)
g_free(priv->geometry_file);
G_OBJECT_CLASS(shimatta_opengl_program_parent_class)->dispose(self);
}
static void shimatta_opengl_program_class_init(ShimattaOpenglProgramClass *klass)

View File

@@ -0,0 +1,351 @@
#include <shimatta-opengl-texture.h>
#include <stb/stb_image.h>
#include <epoxy/gl.h>
typedef struct {
gboolean generate_mipmap;
GLuint texture_id;
} ShimattaOpenglTexturePrivate;
enum shimatta_opengl_texture_properties {
PROP_TEXTURE_WRAP_S = 1,
PROP_TEXTURE_WRAP_T,
PROP_TEXTURE_MIN_FILTER,
PROP_TEXTURE_MAG_FILTER,
PROP_TEXTURE_GEN_MIPMAP,
N_PROPS_TEXTURE
};
G_DEFINE_TYPE_WITH_PRIVATE(ShimattaOpenglTexture, shimatta_opengl_texture, G_TYPE_OBJECT);
static int shimatta_opengl_texure_use_default_impl(ShimattaOpenglTexture *texture, unsigned int slot, gboolean use_slot)
{
ShimattaOpenglTexturePrivate *priv;
g_return_val_if_fail(SHIMATTA_IS_OPENGL_TEXTURE(texture), -1001);
priv = shimatta_opengl_texture_get_instance_private(texture);
if (use_slot) {
if (slot > 31)
return -1002;
else
glActiveTexture(GL_TEXTURE0+slot);
}
glBindTexture(GL_TEXTURE_2D, priv->texture_id);
return 0;
}
static int use_in_slot_default(ShimattaOpenglTexture *texture, unsigned int slot)
{
return shimatta_opengl_texure_use_default_impl(texture, slot, TRUE);
}
static void use_default(ShimattaOpenglTexture *texture)
{
(void)shimatta_opengl_texure_use_default_impl(texture, 0U, FALSE);
}
static void shimatta_opengl_texture_dispose(ShimattaOpenglTexture *self)
{
ShimattaOpenglTexturePrivate *priv;
priv = shimatta_opengl_texture_get_instance_private(self);
if (priv->texture_id) {
glDeleteTextures(1, &priv->texture_id);
priv->texture_id = 0;
}
G_OBJECT_CLASS(shimatta_opengl_texture_parent_class)->dispose(G_OBJECT(self));
}
static void shimatta_opengl_texture_get_property(GObject *obj, guint id, GValue *val, GParamSpec *spec)
{
ShimattaOpenglTexture *self;
self = SHIMATTA_OPENGL_TEXTURE(obj);
switch (id) {
case PROP_TEXTURE_WRAP_S:
g_value_set_int(val, shimatta_opengl_texture_get_wrap_s(self));
break;
case PROP_TEXTURE_WRAP_T:
g_value_set_int(val, shimatta_opengl_texture_get_wrap_t(self));
break;
case PROP_TEXTURE_GEN_MIPMAP:
g_value_set_boolean(val, shimatta_opengl_texture_get_gen_mipmap(self));
break;
case PROP_TEXTURE_MAG_FILTER:
g_value_set_int(val, shimatta_opengl_texture_get_mag_filter(self));
break;
case PROP_TEXTURE_MIN_FILTER:
g_value_set_int(val, shimatta_opengl_texture_get_min_filter(self));
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID(obj, id, spec);
break;
}
}
static void shimatta_opengl_texture_set_property(GObject *obj, guint id, const GValue *value, GParamSpec *spec)
{
ShimattaOpenglTexture *self;
self = SHIMATTA_OPENGL_TEXTURE(obj);
switch (id) {
case PROP_TEXTURE_WRAP_S:
shimatta_opengl_texture_set_wrap_s(self, g_value_get_int(value));
break;
case PROP_TEXTURE_WRAP_T:
shimatta_opengl_texture_set_wrap_t(self, g_value_get_int(value));
break;
case PROP_TEXTURE_GEN_MIPMAP:
shimatta_opengl_texture_set_gen_mipmap(self, g_value_get_boolean(value));
break;
case PROP_TEXTURE_MAG_FILTER:
shimatta_opengl_texture_set_mag_filter(self, g_value_get_int(value));
break;
case PROP_TEXTURE_MIN_FILTER:
shimatta_opengl_texture_set_min_filter(self, g_value_get_int(value));
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID(obj, id, spec);
break;
}
}
static GParamSpec *shimatta_opengl_texture_props[N_PROPS_TEXTURE] = {NULL, };
static void shimatta_opengl_texture_class_init(ShimattaOpenglTextureClass *klass)
{
GObjectClass *gclass;
gclass = G_OBJECT_CLASS(klass);
klass->use = use_default;
klass->use_in_slot = use_in_slot_default;
gclass->dispose = (void (*)(GObject *))shimatta_opengl_texture_dispose;
gclass->set_property = shimatta_opengl_texture_set_property;
gclass->get_property = shimatta_opengl_texture_get_property;
shimatta_opengl_texture_props[PROP_TEXTURE_WRAP_S] =
g_param_spec_int("wrap-s", "WRAP_S", "Wrapping in S",
0, INT_MAX, GL_REPEAT, G_PARAM_READWRITE);
shimatta_opengl_texture_props[PROP_TEXTURE_WRAP_T] =
g_param_spec_int("wrap-t", "WRAP_T", "Wrapping in T",
0, INT_MAX, GL_REPEAT, G_PARAM_READWRITE);
shimatta_opengl_texture_props[PROP_TEXTURE_MIN_FILTER] =
g_param_spec_int("min-filter", "MIN_FILTER", "Min filtering",
0, INT_MAX, GL_LINEAR, G_PARAM_READWRITE);
shimatta_opengl_texture_props[PROP_TEXTURE_MAG_FILTER] =
g_param_spec_int("max-filter", "MAG_FILTER", "Mag filtering",
0, INT_MAX, GL_LINEAR, G_PARAM_READWRITE);
shimatta_opengl_texture_props[PROP_TEXTURE_GEN_MIPMAP] =
g_param_spec_boolean("generate-mipmap", "gen-mipmap", "Generate Mipmap for texture",
TRUE, G_PARAM_READWRITE);
g_object_class_install_properties(gclass, N_PROPS_TEXTURE, shimatta_opengl_texture_props);
}
static void shimatta_opengl_texture_init(ShimattaOpenglTexture *self)
{
ShimattaOpenglTexturePrivate *priv;
priv = shimatta_opengl_texture_get_instance_private(self);
priv->texture_id = 0;
glGenTextures(1, &priv->texture_id);
glBindTexture(GL_TEXTURE_2D, priv->texture_id);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
}
void shimatta_opengl_texture_use(ShimattaOpenglTexture *texture)
{
ShimattaOpenglTextureClass *klass;
g_return_if_fail(SHIMATTA_IS_OPENGL_TEXTURE(texture));
klass = SHIMATTA_OPENGL_TEXTURE_GET_CLASS(texture);
klass->use(texture);
}
int shimatta_opengl_texture_use_in_slot(ShimattaOpenglTexture *texture, unsigned int slot)
{
ShimattaOpenglTextureClass *klass;
g_return_val_if_fail(SHIMATTA_IS_OPENGL_TEXTURE(texture), -1001);
klass = SHIMATTA_OPENGL_TEXTURE_GET_CLASS(texture);
return klass->use_in_slot(texture, slot);
}
ShimattaOpenglTexture *shimatta_opengl_texture_new()
{
return g_object_new(SHIMATTA_TYPE_OPENGL_TEXTURE, NULL);
}
int shimatta_opengl_texture_set_data(ShimattaOpenglTexture *texture, unsigned int width, unsigned int height,
GLenum format, void *buffer)
{
ShimattaOpenglTexturePrivate *priv;
g_return_val_if_fail(SHIMATTA_IS_OPENGL_TEXTURE(texture), -1001);
g_return_val_if_fail(!!buffer, -1005);
priv = shimatta_opengl_texture_get_instance_private(texture);
shimatta_opengl_texture_use(texture);
glTexImage2D(GL_TEXTURE_2D, 0, format, width, height, 0, format, GL_UNSIGNED_BYTE, buffer);
if (priv->generate_mipmap)
glGenerateMipmap(GL_TEXTURE_2D);
return 0;
}
int shimatta_opengl_texture_set_data_from_image(ShimattaOpenglTexture *texture, const char *path)
{
int ret = -1;
unsigned char *data;
int width, height;
int nr_channels;
GLenum format;
data = stbi_load(path, &width, &height, &nr_channels, 0);
if (!data)
goto go_out;
switch (nr_channels) {
case 1:
format = GL_RED;
break;
case 3:
format = GL_RGB;
break;
case 4:
format = GL_RGBA;
break;
}
shimatta_opengl_texture_set_data(texture, width, height, format, data);
ret = 0;
stbi_image_free(data);
go_out:
return ret;
}
void shimatta_opengl_texture_set_wrap_s(ShimattaOpenglTexture *self, int wrap_s)
{
g_return_if_fail(SHIMATTA_IS_OPENGL_TEXTURE(self));
shimatta_opengl_texture_use(self);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, wrap_s);
g_object_notify_by_pspec(G_OBJECT(self), shimatta_opengl_texture_props[PROP_TEXTURE_WRAP_S]);
}
void shimatta_opengl_texture_set_wrap_t(ShimattaOpenglTexture *self, int wrap_t)
{
g_return_if_fail(SHIMATTA_IS_OPENGL_TEXTURE(self));
shimatta_opengl_texture_use(self);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, wrap_t);
g_object_notify_by_pspec(G_OBJECT(self), shimatta_opengl_texture_props[PROP_TEXTURE_WRAP_T]);
}
void shimatta_opengl_texture_set_min_filter(ShimattaOpenglTexture *self, int min_filter)
{
g_return_if_fail(SHIMATTA_IS_OPENGL_TEXTURE(self));
shimatta_opengl_texture_use(self);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, min_filter);
g_object_notify_by_pspec(G_OBJECT(self), shimatta_opengl_texture_props[PROP_TEXTURE_MIN_FILTER]);
}
void shimatta_opengl_texture_set_mag_filter(ShimattaOpenglTexture *self, int mag_filter)
{
g_return_if_fail(SHIMATTA_IS_OPENGL_TEXTURE(self));
shimatta_opengl_texture_use(self);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, mag_filter);
g_object_notify_by_pspec(G_OBJECT(self), shimatta_opengl_texture_props[PROP_TEXTURE_MAG_FILTER]);
}
void shimatta_opengl_texture_set_gen_mipmap(ShimattaOpenglTexture *self, gboolean gen_mipmap)
{
ShimattaOpenglTexturePrivate *priv;
priv = shimatta_opengl_texture_get_instance_private(self);
g_return_if_fail(!!priv);
priv->generate_mipmap = gen_mipmap;
}
int shimatta_opengl_texture_get_wrap_s(ShimattaOpenglTexture *self)
{
GLint val;
g_return_val_if_fail(SHIMATTA_IS_OPENGL_TEXTURE(self), -1);
shimatta_opengl_texture_use(self);
glGetTexParameteriv(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, &val);
return val;
}
int shimatta_opengl_texture_get_wrap_t(ShimattaOpenglTexture *self)
{
GLint val;
g_return_val_if_fail(SHIMATTA_IS_OPENGL_TEXTURE(self), -1);
shimatta_opengl_texture_use(self);
glGetTexParameteriv(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, &val);
return val;
}
int shimatta_opengl_texture_get_mag_filter(ShimattaOpenglTexture *self)
{
GLint val;
g_return_val_if_fail(SHIMATTA_IS_OPENGL_TEXTURE(self), -1);
shimatta_opengl_texture_use(self);
glGetTexParameteriv(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, &val);
return val;
}
int shimatta_opengl_texture_get_min_filter(ShimattaOpenglTexture *self)
{
GLint val;
g_return_val_if_fail(SHIMATTA_IS_OPENGL_TEXTURE(self), -1);
shimatta_opengl_texture_use(self);
glGetTexParameteriv(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, &val);
return val;
}
gboolean shimatta_opengl_texture_get_gen_mipmap(ShimattaOpenglTexture *self)
{
ShimattaOpenglTexturePrivate *priv;
g_return_val_if_fail(SHIMATTA_IS_OPENGL_TEXTURE(self), -1);
priv = shimatta_opengl_texture_get_instance_private(self);
return priv->generate_mipmap;
}

2
stb/stb-implementation.c Normal file
View File

@@ -0,0 +1,2 @@
#define STB_IMAGE_IMPLEMENTATION
#include <stb/stb_image.h>