-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvrUserGeometry.cpp
More file actions
172 lines (141 loc) · 5.13 KB
/
vrUserGeometry.cpp
File metadata and controls
172 lines (141 loc) · 5.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
#include "stdafx.h"
#include "vrUserGeometry.h"
VUBASE_SOURCE_INCLUDES_COMPOSITE(vrUserGeometry, vrGeometryBase,
new vrUserGeometry, true, false)
///////////////////////////////////////////////////////////////////////////
//
// constructor / public
//
vrUserGeometry::vrUserGeometry( int num ) //注意:在使用这个类的时候一定要vrUserGeometry::initializeClass(),并在程序最终结束时执行vrUserGeometry::shutdownClass()。这两个函数最好在main函数里执行,没有执行或者执行的位置(时机)不对将导致构造
//函数或析构函数中的registerInstance或unregisterInstance方法崩溃,原因是在这种情形下,s_classType是无效的指针NULL。
{
// our geometry class will use line primitives
setPrimitive(vrGeometryBase::PRIMITIVE_LINE);
// if the user gave us a valid number of lines, preallocate the array
if( num > 0 )
{
vuVec3<float> *vertex = vuAllocArray<vuVec3<float> >::malloc(2 * num);
setVertices(vertex);
setNumPrimitives(num);
}
// create the color attribute array. We'll use an overall color for all
// of the lines and set the default color to white
vuVec4<float> *color = vuAllocArray<vuVec4<float> >::malloc(1);
color[0].set(1.0f, 1.0f, 1.0f, 1.0f);
setColors(color, vrGeometryBase::BINDING_OVERALL);
// register the instance w/ the system
registerInstance(s_classType);
}
///////////////////////////////////////////////////////////////////////////
//
// destructor / protected
//
vrUserGeometry::~vrUserGeometry()
{
// unregister the instance w/ the system
unregisterInstance(s_classType);
}
vrGeometryBase *vrUserGeometry::makeCopy(const vsgu::Options& options) const
{
return vsgu::makeCopy(this,options);
}
///////////////////////////////////////////////////////////////////////////
//
// setLineSegment / public
//
int vrUserGeometry::setLineSegment( int index, const vuVec3<float> &begin,
const vuVec3<float> &end )
{
// make sure we've got a valid index
int numLines = getNumPrimitives();
VUNOTIFY_CHECK_IF(index > numLines, (vuNotify::LEVEL_WARN, s_classType,
"vrUserGeometry::setEndPoints - invalid index"), vsgu::FAILURE);
// if necessary, allocate / reallocate the vertex array and increment the
// number of primitives
vuVec3<float> *vertex = getVertices();
if( index == numLines )
{
int numVertices = 2 * (numLines + 1);
if( vertex == NULL )
vertex = vuAllocArray<vuVec3<float> >::malloc(numVertices);
else
vertex = vuAllocArray<vuVec3<float> >::realloc(vertex, numVertices);
setNumPrimitives(numLines + 1);
setVertices(vertex);
}
vertex[2*index] = begin;
vertex[2*index+1] = end;
// Reset the vertex array on the geometry so it "knows" that it's been
// changed
setVertices( vertex );
return vsgu::SUCCESS;
}
///////////////////////////////////////////////////////////////////////////
//
// getLineSegment / public
//
int vrUserGeometry::getLineSegment( int index, vuVec3<float> *begin,
vuVec3<float> *end ) const
{
// Make sure we've got a valid index
int numLines = getNumPrimitives();
VUNOTIFY_CHECK_IF(index >= numLines, (vuNotify::LEVEL_WARN, s_classType,
"vrUserGeometry::getEndPoints - invalid index"), vsgu::FAILURE);
vuVec3<float> *vertex = getVertices();
*begin = vertex[2*index];
*end = vertex[2*index+1];
return vsgu::SUCCESS;
}
float* vrUserGeometry::getValue(int _index, int _pos, int _axis)
{
int numLines = getNumPrimitives();
if(_index >= numLines)
{
std::cout << "vrUserGeometry::getValue(): Invalid index:" << _index << std::endl;
return NULL;
}
if (_pos != 0 && _pos != 1)
{
std::cout << "vrUserGeometry::getValue(): Invalid pos:" << _pos << std::endl;
return NULL;
}
if (_axis != 0 && _axis != 1 && _axis != 2)
{
std::cout << "vrUserGeometry::getValue(): Invalid axis:" << _axis << std::endl;
return NULL;
}
vuVec3f* vertex = getVertices();
float* ret =&(((vertex[2*_index+_pos]).m_vec)[_axis]);
return ret;
}
int vrUserGeometry::setNumPrimitives(int num)
{
return vrGeometryBase::setNumPrimitives(num);
}
///////////////////////////////////////////////////////////////////////////
//
// setColor / public
//
int vrUserGeometry::setColor(float r, float g, float b, float a)
{
// Make sure we've got a valid color
VUNOTIFY_CHECK_COLOR(r, g, b, a, (vuNotify::LEVEL_WARN, s_classType,
"vrUserGeometry::setColor - invalid color"), vsgu::FAILURE);
// Update the entry in the color array and then reset the array on the
// geometry so it "knows" that it's been changed
vrGeometryBase::Binding binding;
vuVec4<float> *color = getColors( &binding );
color[0].set(r, g, b, a);
setColors( color, vrGeometryBase::BINDING_OVERALL );
return vsgu::SUCCESS;
}
///////////////////////////////////////////////////////////////////////////
//
// getColor / public
//
void vrUserGeometry::getColor(float *r, float *g, float *b, float *a) const
{
vrGeometryBase::Binding binding;
vuVec4<float> *color = getColors( &binding );
color[0].get(r, g, b, a);
}