0024192: Adding support for shaders to OCCT visualization toolkit
[occt.git] / src / Graphic3d / Graphic3d_ArrayOfPrimitives.lxx
1 // Created on: 2000-06-16
2 // Copyright (c) 2000-2012 OPEN CASCADE SAS
3 //
4 // The content of this file is subject to the Open CASCADE Technology Public
5 // License Version 6.5 (the "License"). You may not use the content of this file
6 // except in compliance with the License. Please obtain a copy of the License
7 // at http://www.opencascade.org and read it completely before using this file.
8 //
9 // The Initial Developer of the Original Code is Open CASCADE S.A.S., having its
10 // main offices at: 1, place des Freres Montgolfier, 78280 Guyancourt, France.
11 //
12 // The Original Code and all software distributed under the License is
13 // distributed on an "AS IS" basis, without warranty of any kind, and the
14 // Initial Developer hereby disclaims all such warranties, including without
15 // limitation, any warranties of merchantability, fitness for a particular
16 // purpose or non-infringement. Please see the License for the specific terms
17 // and conditions governing the rights and limitations under the License.
18
19
20 #include <Graphic3d_ArrayOfPrimitives.hxx>
21 #include <Standard_OutOfRange.hxx>
22
23 #include <stdio.h>
24 #include <stdlib.h>
25
26 #include <gp_Dir.hxx>
27 #include <gp_Pnt.hxx>
28
29 inline Graphic3d_PrimitiveArray Graphic3d_ArrayOfPrimitives::Array() const
30 {
31   return myPrimitiveArray;
32 }
33
34 inline Graphic3d_TypeOfPrimitiveArray Graphic3d_ArrayOfPrimitives::Type() const
35 {
36   Graphic3d_TypeOfPrimitiveArray type = Graphic3d_TOPA_UNDEFINED;
37   if( myPrimitiveArray ) type = (Graphic3d_TypeOfPrimitiveArray) myPrimitiveArray->type;
38   return type;
39 }
40
41 inline Standard_Boolean Graphic3d_ArrayOfPrimitives::HasVertexNormals() const
42 {
43   Standard_Boolean defined = Standard_False;
44   if( myPrimitiveArray && myPrimitiveArray->vnormals ) defined = Standard_True;
45   return defined;
46 }
47
48 inline Standard_Boolean Graphic3d_ArrayOfPrimitives::HasVertexColors() const
49 {
50   Standard_Boolean defined = Standard_False;
51   if( myPrimitiveArray && myPrimitiveArray->vcolours ) defined = Standard_True;
52   return defined;
53 }
54
55 inline Standard_Boolean Graphic3d_ArrayOfPrimitives::HasVertexTexels() const
56 {
57   Standard_Boolean defined = Standard_False;
58   if( myPrimitiveArray && myPrimitiveArray->vtexels ) defined = Standard_True;
59   return defined;
60 }
61
62 inline Standard_Boolean Graphic3d_ArrayOfPrimitives::HasEdgeInfos() const
63 {
64   Standard_Boolean defined = Standard_False;
65   if( myPrimitiveArray && myPrimitiveArray->edge_vis ) defined = Standard_True;
66   return defined;
67 }
68
69 inline Standard_Integer Graphic3d_ArrayOfPrimitives::VertexNumber() const
70 {
71   Standard_Integer number = -1;
72   if( myPrimitiveArray ) number = myPrimitiveArray->num_vertexs;
73   return number;
74 }
75
76 inline Standard_Integer Graphic3d_ArrayOfPrimitives::AddVertex(const gp_Pnt& aVertice)
77 {
78   return AddVertex(aVertice.X(),aVertice.Y(),aVertice.Z());
79 }
80
81 inline Standard_Integer Graphic3d_ArrayOfPrimitives::AddVertex(
82         const Standard_Real X, const Standard_Real Y, const Standard_Real Z)
83 {
84   return AddVertex(Standard_ShortReal(X),Standard_ShortReal(Y),Standard_ShortReal(Z));
85 }
86
87 inline Standard_Integer Graphic3d_ArrayOfPrimitives::AddVertex(const gp_Pnt& aVertice, const gp_Dir& aNormal)
88 {
89   return AddVertex(aVertice.X(),aVertice.Y(),aVertice.Z(),aNormal.X(),aNormal.Y(),aNormal.Z());
90 }
91
92 inline Standard_Integer Graphic3d_ArrayOfPrimitives::AddVertex(
93         const Standard_Real X, const Standard_Real Y, const Standard_Real Z,
94         const Standard_Real NX, const Standard_Real NY, const Standard_Real NZ)
95 {
96   return AddVertex(Standard_ShortReal(X),Standard_ShortReal(Y),Standard_ShortReal(Z),
97                    Standard_ShortReal(NX),Standard_ShortReal(NY),Standard_ShortReal(NZ));
98 }
99
100 inline Standard_Integer Graphic3d_ArrayOfPrimitives::AddVertex(const gp_Pnt& aVertice, const gp_Pnt2d& aTexel)
101 {
102   return AddVertex(aVertice.X(),aVertice.Y(),aVertice.Z(),aTexel.X(),aTexel.Y());
103 }
104
105 inline Standard_Integer Graphic3d_ArrayOfPrimitives::AddVertex(
106         const Standard_Real X, const Standard_Real Y, const Standard_Real Z,
107         const Standard_Real TX, const Standard_Real TY)
108 {
109   return AddVertex(Standard_ShortReal(X),Standard_ShortReal(Y),Standard_ShortReal(Z),
110                    Standard_ShortReal(TX),Standard_ShortReal(TY));
111 }
112
113 inline Standard_Integer Graphic3d_ArrayOfPrimitives::AddVertex(const gp_Pnt& aVertice, const gp_Dir& aNormal, const gp_Pnt2d& aTexel)
114 {
115   return AddVertex(aVertice.X(),aVertice.Y(),aVertice.Z(),aNormal.X(),aNormal.Y(),aNormal.Z(),aTexel.X(),aTexel.Y());
116 }
117
118 inline Standard_Integer Graphic3d_ArrayOfPrimitives::AddVertex(
119         const Standard_Real X, const Standard_Real Y, const Standard_Real Z,
120         const Standard_Real NX, const Standard_Real NY, const Standard_Real NZ,
121         const Standard_Real TX, const Standard_Real TY)
122 {
123   return AddVertex(Standard_ShortReal(X),Standard_ShortReal(Y),Standard_ShortReal(Z),
124                    Standard_ShortReal(NX),Standard_ShortReal(NY),Standard_ShortReal(NZ),
125                    Standard_ShortReal(TX),Standard_ShortReal(TY));
126 }
127
128 inline void Graphic3d_ArrayOfPrimitives::SetVertice(
129         const Standard_Integer anIndex,
130         const Standard_ShortReal X, const Standard_ShortReal Y, const Standard_ShortReal Z)
131 {
132   if( !myPrimitiveArray ) return;
133   if( anIndex < 1 || anIndex > myMaxVertexs )
134     Standard_OutOfRange::Raise(" BAD VERTEX index");
135
136   if( myPrimitiveArray->vertices ) {
137     Tfloat *p = myPrimitiveArray->vertices[anIndex-1].xyz;
138     *p++ = X;
139     *p++ = Y;
140     *p   = Z;
141   }
142   myPrimitiveArray->num_vertexs = Max(anIndex,myPrimitiveArray->num_vertexs);
143 }
144
145 inline void Graphic3d_ArrayOfPrimitives::SetVertexColor(const Standard_Integer anIndex,
146                                                         const Standard_Real R,
147                                                         const Standard_Real G,
148                                                         const Standard_Real B)
149 {
150   if( !myPrimitiveArray ) return;
151   if( anIndex < 1 || anIndex > myMaxVertexs ) {
152     Standard_OutOfRange::Raise(" BAD VERTEX index");
153   }
154
155   if( myPrimitiveArray->vcolours ) {
156     unsigned int red   = (unsigned int)(R * 255.);
157     unsigned int green = (unsigned int)(G * 255.);
158     unsigned int blue  = (unsigned int)(B * 255.);
159     unsigned int alpha = 0;
160     Standard_Integer outColor = alpha << 24 | blue << 16 | green << 8 | red;
161     SetVertexColor( anIndex, outColor );
162   }
163   myPrimitiveArray->num_vertexs = Max(anIndex,myPrimitiveArray->num_vertexs);
164 }
165
166 inline void Graphic3d_ArrayOfPrimitives::SetVertexNormal(
167         const Standard_Integer anIndex,
168         const Standard_Real NX, const Standard_Real NY, const Standard_Real NZ)
169 {
170   if( !myPrimitiveArray ) return;
171   if( anIndex < 1 || anIndex > myMaxVertexs ) {
172     Standard_OutOfRange::Raise(" BAD VERTEX index");
173   }
174
175   Standard_Integer index = anIndex-1;
176
177   {
178     if( myPrimitiveArray->vnormals ) {
179       Tfloat *p = myPrimitiveArray->vnormals[index].xyz;
180       *p++ = Standard_ShortReal(NX);
181       *p++ = Standard_ShortReal(NY);
182       *p = Standard_ShortReal(NZ);
183     }
184   }
185   myPrimitiveArray->num_vertexs = Max(anIndex,myPrimitiveArray->num_vertexs);
186 }
187
188 inline void Graphic3d_ArrayOfPrimitives::SetVertexTexel(
189         const Standard_Integer anIndex,
190         const Standard_Real TX, const Standard_Real TY)
191 {
192   if( !myPrimitiveArray ) return;
193   if( anIndex < 1 || anIndex > myMaxVertexs ) {
194     Standard_OutOfRange::Raise(" BAD VERTEX index");
195   }
196
197   Standard_Integer index = anIndex-1;
198   if( myPrimitiveArray->vtexels ) {
199     Tfloat *p = myPrimitiveArray->vtexels[index].xy;
200     *p++ = Standard_ShortReal(TX);
201     *p = Standard_ShortReal(TY);
202   }
203   myPrimitiveArray->num_vertexs = Max(anIndex,myPrimitiveArray->num_vertexs);
204 }
205
206 inline void Graphic3d_ArrayOfPrimitives::SetBoundColor(
207         const Standard_Integer anIndex,
208         const Standard_Real R, const Standard_Real G, const Standard_Real B)
209 {
210   if( !myPrimitiveArray ) return;
211   if( anIndex < 1 || anIndex > myMaxBounds ) {
212     Standard_OutOfRange::Raise(" BAD BOUND index");
213   }
214
215   Standard_Integer index = anIndex-1;
216   Tfloat *p = myPrimitiveArray->fcolours[index].rgb;
217   *p++ = Standard_ShortReal(R);
218   *p++ = Standard_ShortReal(G);
219   *p = Standard_ShortReal(B);
220
221   myPrimitiveArray->num_bounds = Max(anIndex,myPrimitiveArray->num_bounds);
222 }
223
224 inline void Graphic3d_ArrayOfPrimitives::Vertice(const Standard_Integer anIndex,
225         Standard_Real& X, Standard_Real& Y, Standard_Real& Z) const
226 {
227   X = Y = Z = 0.;
228   if( !myPrimitiveArray ) return;
229   if( anIndex < 1 || anIndex > myPrimitiveArray->num_vertexs ) {
230     Standard_OutOfRange::Raise(" BAD VERTEX index");
231   }
232
233   Standard_Integer index = anIndex-1;
234   if( myPrimitiveArray->vertices ) {
235     Tfloat *p = myPrimitiveArray->vertices[index].xyz;
236     X = Standard_Real(*p++); Y = Standard_Real(*p++); Z = Standard_Real(*p);
237   }
238 }
239
240 inline void Graphic3d_ArrayOfPrimitives::VertexColor(const Standard_Integer anIndex,
241         Standard_Real& R, Standard_Real& G, Standard_Real& B) const
242 {
243   R = G = B = 0;
244   if( !myPrimitiveArray ) return;
245   if( anIndex < 1 || anIndex > myPrimitiveArray->num_vertexs ) {
246     Standard_OutOfRange::Raise(" BAD VERTEX index");
247   }
248   Standard_Integer aColor;
249   VertexColor(anIndex, aColor);
250   if( myPrimitiveArray->vcolours ) {
251     Standard_Integer r,g,b;
252     Standard_Integer aColor;
253     VertexColor(anIndex, aColor);
254     /*Standard_Real A;
255     Standard_Integer a = aColor & 0xff000000;
256     a >>= 24;
257     A = ((Standard_Real) a) / 255.;*/
258
259     b = aColor & 0x00ff0000;
260     b >>= 16;
261     B =((Standard_Real) b) / 255.;
262
263     g = aColor & 0x0000ff00;
264     g >>= 8;
265     G = ((Standard_Real) g) / 255.;
266
267     r = aColor & 0x000000ff;
268     r >>= 0;
269     R = ((Standard_Real) r) / 255.;
270   }
271 }
272
273 inline void Graphic3d_ArrayOfPrimitives::VertexColor(const Standard_Integer anIndex,
274         Standard_Integer& aColor) const
275 {
276   Standard_Integer index = anIndex-1;
277
278   if( myPrimitiveArray->vcolours ) {
279 #if defined (sparc) || defined (__sparc__) || defined (__sparc)
280     aColor = 0;
281     const char* p_ch = (const char*)&(myPrimitiveArray->vcolours[index]);
282     aColor += p_ch[0];
283     aColor += p_ch[1] << 8 ;
284     aColor += p_ch[2] << 16;
285     aColor += p_ch[3] << 24;
286 #else
287     aColor = myPrimitiveArray->vcolours[index];
288 #endif
289     }
290 }
291
292 inline void Graphic3d_ArrayOfPrimitives::VertexNormal(const Standard_Integer anIndex,
293         Standard_Real& NX, Standard_Real& NY, Standard_Real& NZ) const
294 {
295   NX = NY = NZ = 0.;
296   if( !myPrimitiveArray ) return;
297   if( anIndex < 1 || anIndex > myPrimitiveArray->num_vertexs ) {
298     Standard_OutOfRange::Raise(" BAD VERTEX index");
299   }
300
301   Standard_Integer index = anIndex-1;
302   if( myPrimitiveArray->vnormals ) {
303     Tfloat *p = myPrimitiveArray->vnormals[index].xyz;
304     NX = Standard_Real(*p++); NY = Standard_Real(*p++); NZ = Standard_Real(*p);
305   }
306 }
307
308 inline void Graphic3d_ArrayOfPrimitives::VertexTexel(const Standard_Integer anIndex,
309         Standard_Real& TX, Standard_Real& TY) const
310 {
311   TX = TY = 0.;
312   if( !myPrimitiveArray ) return;
313   if( anIndex < 1 || anIndex > myPrimitiveArray->num_vertexs ) {
314     Standard_OutOfRange::Raise(" BAD VERTEX index");
315   }
316
317   Standard_Integer index = anIndex-1;
318   if( myPrimitiveArray->vtexels ) {
319     Tfloat *p = myPrimitiveArray->vtexels[index].xy;
320     TX = Standard_Real(*p++); TY = Standard_Real(*p);
321   }
322 }
323
324 inline Standard_Integer Graphic3d_ArrayOfPrimitives::EdgeNumber() const
325 {
326   Standard_Integer number = -1;
327   if( myPrimitiveArray ) number = myPrimitiveArray->num_edges;
328   return number;
329 }
330
331 inline Standard_Integer Graphic3d_ArrayOfPrimitives::Edge(const Standard_Integer anIndex ) const
332 {
333   Standard_Integer index=0;
334   if( myPrimitiveArray && myPrimitiveArray->edge_vis &&
335     (anIndex > 0) && (anIndex <= myPrimitiveArray->num_edges) ) {
336     index = myPrimitiveArray->edges[anIndex-1];
337   } else {
338     Standard_OutOfRange::Raise(" BAD EDGE index");
339   }
340   return index+1;
341 }
342
343 inline Standard_Boolean Graphic3d_ArrayOfPrimitives::EdgeIsVisible(const Standard_Integer anIndex ) const
344 {
345   Standard_Boolean isVisible = Standard_False;
346   if( myPrimitiveArray && myPrimitiveArray->edge_vis &&
347     (anIndex > 0) && (anIndex <= myPrimitiveArray->num_edges) ) {
348     isVisible = (myPrimitiveArray->edge_vis[anIndex-1] == 0) ?
349                     Standard_False : Standard_True;
350   } else {
351     Standard_OutOfRange::Raise(" BAD EDGE index");
352   }
353   return isVisible;
354 }
355
356 inline Standard_Boolean Graphic3d_ArrayOfPrimitives::HasBoundColors() const
357 {
358   Standard_Boolean defined = Standard_False;
359   if( myPrimitiveArray && myPrimitiveArray->fcolours ) defined = Standard_True;
360   return defined;
361 }
362
363 inline Standard_Integer Graphic3d_ArrayOfPrimitives::BoundNumber() const
364 {
365   Standard_Integer number = -1;
366   if( myPrimitiveArray ) number = myPrimitiveArray->num_bounds;
367   return number;
368 }
369
370 inline Standard_Integer Graphic3d_ArrayOfPrimitives::Bound(const Standard_Integer anIndex) const
371 {
372   Standard_Integer number=-1;
373   if( myPrimitiveArray && myPrimitiveArray->bounds &&
374     (anIndex > 0) && (anIndex <= myPrimitiveArray->num_bounds) ) {
375     number = myPrimitiveArray->bounds[anIndex-1];
376   } else {
377     Standard_OutOfRange::Raise(" BAD BOUND index");
378   }
379   return number;
380 }
381
382 inline void Graphic3d_ArrayOfPrimitives::BoundColor(const Standard_Integer anIndex,
383         Standard_Real& R, Standard_Real& G, Standard_Real& B) const
384 {
385   if( myPrimitiveArray && myPrimitiveArray->fcolours &&
386     (anIndex > 0) && (anIndex <= myPrimitiveArray->num_bounds) ) {
387     Tfloat *p = myPrimitiveArray->fcolours[anIndex-1].rgb;
388     R = Standard_Real(*p++); G = Standard_Real(*p++); B = Standard_Real(*p);
389   } else {
390     Standard_OutOfRange::Raise(" BAD BOUND index");
391   }
392 }