Textures Example
// Writes an OMF file containing a projected texture and a mapped texture.
#include <assert.h>
#include <omf.h>
#include <stdio.h>
#include <stdlib.h>
static const double VERTICES[][3] = {
{ -1.0, -1.0, 0.0 },
{ 1.0, -1.0, 0.0 },
{ 1.0, 1.0, 0.0 },
{ -1.0, 1.0, 0.0 },
// The tip vertex is duplicated here so it can have different texture coordinates for
// each side face.
{ 0.0, 0.0, 1.0 },
{ 0.0, 0.0, 1.0 },
{ 0.0, 0.0, 1.0 },
{ 0.0, 0.0, 1.0 },
};
static const uint32_t TRIANGLES[][3] = {
{ 0, 1, 4 },
{ 1, 2, 5 },
{ 2, 3, 6 },
{ 3, 0, 7 },
{ 0, 2, 1 },
{ 0, 3, 2 },
};
static const float TEXCOORDS[][2] = {
{ 0.5, 0.0 },
{ 1.0, 0.5 },
{ 0.5, 1.0 },
{ 0.0, 0.5 },
{ 1.0, 0.0 },
{ 1.0, 1.0 },
{ 0.0, 1.0 },
{ 0.0, 0.0 },
};
static const char EXAMPLE_PNG[] = {
0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a, 0x00, 0x00, 0x00, 0x0d,
0x49, 0x48, 0x44, 0x52, 0x00, 0x00, 0x00, 0x40, 0x00, 0x00, 0x00, 0x40,
0x08, 0x02, 0x00, 0x00, 0x00, 0x25, 0x0b, 0xe6, 0x89, 0x00, 0x00, 0x00,
0x09, 0x70, 0x48, 0x59, 0x73, 0x00, 0x00, 0x02, 0x80, 0x00, 0x00, 0x02,
0x80, 0x01, 0x5b, 0x4d, 0x5f, 0x70, 0x00, 0x00, 0x00, 0x19, 0x74, 0x45,
0x58, 0x74, 0x53, 0x6f, 0x66, 0x74, 0x77, 0x61, 0x72, 0x65, 0x00, 0x77,
0x77, 0x77, 0x2e, 0x69, 0x6e, 0x6b, 0x73, 0x63, 0x61, 0x70, 0x65, 0x2e,
0x6f, 0x72, 0x67, 0x9b, 0xee, 0x3c, 0x1a, 0x00, 0x00, 0x01, 0x9a, 0x49,
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d6, 0x8c, 0xee,
0x5a, 0x3c, 0x35, 0x30, 0xb8, 0x60, 0x42, 0xdf, 0xe8, 0x81, 0x81, 0xf1,
0xd5, 0x72, 0x3a, 0x77, 0x5f, 0x1a, 0x98, 0x52, 0x2a, 0xad, 0x77, 0xfa,
0xc1, 0xc0, 0xac, 0x3a, 0x99, 0xdd, 0xeb, 0xff, 0x06, 0x26, 0x16, 0xb9,
0x01, 0x89, 0x4f, 0xa5, 0x0e, 0xa3, 0x13, 0xdc, 0x24, 0x00, 0x00, 0x00,
0x00, 0x49, 0x45, 0x4e, 0x44, 0xae, 0x42, 0x60, 0x82
};
static const size_t EXAMPLE_PNG_LEN = 525;
static bool write(const char *path) {
OmfError *error;
OmfWriter *writer;
OmfProject project;
OmfElement element;
OmfSurface surface;
OmfAttribute attribute;
OmfMappedTexture mapped;
OmfProjectedTexture projected;
OmfHandle *proj_handle, *surf_handle;
const OmfArray *image;
// Open the file and create a project.
writer = omf_writer_open(path);
project = omf_project_init("textures.omf");
proj_handle = omf_writer_project(writer, &project);
// Create a pyramid surface element and keep that handle too.
surface = omf_surface_init(
omf_writer_array_vertices64(writer, VERTICES, 8),
omf_writer_array_triangles(writer, TRIANGLES, 5)
);
element = omf_element_init("Pyramid");
element.surface = &surface;
surf_handle = omf_writer_element(writer, proj_handle, &element);
// Add the image that both texture types will use.
image = omf_writer_image_bytes(writer, EXAMPLE_PNG, EXAMPLE_PNG_LEN);
// Add mapped texture attribute.
mapped = omf_mapped_texture_init(image, omf_writer_array_texcoords32(writer, TEXCOORDS, 8));
attribute = omf_attribute_init("Mapped", OMF_LOCATION_VERTICES);
attribute.mapped_texture_data = &mapped;
omf_writer_attribute(writer, surf_handle, &attribute);
// Add projected texture attribute.
projected = omf_projected_texture_init(image);
projected.orient.origin[0] = -1.0;
projected.orient.origin[1] = -1.0;
projected.orient.origin[2] = 0.0;
projected.orient.u[0] = 1.0;
projected.orient.u[1] = 0.0;
projected.orient.u[2] = 0.0;
projected.orient.v[0] = 0.0;
projected.orient.v[1] = 0.0;
projected.orient.v[2] = 1.0;
projected.width = 2.0;
projected.height = 2.0;
attribute = omf_attribute_init("Projected", OMF_LOCATION_PROJECTED);
attribute.projected_texture_data = &projected;
omf_writer_attribute(writer, surf_handle, &attribute);
// Finish writing and close the file.
omf_writer_finish(writer, NULL);
// Check for errors.
if ((error = omf_error()) != NULL) {
fprintf(stderr, "[write failed] %s (%d)\n", error->message, error->code);
omf_error_free(error);
return false;
}
return true;
}
static bool read(const char *path) {
OmfReader *reader;
const OmfProject *project;
OmfError *error;
OmfImageData *image_data;
// Open the file and read the project.
reader = omf_reader_open(path);
project = omf_reader_project(reader, NULL);
if (!project) {
error = omf_error();
fprintf(stderr, "[read failed] %s (%d)\n", error->message, error->code);
omf_error_free(error);
return false;
}
printf("name: %s\n", project->name);
// Read the texture image back.
image_data = omf_reader_image(
reader, project->elements[0].attributes[0].mapped_texture_data->image);
assert(image_data);
assert(image_data->mode == OMF_IMAGE_MODE_RGB);
assert(image_data->width == 64);
assert(image_data->height == 64);
// This now contains the pixels in RGB order with no padding.
assert(image_data->uint8);
// Free the image data.
omf_image_data_free(image_data);
// Close the reader only once we're done with `project`.
omf_reader_close(reader);
// Check for errors.
if ((error = omf_error()) != NULL) {
fprintf(stderr, "[read failed] %s (%d)\n", error->message, error->code);
omf_error_free(error);
return false;
}
return true;
}
int main() {
if (!write("textures.omf")) return 1;
if (!read("textures.omf")) return 1;
return 0;
}