00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015 #include "meshDrawer2D.h"
00016
00017 #include "geomVertexFormat.h"
00018 #include "geomVertexArrayFormat.h"
00019 #include "geomVertexData.h"
00020 #include "geomVertexWriter.h"
00021 #include "geomVertexRewriter.h"
00022 #include "camera.h"
00023 #include "boundingSphere.h"
00024 #include "geomTristrips.h"
00025 #include "geomTriangles.h"
00026 #include "geom.h"
00027 #include "geomNode.h"
00028 #include "pnmPainter.h"
00029 #include "pnmBrush.h"
00030 #include "lvecBase4.h"
00031 #include "lvector3.h"
00032 #include "pandaNode.h"
00033
00034 TypeHandle MeshDrawer2D::_type_handle;
00035
00036
00037 #define RANDF ((PN_stdfloat) rand() / (PN_stdfloat) 0x7fffffff)
00038
00039
00040
00041
00042
00043
00044
00045 void MeshDrawer2D::generator(int budget) {
00046
00047 _vdata = new GeomVertexData(_root.get_name(), GeomVertexFormat::get_v3c4t2(), Geom::UH_static);
00048 GeomVertexWriter *tvertex = new GeomVertexWriter(_vdata, "vertex");
00049 GeomVertexWriter *tuv = new GeomVertexWriter(_vdata, "texcoord");
00050 GeomVertexWriter *tcolor = new GeomVertexWriter(_vdata, "color");
00051 _prim = new GeomTriangles(Geom::UH_static);
00052
00053
00054
00055 for(int i = 0; i < budget; i++) {
00056 for( int vert = 0; vert < 4; vert++) {
00057
00058 LVector3 vec3 = LVector3(RANDF+10000,RANDF,RANDF);
00059 LVector4 vec4 = LVector4(RANDF,RANDF,RANDF,0);
00060 LVector2 vec2 = LVector2(RANDF,RANDF);
00061
00062 tvertex->add_data3(vec3);
00063 tcolor->add_data4(vec4);
00064 tuv->add_data2(vec2);
00065
00066 }
00067
00068 _prim->add_vertices(i*4+0, i*4+1, i*4+2);
00069 _prim->close_primitive();
00070
00071 _prim->add_vertices(i*4+1, i*4+2, i*4+3);
00072 _prim->close_primitive();
00073
00074 }
00075
00076 _geom = new Geom(_vdata);
00077 _geom->add_primitive(_prim);
00078 _geomnode = new GeomNode("__MeshDrawer_GeomNode");
00079 _geomnode->add_geom(_geom);
00080 _root.attach_new_node(_geomnode);
00081 _last_clear_index = budget;
00082
00083 delete tvertex;
00084 delete tuv;
00085 delete tcolor;
00086 }
00087
00088
00089
00090
00091
00092
00093
00094 void MeshDrawer2D::begin() {
00095
00096
00097 if (_vertex != NULL) delete _vertex;
00098 if (_uv != NULL) delete _uv;
00099 if (_color != NULL) delete _color;
00100
00101 _vertex = new GeomVertexRewriter(_vdata, "vertex");
00102 _uv = new GeomVertexRewriter(_vdata, "texcoord");
00103 _color = new GeomVertexRewriter(_vdata, "color");
00104 _dprim = _prim->decompose();
00105
00106
00107 _start_clear_index = 0;
00108 _end_clear_index = _budget;
00109 _clear_index = _start_clear_index;
00110
00111 }
00112
00113
00114
00115
00116
00117
00118
00119 void MeshDrawer2D::end() {
00120
00121
00122 for(int i = _clear_index ; i < _last_clear_index; i ++ ) {
00123 _vertex->add_data3(0,0,0);
00124 _vertex->add_data3(0,0,0);
00125 _vertex->add_data3(0,0,0);
00126 _vertex->add_data3(0,0,0);
00127 }
00128
00129 _last_clear_index = _clear_index;
00130
00131
00132 delete _vertex; _vertex = NULL;
00133 delete _uv; _uv = NULL;
00134 delete _color; _color = NULL;
00135
00136 }
00137
00138
00139
00140
00141
00142
00143
00144
00145
00146 void MeshDrawer2D::
00147 rectangle_tiled(PN_stdfloat x, PN_stdfloat y, PN_stdfloat w, PN_stdfloat h,
00148 PN_stdfloat u, PN_stdfloat v, PN_stdfloat us, PN_stdfloat vs,
00149 const LVector4 &color
00150 ) {
00151
00152 PN_stdfloat x_fit = w/us;
00153 PN_stdfloat y_fit = h/vs;
00154 PN_stdfloat x_pos = x;
00155
00156 while (x_fit > 0){
00157 PN_stdfloat y_pos = y;
00158 y_fit = h/vs;
00159 while (y_fit > 0){
00160
00161 PN_stdfloat fixed_us = us;
00162 PN_stdfloat fixed_vs = vs;
00163
00164
00165 if (x_fit < 1){
00166 fixed_us = w;
00167 while (fixed_us > us){
00168 fixed_us -= us;
00169 }
00170 }
00171
00172
00173 if (y_fit < 1){
00174 fixed_vs = h;
00175 while (fixed_vs > vs){
00176 fixed_vs -= vs;
00177 }
00178 }
00179
00180 rectangle(x_pos,y_pos,fixed_us,fixed_vs,u,v,fixed_us,fixed_vs,color);
00181
00182 y_pos += vs;
00183 y_fit -= 1;
00184 }
00185 x_pos += us;
00186 x_fit -= 1;
00187 }
00188
00189
00190 }
00191
00192
00193
00194
00195
00196
00197
00198
00199 void MeshDrawer2D::
00200 rectangle_border(
00201 PN_stdfloat x, PN_stdfloat y, PN_stdfloat w, PN_stdfloat h,
00202 PN_stdfloat r, PN_stdfloat t, PN_stdfloat l, PN_stdfloat b,
00203 PN_stdfloat tr, PN_stdfloat tt, PN_stdfloat tl, PN_stdfloat tb,
00204 PN_stdfloat u, PN_stdfloat v, PN_stdfloat us, PN_stdfloat vs,
00205 const LVector4 &color){
00206
00207 rectangle(x,y,w,h,u,v,us,vs,color);
00208
00209
00210 rectangle(x, y+h, w, t, u, v+vs, us, tt, color);
00211 rectangle(x, y-b, w, b, u, v-tb, us, tb, color);
00212
00213
00214 rectangle(x-l, y, l, h, u-tl, v, tl, vs, color);
00215 rectangle(x+w, y, r, h, r, v, tr, vs, color);
00216
00217
00218
00219
00220
00221
00222
00223 }
00224
00225
00226
00227
00228
00229
00230
00231 void MeshDrawer2D::
00232 rectangle_border_tiled(
00233 PN_stdfloat x, PN_stdfloat y, PN_stdfloat w, PN_stdfloat h,
00234 PN_stdfloat r, PN_stdfloat t, PN_stdfloat l, PN_stdfloat b,
00235 PN_stdfloat tr, PN_stdfloat tt, PN_stdfloat tl, PN_stdfloat tb,
00236 PN_stdfloat u, PN_stdfloat v, PN_stdfloat us, PN_stdfloat vs,
00237 const LVector4 &color){
00238
00239 rectangle_tiled(x,y,w,h,u,v,us,vs,color);
00240
00241 rectangle_tiled(x, y+h, w, t, u, v+t, us, t, color);
00242 rectangle_tiled(x, y-b, w, b, u, v-b, us, b, color);
00243 rectangle_tiled(x-l, y, l, h, u-l, v, l, vs, color);
00244 rectangle_tiled(x+w, y, r, h, r, v, r, vs, color);
00245
00246 rectangle_tiled(x-l, y+h, l, t, u-l, v, l, t, color);
00247 rectangle_tiled(x-l, y-b, l, b, u-l, v-b, l, b, color);
00248 rectangle_tiled(x+w, y+h, r, t, u, v, r, t, color);
00249 rectangle_tiled(x+w, y-b, r, b, u, v-b, r, b, color);
00250 }