Subastra
Loading...
Searching...
No Matches
image.h
Go to the documentation of this file.
1#ifndef __H__RENDERING_IMAGE__
2#define __H__RENDERING_IMAGE__
3
4#include <GLFW/glfw3.h>
5#include <stb/stb_image.h>
6
7#include "../memory.h"
8
10typedef struct {
13 byte *raw;
14 u32 width, height, channel;
15 GLuint gl;
16} image_t;
17
21static void image_init(image_t *image, allocator_t alloc, const void *buffer,
22 sz size) {
23 int width, height, channels;
24
25 image->alloc = alloc;
26
27 // TODO: decide if the texture should be premultiplied
28 void *temp = stbi_load_from_memory((const stbi_uc *)buffer, size, &width,
29 &height, &channels, 4);
30
31 image->raw =
32 allocator_alloc_copy_ty(byte, alloc, temp, width * height * channels * 2);
33 stbi_image_free(temp);
34
35 image->width = width;
36 image->height = height;
37 image->channel = channels;
38
39 glGenTextures(1, &image->gl);
40 glBindTexture(GL_TEXTURE_2D, image->gl);
41 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
42 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
43 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
44 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
45
46 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, image->width, image->height, 0, GL_RGBA,
47 GL_UNSIGNED_BYTE, image->raw);
48 glBindTexture(GL_TEXTURE_2D, 0);
49}
50
51static void image_bind(image_t *image) {
52 glActiveTexture(GL_TEXTURE0);
53 glBindTexture(GL_TEXTURE_2D, image->gl);
54}
55
57static void image_cleanup(image_t *image) {
58 allocator_free(image->alloc, image->raw);
59}
60
61#endif
size_t sz
Definition defs.h:51
uint32_t u32
Definition defs.h:45
static void image_bind(image_t *image)
Definition image.h:51
A generic allocator type passed by value. Contains a fallback allocator and a set of function pointer...
Definition memory.h:30
The raw data of an image.
Definition image.h:10
static void image_init(image_t *image, allocator_t alloc, const void *buffer, sz size)
Reinterprets the buffer as the contents of the image file and loads the raw image into memory.
Definition image.h:21
u32 channel
Definition image.h:14
GLuint gl
Definition image.h:15
u32 width
Definition image.h:14
byte * raw
Definition image.h:13
static void image_cleanup(image_t *image)
Definition image.h:57
allocator_t alloc
The allocator that's used for the internal raw buffer.
Definition image.h:12
u32 height
Definition image.h:14