c757b1826a42996aa643fe51259195e5d7d66c3f
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
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
, GL_LINEAR_MIPMAP_LINEAR
);
48 glTexParameteri( GL_TEXTURE_2D
, GL_TEXTURE_MAG_FILTER
, GL_LINEAR
);
51 static inline void vg_tex2d_repeat(void)
53 glTexParameteri( GL_TEXTURE_2D
, GL_TEXTURE_WRAP_S
, GL_REPEAT
);
54 glTexParameteri( GL_TEXTURE_2D
, GL_TEXTURE_WRAP_T
, GL_REPEAT
);
57 static inline void vg_tex2d_clamp(void)
59 glTexParameteri( GL_TEXTURE_2D
, GL_TEXTURE_WRAP_S
, GL_CLAMP_TO_EDGE
);
60 glTexParameteri( GL_TEXTURE_2D
, GL_TEXTURE_WRAP_T
, GL_CLAMP_TO_EDGE
);
63 static GLuint
vg_tex2d_rgba( const char *path
)
66 u8
*src_data
= vg_asset_read_s( path
, &length
);
69 glGenTextures( 1, &texture_name
);
70 glBindTexture( GL_TEXTURE_2D
, texture_name
);
75 u8
*tex_buffer
= qoi_decode( src_data
, length
, &info
, 4 );
77 glTexImage2D( GL_TEXTURE_2D
, 0, GL_RGBA
, info
.width
, info
.height
, 0, GL_RGBA
, GL_UNSIGNED_BYTE
, tex_buffer
);
92 vg_error( "Loading texture failed (%s)\n", path
);
93 glTexImage2D( GL_TEXTURE_2D
, 0, GL_RGBA
, 2, 2, 0, GL_RGBA
, GL_UNSIGNED_BYTE
, tex_err
);
99 static void vg_tex2d_init( vg_tex2d
*textures
[], int num
)
101 for( int i
= 0; i
< num
; i
++ )
103 vg_tex2d
*tex
= textures
[i
];
104 tex
->name
= vg_tex2d_rgba( tex
->path
);
105 if( !(tex
->flags
& VG_TEXTURE_NO_MIP
) )
108 if( tex
->flags
& VG_TEXTURE_NEAREST
)
110 if( tex
->flags
& VG_TEXTURE_NO_MIP
)
111 vg_error( "Invalid texture settings\n" );
117 if( tex
->flags
& VG_TEXTURE_NO_MIP
)
120 vg_tex2d_linear_mipmap();
123 if( tex
->flags
& VG_TEXTURE_CLAMP
)
130 static void vg_tex2d_free( vg_tex2d
*textures
[], int num
)
132 for( int i
= 0; i
< num
; i
++ )
134 glDeleteTextures( 1, &textures
[i
]->name
);