Techneck/code/source-c/cube_premeshing.c

259 lines
6.7 KiB
C

#include "blocks.h"
#include "state.h"
float tc_cube_front_uv[12] =
{
0.0f, 0.0f,
0.0f, 1.0f,
1.0f, 0.0f,
1.0f, 1.0f,
1.0f, 0.0f,
0.0f, 1.0f
};
float tc_cube_front_xyz[18] =
{
-0.5f, -0.5f, -0.5f,
-0.5f, 0.5f, -0.5f,
0.5f, -0.5f, -0.5f,
0.5f, 0.5f, -0.5f,
0.5f, -0.5f, -0.5f,
-0.5f, 0.5f, -0.5f
};
float tc_cube_back_uv[12] =
{
1.0f, 0.0f,
0.0f, 1.0f,
0.0f, 0.0f,
0.0f, 1.0f,
1.0f, 0.0f,
1.0f, 1.0f
};
float tc_cube_back_xyz[18] =
{
0.5f, -0.5f, 0.5f,
-0.5f, 0.5f, 0.5f,
-0.5f, -0.5f, 0.5f,
-0.5f, 0.5f, 0.5f,
0.5f, -0.5f, 0.5f,
0.5f, 0.5f, 0.5f
};
float tc_cube_west_uv[12] =
{
0.0f, 1.0f,
0.0f, 0.0f,
1.0f, 0.0f,
1.0f, 0.0f,
1.0f, 1.0f,
0.0f, 1.0f
};
float tc_cube_west_xyz[18] =
{
-0.5f, 0.5f, -0.5f,
-0.5f, -0.5f, -0.5f,
-0.5f, -0.5f, 0.5f,
-0.5f, -0.5f, 0.5f,
-0.5f, 0.5f, 0.5f,
-0.5f, 0.5f, -0.5f
};
float tc_cube_east_uv[12] =
{
1.0f, 0.0f,
0.0f, 0.0f,
0.0f, 1.0f,
0.0f, 1.0f,
1.0f, 1.0f,
1.0f, 0.0f
};
float tc_cube_east_xyz[18] =
{
0.5f, -0.5f, 0.5f,
0.5f, -0.5f, -0.5f,
0.5f, 0.5f, -0.5f,
0.5f, 0.5f, -0.5f,
0.5f, 0.5f, 0.5f,
0.5f, -0.5f, 0.5f
};
float tc_cube_top_uv[12] =
{
0.0f, 0.0f,
0.0f, 1.0f,
1.0f, 0.0f,
0.0f, 1.0f,
1.0f, 1.0f,
1.0f, 0.0f
};
float tc_cube_top_xyz[18] =
{
-0.5f, 0.5f, -0.5f,
-0.5f, 0.5f, 0.5f,
0.5f, 0.5f, -0.5f,
-0.5f, 0.5f, 0.5f,
0.5f, 0.5f, 0.5f,
0.5f, 0.5f, -0.5f
};
float tc_cube_bottom_uv[12] =
{
0.0f, 1.0f,
0.0f, 0.0f,
1.0f, 0.0f,
1.0f, 1.0f,
0.0f, 1.0f,
1.0f, 0.0f
};
float tc_cube_bottom_xyz[18] =
{
-0.5f, -0.5f, 0.5f,
-0.5f, -0.5f, -0.5f,
0.5f, -0.5f, -0.5f,
0.5f, -0.5f, 0.5f,
-0.5f, -0.5f, 0.5f,
0.5f, -0.5f, -0.5f
};
void tc_adjust_cube_uvs_for_image(tc_image_s *image, float *face_uvs)
{
float uv_per_pixel_w = 1.0f / image->width;
float uv_per_pixel_h = 1.0f / image->height;
if(image->superimage != NULL)
{
uv_per_pixel_w = 1.0f / image->superimage->width;
uv_per_pixel_h = 1.0f / image->superimage->height;
}
float lower_x = uv_per_pixel_w * ((float) image->x);
float lower_y = uv_per_pixel_h * ((float) image->y);
float higher_x = 1.0f;
float higher_y = 1.0f;
higher_x = lower_x + uv_per_pixel_w * 8; // image->width;
higher_y = lower_y + uv_per_pixel_h * 8; // image->height;
uint32_t num_floats = 2 * 6;
uint32_t index = 0;
while(index < num_floats)
{
if(face_uvs[index] == 1.0f)
{
face_uvs[index] = higher_x;
}
else
{
face_uvs[index] = lower_x;
}
++index;
if(face_uvs[index] == 1.0f)
{
face_uvs[index] = higher_y;
}
else
{
face_uvs[index] = lower_y;
}
++index;
}
}
tc_cube_side_mesh_s * tc_premesh_cube_block_side(
tc_image_s *image, float *xyz, float *uv, uint32_t num_vertices
) {
tc_cube_side_mesh_s *side = malloc(sizeof(tc_cube_side_mesh_s));
side->num_vertices = num_vertices;
side->allocation = malloc(sizeof(float) * num_vertices * (2+3));
side->xyz = side->allocation;
memcpy(side->xyz, xyz, sizeof(float) * num_vertices * 3);
side->uv = &side->allocation[num_vertices*3];
memcpy(side->uv, uv, sizeof(float) * num_vertices * 2);
tc_adjust_cube_uvs_for_image(image, side->uv);
return side;
}
void tc_premesh_cube(tc_block_entry_s *entry)
{
tc_image_s *front_texture = tc_block_resolve_name_to_attribute(entry, "front_texture")->value.pointer;
tc_image_s *back_texture = tc_block_resolve_name_to_attribute(entry, "back_texture")->value.pointer;
tc_image_s *west_texture = tc_block_resolve_name_to_attribute(entry, "west_texture")->value.pointer;
tc_image_s *east_texture = tc_block_resolve_name_to_attribute(entry, "east_texture")->value.pointer;
tc_image_s *top_texture = tc_block_resolve_name_to_attribute(entry, "top_texture")->value.pointer;
tc_image_s *bottom_texture = tc_block_resolve_name_to_attribute(entry, "bottom_texture")->value.pointer;
tc_cube_side_mesh_s *front = tc_premesh_cube_block_side(front_texture, tc_cube_front_xyz, tc_cube_front_uv, 6);
tc_cube_side_mesh_s *back = tc_premesh_cube_block_side(back_texture, tc_cube_back_xyz, tc_cube_back_uv, 6);
tc_cube_side_mesh_s *west = tc_premesh_cube_block_side(west_texture, tc_cube_west_xyz, tc_cube_west_uv, 6);
tc_cube_side_mesh_s *east = tc_premesh_cube_block_side(east_texture, tc_cube_east_xyz, tc_cube_east_uv, 6);
tc_cube_side_mesh_s *top = tc_premesh_cube_block_side(top_texture, tc_cube_top_xyz, tc_cube_top_uv, 6);
tc_cube_side_mesh_s *bottom = tc_premesh_cube_block_side(bottom_texture, tc_cube_bottom_xyz, tc_cube_bottom_uv, 6);
tc_block_attribute_s front_attribute;
front_attribute.name = "south_mesh";
front_attribute.value.pointer = front;
tc_block_attribute_s back_attribute;
back_attribute.name = "north_mesh";
back_attribute.value.pointer = back;
tc_block_attribute_s west_attribute;
west_attribute.name = "west_mesh";
west_attribute.value.pointer = west;
tc_block_attribute_s east_attribute;
east_attribute.name = "east_mesh";
east_attribute.value.pointer = east;
tc_block_attribute_s top_attribute;
top_attribute.name = "top_mesh";
top_attribute.value.pointer = top;
tc_block_attribute_s bottom_attribute;
bottom_attribute.name = "bottom_mesh";
bottom_attribute.value.pointer = bottom;
tc_add_attribute_to_block_entry(entry, front_attribute);
tc_add_attribute_to_block_entry(entry, back_attribute);
tc_add_attribute_to_block_entry(entry, east_attribute);
tc_add_attribute_to_block_entry(entry, west_attribute);
tc_add_attribute_to_block_entry(entry, top_attribute);
tc_add_attribute_to_block_entry(entry, bottom_attribute);
}