1 /* Copyright (C) 2021-2022 Harry Godden (hgn) - All Rights Reserved */
8 #define VG_TEXTURE_NO_MIP 0x1
9 #define VG_TEXTURE_REPEAT 0x2
10 #define VG_TEXTURE_CLAMP 0x4
11 #define VG_TEXTURE_NEAREST 0x8
12 #define VG_TEXTURE_ALLOCATED_INTERNAL 0x10
14 VG_STATIC
void *vg_qoi_malloc( size_t size
)
16 return vg_linear_alloc( vg_mem
.scratch
, size
);
19 VG_STATIC
void vg_qoi_free( void *ptr
)
24 #define QOI_IMPLEMENTATION
26 #define QOI_MALLOC(sz) vg_qoi_malloc( sz )
27 #define QOI_FREE(p) vg_qoi_free( p )
29 #include "submodules/qoi/qoi.h"
43 VG_STATIC
void vg_tex2d_bind( vg_tex2d
*tex
, u32 id
)
45 if( !(tex
->flags
& VG_TEXTURE_ALLOCATED_INTERNAL
) )
47 vg_error( "Tried to use '%s' while unloaded!\n", tex
->path
);
51 glActiveTexture( GL_TEXTURE0
+ id
);
52 glBindTexture( GL_TEXTURE_2D
, tex
->name
);
55 static inline void vg_tex2d_mipmap(void)
57 glGenerateMipmap( GL_TEXTURE_2D
);
60 static inline void vg_tex2d_linear(void)
62 glTexParameteri( GL_TEXTURE_2D
, GL_TEXTURE_MIN_FILTER
, GL_LINEAR
);
63 glTexParameteri( GL_TEXTURE_2D
, GL_TEXTURE_MAG_FILTER
, GL_LINEAR
);
66 static inline void vg_tex2d_nearest(void)
68 glTexParameteri( GL_TEXTURE_2D
, GL_TEXTURE_MIN_FILTER
, GL_NEAREST
);
69 glTexParameteri( GL_TEXTURE_2D
, GL_TEXTURE_MAG_FILTER
, GL_NEAREST
);
72 static inline void vg_tex2d_linear_mipmap(void)
74 glTexParameteri( GL_TEXTURE_2D
, GL_TEXTURE_MIN_FILTER
,
75 GL_LINEAR_MIPMAP_LINEAR
);
76 glTexParameteri( GL_TEXTURE_2D
, GL_TEXTURE_MAG_FILTER
, GL_LINEAR
);
79 static inline void vg_tex2d_repeat(void)
81 glTexParameteri( GL_TEXTURE_2D
, GL_TEXTURE_WRAP_S
, GL_REPEAT
);
82 glTexParameteri( GL_TEXTURE_2D
, GL_TEXTURE_WRAP_T
, GL_REPEAT
);
85 static inline void vg_tex2d_clamp(void)
87 glTexParameteri( GL_TEXTURE_2D
, GL_TEXTURE_WRAP_S
, GL_CLAMP_TO_EDGE
);
88 glTexParameteri( GL_TEXTURE_2D
, GL_TEXTURE_WRAP_T
, GL_CLAMP_TO_EDGE
);
91 VG_STATIC GLuint
vg_tex2d_new(void)
94 glGenTextures( 1, &texture_name
);
95 glBindTexture( GL_TEXTURE_2D
, texture_name
);
100 VG_STATIC
void vg_tex2d_set_error(void)
110 glTexImage2D( GL_TEXTURE_2D
, 0, GL_RGBA
, 2, 2,
111 0, GL_RGBA
, GL_UNSIGNED_BYTE
, tex_err
);
114 VG_STATIC
void vg_tex2d_qoi( void *mem
, u32 size
, const char *name
)
117 u8
*tex_buffer
= qoi_decode( mem
, size
, &info
, 4 );
121 vg_info( "Texture decoded: [%u %u] %s\n",
122 info
.width
, info
.height
, name
);
124 glTexImage2D( GL_TEXTURE_2D
, 0, GL_RGBA
, info
.width
, info
.height
,
125 0, GL_RGBA
, GL_UNSIGNED_BYTE
, tex_buffer
);
127 QOI_FREE(tex_buffer
);
131 vg_error( "File size: %u\n", size
);
132 vg_tex2d_set_error();
136 VG_STATIC GLuint
vg_tex2d_rgba( const char *path
)
138 GLuint texture_name
= vg_tex2d_new();
140 vg_linear_clear( vg_mem
.scratch
);
142 void *file
= vg_file_read( vg_mem
.scratch
, path
, &size
);
146 vg_tex2d_qoi( file
, size
, path
);
150 vg_error( "Loading texture failed (%s)\n", path
);
151 vg_tex2d_set_error();
157 VG_STATIC
void vg_tex2d_init( vg_tex2d
*textures
[], int num
)
159 for( int i
=0; i
<num
; i
++ )
161 vg_tex2d
*tex
= textures
[i
];
162 tex
->name
= vg_tex2d_rgba( tex
->path
);
163 if( !(tex
->flags
& VG_TEXTURE_NO_MIP
) )
166 if( tex
->flags
& VG_TEXTURE_NEAREST
)
168 if( tex
->flags
& VG_TEXTURE_NO_MIP
)
169 vg_error( "Invalid texture settings\n" );
175 if( tex
->flags
& VG_TEXTURE_NO_MIP
)
178 vg_tex2d_linear_mipmap();
181 if( tex
->flags
& VG_TEXTURE_CLAMP
)
186 tex
->flags
|= VG_TEXTURE_ALLOCATED_INTERNAL
;
190 VG_STATIC
void vg_tex2d_free( vg_tex2d
*textures
[], int num
)
192 for( int i
= 0; i
< num
; i
++ )
194 glDeleteTextures( 1, &textures
[i
]->name
);
198 #endif /* VG_TEX_H */