diff options
author | Campbell Barton <ideasman42@gmail.com> | 2018-07-29 05:12:36 +0300 |
---|---|---|
committer | Campbell Barton <ideasman42@gmail.com> | 2018-07-29 05:12:36 +0300 |
commit | 66a00b64c5cc64e28289147fdfbcf5bf8f046c34 (patch) | |
tree | da8549b8e9b8b49d8d80f5801a83b80c9b72e2cb /intern | |
parent | 8f01623ef314f72ac7628d7fd54c1855cd0cb731 (diff) | |
parent | 018c9af446de10f038a6e641ca3a61ce451a7c92 (diff) |
Merge branch 'master' into blender2.8
Diffstat (limited to 'intern')
-rw-r--r-- | intern/cycles/kernel/kernel_light.h | 25 | ||||
-rw-r--r-- | intern/cycles/render/mesh_volume.cpp | 91 |
2 files changed, 35 insertions, 81 deletions
diff --git a/intern/cycles/kernel/kernel_light.h b/intern/cycles/kernel/kernel_light.h index 32cb924d25f..0165eaa606f 100644 --- a/intern/cycles/kernel/kernel_light.h +++ b/intern/cycles/kernel/kernel_light.h @@ -72,24 +72,17 @@ ccl_device_inline float rect_light_sample(float3 P, float y0 = dot(dir, y); float x1 = x0 + axisu_len; float y1 = y0 + axisv_len; - /* Create vectors to four vertices. */ - float3 v00 = make_float3(x0, y0, z0); - float3 v01 = make_float3(x0, y1, z0); - float3 v10 = make_float3(x1, y0, z0); - float3 v11 = make_float3(x1, y1, z0); - /* Compute normals to edges. */ - float3 n0 = normalize(cross(v00, v10)); - float3 n1 = normalize(cross(v10, v11)); - float3 n2 = normalize(cross(v11, v01)); - float3 n3 = normalize(cross(v01, v00)); /* Compute internal angles (gamma_i). */ - float g0 = safe_acosf(-dot(n0, n1)); - float g1 = safe_acosf(-dot(n1, n2)); - float g2 = safe_acosf(-dot(n2, n3)); - float g3 = safe_acosf(-dot(n3, n0)); + float4 diff = make_float4(x0, y1, x1, y0) - make_float4(x1, y0, x0, y1); + float4 nz = make_float4(y0, x1, y1, x0) * diff; + nz = nz / sqrt(sqr(z0 * diff) + sqr(nz)); + float g0 = safe_acosf(-nz.x * nz.y); + float g1 = safe_acosf(-nz.y * nz.z); + float g2 = safe_acosf(-nz.z * nz.w); + float g3 = safe_acosf(-nz.w * nz.x); /* Compute predefined constants. */ - float b0 = n0.z; - float b1 = n2.z; + float b0 = nz.x; + float b1 = nz.z; float b0sq = b0 * b0; float k = M_2PI_F - g2 - g3; /* Compute solid angle from internal angles. */ diff --git a/intern/cycles/render/mesh_volume.cpp b/intern/cycles/render/mesh_volume.cpp index d1c49b456ff..3ee4124ba0f 100644 --- a/intern/cycles/render/mesh_volume.cpp +++ b/intern/cycles/render/mesh_volume.cpp @@ -87,23 +87,31 @@ const float3 quads_normals[6] = { make_float3(0.0f, 0.0f, 1.0f), }; -static void create_quad(int3 corners[8], vector<int3> &vertices, vector<QuadData> &quads, int face_index) +static int add_vertex(int3 v, vector<int3> &vertices, int3 res, unordered_map<size_t, int> &used_verts) { - size_t vertex_offset = vertices.size(); + size_t vert_key = v.x + v.y * (res.x+1) + v.z * (res.x+1)*(res.y+1); + unordered_map<size_t, int>::iterator it = used_verts.find(vert_key); + if(it != used_verts.end()) { + return it->second; + } + + int vertex_offset = vertices.size(); + used_verts[vert_key] = vertex_offset; + vertices.push_back(v); + return vertex_offset; +} + +static void create_quad(int3 corners[8], vector<int3> &vertices, vector<QuadData> &quads, int3 res, unordered_map<size_t, int> &used_verts, int face_index) +{ QuadData quad; - quad.v0 = vertex_offset + 0; - quad.v1 = vertex_offset + 1; - quad.v2 = vertex_offset + 2; - quad.v3 = vertex_offset + 3; + quad.v0 = add_vertex(corners[quads_indices[face_index][0]], vertices, res, used_verts); + quad.v1 = add_vertex(corners[quads_indices[face_index][1]], vertices, res, used_verts); + quad.v2 = add_vertex(corners[quads_indices[face_index][2]], vertices, res, used_verts); + quad.v3 = add_vertex(corners[quads_indices[face_index][3]], vertices, res, used_verts); quad.normal = quads_normals[face_index]; quads.push_back(quad); - - vertices.push_back(corners[quads_indices[face_index][0]]); - vertices.push_back(corners[quads_indices[face_index][1]]); - vertices.push_back(corners[quads_indices[face_index][2]]); - vertices.push_back(corners[quads_indices[face_index][3]]); } struct VolumeParams { @@ -159,9 +167,6 @@ private: void generate_vertices_and_quads(vector<int3> &vertices_is, vector<QuadData> &quads); - void deduplicate_vertices(vector<int3> &vertices, - vector<QuadData> &quads); - void convert_object_space(const vector<int3> &vertices, vector<float3> &out_vertices); @@ -234,8 +239,6 @@ void VolumeMeshBuilder::create_mesh(vector<float3> &vertices, generate_vertices_and_quads(vertices_is, quads); - deduplicate_vertices(vertices_is, quads); - convert_object_space(vertices_is, vertices); convert_quads_to_tris(quads, indices, face_normals); @@ -245,10 +248,7 @@ void VolumeMeshBuilder::generate_vertices_and_quads( vector<ccl::int3> &vertices_is, vector<QuadData> &quads) { - /* Overallocation, we could count the number of quads and vertices to create - * in a pre-pass if memory becomes an issue. */ - vertices_is.reserve(number_of_nodes*8); - quads.reserve(number_of_nodes*6); + unordered_map<size_t, int> used_verts; for(int z = 0; z < res.z; ++z) { for(int y = 0; y < res.y; ++y) { @@ -283,77 +283,38 @@ void VolumeMeshBuilder::generate_vertices_and_quads( voxel_index = compute_voxel_index(res, x - 1, y, z); if(voxel_index == -1 || grid[voxel_index] == 0) { - create_quad(corners, vertices_is, quads, QUAD_X_MIN); + create_quad(corners, vertices_is, quads, res, used_verts, QUAD_X_MIN); } voxel_index = compute_voxel_index(res, x + 1, y, z); if(voxel_index == -1 || grid[voxel_index] == 0) { - create_quad(corners, vertices_is, quads, QUAD_X_MAX); + create_quad(corners, vertices_is, quads, res, used_verts, QUAD_X_MAX); } voxel_index = compute_voxel_index(res, x, y - 1, z); if(voxel_index == -1 || grid[voxel_index] == 0) { - create_quad(corners, vertices_is, quads, QUAD_Y_MIN); + create_quad(corners, vertices_is, quads, res, used_verts, QUAD_Y_MIN); } voxel_index = compute_voxel_index(res, x, y + 1, z); if(voxel_index == -1 || grid[voxel_index] == 0) { - create_quad(corners, vertices_is, quads, QUAD_Y_MAX); + create_quad(corners, vertices_is, quads, res, used_verts, QUAD_Y_MAX); } voxel_index = compute_voxel_index(res, x, y, z - 1); if(voxel_index == -1 || grid[voxel_index] == 0) { - create_quad(corners, vertices_is, quads, QUAD_Z_MIN); + create_quad(corners, vertices_is, quads, res, used_verts, QUAD_Z_MIN); } voxel_index = compute_voxel_index(res, x, y, z + 1); if(voxel_index == -1 || grid[voxel_index] == 0) { - create_quad(corners, vertices_is, quads, QUAD_Z_MAX); + create_quad(corners, vertices_is, quads, res, used_verts, QUAD_Z_MAX); } } } } } -void VolumeMeshBuilder::deduplicate_vertices(vector<int3> &vertices, - vector<QuadData> &quads) -{ - vector<int3> sorted_vertices = vertices; - std::sort(sorted_vertices.begin(), sorted_vertices.end()); - vector<int3>::iterator it = std::unique(sorted_vertices.begin(), sorted_vertices.end()); - sorted_vertices.resize(std::distance(sorted_vertices.begin(), it)); - - vector<QuadData> new_quads = quads; - - for(size_t i = 0; i < vertices.size(); ++i) { - for(size_t j = 0; j < sorted_vertices.size(); ++j) { - if(vertices[i] != sorted_vertices[j]) { - continue; - } - - for(int k = 0; k < quads.size(); ++k) { - if(quads[k].v0 == i) { - new_quads[k].v0 = j; - } - else if(quads[k].v1 == i) { - new_quads[k].v1 = j; - } - else if(quads[k].v2 == i) { - new_quads[k].v2 = j; - } - else if(quads[k].v3 == i) { - new_quads[k].v3 = j; - } - } - - break; - } - } - - vertices = sorted_vertices; - quads = new_quads; -} - void VolumeMeshBuilder::convert_object_space(const vector<int3> &vertices, vector<float3> &out_vertices) { |