void uniqueMap(obj* object) { int i, j, k; int vIndices[3], tIndices[3]; for(i = 0; i < object->faceCount; ++i) { for(j = 0; j < 3; ++j) { vIndices[j] = object->faces[i][j][0] - 1; tIndices[j] = object->faces[i][j][1] - 1; } for(j = 0; j < 3; ++j) { vec3 V[3]; vec2 T[3]; for(k = 0; k < 3; ++k) { V[k] = object->vertices[vIndices[(j+k)%3]]; T[k] = object->textures[tIndices[(j+k)%3]]; } vec3 edge1 = subV3(V[1], V[0]); vec3 edge2 = subV3(V[2], V[0]); vec2 deltaUV1 = subV2(T[1], T[0]); vec2 deltaUV2 = subV2(T[2], T[0]); GLfloat f = 1.0f / (deltaUV1.x * deltaUV2.y - deltaUV2.x * deltaUV1.y); vec3 tangent; tangent.x = f * (deltaUV2.y * edge1.x - deltaUV1.y * edge2.x); tangent.y = f * (deltaUV2.y * edge1.y - deltaUV1.y * edge2.y); tangent.z = f * (deltaUV2.y * edge1.z - deltaUV1.y * edge2.z); tangent = unit(tangent); vec3 bitangent; bitangent.x = f * (-deltaUV2.x * edge1.x + deltaUV1.x * edge2.x); bitangent.y = f * (-deltaUV2.x * edge1.y + deltaUV1.x * edge2.y); bitangent.z = f * (-deltaUV2.x * edge1.z + deltaUV1.x * edge2.z); bitangent = unit(bitangent); object->tangents[vIndices[j]] = addV3(object->tangents[vIndices[j]], tangent); object->bitangents[vIndices[j]] = addV3(object->bitangents[vIndices[j]], bitangent); } for(j = 0; j < 3; ++j) ++object->shared[vIndices[j]]; } for(i = 0; i < object->vertexCount; ++i) { object->tangents[i] = unit(scale(object->tangents[i], 1.0f / object->shared[i])); object->bitangents[i] = unit(scale(object->bitangents[i], 1.0f / object->shared[i])); } }
void render_hex(v2 hex_pos, uint32_t * pixels, HexShape hexes, v2 window_dimensions_in_pixels, uint32_t color) { // TODO: Sub-pixel rendering, instead of flooring v2 startPos = subV2(hex_pos, floorVf2(hexes.dimensions_in_pixels)); v2 endPos = addV2(hex_pos, floorVf2(hexes.dimensions_in_pixels)); if (startPos.x < 0) { startPos.x = 0; } else if (startPos.x > window_dimensions_in_pixels.x) { startPos.x = window_dimensions_in_pixels.x; } if (startPos.y < 0) { startPos.y = 0; } else if (startPos.y > window_dimensions_in_pixels.y) { startPos.y = window_dimensions_in_pixels.y; } v2 pos; for (pos.y = startPos.y; pos.y < endPos.y; pos.y++) { for (pos.x = startPos.x; pos.x < endPos.x; pos.x++) { v2 rel_to_hex_pos = subV2(pos, hex_pos); if (absInt32(rel_to_hex_pos.x) < hexes.radius_in_pixels * (hexes.dimensions_in_pixels.y - absInt32(rel_to_hex_pos.y) * .5f) / hexes.dimensions_in_pixels.y) { pixels[v2ToV1(pos, window_dimensions_in_pixels.x)] = color; } } } }