Ticket #169: terrain_triangulation.patch

File terrain_triangulation.patch, 4.8 KB (added by Xin, 14 years ago)

Initial patch of terrain triangulation for testing

  • PatchRData.cpp

     
    330330        for (ssize_t j=0;j<PATCH_SIZE;j++) {
    331331            for (ssize_t i=0;i<PATCH_SIZE;i++) {
    332332                if (texgrid[j][i]==h){
     333
     334                    //save these values for convenience
     335                    u16 index1 = u16(((j+0)*vsize+(i+0))+base);
     336                    u16 index2 = u16(((j+0)*vsize+(i+1))+base);
     337                    u16 index3 = u16(((j+1)*vsize+(i+1))+base);
     338                    u16 index4 = u16(((j+1)*vsize+(i+0))+base);
     339
     340                    //calculate triangulation
     341                    //reminder: incrementing i shifts x coordinates, j shifts z
     342                    //first, get all the positions of the vertices in world space
     343                    CVector3D v1 = m_Vertices[(j+0)*vsize+(i+0)].m_Position;
     344                    CVector3D v2 = m_Vertices[(j+0)*vsize+(i+1)].m_Position;
     345                    CVector3D v3 = m_Vertices[(j+1)*vsize+(i+1)].m_Position;
     346                    CVector3D v4 = m_Vertices[(j+1)*vsize+(i+0)].m_Position;
     347                   
     348                    /*
     349                    debug_printf(L"Vertex 1 is at position: %f, %f, %f\n", v1.X, v1.Y, v1.Z);
     350                    debug_printf(L"Vertex 2 is at position: %f, %f, %f\n", v2.X, v2.Y, v2.Z);
     351                    debug_printf(L"Vertex 3 is at position: %f, %f, %f\n", v3.X, v3.Y, v3.Z);
     352                    debug_printf(L"Vertex 4 is at position: %f, %f, %f\n", v4.X, v4.Y, v4.Z);
     353                    */
     354
     355                   
     356                    //we have two possible triangulations to check: (123, 143) and (234, 214)
     357                    //our goal is to pick the one that minimizes the angle between the planes formed by the triangles
     358                    CVector3D normal1 = (v2-v1).Cross(v3-v1);
     359                    CVector3D normal2 = (v3-v1).Cross(v4-v1);
     360                    CVector3D normal3 = (v4-v2).Cross(v3-v2);
     361                    CVector3D normal4 = (v1-v2).Cross(v4-v2);
     362                    normal1.Normalize();
     363                    normal2.Normalize();
     364                    normal3.Normalize();
     365                    normal4.Normalize();
     366                    float angle1 = normal1.Dot(normal2);    //since we don't need the exact angle, we can just use the value of the dot product
     367                    float angle2 = normal3.Dot(normal4);
     368
     369                    //cos gets smaller as the angle increases so we take the triangulation with the larger value
     370                    if (angle1 < angle2)
     371                    {
     372                       
     373                       
     374                        m_Indices.push_back(index3);
     375                        m_Indices.push_back(index4);
     376                        m_Indices.push_back(index2);
     377                        m_Indices.push_back(index2);
     378                        m_Indices.push_back(index4);
     379                        m_Indices.push_back(index1);
     380                       
     381                        /*
     382                        //for triangle strip
     383                        m_Indices.push_back(index3);
     384                        m_Indices.push_back(index4);
     385                        m_Indices.push_back(index2);
     386                        m_Indices.push_back(index1);
     387                        */
     388                    }
     389                    else
     390                    {
     391                       
     392                        m_Indices.push_back(index2);
     393                        m_Indices.push_back(index3);
     394                        m_Indices.push_back(index1);
     395                        m_Indices.push_back(index1);
     396                        m_Indices.push_back(index3);
     397                        m_Indices.push_back(index4);
     398                       
     399                        /*
     400                        //for triangle strip
     401                        m_Indices.push_back(index2);
     402                        m_Indices.push_back(index3);
     403                        m_Indices.push_back(index1);
     404                        m_Indices.push_back(index4);
     405                        */
     406                    }
     407                   
     408                    /*
    333409                    m_Indices.push_back(u16(((j+0)*vsize+(i+0))+base));
    334410                    m_Indices.push_back(u16(((j+0)*vsize+(i+1))+base));
    335411                    m_Indices.push_back(u16(((j+1)*vsize+(i+1))+base));
    336412                    m_Indices.push_back(u16(((j+1)*vsize+(i+0))+base));
     413                    */
     414
    337415                }
    338416            }
    339417        }
    340418        splat.m_IndexCount=m_Indices.size()-splat.m_IndexStart;
    341419    }
    342 
    343     // build indices for the shadow map pass
    344     for (ssize_t j=0;j<PATCH_SIZE;j++) {
    345         for (ssize_t i=0;i<PATCH_SIZE;i++) {
    346             m_ShadowMapIndices.push_back(u16(((j+0)*vsize+(i+0))+base));
    347             m_ShadowMapIndices.push_back(u16(((j+0)*vsize+(i+1))+base));
    348             m_ShadowMapIndices.push_back(u16(((j+1)*vsize+(i+1))+base));
    349             m_ShadowMapIndices.push_back(u16(((j+1)*vsize+(i+0))+base));
    350         }
    351     }
    352420}
    353421
    354422
     
    517585        ogl_tex_bind(splat.m_Texture);
    518586
    519587        if (!g_Renderer.m_SkipSubmit) {
    520             glDrawElements(GL_QUADS, (GLsizei)splat.m_IndexCount,
     588            glDrawElements(GL_TRIANGLES, (GLsizei)splat.m_IndexCount,
    521589                GL_UNSIGNED_SHORT, &m_Indices[splat.m_IndexStart]);
    522590        }
    523 
     591       
    524592        // bump stats
    525593        g_Renderer.m_Stats.m_DrawCalls++;
    526594        g_Renderer.m_Stats.m_TerrainTris+=splat.m_IndexCount/2;
     
    548616
    549617    // render all base splats at once
    550618    if (!g_Renderer.m_SkipSubmit) {
    551         glDrawElements(GL_QUADS,(GLsizei)m_Indices.size(),GL_UNSIGNED_SHORT,&m_Indices[0]);
     619        glDrawElements(GL_TRIANGLES,(GLsizei)m_Indices.size(),GL_UNSIGNED_SHORT,&m_Indices[0]);
    552620    }
    553621
    554622    // bump stats
  • TerrainRenderer.cpp

     
    216216    for(size_t i = 0; i < m->visiblePatches.size(); ++i)
    217217    {
    218218        CPatchRData* patchdata = (CPatchRData*)m->visiblePatches[i]->GetRenderData();
    219         patchdata->RenderBlends();
     219        //patchdata->RenderBlends();
    220220    }
    221221   
    222222    // Disable second texcoord array