1 // Copyright (C) 2021 Harry Godden (hgn) - All Rights Reserved
3 #define VG_TEXTURE_NO_MIP 0x1
4 #define VG_TEXTURE_REPEAT 0x2
5 #define VG_TEXTURE_CLAMP 0x4
6 #define VG_TEXTURE_NEAREST 0x8
15 static void vg_tex2d_bind( vg_tex2d
*tex
, u32 id
)
17 glActiveTexture( GL_TEXTURE0
+ id
);
18 glBindTexture( GL_TEXTURE_2D
, tex
->name
);
21 static inline void vg_tex2d_mipmap(void)
23 glGenerateMipmap( GL_TEXTURE_2D
);
26 static inline void vg_tex2d_linear(void)
28 glTexParameteri( GL_TEXTURE_2D
, GL_TEXTURE_MIN_FILTER
, GL_LINEAR
);
29 glTexParameteri( GL_TEXTURE_2D
, GL_TEXTURE_MAG_FILTER
, GL_LINEAR
);
32 static inline void vg_tex2d_nearest(void)
34 glTexParameteri( GL_TEXTURE_2D
, GL_TEXTURE_MIN_FILTER
, GL_NEAREST
);
35 glTexParameteri( GL_TEXTURE_2D
, GL_TEXTURE_MAG_FILTER
, GL_NEAREST
);
38 static inline void vg_tex2d_linear_mipmap(void)
40 glTexParameteri( GL_TEXTURE_2D
, GL_TEXTURE_MIN_FILTER
, GL_LINEAR_MIPMAP_LINEAR
);
41 glTexParameteri( GL_TEXTURE_2D
, GL_TEXTURE_MAG_FILTER
, GL_LINEAR
);
44 static inline void vg_tex2d_repeat(void)
46 glTexParameteri( GL_TEXTURE_2D
, GL_TEXTURE_WRAP_S
, GL_REPEAT
);
47 glTexParameteri( GL_TEXTURE_2D
, GL_TEXTURE_WRAP_T
, GL_REPEAT
);
50 static inline void vg_tex2d_clamp(void)
52 glTexParameteri( GL_TEXTURE_2D
, GL_TEXTURE_WRAP_S
, GL_CLAMP_TO_EDGE
);
53 glTexParameteri( GL_TEXTURE_2D
, GL_TEXTURE_WRAP_T
, GL_CLAMP_TO_EDGE
);
56 static GLuint
vg_tex2d_rgba( const char *path
)
59 u8
*src_data
= vg_asset_read_s( path
, &length
);
62 glGenTextures( 1, &texture_name
);
63 glBindTexture( GL_TEXTURE_2D
, texture_name
);
68 u8
*tex_buffer
= qoi_decode( src_data
, length
, &info
, 4 );
70 glTexImage2D( GL_TEXTURE_2D
, 0, GL_RGBA
, info
.width
, info
.height
, 0, GL_RGBA
, GL_UNSIGNED_BYTE
, tex_buffer
);
85 vg_error( "Loading texture failed (%s)\n", path
);
86 glTexImage2D( GL_TEXTURE_2D
, 0, GL_RGBA
, 2, 2, 0, GL_RGBA
, GL_UNSIGNED_BYTE
, tex_err
);
92 static void vg_tex2d_init( vg_tex2d
*textures
[], int num
)
94 for( int i
= 0; i
< num
; i
++ )
96 vg_tex2d
*tex
= textures
[i
];
97 tex
->name
= vg_tex2d_rgba( tex
->path
);
98 if( !(tex
->flags
& VG_TEXTURE_NO_MIP
) )
101 if( tex
->flags
& VG_TEXTURE_NEAREST
)
103 if( tex
->flags
& VG_TEXTURE_NO_MIP
)
104 vg_error( "Invalid texture settings\n" );
110 if( tex
->flags
& VG_TEXTURE_NO_MIP
)
113 vg_tex2d_linear_mipmap();
116 if( tex
->flags
& VG_TEXTURE_CLAMP
)
123 static void vg_tex2d_free( vg_tex2d
*textures
[], int num
)
125 for( int i
= 0; i
< num
; i
++ )
127 glDeleteTextures( 1, &textures
[i
]->name
);