Added neighbour test on detected ray hit
[blender-staging.git] / source / blender / render / intern / source / rayobject_mesh.c
1 /**
2  * $Id$
3  *
4  * ***** BEGIN GPL LICENSE BLOCK *****
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (at your option) any later version. 
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software Foundation,
18  * Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
19  *
20  * The Original Code is Copyright (C) 2009 Blender Foundation.
21  * All rights reserved.
22  *
23  * The Original Code is: all of this file.
24  *
25  * Contributor(s): AndrĂ© Pinto.
26  *
27  * ***** END GPL LICENSE BLOCK *****
28  */
29 #include "rayobject.h"
30
31 #include "MEM_guardedalloc.h"
32 #include "DNA_mesh_types.h"
33 #include "DNA_meshdata_types.h"
34 #include "BKE_utildefines.h"
35
36 typedef struct RayMesh
37 {
38         RayObject rayobj;
39         
40         Mesh *mesh;
41         void *ob;
42         
43         RayFace *faces;
44         int num_faces;
45         
46 } RayMesh;
47
48 static int  RayObject_mesh_intersect(RayObject *o, Isect *isec);
49 static void RayObject_mesh_add(RayObject *o, RayObject *ob);
50 static void RayObject_mesh_done(RayObject *o);
51 static void RayObject_mesh_free(RayObject *o);
52 static void RayObject_mesh_bb(RayObject *o, float *min, float *max);
53
54 static RayObjectAPI mesh_api =
55 {
56         RayObject_mesh_intersect,
57         RayObject_mesh_add,
58         RayObject_mesh_done,
59         RayObject_mesh_free,
60         RayObject_mesh_bb
61 };
62
63
64 static int  RayObject_mesh_intersect(RayObject *o, Isect *isec)
65 {
66         RayMesh *rm= (RayMesh*)o;
67         int i, hit = 0;
68         for(i = 0; i<rm->num_faces; i++)
69                 if(RayObject_raycast( (RayObject*)rm->faces+i, isec ))
70                 {
71                         hit = 1;
72                         if(isec->mode == RE_RAY_SHADOW)
73                                 break;
74                 }
75
76         return hit;
77 }
78
79 static void RayObject_mesh_add(RayObject *o, RayObject *ob)
80 {
81 }
82
83 static void RayObject_mesh_done(RayObject *o)
84 {
85 }
86
87 static void RayObject_mesh_free(RayObject *o)
88 {
89         RayMesh *rm= (RayMesh*)o;
90         MEM_freeN( rm->faces );
91         MEM_freeN( rm );
92 }
93
94 static void RayObject_mesh_bb(RayObject *o, float *min, float *max)
95 {
96         RayMesh *rm= (RayMesh*)o;
97         int i;
98         for(i = 0; i<rm->mesh->totvert; i++)
99                 DO_MINMAX( rm->mesh->mvert[i].co, min, max);
100 }
101
102 RayObject* RayObject_mesh_create(Mesh *mesh, void *ob)
103 {
104         RayMesh *rm= MEM_callocN(sizeof(RayMesh), "Octree");
105         int i;
106         RayFace *face;
107         MFace *mface;
108         
109         rm->rayobj.api = &mesh_api;
110         rm->mesh = mesh;
111         rm->faces = MEM_callocN(sizeof(RayFace)*mesh->totface, "octree rayobject nodes");
112         rm->num_faces = mesh->totface;
113         
114         face = rm->faces;
115         mface = mesh->mface;
116         for(i=0; i<mesh->totface; i++, face++, mface++)
117         {
118                 face->v1 = mesh->mvert[mface->v1].co;
119                 face->v2 = mesh->mvert[mface->v2].co;
120                 face->v3 = mesh->mvert[mface->v3].co;
121                 face->v4 = mface->v4 ? mesh->mvert[mface->v4].co : NULL;
122                 
123                 face->ob = ob;
124                 face->face = (void*)i;
125         }
126         
127         return RayObject_unalign((RayObject*) rm);
128 }