1 /* Copyright (C) 2021-2022 Harry Godden (hgn) - All Rights Reserved */
5 #define VG_TEXTURE_NO_MIP 0x1
6 #define VG_TEXTURE_REPEAT 0x2
7 #define VG_TEXTURE_CLAMP 0x4
8 #define VG_TEXTURE_NEAREST 0x8
22 static void vg_tex2d_bind( vg_tex2d
*tex
, u32 id
)
24 glActiveTexture( GL_TEXTURE0
+ id
);
25 glBindTexture( GL_TEXTURE_2D
, tex
->name
);
28 static inline void vg_tex2d_mipmap(void)
30 glGenerateMipmap( GL_TEXTURE_2D
);
33 static inline void vg_tex2d_linear(void)
35 glTexParameteri( GL_TEXTURE_2D
, GL_TEXTURE_MIN_FILTER
, GL_LINEAR
);
36 glTexParameteri( GL_TEXTURE_2D
, GL_TEXTURE_MAG_FILTER
, GL_LINEAR
);
39 static inline void vg_tex2d_nearest(void)
41 glTexParameteri( GL_TEXTURE_2D
, GL_TEXTURE_MIN_FILTER
, GL_NEAREST
);
42 glTexParameteri( GL_TEXTURE_2D
, GL_TEXTURE_MAG_FILTER
, GL_NEAREST
);
45 static inline void vg_tex2d_linear_mipmap(void)
47 glTexParameteri( GL_TEXTURE_2D
, GL_TEXTURE_MIN_FILTER
,
48 GL_LINEAR_MIPMAP_LINEAR
);
49 glTexParameteri( GL_TEXTURE_2D
, GL_TEXTURE_MAG_FILTER
, GL_LINEAR
);
52 static inline void vg_tex2d_repeat(void)
54 glTexParameteri( GL_TEXTURE_2D
, GL_TEXTURE_WRAP_S
, GL_REPEAT
);
55 glTexParameteri( GL_TEXTURE_2D
, GL_TEXTURE_WRAP_T
, GL_REPEAT
);
58 static inline void vg_tex2d_clamp(void)
60 glTexParameteri( GL_TEXTURE_2D
, GL_TEXTURE_WRAP_S
, GL_CLAMP_TO_EDGE
);
61 glTexParameteri( GL_TEXTURE_2D
, GL_TEXTURE_WRAP_T
, GL_CLAMP_TO_EDGE
);
64 static GLuint
vg_tex2d_rgba( const char *path
)
67 u8
*src_data
= vg_asset_read_s( path
, &length
);
70 glGenTextures( 1, &texture_name
);
71 glBindTexture( GL_TEXTURE_2D
, texture_name
);
76 u8
*tex_buffer
= qoi_decode( src_data
, length
, &info
, 4 );
78 glTexImage2D( GL_TEXTURE_2D
, 0, GL_RGBA
, info
.width
, info
.height
,
79 0, GL_RGBA
, GL_UNSIGNED_BYTE
, tex_buffer
);
94 vg_error( "Loading texture failed (%s)\n", path
);
95 glTexImage2D( GL_TEXTURE_2D
, 0, GL_RGBA
, 2, 2,
96 0, GL_RGBA
, GL_UNSIGNED_BYTE
, tex_err
);
102 static void vg_tex2d_init( vg_tex2d
*textures
[], int num
)
104 for( int i
= 0; i
< num
; i
++ )
106 vg_tex2d
*tex
= textures
[i
];
107 tex
->name
= vg_tex2d_rgba( tex
->path
);
108 if( !(tex
->flags
& VG_TEXTURE_NO_MIP
) )
111 if( tex
->flags
& VG_TEXTURE_NEAREST
)
113 if( tex
->flags
& VG_TEXTURE_NO_MIP
)
114 vg_error( "Invalid texture settings\n" );
120 if( tex
->flags
& VG_TEXTURE_NO_MIP
)
123 vg_tex2d_linear_mipmap();
126 if( tex
->flags
& VG_TEXTURE_CLAMP
)
133 static void vg_tex2d_free( vg_tex2d
*textures
[], int num
)
135 for( int i
= 0; i
< num
; i
++ )
137 glDeleteTextures( 1, &textures
[i
]->name
);
141 #endif /* VG_TEX_H */