One more fix for merged 'normal map tangents not working correctly when there are...
authorAlexander Romanov <a.romanov@blend4web.com>
Tue, 4 Jul 2017 13:03:04 +0000 (16:03 +0300)
committerAlexander Romanov <a.romanov@blend4web.com>
Tue, 4 Jul 2017 13:25:49 +0000 (16:25 +0300)
source/blender/blenkernel/intern/editmesh_tangent.c
source/blender/blenkernel/intern/mesh_tangent.c
source/blender/draw/intern/draw_cache_impl_mesh.c

index 314c50d42c6c09ec43e7e4ea53b7627b80efec79..589c346b2482cc81d36a8829e6ad665dcc42db62 100644 (file)
@@ -287,8 +287,6 @@ void BKE_editmesh_loop_tangent_calc(
 {
        BMesh *bm = em->bm;
 
-       BLI_assert(CustomData_number_of_layers(&bm->ldata, CD_MLOOPUV) != 0);
-
        int act_uv_n = -1;
        int ren_uv_n = -1;
        bool calc_act = false;
@@ -374,7 +372,7 @@ void BKE_editmesh_loop_tangent_calc(
                                                continue;
                                        /* needed for orco lookups */
                                        htype_index |= BM_VERT;
-                                       *tangent_mask_curr_p |= DM_TANGENT_MASK_ORCO;
+                                       tangent_mask_curr |= DM_TANGENT_MASK_ORCO;
                                }
                                else {
                                        /* Fill the resulting tangent_mask */
@@ -382,7 +380,7 @@ void BKE_editmesh_loop_tangent_calc(
                                        int uv_start = CustomData_get_layer_index(&bm->ldata, CD_MLOOPUV);
                                        BLI_assert(uv_ind != -1 && uv_start != -1);
                                        BLI_assert(uv_ind - uv_start < MAX_MTFACE);
-                                       *tangent_mask_curr_p |= 1 << (uv_ind - uv_start);
+                                       tangent_mask_curr |= 1 << (uv_ind - uv_start);
                                }
                                if (mesh2tangent->precomputedFaceNormals) {
                                        /* needed for face normal lookups */
index 294ed60488507a057c252a76f86cab81543a084f..d370e2021349fa42441b334196e63143759cbbb4 100644 (file)
@@ -573,7 +573,7 @@ void BKE_mesh_calc_loop_tangent_ex(
                        if (tangent_names[i][0])
                                BKE_mesh_add_loop_tangent_named_layer_for_uv(loopdata, loopdata_out, (int)loopdata_out_len, tangent_names[i]);
                if ((tangent_mask & DM_TANGENT_MASK_ORCO) && CustomData_get_named_layer_index(loopdata, CD_TANGENT, "") == -1)
-                           CustomData_add_layer_named(loopdata, CD_TANGENT, CD_CALLOC, NULL, (int)loopdata_out_len, "");
+                           CustomData_add_layer_named(loopdata_out, CD_TANGENT, CD_CALLOC, NULL, (int)loopdata_out_len, "");
                if (calc_act && act_uv_name[0])
                        BKE_mesh_add_loop_tangent_named_layer_for_uv(loopdata, loopdata_out, (int)loopdata_out_len, act_uv_name);
                if (calc_ren && ren_uv_name[0])
@@ -642,14 +642,14 @@ void BKE_mesh_calc_loop_tangent_ex(
                                    if (!mesh2tangent->orco)
                                            continue;
 
-                                   *tangent_mask_curr_p |= DM_TANGENT_MASK_ORCO;
+                                   tangent_mask_curr |= DM_TANGENT_MASK_ORCO;
                                }
                                else {
                                    int uv_ind = CustomData_get_named_layer_index(loopdata, CD_MLOOPUV, loopdata->layers[index].name);
                                    int uv_start = CustomData_get_layer_index(loopdata, CD_MLOOPUV);
                                    BLI_assert(uv_ind != -1 && uv_start != -1);
                                    BLI_assert(uv_ind - uv_start < MAX_MTFACE);
-                                   *tangent_mask_curr_p |= (short)(1 << (uv_ind - uv_start));
+                                   tangent_mask_curr |= (short)(1 << (uv_ind - uv_start));
                                }
 
                                mesh2tangent->tangent = loopdata_out->layers[index].data;
index b8875ec79fdc728cb325667ebb81f40d64e55fa9..495e72fc9acc534abf0013b3a89526624197fab5 100644 (file)
@@ -241,7 +241,7 @@ static bool bm_edge_has_visible_face(const BMEdge *e)
 
 static void mesh_cd_calc_used_gpu_layers(
         CustomData *UNUSED(cd_vdata), uchar cd_vused[CD_NUMTYPES],
-        CustomData *cd_ldata, uchar cd_lused[CD_NUMTYPES],
+        CustomData *cd_ldata, ushort cd_lused[CD_NUMTYPES],
         struct GPUMaterial **gpumat_array, int gpumat_array_len)
 {
        /* See: DM_vertex_attributes_from_gpu for similar logic */
@@ -308,6 +308,11 @@ static void mesh_cd_calc_used_gpu_layers(
                                                if (layer != -1) {
                                                        cd_lused[CD_TANGENT] |= (1 << layer);
                                                }
+                                               else {
+                                                       /* no UV layers at all => requesting orco */
+                                                       cd_lused[CD_TANGENT] |= DM_TANGENT_MASK_ORCO;
+                                                       cd_vused[CD_ORCO] |= 1;
+                                               }
                                                break;
                                        }
                                        case CD_MCOL:
@@ -477,7 +482,7 @@ static MeshRenderData *mesh_render_data_create_ex(
 
                /* Add edge/poly if we need them */
                uchar cd_vused[CD_NUMTYPES] = {0};
-               uchar cd_lused[CD_NUMTYPES] = {0};
+               ushort cd_lused[CD_NUMTYPES] = {0};
 
                mesh_cd_calc_used_gpu_layers(
                        cd_vdata, cd_vused,
@@ -647,6 +652,9 @@ static MeshRenderData *mesh_render_data_create_ex(
                                }
                        }
 
+                       /* If tangent from orco is requested, decrement tangent_len */
+                       int actual_tangent_len = (cd_lused[CD_TANGENT] & DM_TANGENT_MASK_ORCO) ?
+                                               rdata->cd.layers.tangent_len - 1 : rdata->cd.layers.tangent_len;
                        if (rdata->edit_bmesh) {
                                BMEditMesh *em = rdata->edit_bmesh;
                                BMesh *bm = em->bm;
@@ -661,7 +669,7 @@ static MeshRenderData *mesh_render_data_create_ex(
 
                                BKE_editmesh_loop_tangent_calc(
                                        em, calc_active_tangent,
-                                       tangent_names, rdata->cd.layers.tangent_len,
+                                       tangent_names, actual_tangent_len,
                                        poly_normals, loop_normals,
                                        rdata->orco,
                                        &rdata->cd.output.ldata, bm->totloop,
@@ -687,7 +695,7 @@ static MeshRenderData *mesh_render_data_create_ex(
                                        rdata->mlooptri, rdata->tri_len,
                                        cd_ldata,
                                        calc_active_tangent,
-                                       tangent_names, rdata->cd.layers.tangent_len,
+                                       tangent_names, actual_tangent_len,
                                        poly_normals, loop_normals,
                                        rdata->orco,
                                        &rdata->cd.output.ldata, me->totloop,
@@ -708,7 +716,8 @@ static MeshRenderData *mesh_render_data_create_ex(
 
                        BLI_assert(CustomData_number_of_layers(&rdata->cd.output.ldata, CD_TANGENT) == rdata->cd.layers.tangent_len);
 
-                       for (int i_src = 0, i_dst = 0; i_src < cd_layers_src.uv_len; i_src++, i_dst++) {
+                       int i_dst = 0;
+                       for (int i_src = 0; i_src < cd_layers_src.uv_len; i_src++, i_dst++) {
                                if ((cd_lused[CD_TANGENT] & (1 << i_src)) == 0) {
                                        i_dst--;
                                        if (rdata->cd.layers.tangent_active >= i_src) {
@@ -733,6 +742,13 @@ static MeshRenderData *mesh_render_data_create_ex(
                                        }
                                }
                        }
+                       if (cd_lused[CD_TANGENT] & DM_TANGENT_MASK_ORCO) {
+                               const char *name = CustomData_get_layer_name(&rdata->cd.output.ldata, CD_TANGENT, i_dst);
+                               unsigned int hash = BLI_ghashutil_strhash_p(name);
+                               BLI_snprintf(rdata->cd.uuid.tangent[i_dst], sizeof(*rdata->cd.uuid.tangent), "t%u", hash);
+
+                               rdata->cd.layers.tangent[i_dst] = CustomData_get_layer_n(&rdata->cd.output.ldata, CD_TANGENT, i_dst);
+                       }
                }
 
 #undef me