Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download

open-axiom repository from github

24005 views
1
/*
2
Copyright (C) 1991-2002, The Numerical Algorithms Group Ltd.
3
All rights reserved.
4
Copyright (C) 2007-2008, Gabriel Dos Reis.
5
All rights reserved.
6
7
Redistribution and use in source and binary forms, with or without
8
modification, are permitted provided that the following conditions are
9
met:
10
11
- Redistributions of source code must retain the above copyright
12
notice, this list of conditions and the following disclaimer.
13
14
- Redistributions in binary form must reproduce the above copyright
15
notice, this list of conditions and the following disclaimer in
16
the documentation and/or other materials provided with the
17
distribution.
18
19
- Neither the name of The Numerical Algorithms Group Ltd. nor the
20
names of its contributors may be used to endorse or promote products
21
derived from this software without specific prior written permission.
22
23
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
24
IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
25
TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
26
PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
27
OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
28
EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
29
PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
30
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
31
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
32
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
33
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34
*/
35
36
#define _ILLUMINATE3D_C
37
#include "openaxiom-c-macros.h"
38
39
40
#include <math.h>
41
42
#include "header.h"
43
#include "draw.h"
44
45
#include "all_3d.H1"
46
47
/***********************
48
* void phong(pt,N) *
49
* *
50
* a general routine *
51
* for determining the *
52
* intensity values at *
53
* a particular point *
54
* using the Phong *
55
* illumination model *
56
* with Phong shading *
57
***********************/
58
59
float
60
phong(triple pt,float N[3])
61
{
62
float dotLN, dotHN, H[3], E[3], P[3], NM[3], L[3];
63
float color, diffuse, specular;
64
65
diffuse = 0.0; specular = 0.0;
66
67
/* renormalize average normal vector for the point */
68
normalizeVector(N);
69
/* temporary norm, in case the sign is switched */
70
NM[0] = N[0]; NM[1] = N[1]; NM[2] = N[2];
71
72
P[0] = pt.x; P[1] = pt.y; P[2] = pt.z;
73
normalizeVector(P);
74
75
/* vector from infinite light source */
76
L[0] = viewport->lightVector[0];
77
L[1] = viewport->lightVector[1];
78
L[2] = viewport->lightVector[2];
79
normalizeVector(L);
80
81
/* vector from point to observers eye */
82
normalizeVector(eyePoint);
83
E[0] = 8.0*eyePoint[0] - P[0];
84
E[1] = 8.0*eyePoint[1] - P[1];
85
E[2] = 8.0*eyePoint[2] - P[2];
86
normalizeVector(E);
87
88
/* light returned even if normal faces away from light source */
89
dotLN = L[0]*NM[0] + L[1]*NM[1] + L[2]*NM[2];
90
if (dotLN < 0.0) dotLN = -dotLN;
91
diffuse = dotLN*lightIntensity;
92
/* calculate specular highlight if surface faces light source */
93
if (dotLN > 0.0) {
94
H[0] = E[0] + L[0];
95
H[1] = E[1] + L[1];
96
H[2] = E[2] + L[2];
97
normalizeVector(H);
98
dotHN = NM[0]*H[0]+NM[1]*H[1]+NM[2]*H[2];
99
if (dotHN < 0.0) dotHN = -dotHN;
100
specular = pow((double)dotHN,coeff)*lightIntensity;
101
}
102
103
/* return intensity value from 0.0 to 1.0 */
104
color = Camb + diffuse*Cdiff + specular*Cspec;
105
106
if (color > 1.0) color = 1.0;
107
if (color < 0.0) color = 0.0;
108
109
return(color);
110
}
111
112
int
113
hueValue(float val)
114
{
115
int hue;
116
117
hue = floor(absolute(val) * viewport->numberOfHues) + viewport->hueOffset;
118
if (hue > 26) hue = 26;
119
120
return hue;
121
}
122
123
int
124
getHue(float val)
125
{
126
int hue;
127
128
hue = hueValue(val);
129
if (hue < 11)
130
hue *= 6;
131
else
132
if (hue > 10 && hue < 16)
133
hue = hue*20 - 140;
134
else
135
hue = hue*12 - 12;
136
137
return hue;
138
}
139
140
/**** Conversion functions for different color models ****/
141
142
float
143
Value(float n1, float n2, float hue)
144
{
145
float v;
146
147
if (hue > 360.0) hue -= 360.0;
148
if (hue < 0.0) hue += 360.0;
149
if (hue < 60.0) {
150
v = n1 + (n2-n1)*hue/60.0;
151
} else {
152
if (hue < 180.0) {
153
v = n2;
154
} else {
155
if (hue < 240.0) {
156
v = n1 + (n2-n1)*(240.0-hue)/60.0;
157
} else {
158
v = n1;
159
}
160
}
161
}
162
return(v);
163
}
164
165
166
RGB
167
hlsTOrgb(float h,float l,float s)
168
{
169
RGB rgb;
170
float m1, m2;
171
172
if (l <= 0.5) {
173
m2 = l*(1.0+s);
174
}
175
else {
176
m2 = l+s-l*s;
177
}
178
m1 = 2.0*l-m2;
179
rgb.r = Value(m1,m2,h+120.0);
180
rgb.g = Value(m1,m2,h);
181
rgb.b = Value(m1,m2,h-120.0);
182
183
return(rgb);
184
}
185
186
187
188
189
190
191
192