FreeWRL / FreeX3D 4.3.0
NormalCalcs.c
1/*
2
3
4???
5
6*/
7
8
9/****************************************************************************
10 This file is part of the FreeWRL/FreeX3D Distribution.
11
12 Copyright 2009 CRC Canada. (http://www.crc.gc.ca)
13
14 FreeWRL/FreeX3D is free software: you can redistribute it and/or modify
15 it under the terms of the GNU Lesser Public License as published by
16 the Free Software Foundation, either version 3 of the License, or
17 (at your option) any later version.
18
19 FreeWRL/FreeX3D is distributed in the hope that it will be useful,
20 but WITHOUT ANY WARRANTY; without even the implied warranty of
21 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 GNU General Public License for more details.
23
24 You should have received a copy of the GNU General Public License
25 along with FreeWRL/FreeX3D. If not, see <http://www.gnu.org/licenses/>.
26****************************************************************************/
27
28
29
30#include <config.h>
31#include <system.h>
32#include <display.h>
33#include <internal.h>
34
35#include <libFreeWRL.h>
36
37#include "../vrml_parser/Structs.h"
38#include "../main/headers.h"
39
40#include "LinearAlgebra.h"
41
42
43void fwnorprint (float *norm) {
44 printf ("normals %f %f %f\n",norm[0],norm[1],norm[2]);
45}
46
47void normalize_ifs_face (float *point_normal,
48 struct SFVec3f *facenormals, //struct point_XYZ *facenormals,
49 int *pointfaces,
50 int mypoint,
51 int curpoly,
52 float creaseAngle) {
53
54 /* IndexedFaceSet (and possibly sometime, others)
55 normal generator
56
57 Passed in:
58 point_normal - where to put the calculated normal
59 facenormals - normals of each face of a polygon
60 pointfaces - each point - which face(s) is it part of
61 mypoint - which point are we looking at
62 curpoly - which poly (face) we are working on
63 creaseAngle - creaseAngle of polygon
64 */
65 int tmp_a;
66 int tmp_b;
67 float zz;
68 //struct point_XYZ temp;
69 bool foundInOtherFaces = false;
70
71 point_normal[0] = 0.0f; point_normal[1] = 0.0f; point_normal[2] = 0.0f;
72
73 //printf ("\nstart normalize_ifs_face\n");
74 //printf ("my normal is %f %f %f\n", facenormals[curpoly].x,facenormals[curpoly].y,facenormals[curpoly].z);
75
76 /* short cut for a point in only 1 face */
77 if (pointfaces[mypoint*POINT_FACES] == 1) {
78 //point_normal[0]=(float) facenormals[curpoly].x;
79 //point_normal[1]=(float) facenormals[curpoly].y;
80 //point_normal[2]=(float) facenormals[curpoly].z;
81 veccopy3f(point_normal,facenormals[curpoly].c);
82 //printf ("normalize_ifs_face: quick return normalized vector is %f %f %f\n",point_normal[0], point_normal[1], point_normal[2]);
83 return;
84 }
85
86 /* ok, calculate normal */
87 for (tmp_b=0; tmp_b<pointfaces[mypoint*POINT_FACES]; tmp_b++) {
88 tmp_a = pointfaces[mypoint*POINT_FACES+tmp_b+1];
89 //printf ("comparing myface %d to %d\n",curpoly,tmp_a);
90
91 if (curpoly == tmp_a) {
92 zz = 0.0f;
93 } else {
94 zz = calc_angle_between_two_vectors3f(facenormals[curpoly].c,facenormals[tmp_a].c );
95 }
96 //printf ("angle between faces is %f, creaseAngle is %f\n",zz,creaseAngle);
97
98
99 if (zz <= creaseAngle) {
100 //printf ("count this one in; adding %f %f %f\n",facenormals[tmp_a].x,facenormals[tmp_a].y,facenormals[tmp_a].z);
101 foundInOtherFaces = true;
102 //point_normal[0] += (float) facenormals[tmp_a].x;
103 //point_normal[1] += (float) facenormals[tmp_a].y;
104 //point_normal[2] += (float) facenormals[tmp_a].z;
105 vecadd3f(point_normal,point_normal,facenormals[tmp_a].c);
106 }
107 }
108
109 // do we have to average this one, or should we just return our original normal?
110 if (foundInOtherFaces) {
111 //temp.x = point_normal[0]; temp.y=point_normal[1]; temp.z=point_normal[2];
112 //normalize_vector(&temp);
113 //point_normal[0]=(float) temp.x; point_normal[1]=(float) temp.y; point_normal[2]=(float) temp.z;
114 vecnormalize3f(point_normal,point_normal);
115 } else {
116 //printf ("false alarm - just copy original over");
117 // point_normal[0]=(float) facenormals[curpoly].x;
118 //point_normal[1]=(float) facenormals[curpoly].y;
119 //point_normal[2]=(float) facenormals[curpoly].z;
120 veccopy3f(point_normal,facenormals[curpoly].c);
121 }
122
123 //printf ("normalize_ifs_face: normalized vector is %f %f %f\n",point_normal[0], point_normal[1], point_normal[2]);
124}