-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathVertexBuffer.cpp
160 lines (144 loc) · 4.84 KB
/
VertexBuffer.cpp
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
#include "VertexBuffer.h"
void VertexBuffer::reserve(int cnt){
assertmsg(fmt, "vertex format is not set" );
array_len = cnt;
render_len = cnt;
unit_num_float = fmt->getNumFloat();
total_num_float = array_len * unit_num_float;
buf = (float*)MALLOC( total_num_float * sizeof(float));
assert(buf);
}
void VertexBuffer::copyFromBuffer( float *v, int vert_cnt ) {
assertmsg( unit_num_float > 0, "call setFormat() before this." );
assertmsg( vert_cnt <= array_len, "size too big");
array_len = vert_cnt;
total_num_float = vert_cnt * unit_num_float;
memcpy( buf, v, vert_cnt * unit_num_float * sizeof(float) );
}
void VertexBuffer::setCoord( int index, Vec3 v ) {
assertmsg(fmt, "vertex format is not set" );
assertmsg( index < array_len, "invalid index:%d array_len:%d", index, array_len );
int ofs = fmt->coord_offset;
assertmsg( ofs >= 0, "coord have not declared in vertex format" );
int index_in_array = index * unit_num_float + ofs;
buf[index_in_array] = v.x;
buf[index_in_array+1] = v.y;
buf[index_in_array+2] = v.z;
}
Vec3 VertexBuffer::getCoord( int index ) {
assertmsg(fmt, "vertex format is not set" );
assert( index < array_len );
int ofs = fmt->coord_offset;
assertmsg( ofs >= 0, "coord have not declared in vertex format" );
int index_in_array = index * unit_num_float + ofs;
return Vec3( buf[index_in_array], buf[index_in_array+1], buf[index_in_array+2] );
}
void VertexBuffer::setCoordBulk( Vec3 *v, int num ) {
for(int i=0;i<num;i++) {
setCoord( i, v[i] );
}
}
void VertexBuffer::setColor( int index, Color c ) {
assertmsg(fmt, "vertex format is not set");
assert( index < array_len );
int ofs = fmt->color_offset;
assertmsg( ofs >= 0, "color have not declared in vertex format");
int index_in_array = index * unit_num_float + ofs;
buf[index_in_array] = c.r;
buf[index_in_array+1] = c.g;
buf[index_in_array+2] = c.b;
buf[index_in_array+3] = c.a;
}
Color VertexBuffer::getColor( int index ) {
assertmsg(fmt, "vertex format is not set" );
assert( index < array_len );
int ofs = fmt->color_offset;
assertmsg( ofs >= 0, "color have not declared in vertex format" );
int index_in_array = index * unit_num_float + ofs;
return Color( buf[index_in_array], buf[index_in_array+1], buf[index_in_array+2], buf[index_in_array+3] );
}
void VertexBuffer::setUV( int index, Vec2 uv ) {
assertmsg(fmt, "vertex format is not set");
assert( index < array_len );
int ofs = fmt->texture_offset;
assertmsg( ofs >= 0, "texcoord have not declared in vertex format");
int index_in_array = index * unit_num_float + ofs;
buf[index_in_array] = uv.x;
buf[index_in_array+1] = uv.y;
}
Vec2 VertexBuffer::getUV( int index ) {
assertmsg(fmt, "vertex format is not set" );
assert( index < array_len );
int ofs = fmt->texture_offset;
assertmsg( ofs >= 0, "texuv have not declared in vertex format" );
int index_in_array = index * unit_num_float + ofs;
return Vec2( buf[index_in_array], buf[index_in_array+1] );
}
void VertexBuffer::setUVBulk( Vec2 *uv, int num ) {
for(int i=0;i<num;i++) {
setUV( i, uv[i] );
}
}
void VertexBuffer::setNormal( int index, Vec3 v ) {
assertmsg(fmt, "vertex format is not set");
assert( index < array_len );
int ofs = fmt->normal_offset;
assertmsg( ofs >= 0, "normal have not declared in vertex format" );
int index_in_array = index * unit_num_float + ofs;
buf[index_in_array] = v.x;
buf[index_in_array+1] = v.y;
buf[index_in_array+2] = v.z;
}
Vec3 VertexBuffer::getNormal( int index ) {
assertmsg(fmt, "vertex format is not set" );
assert( index < array_len );
int ofs = fmt->normal_offset;
assertmsg( ofs >= 0, "normal have not declared in vertex format" );
int index_in_array = index * unit_num_float + ofs;
return Vec3( buf[index_in_array], buf[index_in_array+1], buf[index_in_array+2] );
}
void VertexBuffer::setNormalBulk( Vec3 *v, int num ) {
for(int i=0;i<num;i++) {
setNormal( i, v[i] );
}
}
void VertexBuffer::bless(){
#if !defined(__linux__)
assert(fmt);
if( gl_name == 0 ){
glGenBuffers(1, &gl_name);
glBindBuffer( GL_ARRAY_BUFFER, gl_name );
glBufferData( GL_ARRAY_BUFFER, render_len * unit_num_float * sizeof(float), buf, GL_STATIC_DRAW );
glBindBuffer( GL_ARRAY_BUFFER, 0 );
}
#endif
}
void VertexBuffer::unbless() {
#if !defined(__linux__)
if( gl_name != 0 ) {
glDeleteBuffers(1, &gl_name);
gl_name = 0;
}
#endif
}
Vec3 VertexBuffer::calcCenterOfCoords() {
Vec3 c(0,0,0);
for(int i=0;i<array_len;i++) {
c += getCoord(i);
}
c /= (float)array_len;
return c;
}
void VertexBuffer::dump(int lim) {
print("vb[%p]: len:%d nfloat:%d unitfloat:%d glname:%d", this,array_len, total_num_float, unit_num_float, gl_name );
if(fmt) fmt->dump(); else print("vb:nofmt");
if(buf) {
for(int i=0;i<array_len&&i<lim;i++){
prt("[%d]:",i);
for(int j=0;j<unit_num_float;j++){
prt("%.10f ", buf[i*unit_num_float+j]);
}
print("");
}
}
}