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 /* TODO: Update this implementation */
15 #define QOI_IMPLEMENTATION
18 #include "phoboslab/qoi.h"
32 VG_STATIC
void vg_tex2d_bind( vg_tex2d
*tex
, u32 id
)
34 if( !(tex
->flags
& VG_TEXTURE_ALLOCATED_INTERNAL
) )
36 vg_error( "Tried to use '%s' while unloaded!\n", tex
->path
);
40 glActiveTexture( GL_TEXTURE0
+ id
);
41 glBindTexture( GL_TEXTURE_2D
, tex
->name
);
44 static inline void vg_tex2d_mipmap(void)
46 glGenerateMipmap( GL_TEXTURE_2D
);
49 static inline void vg_tex2d_linear(void)
51 glTexParameteri( GL_TEXTURE_2D
, GL_TEXTURE_MIN_FILTER
, GL_LINEAR
);
52 glTexParameteri( GL_TEXTURE_2D
, GL_TEXTURE_MAG_FILTER
, GL_LINEAR
);
55 static inline void vg_tex2d_nearest(void)
57 glTexParameteri( GL_TEXTURE_2D
, GL_TEXTURE_MIN_FILTER
, GL_NEAREST
);
58 glTexParameteri( GL_TEXTURE_2D
, GL_TEXTURE_MAG_FILTER
, GL_NEAREST
);
61 static inline void vg_tex2d_linear_mipmap(void)
63 glTexParameteri( GL_TEXTURE_2D
, GL_TEXTURE_MIN_FILTER
,
64 GL_LINEAR_MIPMAP_LINEAR
);
65 glTexParameteri( GL_TEXTURE_2D
, GL_TEXTURE_MAG_FILTER
, GL_LINEAR
);
68 static inline void vg_tex2d_repeat(void)
70 glTexParameteri( GL_TEXTURE_2D
, GL_TEXTURE_WRAP_S
, GL_REPEAT
);
71 glTexParameteri( GL_TEXTURE_2D
, GL_TEXTURE_WRAP_T
, GL_REPEAT
);
74 static inline void vg_tex2d_clamp(void)
76 glTexParameteri( GL_TEXTURE_2D
, GL_TEXTURE_WRAP_S
, GL_CLAMP_TO_EDGE
);
77 glTexParameteri( GL_TEXTURE_2D
, GL_TEXTURE_WRAP_T
, GL_CLAMP_TO_EDGE
);
80 VG_STATIC GLuint
vg_tex2d_rgba( const char *path
)
83 glGenTextures( 1, &texture_name
);
84 glBindTexture( GL_TEXTURE_2D
, texture_name
);
86 vg_linear_clear( vg_mem
.scratch
);
87 void *file
= vg_file_read( vg_mem
.scratch
, path
);
93 u32 size
= vg_file_size( vg_mem
.scratch
);
94 u8
*tex_buffer
= qoi_decode( file
, size
, &info
, 4 );
98 vg_info( "Texture decoded: [%u %u] %s\n",
99 info
.width
, info
.height
, path
);
101 glTexImage2D( GL_TEXTURE_2D
, 0, GL_RGBA
, info
.width
, info
.height
,
102 0, GL_RGBA
, GL_UNSIGNED_BYTE
, tex_buffer
);
104 /* TODO: pass through linear_alloc function */
105 QOI_FREE(tex_buffer
);
109 vg_error( "File size: %u\n", size
);
116 vg_error( "Loading texture failed (%s)\n", path
);
126 glTexImage2D( GL_TEXTURE_2D
, 0, GL_RGBA
, 2, 2,
127 0, GL_RGBA
, GL_UNSIGNED_BYTE
, tex_err
);
133 VG_STATIC
void vg_tex2d_init( vg_tex2d
*textures
[], int num
)
135 for( int i
=0; i
<num
; i
++ )
137 vg_tex2d
*tex
= textures
[i
];
138 tex
->name
= vg_tex2d_rgba( tex
->path
);
139 if( !(tex
->flags
& VG_TEXTURE_NO_MIP
) )
142 if( tex
->flags
& VG_TEXTURE_NEAREST
)
144 if( tex
->flags
& VG_TEXTURE_NO_MIP
)
145 vg_error( "Invalid texture settings\n" );
151 if( tex
->flags
& VG_TEXTURE_NO_MIP
)
154 vg_tex2d_linear_mipmap();
157 if( tex
->flags
& VG_TEXTURE_CLAMP
)
162 tex
->flags
|= VG_TEXTURE_ALLOCATED_INTERNAL
;
166 VG_STATIC
void vg_tex2d_free( vg_tex2d
*textures
[], int num
)
168 for( int i
= 0; i
< num
; i
++ )
170 glDeleteTextures( 1, &textures
[i
]->name
);
174 #endif /* VG_TEX_H */