the game where you go into mines and start crafting! but for consoles (forked directly from smartcmd's github)
at main 327 lines 6.7 kB view raw
1#include "stdafx.h" 2#include "TexOffs.h" 3#include "ModelPart.h" 4#include "Cube.h" 5 6const float ModelPart::RAD = (180.0f / PI); 7 8void ModelPart::_init() 9{ 10 xTexSize = 64.0f; 11 yTexSize = 32.0f; 12 list = 0; 13 compiled=false; 14 bMirror = false; 15 visible = true; 16 neverRender = false; 17 x=y=z = 0.0f; 18 xRot=yRot=zRot = 0.0f; 19 translateX = translateY = translateZ = 0.0f; 20} 21 22ModelPart::ModelPart() 23{ 24 _init(); 25} 26 27ModelPart::ModelPart(Model *model, const wstring& id) 28{ 29 construct(model, id); 30} 31 32ModelPart::ModelPart(Model *model) 33{ 34 construct(model); 35} 36 37ModelPart::ModelPart(Model *model, int xTexOffs, int yTexOffs) 38{ 39 construct(model, xTexOffs, yTexOffs); 40} 41 42 43void ModelPart::construct(Model *model, const wstring& id) 44{ 45 _init(); 46 this->model = model; 47 model->cubes.push_back(this); 48 this->id = id; 49 setTexSize(model->texWidth, model->texHeight); 50} 51 52void ModelPart::construct(Model *model) 53{ 54 _init(); 55 construct(model, L""); 56} 57 58void ModelPart::construct(Model *model, int xTexOffs, int yTexOffs) 59{ 60 _init(); 61 construct(model); 62 texOffs(xTexOffs, yTexOffs); 63} 64 65 66void ModelPart::addChild(ModelPart *child) 67{ 68 //if (children == NULL) children = new ModelPartArray; 69 children.push_back(child); 70} 71 72ModelPart * ModelPart::retrieveChild(SKIN_BOX *pBox) 73{ 74 for(AUTO_VAR(it, children.begin()); it != children.end(); ++it) 75 { 76 ModelPart *child=*it; 77 78 for(AUTO_VAR(itcube, child->cubes.begin()); itcube != child->cubes.end(); ++itcube) 79 { 80 Cube *pCube=*itcube; 81 82 if((pCube->x0==pBox->fX) && 83 (pCube->y0==pBox->fY) && 84 (pCube->z0==pBox->fZ) && 85 (pCube->x1==(pBox->fX + pBox->fW)) && 86 (pCube->y1==(pBox->fY + pBox->fH)) && 87 (pCube->z1==(pBox->fZ + pBox->fD)) 88 ) 89 { 90 return child; 91 break; 92 } 93 } 94 } 95 96 return NULL; 97} 98 99ModelPart *ModelPart::mirror() 100{ 101 bMirror = !bMirror; 102 return this; 103} 104 105ModelPart *ModelPart::texOffs(int xTexOffs, int yTexOffs) 106{ 107 this->xTexOffs = xTexOffs; 108 this->yTexOffs = yTexOffs; 109 return this; 110} 111 112ModelPart *ModelPart::addBox(wstring id, float x0, float y0, float z0, int w, int h, int d) 113{ 114 id = this->id + L"." + id; 115 TexOffs *offs = model->getMapTex(id); 116 texOffs(offs->x, offs->y); 117 cubes.push_back((new Cube(this, xTexOffs, yTexOffs, x0, y0, z0, w, h, d, 0))->setId(id)); 118 return this; 119} 120 121ModelPart *ModelPart::addBox(float x0, float y0, float z0, int w, int h, int d) 122{ 123 cubes.push_back(new Cube(this, xTexOffs, yTexOffs, x0, y0, z0, w, h, d, 0)); 124 return this; 125} 126 127void ModelPart::addHumanoidBox(float x0, float y0, float z0, int w, int h, int d, float g) 128{ 129 cubes.push_back(new Cube(this, xTexOffs, yTexOffs, x0, y0, z0, w, h, d, g, 63, true)); 130} 131 132ModelPart *ModelPart::addBoxWithMask(float x0, float y0, float z0, int w, int h, int d, int faceMask) 133{ 134 cubes.push_back(new Cube(this, xTexOffs, yTexOffs, x0, y0, z0, w, h, d, 0, faceMask)); 135 return this; 136} 137 138void ModelPart::addBox(float x0, float y0, float z0, int w, int h, int d, float g) 139{ 140 cubes.push_back(new Cube(this, xTexOffs, yTexOffs, x0, y0, z0, w, h, d, g)); 141} 142 143 144void ModelPart::addTexBox(float x0, float y0, float z0, int w, int h, int d, int tex) 145{ 146 cubes.push_back(new Cube(this, xTexOffs, yTexOffs, x0, y0, z0, w, h, d, (float)tex)); 147} 148 149void ModelPart::setPos(float x, float y, float z) 150{ 151 this->x = x; 152 this->y = y; 153 this->z = z; 154} 155 156void ModelPart::render(float scale, bool usecompiled, bool bHideParentBodyPart) 157{ 158 if (neverRender) return; 159 if (!visible) return; 160 if (!compiled) compile(scale); 161 162 glTranslatef(translateX, translateY, translateZ); 163 164 if (xRot != 0 || yRot != 0 || zRot != 0) 165 { 166 glPushMatrix(); 167 glTranslatef(x * scale, y * scale, z * scale); 168 if (zRot != 0) glRotatef(zRot * RAD, 0, 0, 1); 169 if (yRot != 0) glRotatef(yRot * RAD, 0, 1, 0); 170 if (xRot != 0) glRotatef(xRot * RAD, 1, 0, 0); 171 172 if(!bHideParentBodyPart) 173 { 174 if( usecompiled ) 175 { 176 glCallList(list); 177 } 178 else 179 { 180 Tesselator *t = Tesselator::getInstance(); 181 for (unsigned int i = 0; i < cubes.size(); i++) 182 { 183 cubes[i]->render(t, scale); 184 } 185 } 186 } 187 //if (children != NULL) 188 { 189 for (unsigned int i = 0; i < children.size(); i++) 190 { 191 children.at(i)->render(scale,usecompiled); 192 } 193 } 194 195 glPopMatrix(); 196 } 197 else if (x != 0 || y != 0 || z != 0) 198 { 199 glTranslatef(x * scale, y * scale, z * scale); 200 if(!bHideParentBodyPart) 201 { 202 if( usecompiled ) 203 { 204 glCallList(list); 205 } 206 else 207 { 208 Tesselator *t = Tesselator::getInstance(); 209 for (unsigned int i = 0; i < cubes.size(); i++) 210 { 211 cubes[i]->render(t, scale); 212 } 213 } 214 } 215 //if (children != NULL) 216 { 217 for (unsigned int i = 0; i < children.size(); i++) 218 { 219 children.at(i)->render(scale,usecompiled); 220 } 221 } 222 glTranslatef(-x * scale, -y * scale, -z * scale); 223 } 224 else 225 { 226 if(!bHideParentBodyPart) 227 { 228 if( usecompiled ) 229 { 230 glCallList(list); 231 } 232 else 233 { 234 Tesselator *t = Tesselator::getInstance(); 235 for (unsigned int i = 0; i < cubes.size(); i++) 236 { 237 cubes[i]->render(t, scale); 238 } 239 } 240 } 241 //if (children != NULL) 242 { 243 for (unsigned int i = 0; i < children.size(); i++) 244 { 245 children.at(i)->render(scale,usecompiled); 246 } 247 } 248 } 249 250 glTranslatef(-translateX, -translateY, -translateZ); 251} 252 253void ModelPart::renderRollable(float scale, bool usecompiled) 254{ 255 if (neverRender) return; 256 if (!visible) return; 257 if (!compiled) compile(scale); 258 259 glPushMatrix(); 260 glTranslatef(x * scale, y * scale, z * scale); 261 if (yRot != 0) glRotatef(yRot * RAD, 0, 1, 0); 262 if (xRot != 0) glRotatef(xRot * RAD, 1, 0, 0); 263 if (zRot != 0) glRotatef(zRot * RAD, 0, 0, 1); 264 glCallList(list); 265 glPopMatrix(); 266 267} 268 269void ModelPart::translateTo(float scale) 270{ 271 if (neverRender) return; 272 if (!visible) return; 273 if (!compiled) compile(scale); 274 275 if (xRot != 0 || yRot != 0 || zRot != 0) 276 { 277 glTranslatef(x * scale, y * scale, z * scale); 278 if (zRot != 0) glRotatef(zRot * RAD, 0, 0, 1); 279 if (yRot != 0) glRotatef(yRot * RAD, 0, 1, 0); 280 if (xRot != 0) glRotatef(xRot * RAD, 1, 0, 0); 281 } 282 else if (x != 0 || y != 0 || z != 0) 283 { 284 glTranslatef(x * scale, y * scale, z * scale); 285 } 286 else 287 { 288 } 289} 290 291void ModelPart::compile(float scale) 292{ 293 list = MemoryTracker::genLists(1); 294 295 glNewList(list, GL_COMPILE); 296 // Set a few render states that aren't configured by default 297 glEnable(GL_DEPTH_TEST); 298 glDepthFunc(GL_LEQUAL); 299 glDepthMask(true); 300 Tesselator *t = Tesselator::getInstance(); 301 302 for (unsigned int i = 0; i < cubes.size(); i++) 303 { 304 cubes.at(i)->render(t, scale); 305 } 306 307 glEndList(); 308 309 compiled = true; 310} 311 312ModelPart *ModelPart::setTexSize(int xs, int ys) 313{ 314 this->xTexSize = (float)xs; 315 this->yTexSize = (float)ys; 316 return this; 317} 318 319void ModelPart::mimic(ModelPart *o) 320{ 321 x = o->x; 322 y = o->y; 323 z = o->z; 324 xRot = o->xRot; 325 yRot = o->yRot; 326 zRot = o->zRot; 327}