grid.c (3083B)
1 2 3 #include "grid.h" 4 #include "util.h" 5 6 // v1------v0 7 // | | 8 // | | 9 // | | 10 // v2------v3 11 12 static GLuint quad_indices[] = {0, 1, 2, 0, 2, 3}; 13 14 15 static GLfloat quad_normals[] = { 16 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1, // front 17 }; 18 19 static GLfloat quad_vertices[] = { 20 0.5, 0.5, 0.5, -0.5, 0.5, 0.5, -0.5,-0.5, 0.5, 0.5,-0.5, 0.5, // v0-v1-v2-v3 front 21 }; 22 23 static GLfloat quad_uvs[] = 24 { 25 1.0, 1.0, // v0 26 0.0, 1.0, // v1 27 0.0, 0.0, // v2 28 1.0, 0.0, // v3 29 }; 30 31 static void make_grid(float *verts, float vert_capacity, 32 u32 *indices, float index_capacity, 33 float *normals, float normal_capacity, 34 float *colors, float color_capacity, 35 float *tex_coords, float coords_capacity, 36 int width, int height, float cell_size) 37 { 38 int c = 0; 39 for (int x = 0; x < width; x++) { 40 for (int y = 0; y < height; y++, c++) { 41 int grid_ind = y * height + x; 42 int vind = grid_ind * ARRAY_SIZE(quad_vertices); 43 int iind = grid_ind * ARRAY_SIZE(quad_indices); 44 int i = 0; 45 46 for (i = 0; i < 4; i++) { 47 int nv = i*3; 48 assert(vind+2+nv < vert_capacity); 49 assert(vind+2+nv < color_capacity); 50 assert(vind+2+nv < normal_capacity); 51 verts[vind+0+nv] = (quad_vertices[0+nv] + x) * cell_size; 52 verts[vind+1+nv] = (quad_vertices[1+nv] + y) * cell_size; 53 verts[vind+2+nv] = quad_vertices[2+nv]; 54 55 // recenter 56 verts[vind+0+nv] -= (width * cell_size)/2.0 - 0.5 * cell_size; 57 verts[vind+1+nv] -= (height * cell_size)/2.0 - 0.5 * cell_size; 58 verts[vind+2+nv] -= 0.5; 59 60 normals[vind+0+nv] = quad_normals[0+nv]; 61 normals[vind+1+nv] = quad_normals[1+nv]; 62 normals[vind+2+nv] = quad_normals[2+nv]; 63 64 float checkers = (x ^ y) & 1; 65 colors[vind+0+nv] = checkers; 66 colors[vind+1+nv] = checkers; 67 colors[vind+2+nv] = checkers; 68 } 69 70 for (i = 0; i < 2; i++) { 71 int nv = i*3; 72 int cv = i*2; 73 74 assert(iind+2+nv < index_capacity); 75 indices[iind+0+nv] = quad_indices[0+nv] + 4*c; 76 indices[iind+1+nv] = quad_indices[1+nv] + 4*c; 77 indices[iind+2+nv] = quad_indices[2+nv] + 4*c; 78 79 tex_coords[iind+0+cv] = quad_uvs[0+cv]; 80 tex_coords[iind+1+cv] = quad_uvs[1+cv]; 81 } 82 } 83 } 84 } 85 86 void make_grid_geom(struct geometry *geom, int width, int height, float cell_size) 87 { 88 struct make_geometry mkgeom; 89 init_make_geometry(&mkgeom); 90 91 int wh = width * height; 92 int vert_size = 12 * wh; 93 int ind_size = 6 * wh; 94 int uv_size = 4 * wh; 95 96 float verts[vert_size]; // array_size(quad) 12 * w * h 97 float colors[vert_size]; // array_size(quad) 12 * w * h 98 float normals[vert_size]; // array_size(quad) 12 * w * h 99 float tex_coords[uv_size]; 100 u32 indices[ind_size]; // array_size(quad) 6 * w * h 101 102 make_grid(verts, vert_size, 103 indices, ind_size, 104 normals, vert_size, 105 colors, vert_size, 106 tex_coords, uv_size, 107 width, height, cell_size); 108 109 mkgeom.indices = indices; 110 mkgeom.vertices = verts; 111 mkgeom.normals = normals; 112 mkgeom.colors = colors; 113 mkgeom.tex_coords = tex_coords; 114 115 mkgeom.num_indices = ARRAY_SIZE(indices); 116 mkgeom.num_verts = ARRAY_SIZE(verts)/3; 117 mkgeom.num_uv_components = 2; 118 119 make_buffer_geometry(&mkgeom, geom); 120 check_gl(); 121 } 122