4ab300cbcb3ee93f3be148da29ef25f29c2c17a1
[fishladder.git] / vg / vg_tex.h
1 // Copyright (C) 2021 Harry Godden (hgn) - All Rights Reserved
2
3 static inline void vg_tex2d_mipmap(void)
4 {
5 glGenerateMipmap( GL_TEXTURE_2D );
6 }
7
8 static inline void vg_tex2d_linear(void)
9 {
10 glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR );
11 glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
12 }
13
14 static inline void vg_tex2d_nearest(void)
15 {
16 glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST );
17 glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST );
18 }
19
20 static inline void vg_tex2d_linear_mipmap(void)
21 {
22 glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR );
23 glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
24 }
25
26 static inline void vg_tex2d_repeat(void)
27 {
28 glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT );
29 glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT );
30 }
31
32 static inline void vg_tex2d_clamp(void)
33 {
34 glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE );
35 glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE );
36 }
37
38 static GLuint vg_tex2d_rgba( const char *path )
39 {
40 int x,y,nc;
41 stbi_set_flip_vertically_on_load( 0 );
42
43 i64 length;
44 u8 *src_data = vg_asset_read_s( path, &length );
45
46 GLuint texture_name;
47 glGenTextures( 1, &texture_name );
48 glBindTexture( GL_TEXTURE_2D, texture_name );
49
50 if( src_data )
51 {
52 u8 *tex_buffer = stbi_load_from_memory( src_data, length, &x, &y, &nc, 4 );
53 glTexImage2D( GL_TEXTURE_2D, 0, GL_RGBA, x, y, 0, GL_RGBA, GL_UNSIGNED_BYTE, tex_buffer );
54
55 free( tex_buffer );
56 free( src_data );
57 }
58 else
59 {
60 u32 tex_err[4] =
61 {
62 0xffff00ff,
63 0xff000000,
64 0xff000000,
65 0xffff00ff
66 };
67
68 vg_error( "Loading texture failed (%s)\n", path );
69 glTexImage2D( GL_TEXTURE_2D, 0, GL_RGBA, 2, 2, 0, GL_RGBA, GL_UNSIGNED_BYTE, tex_err );
70 }
71
72 return texture_name;
73 }