4c14540ca431a4b5404534dc5d44f27938e04467
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
20 static void vg_tex2d_bind( vg_tex2d
*tex
, u32 id
)
22 glActiveTexture( GL_TEXTURE0
+ id
);
23 glBindTexture( GL_TEXTURE_2D
, tex
->name
);
26 static inline void vg_tex2d_mipmap(void)
28 glGenerateMipmap( GL_TEXTURE_2D
);
31 static inline void vg_tex2d_linear(void)
33 glTexParameteri( GL_TEXTURE_2D
, GL_TEXTURE_MIN_FILTER
, GL_LINEAR
);
34 glTexParameteri( GL_TEXTURE_2D
, GL_TEXTURE_MAG_FILTER
, GL_LINEAR
);
37 static inline void vg_tex2d_nearest(void)
39 glTexParameteri( GL_TEXTURE_2D
, GL_TEXTURE_MIN_FILTER
, GL_NEAREST
);
40 glTexParameteri( GL_TEXTURE_2D
, GL_TEXTURE_MAG_FILTER
, GL_NEAREST
);
43 static inline void vg_tex2d_linear_mipmap(void)
45 glTexParameteri( GL_TEXTURE_2D
, GL_TEXTURE_MIN_FILTER
, GL_LINEAR_MIPMAP_LINEAR
);
46 glTexParameteri( GL_TEXTURE_2D
, GL_TEXTURE_MAG_FILTER
, GL_LINEAR
);
49 static inline void vg_tex2d_repeat(void)
51 glTexParameteri( GL_TEXTURE_2D
, GL_TEXTURE_WRAP_S
, GL_REPEAT
);
52 glTexParameteri( GL_TEXTURE_2D
, GL_TEXTURE_WRAP_T
, GL_REPEAT
);
55 static inline void vg_tex2d_clamp(void)
57 glTexParameteri( GL_TEXTURE_2D
, GL_TEXTURE_WRAP_S
, GL_CLAMP_TO_EDGE
);
58 glTexParameteri( GL_TEXTURE_2D
, GL_TEXTURE_WRAP_T
, GL_CLAMP_TO_EDGE
);
61 static GLuint
vg_tex2d_rgba( const char *path
)
64 u8
*src_data
= vg_asset_read_s( path
, &length
);
67 glGenTextures( 1, &texture_name
);
68 glBindTexture( GL_TEXTURE_2D
, texture_name
);
73 u8
*tex_buffer
= qoi_decode( src_data
, length
, &info
, 4 );
75 glTexImage2D( GL_TEXTURE_2D
, 0, GL_RGBA
, info
.width
, info
.height
, 0, GL_RGBA
, GL_UNSIGNED_BYTE
, tex_buffer
);
90 vg_error( "Loading texture failed (%s)\n", path
);
91 glTexImage2D( GL_TEXTURE_2D
, 0, GL_RGBA
, 2, 2, 0, GL_RGBA
, GL_UNSIGNED_BYTE
, tex_err
);
97 static void vg_tex2d_init( vg_tex2d
*textures
[], int num
)
99 for( int i
= 0; i
< num
; i
++ )
101 vg_tex2d
*tex
= textures
[i
];
102 tex
->name
= vg_tex2d_rgba( tex
->path
);
103 if( !(tex
->flags
& VG_TEXTURE_NO_MIP
) )
106 if( tex
->flags
& VG_TEXTURE_NEAREST
)
108 if( tex
->flags
& VG_TEXTURE_NO_MIP
)
109 vg_error( "Invalid texture settings\n" );
115 if( tex
->flags
& VG_TEXTURE_NO_MIP
)
118 vg_tex2d_linear_mipmap();
121 if( tex
->flags
& VG_TEXTURE_CLAMP
)
128 static void vg_tex2d_free( vg_tex2d
*textures
[], int num
)
130 for( int i
= 0; i
< num
; i
++ )
132 glDeleteTextures( 1, &textures
[i
]->name
);