bool build(ilG_renderman *rm, char **error) { this->rm = rm; ilG_material m; ilG_material_init(&m); ilG_material_name(&m, "Teapot Material"); ilG_material_arrayAttrib(&m, ILG_MESH_POS, "in_Position"); ilG_material_arrayAttrib(&m, ILG_MESH_TEX, "in_Texcoord"); ilG_material_arrayAttrib(&m, ILG_MESH_NORM, "in_Normal"); ilG_material_arrayAttrib(&m, ILG_MESH_DIFFUSE, "in_Diffuse"); ilG_material_arrayAttrib(&m, ILG_MESH_SPECULAR, "in_Specular"); ilG_material_textureUnit(&m, 0, "tex"); ilG_material_fragData(&m, ILG_GBUFFER_ALBEDO, "out_Albedo"); ilG_material_fragData(&m, ILG_GBUFFER_NORMAL, "out_Normal"); ilG_material_fragData(&m, ILG_GBUFFER_REFRACTION, "out_Refraction"); ilG_material_fragData(&m, ILG_GBUFFER_GLOSS, "out_Gloss"); if (!ilG_renderman_addMaterialFromFile(rm, m, "teapot.vert", "teapot.frag", &mat, error)) { return false; } ilG_material *mat = ilG_renderman_findMaterial(rm, this->mat); mvp_loc = ilG_material_getLoc(mat, "mvp"); imt_loc = ilG_material_getLoc(mat, "imt"); if (!ilG_mesh_fromfile(&mesh, &demo_fs, "teapot.obj")) { return false; } ilG_tex_loadfile(&tex, &demo_fs, "white-marble-texture.png"); return true; }
ilG_renderer ilG_heightmap_new(unsigned w, unsigned h, ilG_tex height, ilG_tex normal, ilG_tex color) { ilA_mesh *mesh = ilA_mesh_new(ILA_MESH_POSITION|ILA_MESH_TEXCOORD, w * h * 6); unsigned x, y, i = 0, j; mesh->mode = ILA_MESH_TRIANGLES; for (y = 1; y < h; y++) { for (x = 1; x < w; x++) { float pos[] = { x, y, 0, 1, x, y-1,0, 1, x-1,y, 0, 1, x, y-1,0, 1, x-1,y-1,0, 1, x-1,y, 0, 1, }; for (j = 0; j < 24; j += 4) { pos[j] /= w; } for (j = 1; j < 24; j += 4) { pos[j] /= h; } memcpy(mesh->position + i, pos, sizeof(pos)); float tex[] = { x, y, 0, 0, x, y, 0, 0, x, y, 0, 0, x, y, 0, 0, x, y, 0, 0, x, y, 0, 0, }; memcpy(mesh->texcoord + i, tex, sizeof(tex)); i += 6; } } ilG_heightmap *self = calloc(1, sizeof(ilG_heightmap)); self->w = w; self->h = h; height.unit = 0; normal.unit = 1; color.unit = 2; self->height = height; self->normal = normal; self->color = color; self->mesh = mesh; ilG_material *mat = self->shader = ilG_material_new(); ilG_material_vertex_file(mat, "heightmap.vert"); ilG_material_fragment_file(mat, "heightmap.frag"); ilG_material_name(mat, "Heightmap Shader"); ilG_material_arrayAttrib(mat, ILG_ARRATTR_POSITION, "in_Position"); ilG_material_arrayAttrib(mat, ILG_ARRATTR_TEXCOORD, "in_Texcoord"); ilG_material_textureUnit(mat, 0, "height_tex"); ilG_material_textureUnit(mat, 1, "normal_tex"); ilG_material_textureUnit(mat, 2, "ambient_tex"); ilG_material_fragData(mat, ILG_FRAGDATA_ACCUMULATION, "out_Ambient"); ilG_material_fragData(mat, ILG_FRAGDATA_NORMAL, "out_Normal"); ilG_material_fragData(mat, ILG_FRAGDATA_DIFFUSE, "out_Diffuse"); ilG_material_fragData(mat, ILG_FRAGDATA_SPECULAR, "out_Specular"); ilG_material_matrix(mat, ILG_MVP, "mvp"); ilG_material_matrix(mat, ILG_INVERSE | ILG_MODEL | ILG_TRANSPOSE, "imt"); ilG_material_bindFunc(mat, width_uniform, self, "size"); return ilG_renderer_wrap(self, &ilG_heightmap_renderer); }
bool ilG_heightmap_build(ilG_heightmap *hm, ilG_renderman *rm, unsigned w, unsigned h, ilG_tex height, ilG_tex normal, ilG_tex color, char **error) { memset(hm, 0, sizeof(*hm)); hm->rm = rm; ilA_mesh *mesh = ilA_mesh_new(ILA_MESH_POSITION|ILA_MESH_TEXCOORD, w * h * 6); unsigned x, y, i = 0, j; mesh->mode = ILA_MESH_TRIANGLES; for (y = 1; y < h; y++) { for (x = 1; x < w; x++) { float pos[] = { x, y, 0, 1, x, y-1,0, 1, x-1,y, 0, 1, x, y-1,0, 1, x-1,y-1,0, 1, x-1,y, 0, 1, }; for (j = 0; j < 24; j += 4) { pos[j] /= w; } for (j = 1; j < 24; j += 4) { pos[j] /= h; } memcpy(mesh->position + i, pos, sizeof(pos)); float tex[] = { x, y, 0, 0, x, y, 0, 0, x, y, 0, 0, x, y, 0, 0, x, y, 0, 0, x, y, 0, 0, }; memcpy(mesh->texcoord + i, tex, sizeof(tex)); i += 6; } } hm->w = w; hm->h = h; hm->height = height; hm->normal = normal; hm->color = color; ilG_material mat; ilG_material_init(&mat); ilG_material_name(&mat, "Heightmap Shader"); ilG_material_arrayAttrib(&mat, ILG_MESH_POS, "in_Position"); ilG_material_arrayAttrib(&mat, ILG_MESH_TEX, "in_Texcoord"); ilG_material_textureUnit(&mat, TEX_HEIGHT, "height_tex"); ilG_material_textureUnit(&mat, TEX_NORMAL, "normal_tex"); ilG_material_textureUnit(&mat, TEX_COLOR, "ambient_tex"); ilG_material_fragData(&mat, ILG_GBUFFER_NORMAL, "out_Normal"); ilG_material_fragData(&mat, ILG_GBUFFER_ALBEDO, "out_Albedo"); if (!ilG_renderman_addMaterialFromFile(hm->rm, mat, "heightmap.vert", "heightmap.frag", &hm->mat, error)) { return false; } ilG_material *mat2 = ilG_renderman_findMaterial(rm, hm->mat); hm->mvp = ilG_material_getLoc(mat2, "mvp"); hm->imt = ilG_material_getLoc(mat2, "imt"); hm->size = ilG_material_getLoc(mat2, "size"); if (!ilG_mesh_init(&hm->mesh, mesh)) { ilA_mesh_free(mesh); return false; } ilA_mesh_free(mesh); return true; }