vg2 port: build script and resource loading
[fishladder.git] / render.h
1 static void init_mesh( struct mesh *m, float const *tris, u32 length ){
2 m->elements = length/3;
3 glGenVertexArrays( 1, &m->vao );
4 glGenBuffers( 1, &m->vbo );
5
6 glBindVertexArray( m->vao );
7 glBindBuffer( GL_ARRAY_BUFFER, m->vbo );
8 glBufferData( GL_ARRAY_BUFFER, length*sizeof(float), tris, GL_STATIC_DRAW );
9
10 glVertexAttribPointer( 0, 2, GL_FLOAT, GL_FALSE, 2*sizeof(float), (void*)0 );
11 glEnableVertexAttribArray( 0 );
12
13 VG_CHECK_GL();
14 }
15
16 static void free_mesh( struct mesh *m ){
17 glDeleteVertexArrays( 1, &m->vao );
18 glDeleteBuffers( 1, &m->vbo );
19 }
20
21 static void draw_mesh( int const start, int const count ){
22 glDrawArrays( GL_TRIANGLES, start*3, count*3 );
23 }
24
25 static void use_mesh( struct mesh *m ){
26 glBindVertexArray( m->vao );
27 }
28
29 #define TRANSFORM_TRI_2D( S, OX, OY, X1, Y1, X2, Y2, X3, Y3 ) \
30 X1*S+OX, Y1*S+OY, X2*S+OX, Y2*S+OY, X3*S+OX, Y3*S+OY
31
32 static void render_init(void){
33 // Combined quad, long quad / empty circle / filled circle mesh
34 float combined_mesh[6*6 + 32*6*3] = {
35 0.0f, 0.0f, 0.0f, 1.0f, 1.0f, 1.0f,
36 0.0f, 0.0f, 1.0f, 1.0f, 1.0f, 0.0f,
37
38 0.0f, 0.0f, 0.0f, 0.2f, 1.0f, 0.2f,
39 0.0f, 0.0f, 1.0f, 0.2f, 1.0f, 0.0f,
40
41 TRANSFORM_TRI_2D( 0.15f,0.05f,0.4f, 0.0f, 1.0f, 1.0f, 2.0f, 1.0f, 0.0f ),
42 TRANSFORM_TRI_2D( 0.15f,0.80f,0.4f, 0.0f, 0.0f, 0.0f, 2.0f, 1.0f, 1.0f )
43 };
44
45 float *circle_mesh = combined_mesh + 6*6;
46 int const res = 32;
47 for( int i=0; i < res; i ++ ){
48 v2f v0 = { sinf( ((float)i/(float)res)*VG_TAUf ),
49 cosf( ((float)i/(float)res)*VG_TAUf ) };
50 v2f v1 = { sinf( ((float)(i+1)/(float)res)*VG_TAUf ),
51 cosf( ((float)(i+1)/(float)res)*VG_TAUf ) };
52
53 circle_mesh[ i*6+0 ] = 0.0f;
54 circle_mesh[ i*6+1 ] = 0.0f;
55
56 v2_copy( v0, circle_mesh + 32*6 + i*12 );
57 v2_muls( v0, 0.8f, circle_mesh + 32*6 + i*12+2 );
58 v2_copy( v1, circle_mesh + 32*6 + i*12+4 );
59
60 v2_copy( v1, circle_mesh + 32*6 + i*12+6 );
61 v2_muls( v1, 0.8f, circle_mesh + 32*6 + i*12+8 );
62 v2_muls( v0, 0.8f, circle_mesh + 32*6 + i*12+10 );
63
64 v2_copy( v0, circle_mesh + i*6+4 );
65 v2_copy( v1, circle_mesh + i*6+2 );
66 v2_copy( v0, circle_mesh+i*6+4 );
67 v2_copy( v1, circle_mesh+i*6+2 );
68 }
69 init_mesh( &world.shapes, combined_mesh, vg_list_size( combined_mesh ) );
70
71 // Create wire mesh
72 int const num_segments = 64;
73 struct mesh_wire *mw = &world.wire;
74
75 v2f wire_points[ num_segments * 2 ];
76 u16 wire_indices[ 6*(num_segments-1) ];
77
78 for( int i = 0; i < num_segments; i ++ ){
79 float l = (float)i / (float)(num_segments-1);
80
81 v2_copy( (v2f){ l, -0.5f }, wire_points[i*2+0] );
82 v2_copy( (v2f){ l, 0.5f }, wire_points[i*2+1] );
83
84 if( i < num_segments-1 ){
85 wire_indices[ i*6+0 ] = i*2 + 0;
86 wire_indices[ i*6+1 ] = i*2 + 1;
87 wire_indices[ i*6+2 ] = i*2 + 3;
88 wire_indices[ i*6+3 ] = i*2 + 0;
89 wire_indices[ i*6+4 ] = i*2 + 3;
90 wire_indices[ i*6+5 ] = i*2 + 2;
91 }
92 }
93
94 glGenVertexArrays( 1, &mw->vao );
95 glGenBuffers( 1, &mw->vbo );
96 glGenBuffers( 1, &mw->ebo );
97 glBindVertexArray( mw->vao );
98
99 glBindBuffer( GL_ARRAY_BUFFER, mw->vbo );
100
101 glBufferData( GL_ARRAY_BUFFER, sizeof( wire_points ),
102 wire_points, GL_STATIC_DRAW );
103 glBindVertexArray( mw->vao );
104
105 glBindBuffer( GL_ELEMENT_ARRAY_BUFFER, mw->ebo );
106 glBufferData( GL_ELEMENT_ARRAY_BUFFER, sizeof( wire_indices ),
107 wire_indices, GL_STATIC_DRAW );
108
109 // XY
110 glVertexAttribPointer( 0, 2, GL_FLOAT, GL_FALSE, 2*sizeof(float), (void*)0 );
111 glEnableVertexAttribArray( 0 );
112
113 VG_CHECK_GL();
114 mw->em = vg_list_size( wire_indices );
115
116 // Create info data texture
117 glGenTextures( 1, &world.background_data );
118 glBindTexture( GL_TEXTURE_2D, world.background_data );
119 glTexImage2D( GL_TEXTURE_2D, 0, GL_RGBA, 64, 64, 0, GL_RGBA,
120 GL_UNSIGNED_BYTE, NULL );
121 vg_tex2d_nearest();
122
123 // Create random smaples texture
124 u8 *data = malloc(512*512*2);
125 for( int i = 0; i < 512*512*2; i ++ )
126 data[ i ] = rand()/(RAND_MAX/255);
127
128 glGenTextures( 1, &world.random_samples );
129 glBindTexture( GL_TEXTURE_2D, world.random_samples );
130 glTexImage2D( GL_TEXTURE_2D, 0, GL_RG, 512, 512, 0, GL_RG,
131 GL_UNSIGNED_BYTE, data );
132 vg_tex2d_nearest();
133 vg_tex2d_repeat();
134 free( data );
135
136 resource_load_main();
137
138 // Init world text
139 //ui_init_context( &world.st.world_text, 15000 );
140
141 // Restore gamestate
142 career_local_data_init();
143 career_load();
144
145 /* Create framebuffers */
146 glGenFramebuffers( 1, &world.st.framebuffer );
147 glBindFramebuffer( GL_FRAMEBUFFER, world.st.framebuffer );
148
149 glGenTextures( 1, &world.st.colourbuffer );
150 glBindTexture( GL_TEXTURE_2D, world.st.colourbuffer );
151 glTexImage2D( GL_TEXTURE_2D, 0, GL_RGB, vg_window_x, vg_window_y,
152 0, GL_RGB, GL_UNSIGNED_BYTE, NULL);
153
154 glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR );
155 glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
156 glFramebufferTexture2D( GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D,
157 world.st.colourbuffer, 0);
158
159 /* Bloom framebuffer (quater res) */
160 glGenFramebuffers( 2, world.st.bloomframebuffer );
161 glGenTextures( 2, world.st.bloomcolourbuffer );
162
163 for( int i=0; i<2; i++ ){
164 glBindFramebuffer( GL_FRAMEBUFFER, world.st.bloomframebuffer[i] );
165
166 glBindTexture( GL_TEXTURE_2D, world.st.bloomcolourbuffer[i] );
167 glTexImage2D( GL_TEXTURE_2D, 0, GL_RGB,
168 vg_window_x/EFFECT_BUFFER_RATIO, vg_window_y/EFFECT_BUFFER_RATIO,
169 0, GL_RGB, GL_UNSIGNED_BYTE, NULL);
170 vg_tex2d_clamp();
171
172 glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR );
173 glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
174 glFramebufferTexture2D( GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0,
175 GL_TEXTURE_2D, world.st.bloomcolourbuffer[i], 0);
176 }
177 }