the game where you go into mines and start crafting! but for consoles (forked directly from smartcmd's github)
at main 410 lines 12 kB view raw
1#include "stdafx.h" 2#include "EntityRenderer.h" 3#include "EntityRenderDispatcher.h" 4#include "HumanoidModel.h" 5#include "LocalPlayer.h" 6#include "Options.h" 7#include "TextureAtlas.h" 8#include "..\Minecraft.World\net.minecraft.world.level.tile.h" 9#include "..\Minecraft.World\net.minecraft.world.h" 10#include "..\Minecraft.World\net.minecraft.world.entity.h" 11#include "..\Minecraft.World\Level.h" 12#include "..\Minecraft.World\AABB.h" 13#include "..\Minecraft.World\Mth.h" 14#include "..\Minecraft.World\net.minecraft.world.entity.animal.h" 15 16ResourceLocation EntityRenderer::SHADOW_LOCATION = ResourceLocation(TN__CLAMP__MISC_SHADOW); 17 18// 4J - added 19EntityRenderer::EntityRenderer() 20{ 21 model = NULL; 22 tileRenderer = new TileRenderer(); 23 shadowRadius = 0; 24 shadowStrength = 1.0f; 25} 26 27EntityRenderer::~EntityRenderer() 28{ 29 delete tileRenderer; 30} 31 32void EntityRenderer::bindTexture(shared_ptr<Entity> entity) 33{ 34 bindTexture(getTextureLocation(entity)); 35} 36 37void EntityRenderer::bindTexture(ResourceLocation *location) 38{ 39 entityRenderDispatcher->textures->bindTexture(location); 40} 41 42bool EntityRenderer::bindTexture(const wstring& urlTexture, int backupTexture) 43{ 44 Textures *t = entityRenderDispatcher->textures; 45 46 // 4J-PB - no http textures on the xbox, mem textures instead 47 48 //int id = t->loadHttpTexture(urlTexture, backupTexture); 49 int id = t->loadMemTexture(urlTexture, backupTexture); 50 51 if (id >= 0) 52 { 53 glBindTexture(GL_TEXTURE_2D, id); 54 t->clearLastBoundId(); 55 return true; 56 } 57 else 58 { 59 return false; 60 } 61} 62 63bool EntityRenderer::bindTexture(const wstring& urlTexture, const wstring &backupTexture) 64{ 65 Textures *t = entityRenderDispatcher->textures; 66 67 // 4J-PB - no http textures on the xbox, mem textures instead 68 69 //int id = t->loadHttpTexture(urlTexture, backupTexture); 70 int id = t->loadMemTexture(urlTexture, backupTexture); 71 72 if (id >= 0) 73 { 74 glBindTexture(GL_TEXTURE_2D, id); 75 t->clearLastBoundId(); 76 return true; 77 } 78 else 79 { 80 return false; 81 } 82} 83 84void EntityRenderer::renderFlame(shared_ptr<Entity> e, double x, double y, double z, float a) 85{ 86 glDisable(GL_LIGHTING); 87 88 Icon *fire1 = Tile::fire->getTextureLayer(0); 89 Icon *fire2 = Tile::fire->getTextureLayer(1); 90 91 glPushMatrix(); 92 glTranslatef((float) x, (float) y, (float) z); 93 94 float s = e->bbWidth * 1.4f; 95 glScalef(s, s, s); 96 MemSect(31); 97 bindTexture(&TextureAtlas::LOCATION_BLOCKS); 98 MemSect(0); 99 Tesselator *t = Tesselator::getInstance(); 100 101 float r = 0.5f; 102 float xo = 0.0f; 103 104 float h = e->bbHeight / s; 105 float yo = (float) (e->y - e->bb->y0); 106 107 glRotatef(-entityRenderDispatcher->playerRotY, 0, 1, 0); 108 109 glTranslatef(0, 0, -0.3f + ((int) h) * 0.02f); 110 glColor4f(1, 1, 1, 1); 111 float zo = 0; 112 int ss = 0; 113 t->begin(); 114 while (h > 0) 115 { 116 Icon *tex = NULL; 117 if (ss % 2 == 0) 118 { 119 tex = fire1; 120 } 121 else 122 { 123 tex = fire2; 124 } 125 126 float u0 = tex->getU0(); 127 float v0 = tex->getV0(); 128 float u1 = tex->getU1(); 129 float v1 = tex->getV1(); 130 131 if (ss / 2 % 2 == 0) 132 { 133 float tmp = u1; 134 u1 = u0; 135 u0 = tmp; 136 } 137 t->vertexUV((float)(r - xo), (float)( 0 - yo), (float)( zo), (float)( u1), (float)( v1)); 138 t->vertexUV((float)(-r - xo), (float)( 0 - yo), (float)( zo), (float)( u0), (float)( v1)); 139 t->vertexUV((float)(-r - xo), (float)( 1.4f - yo), (float)( zo), (float)( u0), (float)( v0)); 140 t->vertexUV((float)(r - xo), (float)( 1.4f - yo), (float)( zo), (float)( u1), (float)( v0)); 141 h -= 0.45f; 142 yo -= 0.45f; 143 r *= 0.9f; 144 zo += 0.03f; 145 ss++; 146 } 147 t->end(); 148 glPopMatrix(); 149 glEnable(GL_LIGHTING); 150 151} 152void EntityRenderer::renderShadow(shared_ptr<Entity> e, double x, double y, double z, float pow, float a) 153{ 154 glDisable(GL_LIGHTING); 155 glEnable(GL_BLEND); 156 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); 157 158 MemSect(31); 159 entityRenderDispatcher->textures->bindTexture(&SHADOW_LOCATION); 160 MemSect(0); 161 162 Level *level = getLevel(); 163 164 glDepthMask(false); 165 float r = shadowRadius; 166 float fYLocalPlayerShadowOffset=0.0f; 167 168 if (e->instanceof(eTYPE_MOB)) 169 { 170 shared_ptr<Mob> mob = dynamic_pointer_cast<Mob>(e); 171 r *= mob->getSizeScale(); 172 173 174 if (mob->instanceof(eTYPE_ANIMAL)) 175 { 176 if (dynamic_pointer_cast<Animal>(mob)->isBaby()) 177 { 178 r *= 0.5f; 179 } 180 } 181 } 182 183 double ex = e->xOld + (e->x - e->xOld) * a; 184 double ey = e->yOld + (e->y - e->yOld) * a + e->getShadowHeightOffs(); 185 186 // 4J-PB - local players seem to have a position at their head, and remote players have a foot position. 187 // get the shadow to render by changing the check here depending on the player type 188 if(e->instanceof(eTYPE_LOCALPLAYER)) 189 { 190 ey-=1.62; 191 fYLocalPlayerShadowOffset=-1.62f; 192 } 193 double ez = e->zOld + (e->z - e->zOld) * a; 194 195 int x0 = Mth::floor(ex - r); 196 int x1 = Mth::floor(ex + r); 197 int y0 = Mth::floor(ey - r); 198 int y1 = Mth::floor(ey); 199 int z0 = Mth::floor(ez - r); 200 int z1 = Mth::floor(ez + r); 201 202 double xo = x - ex; 203 double yo = y - ey; 204 double zo = z - ez; 205 206 Tesselator *tt = Tesselator::getInstance(); 207 tt->begin(); 208 for (int xt = x0; xt <= x1; xt++) 209 for (int yt = y0; yt <= y1; yt++) 210 for (int zt = z0; zt <= z1; zt++) 211 { 212 int t = level->getTile(xt, yt - 1, zt); 213 if (t > 0 && level->getRawBrightness(xt, yt, zt) > 3) 214 { 215 renderTileShadow(Tile::tiles[t], x, y + e->getShadowHeightOffs() + fYLocalPlayerShadowOffset, z, xt, yt , zt, pow, r, xo, yo + e->getShadowHeightOffs() + fYLocalPlayerShadowOffset, zo); 216 } 217 } 218 tt->end(); 219 220 glColor4f(1, 1, 1, 1); 221 glDisable(GL_BLEND); 222 glDepthMask(true); 223 glEnable(GL_LIGHTING); 224 225} 226 227Level *EntityRenderer::getLevel() 228{ 229 return entityRenderDispatcher->level; 230} 231 232void EntityRenderer::renderTileShadow(Tile *tt, double x, double y, double z, int xt, int yt, int zt, float pow, float r, double xo, double yo, double zo) 233{ 234 Tesselator *t = Tesselator::getInstance(); 235 if (!tt->isCubeShaped()) return; 236 237 double a = ((pow - (y - (yt + yo)) / 2) * 0.5f) * getLevel()->getBrightness(xt, yt, zt); 238 if (a < 0) return; 239 if (a > 1) a = 1; 240 241 t->color(1.0f, 1.0f, 1.0f, (float) a); 242 // glColor4f(1, 1, 1, (float) a); 243 244 double x0 = xt + tt->getShapeX0() + xo; 245 double x1 = xt + tt->getShapeX1() + xo; 246 double y0 = yt + tt->getShapeY0() + yo + 1.0 / 64.0f; 247 double z0 = zt + tt->getShapeZ0() + zo; 248 double z1 = zt + tt->getShapeZ1() + zo; 249 250 float u0 = (float) ((x - (x0)) / 2 / r + 0.5f); 251 float u1 = (float) ((x - (x1)) / 2 / r + 0.5f); 252 float v0 = (float) ((z - (z0)) / 2 / r + 0.5f); 253 float v1 = (float) ((z - (z1)) / 2 / r + 0.5f); 254 255 // u0 = 0; 256 // v0 = 0; 257 // u1 = 1; 258 // v1 = 1; 259 260 t->vertexUV((float)(x0), (float)( y0), (float)( z0), (float)( u0), (float)( v0)); 261 t->vertexUV((float)(x0), (float)( y0), (float)( z1), (float)( u0), (float)( v1)); 262 t->vertexUV((float)(x1), (float)( y0), (float)( z1), (float)( u1), (float)( v1)); 263 t->vertexUV((float)(x1), (float)( y0), (float)( z0), (float)( u1), (float)( v0)); 264} 265 266void EntityRenderer::render(AABB *bb, double xo, double yo, double zo) 267{ 268 glDisable(GL_TEXTURE_2D); 269 Tesselator *t = Tesselator::getInstance(); 270 glColor4f(1, 1, 1, 1); 271 t->begin(); 272 t->offset((float)xo, (float)yo, (float)zo); 273 t->normal(0, 0, -1); 274 t->vertex((float)(bb->x0), (float)( bb->y1), (float)( bb->z0)); 275 t->vertex((float)(bb->x1), (float)( bb->y1), (float)( bb->z0)); 276 t->vertex((float)(bb->x1), (float)( bb->y0), (float)( bb->z0)); 277 t->vertex((float)(bb->x0), (float)( bb->y0), (float)( bb->z0)); 278 279 t->normal(0, 0, 1); 280 t->vertex((float)(bb->x0), (float)( bb->y0), (float)( bb->z1)); 281 t->vertex((float)(bb->x1), (float)( bb->y0), (float)( bb->z1)); 282 t->vertex((float)(bb->x1), (float)( bb->y1), (float)( bb->z1)); 283 t->vertex((float)(bb->x0), (float)( bb->y1), (float)( bb->z1)); 284 285 t->normal(0, -1, 0); 286 t->vertex((float)(bb->x0), (float)( bb->y0), (float)( bb->z0)); 287 t->vertex((float)(bb->x1), (float)( bb->y0), (float)( bb->z0)); 288 t->vertex((float)(bb->x1), (float)( bb->y0), (float)( bb->z1)); 289 t->vertex((float)(bb->x0), (float)( bb->y0), (float)( bb->z1)); 290 291 t->normal(0, 1, 0); 292 t->vertex((float)(bb->x0), (float)( bb->y1), (float)( bb->z1)); 293 t->vertex((float)(bb->x1), (float)( bb->y1), (float)( bb->z1)); 294 t->vertex((float)(bb->x1), (float)( bb->y1), (float)( bb->z0)); 295 t->vertex((float)(bb->x0), (float)( bb->y1), (float)( bb->z0)); 296 297 t->normal(-1, 0, 0); 298 t->vertex((float)(bb->x0), (float)( bb->y0), (float)( bb->z1)); 299 t->vertex((float)(bb->x0), (float)( bb->y1), (float)( bb->z1)); 300 t->vertex((float)(bb->x0), (float)( bb->y1), (float)( bb->z0)); 301 t->vertex((float)(bb->x0), (float)( bb->y0), (float)( bb->z0)); 302 303 t->normal(1, 0, 0); 304 t->vertex((float)(bb->x1), (float)( bb->y0), (float)( bb->z0)); 305 t->vertex((float)(bb->x1), (float)( bb->y1), (float)( bb->z0)); 306 t->vertex((float)(bb->x1), (float)( bb->y1), (float)( bb->z1)); 307 t->vertex((float)(bb->x1), (float)( bb->y0), (float)( bb->z1)); 308 t->offset(0, 0, 0); 309 t->end(); 310 glEnable(GL_TEXTURE_2D); 311 // model.render(0, 1) 312} 313 314void EntityRenderer::renderFlat(AABB *bb) 315{ 316 Tesselator *t = Tesselator::getInstance(); 317 t->begin(); 318 t->vertex((float)(bb->x0), (float)( bb->y1), (float)( bb->z0)); 319 t->vertex((float)(bb->x1), (float)( bb->y1), (float)( bb->z0)); 320 t->vertex((float)(bb->x1), (float)( bb->y0), (float)( bb->z0)); 321 t->vertex((float)(bb->x0), (float)( bb->y0), (float)( bb->z0)); 322 t->vertex((float)(bb->x0), (float)( bb->y0), (float)( bb->z1)); 323 t->vertex((float)(bb->x1), (float)( bb->y0), (float)( bb->z1)); 324 t->vertex((float)(bb->x1), (float)( bb->y1), (float)( bb->z1)); 325 t->vertex((float)(bb->x0), (float)( bb->y1), (float)( bb->z1)); 326 t->vertex((float)(bb->x0), (float)( bb->y0), (float)( bb->z0)); 327 t->vertex((float)(bb->x1), (float)( bb->y0), (float)( bb->z0)); 328 t->vertex((float)(bb->x1), (float)( bb->y0), (float)( bb->z1)); 329 t->vertex((float)(bb->x0), (float)( bb->y0), (float)( bb->z1)); 330 t->vertex((float)(bb->x0), (float)( bb->y1), (float)( bb->z1)); 331 t->vertex((float)(bb->x1), (float)( bb->y1), (float)( bb->z1)); 332 t->vertex((float)(bb->x1), (float)( bb->y1), (float)( bb->z0)); 333 t->vertex((float)(bb->x0), (float)( bb->y1), (float)( bb->z0)); 334 t->vertex((float)(bb->x0), (float)( bb->y0), (float)( bb->z1)); 335 t->vertex((float)(bb->x0), (float)( bb->y1), (float)( bb->z1)); 336 t->vertex((float)(bb->x0), (float)( bb->y1), (float)( bb->z0)); 337 t->vertex((float)(bb->x0), (float)( bb->y0), (float)( bb->z0)); 338 t->vertex((float)(bb->x1), (float)( bb->y0), (float)( bb->z0)); 339 t->vertex((float)(bb->x1), (float)( bb->y1), (float)( bb->z0)); 340 t->vertex((float)(bb->x1), (float)( bb->y1), (float)( bb->z1)); 341 t->vertex((float)(bb->x1), (float)( bb->y0), (float)( bb->z1)); 342 t->end(); 343} 344 345void EntityRenderer::renderFlat(float x0, float y0, float z0, float x1, float y1, float z1) 346{ 347 Tesselator *t = Tesselator::getInstance(); 348 t->begin(); 349 t->vertex(x0, y1, z0); 350 t->vertex(x1, y1, z0); 351 t->vertex(x1, y0, z0); 352 t->vertex(x0, y0, z0); 353 t->vertex(x0, y0, z1); 354 t->vertex(x1, y0, z1); 355 t->vertex(x1, y1, z1); 356 t->vertex(x0, y1, z1); 357 t->vertex(x0, y0, z0); 358 t->vertex(x1, y0, z0); 359 t->vertex(x1, y0, z1); 360 t->vertex(x0, y0, z1); 361 t->vertex(x0, y1, z1); 362 t->vertex(x1, y1, z1); 363 t->vertex(x1, y1, z0); 364 t->vertex(x0, y1, z0); 365 t->vertex(x0, y0, z1); 366 t->vertex(x0, y1, z1); 367 t->vertex(x0, y1, z0); 368 t->vertex(x0, y0, z0); 369 t->vertex(x1, y0, z0); 370 t->vertex(x1, y1, z0); 371 t->vertex(x1, y1, z1); 372 t->vertex(x1, y0, z1); 373 t->end(); 374} 375 376void EntityRenderer::init(EntityRenderDispatcher *entityRenderDispatcher) 377{ 378 this->entityRenderDispatcher = entityRenderDispatcher; 379} 380 381void EntityRenderer::postRender(shared_ptr<Entity> entity, double x, double y, double z, float rot, float a, bool bRenderPlayerShadow) 382{ 383 if( !entityRenderDispatcher->isGuiRender ) // 4J - added, don't render shadow in gui as it uses its own blending, and we have globally enabled blending for interface opacity 384 { 385 if (bRenderPlayerShadow && entityRenderDispatcher->options->fancyGraphics && shadowRadius > 0 && !entity->isInvisible()) 386 { 387 double dist = entityRenderDispatcher->distanceToSqr(entity->x, entity->y, entity->z); 388 float pow = (float) ((1 - dist / (16.0f * 16.0f)) * shadowStrength); 389 if (pow > 0) 390 { 391 renderShadow(entity, x, y, z, pow, a); 392 } 393 } 394 } 395 if (entity->isOnFire()) renderFlame(entity, x, y, z, a); 396} 397 398Font *EntityRenderer::getFont() 399{ 400 return entityRenderDispatcher->getFont(); 401} 402 403void EntityRenderer::registerTerrainTextures(IconRegister *iconRegister) 404{ 405} 406 407ResourceLocation *EntityRenderer::getTextureLocation(shared_ptr<Entity> mob) 408{ 409 return NULL; 410}