OLD | NEW |
1 /* | 1 /* |
2 * $Id$ | 2 * $Id$ |
3 * | 3 * |
4 * ***** BEGIN GPL LICENSE BLOCK ***** | 4 * ***** BEGIN GPL LICENSE BLOCK ***** |
5 * | 5 * |
6 * This program is free software; you can redistribute it and/or | 6 * This program is free software; you can redistribute it and/or |
7 * modify it under the terms of the GNU General Public License | 7 * modify it under the terms of the GNU General Public License |
8 * as published by the Free Software Foundation; either version 2 | 8 * as published by the Free Software Foundation; either version 2 |
9 * of the License, or (at your option) any later version. | 9 * of the License, or (at your option) any later version. |
10 * | 10 * |
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
55 #include "BKE_displist.h" | 55 #include "BKE_displist.h" |
56 #include "BKE_key.h" | 56 #include "BKE_key.h" |
57 #include "BKE_modifier.h" | 57 #include "BKE_modifier.h" |
58 #include "BKE_mesh.h" | 58 #include "BKE_mesh.h" |
59 #include "BKE_object.h" | 59 #include "BKE_object.h" |
60 #include "BKE_paint.h" | 60 #include "BKE_paint.h" |
61 #include "BKE_texture.h" | 61 #include "BKE_texture.h" |
62 #include "BKE_multires.h" | 62 #include "BKE_multires.h" |
63 #include "BKE_armature.h" | 63 #include "BKE_armature.h" |
64 | 64 |
| 65 #ifdef WITH_GAMEENGINE |
| 66 #include "BKE_navmesh_conversion.h" |
| 67 #endif |
| 68 |
65 #include "BLO_sys_types.h" // for intptr_t support | 69 #include "BLO_sys_types.h" // for intptr_t support |
66 | 70 |
67 #include "GL/glew.h" | 71 #include "GL/glew.h" |
68 | 72 |
69 #include "GPU_buffers.h" | 73 #include "GPU_buffers.h" |
70 #include "GPU_draw.h" | 74 #include "GPU_draw.h" |
71 #include "GPU_extensions.h" | 75 #include "GPU_extensions.h" |
72 #include "GPU_material.h" | 76 #include "GPU_material.h" |
73 | 77 |
74 extern GLubyte stipple_quarttone[128]; /* glutil.c, bad level data */ | 78 extern GLubyte stipple_quarttone[128]; /* glutil.c, bad level data */ |
75 | 79 |
| 80 static DerivedMesh *navmesh_dm_createNavMeshForVisualization(DerivedMesh *dm); |
| 81 |
76 /////////////////////////////////// | 82 /////////////////////////////////// |
77 /////////////////////////////////// | 83 /////////////////////////////////// |
78 | 84 |
79 static MVert *dm_getVertArray(DerivedMesh *dm) | 85 static MVert *dm_getVertArray(DerivedMesh *dm) |
80 { | 86 { |
81 MVert *mvert = CustomData_get_layer(&dm->vertData, CD_MVERT); | 87 MVert *mvert = CustomData_get_layer(&dm->vertData, CD_MVERT); |
82 | 88 |
83 if (!mvert) { | 89 if (!mvert) { |
84 mvert = CustomData_add_layer(&dm->vertData, CD_MVERT, CD_CALLOC,
NULL, | 90 mvert = CustomData_add_layer(&dm->vertData, CD_MVERT, CD_CALLOC,
NULL, |
85 dm->getNumVerts(dm)); | 91 dm->getNumVerts(dm)); |
(...skipping 2012 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2098 } | 2104 } |
2099 | 2105 |
2100 /* add an orco layer if needed */ | 2106 /* add an orco layer if needed */ |
2101 if(dataMask & CD_MASK_ORCO) { | 2107 if(dataMask & CD_MASK_ORCO) { |
2102 add_orco_dm(ob, NULL, finaldm, orcodm, CD_ORCO); | 2108 add_orco_dm(ob, NULL, finaldm, orcodm, CD_ORCO); |
2103 | 2109 |
2104 if(deform_r && *deform_r) | 2110 if(deform_r && *deform_r) |
2105 add_orco_dm(ob, NULL, *deform_r, NULL, CD_ORCO); | 2111 add_orco_dm(ob, NULL, *deform_r, NULL, CD_ORCO); |
2106 } | 2112 } |
2107 | 2113 |
| 2114 /* NavMesh - this is a hack but better then having a NavMesh modifier */ |
| 2115 if (ob->body_type == OB_BODY_TYPE_NAVMESH && finaldm->type == DM_TYPE_CD
DM) { |
| 2116 DerivedMesh *tdm; |
| 2117 tdm= navmesh_dm_createNavMeshForVisualization(finaldm); |
| 2118 if (finaldm != tdm) { |
| 2119 finaldm->release(finaldm); |
| 2120 finaldm= tdm; |
| 2121 } |
| 2122 } |
| 2123 |
2108 *final_r = finaldm; | 2124 *final_r = finaldm; |
2109 | 2125 |
2110 if(orcodm) | 2126 if(orcodm) |
2111 orcodm->release(orcodm); | 2127 orcodm->release(orcodm); |
2112 if(clothorcodm) | 2128 if(clothorcodm) |
2113 clothorcodm->release(clothorcodm); | 2129 clothorcodm->release(clothorcodm); |
2114 | 2130 |
2115 if(deformedVerts && deformedVerts != inputVertexCos) | 2131 if(deformedVerts && deformedVerts != inputVertexCos) |
2116 MEM_freeN(deformedVerts); | 2132 MEM_freeN(deformedVerts); |
2117 | 2133 |
(...skipping 814 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2932 | 2948 |
2933 INIT_MINMAX(min, max); | 2949 INIT_MINMAX(min, max); |
2934 | 2950 |
2935 dm->getMinMax(dm, min, max); | 2951 dm->getMinMax(dm, min, max); |
2936 | 2952 |
2937 if(!ob->bb) | 2953 if(!ob->bb) |
2938 ob->bb= MEM_callocN(sizeof(BoundBox), "DM-BoundBox"); | 2954 ob->bb= MEM_callocN(sizeof(BoundBox), "DM-BoundBox"); |
2939 | 2955 |
2940 boundbox_set_from_min_max(ob->bb, min, max); | 2956 boundbox_set_from_min_max(ob->bb, min, max); |
2941 } | 2957 } |
| 2958 |
| 2959 /* NAVMESH */ |
| 2960 BM_INLINE int navmesh_bit(int a, int b) |
| 2961 { |
| 2962 return (a & (1 << b)) >> b; |
| 2963 } |
| 2964 |
| 2965 BM_INLINE void navmesh_intToCol(int i, float* col) |
| 2966 { |
| 2967 int r = navmesh_bit(i, 0) + navmesh_bit(i, 3) * 2 + 1; |
| 2968 int g = navmesh_bit(i, 1) + navmesh_bit(i, 4) * 2 + 1; |
| 2969 int b = navmesh_bit(i, 2) + navmesh_bit(i, 5) * 2 + 1; |
| 2970 col[0] = 1 - r*63.0f/255.0f; |
| 2971 col[1] = 1 - g*63.0f/255.0f; |
| 2972 col[2] = 1 - b*63.0f/255.0f; |
| 2973 } |
| 2974 |
| 2975 #ifdef WITH_GAMEENGINE |
| 2976 |
| 2977 static void navmesh_drawColored(DerivedMesh *dm) |
| 2978 { |
| 2979 int a, glmode; |
| 2980 MVert *mvert = (MVert *)CustomData_get_layer(&dm->vertData, CD_MVERT); |
| 2981 MFace *mface = (MFace *)CustomData_get_layer(&dm->faceData, CD_MFACE); |
| 2982 int* polygonIdx = (int*)CustomData_get_layer(&dm->faceData, CD_RECAST); |
| 2983 const float BLACK_COLOR[3] = {0.f, 0.f, 0.f}; |
| 2984 float col[3]; |
| 2985 |
| 2986 if (!polygonIdx) |
| 2987 return; |
| 2988 |
| 2989 /* |
| 2990 //UI_ThemeColor(TH_WIRE); |
| 2991 glDisable(GL_LIGHTING); |
| 2992 glLineWidth(2.0); |
| 2993 dm->drawEdges(dm, 0, 1); |
| 2994 glLineWidth(1.0); |
| 2995 glEnable(GL_LIGHTING);*/ |
| 2996 |
| 2997 glDisable(GL_LIGHTING); |
| 2998 if(GPU_buffer_legacy(dm) ) { |
| 2999 DEBUG_VBO( "Using legacy code. drawNavMeshColored\n" ); |
| 3000 //glShadeModel(GL_SMOOTH); |
| 3001 glBegin(glmode = GL_QUADS); |
| 3002 for(a = 0; a < dm->numFaceData; a++, mface++) { |
| 3003 int new_glmode = mface->v4?GL_QUADS:GL_TRIANGLES; |
| 3004 int polygonIdx = *(int*)CustomData_get(&dm->faceData, a,
CD_RECAST); |
| 3005 if (polygonIdx<=0) |
| 3006 memcpy(col, BLACK_COLOR, 3*sizeof(float)); |
| 3007 else |
| 3008 navmesh_intToCol(polygonIdx, col); |
| 3009 |
| 3010 if(new_glmode != glmode) { |
| 3011 glEnd(); |
| 3012 glBegin(glmode = new_glmode); |
| 3013 } |
| 3014 glColor3fv(col); |
| 3015 glVertex3fv(mvert[mface->v1].co); |
| 3016 glVertex3fv(mvert[mface->v2].co); |
| 3017 glVertex3fv(mvert[mface->v3].co); |
| 3018 if(mface->v4) { |
| 3019 glVertex3fv(mvert[mface->v4].co); |
| 3020 } |
| 3021 } |
| 3022 glEnd(); |
| 3023 } |
| 3024 glEnable(GL_LIGHTING); |
| 3025 } |
| 3026 |
| 3027 static void navmesh_DM_drawFacesTex(DerivedMesh *dm, int (*setDrawOptions)(MTFac
e *tface, int has_mcol, int matnr)) |
| 3028 { |
| 3029 (void) setDrawOptions; |
| 3030 |
| 3031 navmesh_drawColored(dm); |
| 3032 } |
| 3033 |
| 3034 static void navmesh_DM_drawFacesSolid(DerivedMesh *dm, |
| 3035 float (*partial_
redraw_planes)[4], |
| 3036 int UNUSED(fast)
, int (*setMaterial)(int, void *attribs)) |
| 3037 { |
| 3038 (void) partial_redraw_planes; |
| 3039 (void) setMaterial; |
| 3040 |
| 3041 //drawFacesSolid_original(dm, partial_redraw_planes, fast, setMaterial); |
| 3042 navmesh_drawColored(dm); |
| 3043 } |
| 3044 #endif /* WITH_GAMEENGINE */ |
| 3045 |
| 3046 static DerivedMesh *navmesh_dm_createNavMeshForVisualization(DerivedMesh *dm) |
| 3047 { |
| 3048 #ifdef WITH_GAMEENGINE |
| 3049 DerivedMesh *result; |
| 3050 int maxFaces = dm->getNumFaces(dm); |
| 3051 int *recastData; |
| 3052 int vertsPerPoly=0, nverts=0, ndtris=0, npolys=0; |
| 3053 float* verts=NULL; |
| 3054 unsigned short *dtris=NULL, *dmeshes=NULL, *polys=NULL; |
| 3055 int *dtrisToPolysMap=NULL, *dtrisToTrisMap=NULL, *trisToFacesMap=NULL; |
| 3056 int res; |
| 3057 |
| 3058 result = CDDM_copy(dm); |
| 3059 if (!CustomData_has_layer(&result->faceData, CD_RECAST)) |
| 3060 { |
| 3061 int *sourceRecastData = (int*)CustomData_get_layer(&dm->faceData
, CD_RECAST); |
| 3062 CustomData_add_layer_named(&result->faceData, CD_RECAST, CD_DUPL
ICATE, |
| 3063 sourceRecastData, maxFaces, "recastData"); |
| 3064 } |
| 3065 recastData = (int*)CustomData_get_layer(&result->faceData, CD_RECAST); |
| 3066 result->drawFacesTex = navmesh_DM_drawFacesTex; |
| 3067 result->drawFacesSolid = navmesh_DM_drawFacesSolid; |
| 3068 |
| 3069 |
| 3070 //process mesh |
| 3071 res = buildNavMeshDataByDerivedMesh(dm, &vertsPerPoly, &nverts, &verts,
&ndtris, &dtris, |
| 3072
&npolys, &dmeshes, &polys, &dtrisToPolysMap, &dtrisToTrisMap, |
| 3073
&trisToFacesMap); |
| 3074 if (res) |
| 3075 { |
| 3076 size_t polyIdx; |
| 3077 |
| 3078 //invalidate concave polygon |
| 3079 for (polyIdx=0; polyIdx<(size_t)npolys; polyIdx++) |
| 3080 { |
| 3081 unsigned short* poly = &polys[polyIdx*2*vertsPerPoly]; |
| 3082 if (!polyIsConvex(poly, vertsPerPoly, verts)) |
| 3083 { |
| 3084 //set negative polygon idx to all faces |
| 3085 unsigned short *dmesh = &dmeshes[4*polyIdx]; |
| 3086 unsigned short tbase = dmesh[2]; |
| 3087 unsigned short tnum = dmesh[3]; |
| 3088 unsigned short ti; |
| 3089 |
| 3090 for (ti=0; ti<tnum; ti++) |
| 3091 { |
| 3092 unsigned short triidx = dtrisToTrisMap[t
base+ti]; |
| 3093 unsigned short faceidx = trisToFacesMap[
triidx]; |
| 3094 if (recastData[faceidx]>0) |
| 3095 recastData[faceidx] = -recastDat
a[faceidx]; |
| 3096 } |
| 3097 } |
| 3098 } |
| 3099 |
| 3100 } |
| 3101 else |
| 3102 { |
| 3103 printf("Error during creation polygon infos\n"); |
| 3104 } |
| 3105 |
| 3106 //clean up |
| 3107 if (verts!=NULL) |
| 3108 MEM_freeN(verts); |
| 3109 if (dtris!=NULL) |
| 3110 MEM_freeN(dtris); |
| 3111 if (dmeshes!=NULL) |
| 3112 MEM_freeN(dmeshes); |
| 3113 if (polys!=NULL) |
| 3114 MEM_freeN(polys); |
| 3115 if (dtrisToPolysMap!=NULL) |
| 3116 MEM_freeN(dtrisToPolysMap); |
| 3117 if (dtrisToTrisMap!=NULL) |
| 3118 MEM_freeN(dtrisToTrisMap); |
| 3119 if (trisToFacesMap!=NULL) |
| 3120 MEM_freeN(trisToFacesMap); |
| 3121 |
| 3122 return result; |
| 3123 #else // WITH_GAMEENGINE |
| 3124 return dm; |
| 3125 #endif // WITH_GAMEENGINE |
| 3126 } |
OLD | NEW |