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 stbi_set_flip_vertically_on_load( 1 );
62 u8
*src_data
= vg_asset_read_s( path
, &length
);
65 glGenTextures( 1, &texture_name
);
66 glBindTexture( GL_TEXTURE_2D
, texture_name
);
70 u8
*tex_buffer
= stbi_load_from_memory( src_data
, length
, &x
, &y
, &nc
, 4 );
71 glTexImage2D( GL_TEXTURE_2D
, 0, GL_RGBA
, x
, y
, 0, GL_RGBA
, GL_UNSIGNED_BYTE
, tex_buffer
);
86 vg_error( "Loading texture failed (%s)\n", path
);
87 glTexImage2D( GL_TEXTURE_2D
, 0, GL_RGBA
, 2, 2, 0, GL_RGBA
, GL_UNSIGNED_BYTE
, tex_err
);
93 static void vg_tex2d_init( vg_tex2d
*textures
[], int num
)
95 for( int i
= 0; i
< num
; i
++ )
97 vg_tex2d
*tex
= textures
[i
];
98 tex
->name
= vg_tex2d_rgba( tex
->path
);
99 if( !(tex
->flags
& VG_TEXTURE_NO_MIP
) )
102 if( tex
->flags
& VG_TEXTURE_NEAREST
)
104 if( tex
->flags
& VG_TEXTURE_NO_MIP
)
105 vg_error( "Invalid texture settings\n" );
111 if( tex
->flags
& VG_TEXTURE_NO_MIP
)
114 vg_tex2d_linear_mipmap();
117 if( tex
->flags
& VG_TEXTURE_CLAMP
)
124 static void vg_tex2d_free( vg_tex2d
*textures
[], int num
)
126 for( int i
= 0; i
< num
; i
++ )
128 glDeleteTextures( 1, &textures
[i
]->name
);