void gen_player_buffers( GLuint *position_buffer, GLuint *normal_buffer, GLuint *uv_buffer, float x, float y, float z, float rx, float ry) { int faces = 6; glDeleteBuffers(1, position_buffer); glDeleteBuffers(1, normal_buffer); glDeleteBuffers(1, uv_buffer); GLfloat *position_data = malloc(sizeof(GLfloat) * faces * 18); GLfloat *normal_data = malloc(sizeof(GLfloat) * faces * 18); GLfloat *uv_data = malloc(sizeof(GLfloat) * faces * 12); make_player( position_data, normal_data, uv_data, x, y, z, rx, ry); *position_buffer = gen_buffer( GL_ARRAY_BUFFER, sizeof(GLfloat) * faces * 18, position_data ); *normal_buffer = gen_buffer( GL_ARRAY_BUFFER, sizeof(GLfloat) * faces * 18, normal_data ); *uv_buffer = gen_buffer( GL_ARRAY_BUFFER, sizeof(GLfloat) * faces * 12, uv_data ); free(position_data); free(normal_data); free(uv_data); }
void gen_item_buffers( GLuint *position_buffer, GLuint *normal_buffer, GLuint *uv_buffer, int w) { int faces = 6; glDeleteBuffers(1, position_buffer); glDeleteBuffers(1, normal_buffer); glDeleteBuffers(1, uv_buffer); GLfloat *position_data = malloc(sizeof(GLfloat) * faces * 18); GLfloat *normal_data = malloc(sizeof(GLfloat) * faces * 18); GLfloat *uv_data = malloc(sizeof(GLfloat) * faces * 12); make_cube( position_data, normal_data, uv_data, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0.5, w); *position_buffer = gen_buffer( GL_ARRAY_BUFFER, sizeof(GLfloat) * faces * 18, position_data ); *normal_buffer = gen_buffer( GL_ARRAY_BUFFER, sizeof(GLfloat) * faces * 18, normal_data ); *uv_buffer = gen_buffer( GL_ARRAY_BUFFER, sizeof(GLfloat) * faces * 12, uv_data ); free(position_data); free(normal_data); free(uv_data); }
GLuint gen_wireframe_buffer(float x, float y, float z, float n) { float data[144]; make_cube_wireframe(data, x, y, z, n); GLuint buffer = gen_buffer( GL_ARRAY_BUFFER, sizeof(data), data ); return buffer; }
Mesh gen_terrain_mesh(int samples_x, int samples_y) { vector<vec3> vertices; vector<uint32> indices; int i = 0; for (int y = 0; y < samples_y; y++) { for (int x = 0; x < samples_x; x++) { float x0 = -1.0f + 2.0f * x / (samples_x - 1); float y0 = -1.0f + 2.0f * y / (samples_y - 1); float x1 = -1.0f + 2.0f * (x + 1) / (samples_x - 1); float y1 = -1.0f + 2.0f * (y + 1) / (samples_y - 1); vertices.push_back(vec3(x0, 0.0f, y0)); vertices.push_back(vec3(x1, 0.0f, y0)); vertices.push_back(vec3(x1, 0.0f, y1)); vertices.push_back(vec3(x0, 0.0f, y1)); indices.push_back(i + 0); indices.push_back(i + 1); indices.push_back(i + 2); indices.push_back(i + 2); indices.push_back(i + 3); indices.push_back(i + 0); i += 4; } } Mesh mesh; mesh.num_indices = indices.size(); mesh.num_vertices = vertices.size(); mesh.vertex_buffer = gen_buffer(GL_ARRAY_BUFFER, vertices.size() * sizeof(vec3), &vertices[0], GL_STATIC_DRAW); mesh.index_buffer = gen_buffer(GL_ELEMENT_ARRAY_BUFFER, indices.size() * sizeof(uint32), &indices[0], GL_STATIC_DRAW); mesh.draw = [=]() { glBindBuffer(GL_ARRAY_BUFFER, mesh.vertex_buffer); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, mesh.index_buffer); attribfv("position", 3, 3, 0); glDrawElements(GL_TRIANGLES, mesh.num_indices, GL_UNSIGNED_INT, 0); }; return mesh; }
GLuint gen_crosshair_buffer(int width, int height) { int x = width / 2; int y = height / 2; int p = 10; float data[] = { x, y - p, x, y + p, x - p, y, x + p, y }; GLuint buffer = gen_buffer( GL_ARRAY_BUFFER, sizeof(data), data ); return buffer; }
void gen_chunk_buffers(Chunk *chunk) { Map *map = &chunk->map; glDeleteBuffers(1, &chunk->position_buffer); glDeleteBuffers(1, &chunk->normal_buffer); glDeleteBuffers(1, &chunk->uv_buffer); int faces = 0; MAP_FOR_EACH(map, e) { if (e->w <= 0) { continue; } int f1, f2, f3, f4, f5, f6; exposed_faces(map, e->x, e->y, e->z, &f1, &f2, &f3, &f4, &f5, &f6); int total = f1 + f2 + f3 + f4 + f5 + f6; if (is_plant(e->w)) { total = total ? 4 : 0; } faces += total; } END_MAP_FOR_EACH; GLfloat *position_data = malloc(sizeof(GLfloat) * faces * 18); GLfloat *normal_data = malloc(sizeof(GLfloat) * faces * 18); GLfloat *uv_data = malloc(sizeof(GLfloat) * faces * 12); int position_offset = 0; int uv_offset = 0; MAP_FOR_EACH(map, e) { if (e->w <= 0) { continue; } int f1, f2, f3, f4, f5, f6; exposed_faces(map, e->x, e->y, e->z, &f1, &f2, &f3, &f4, &f5, &f6); int total = f1 + f2 + f3 + f4 + f5 + f6; if (is_plant(e->w)) { total = total ? 4 : 0; } if (total == 0) { continue; } if (is_plant(e->w)) { float rotation = simplex3(e->x, e->y, e->z, 4, 0.5, 2) * 360; make_plant( position_data + position_offset, normal_data + position_offset, uv_data + uv_offset, e->x, e->y, e->z, 0.5, e->w, rotation); } else { make_cube( position_data + position_offset, normal_data + position_offset, uv_data + uv_offset, f1, f2, f3, f4, f5, f6, e->x, e->y, e->z, 0.5, e->w); } position_offset += total * 18; uv_offset += total * 12; } END_MAP_FOR_EACH; GLuint position_buffer = gen_buffer( GL_ARRAY_BUFFER, sizeof(GLfloat) * faces * 18, position_data ); GLuint normal_buffer = gen_buffer( GL_ARRAY_BUFFER, sizeof(GLfloat) * faces * 18, normal_data ); GLuint uv_buffer = gen_buffer( GL_ARRAY_BUFFER, sizeof(GLfloat) * faces * 12, uv_data ); free(position_data); free(normal_data); free(uv_data); chunk->faces = faces; chunk->dirty = 0; chunk->position_buffer = position_buffer; chunk->normal_buffer = normal_buffer; chunk->uv_buffer = uv_buffer; }
GLuint gen_faces(int components, int faces, GLfloat *data) { GLuint buffer = gen_buffer( sizeof(GLfloat) * 6 * components * faces, data); free(data); return buffer; }