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 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515
|
/* This file is part of the Spring engine (GPL v2 or later), see LICENSE.html */
#include "LuaObjectRendering.h"
#include "LuaMaterial.h"
#include "LuaInclude.h"
#include "LuaHandle.h"
#include "LuaHashString.h"
#include "LuaUtils.h"
#include "Rendering/LuaObjectDrawer.h"
#include "Rendering/Models/3DModel.h"
// see ParseUnitTexture
// #include "Rendering/Textures/3DOTextureHandler.h"
// #include "Rendering/Textures/S3OTextureHandler.h"
#include "Sim/Units/UnitHandler.h"
#include "Sim/Features/FeatureHandler.h"
#include "System/Log/ILog.h"
#include "System/StringUtil.h"
static const spring::unordered_map<std::string, LuaMatType> matNameMap = {
{"alpha", LUAMAT_ALPHA},
{"opaque", LUAMAT_OPAQUE},
{"alpha_reflect", LUAMAT_ALPHA_REFLECT},
{"opaque_reflect", LUAMAT_OPAQUE_REFLECT},
{"shadow", LUAMAT_SHADOW},
};
static int material_index(lua_State* L)
{
LuaMatRef** matRef = (LuaMatRef**) luaL_checkudata(L, 1, "MatRef");
const string key = luaL_checkstring(L, 2);
const LuaMatBin* bin = (*matRef)->GetBin();
if (bin == nullptr)
return 0;
//const LuaMaterial& mat = *bin;
//if (key == "type") {
//}
return 0;
}
static int material_newindex(lua_State* L)
{
luaL_checkudata(L, 1, "MatRef");
return 0;
}
static int material_gc(lua_State* L)
{
LuaMatRef** matRef = (LuaMatRef**) luaL_checkudata(L, 1, "MatRef");
delete *matRef;
return 0;
}
/******************************************************************************/
/******************************************************************************/
//
// Parsing helpers
//
static inline CSolidObject* ParseSolidObject(lua_State* L, const char* caller, int index, int objType)
{
if (!lua_isnumber(L, index)) {
luaL_error(L, "[%s] objectID (arg #%d) not a number\n", caller, index);
return nullptr;
}
switch (objType) {
case LUAOBJ_UNIT : { return ( unitHandler->GetUnit (lua_toint(L, index))); } break;
case LUAOBJ_FEATURE: { return (featureHandler->GetFeature(lua_toint(L, index))); } break;
default : { assert(false); } break;
}
return nullptr;
}
/*
static inline CUnit* ParseUnit(lua_State* L, const char* caller, int index)
{
return (static_cast<CUnit*>(ParseSolidObject(L, caller, index, LUAOBJ_UNIT)));
}
static inline CFeature* ParseFeature(lua_State* L, const char* caller, int index)
{
return (static_cast<CFeature*>(ParseSolidObject(L, caller, index, LUAOBJ_FEATURE)));
}
*/
/******************************************************************************/
/******************************************************************************/
std::vector<LuaObjType> LuaObjectRenderingImpl::objectTypeStack;
void LuaObjectRenderingImpl::CreateMatRefMetatable(lua_State* L)
{
luaL_newmetatable(L, "MatRef");
HSTR_PUSH_CFUNC(L, "__gc", material_gc);
HSTR_PUSH_CFUNC(L, "__index", material_index);
HSTR_PUSH_CFUNC(L, "__newindex", material_newindex);
lua_pop(L, 1);
}
void LuaObjectRenderingImpl::PushFunction(lua_State* L, int (*fnPntr)(lua_State*), const char* fnName)
{
lua_pushstring(L, fnName);
lua_pushcfunction(L, fnPntr);
lua_rawset(L, -3);
}
int LuaObjectRenderingImpl::GetLODCount(lua_State* L)
{
const CSolidObject* obj = ParseSolidObject(L, __FUNCTION__, 1, GetObjectType());
if (obj == nullptr)
return 0;
const LuaObjectMaterialData* lmd = obj->GetLuaMaterialData();
lua_pushnumber(L, lmd->GetLODCount());
lua_pushnumber(L, lmd->GetCurrentLOD());
return 2;
}
int LuaObjectRenderingImpl::SetLODCount(lua_State* L)
{
// args=<objID, lodCount>
const unsigned int objType = GetObjectType();
const unsigned int lodCount = std::min(1024, luaL_checkint(L, 2));
CSolidObject* obj = ParseSolidObject(L, __FUNCTION__, 1, objType);
if (obj == nullptr)
return 0;
LuaObjectDrawer::SetObjectLOD(obj, LuaObjType(objType), lodCount);
return 0;
}
static int SetLODLengthCommon(lua_State* L, CSolidObject* obj, float scale)
{
if (obj == nullptr)
return 0;
LuaObjectMaterialData* lmd = obj->GetLuaMaterialData();
// actual Length-Per-Pixel
lmd->SetLODLength(luaL_checknumber(L, 2) - 1, luaL_checkfloat(L, 3) * scale);
return 0;
}
int LuaObjectRenderingImpl::SetLODLength(lua_State* L)
{
// args=<objID, lodLevel, lodLength>
return (SetLODLengthCommon(L, ParseSolidObject(L, __FUNCTION__, 1, GetObjectType()), 1.0f));
}
int LuaObjectRenderingImpl::SetLODDistance(lua_State* L)
{
// args=<objID, lodLevel, lodLength>
//
// length adjusted for 45 degree FOV with a 1024x768 screen; the magic
// constant is 2.0f * math::tanf((45.0f * 0.5f) * (PI / 180.0f)) / 768.0f)
return (SetLODLengthCommon(L, ParseSolidObject(L, __FUNCTION__, 1, GetObjectType()), 0.0010786811520132682f));
}
/******************************************************************************/
int LuaObjectRenderingImpl::SetPieceList(lua_State* L)
{
CSolidObject* obj = ParseSolidObject(L, __FUNCTION__, 1, GetObjectType());
if (obj == nullptr)
return 0;
const LuaObjectMaterialData* lmd = obj->GetLuaMaterialData();
LocalModelPiece* lmp = ParseObjectLocalModelPiece(L, obj, 3);
if (lmp == nullptr)
return 0;
const unsigned int lod = luaL_checknumber(L, 2) - 1;
if (lod >= lmd->GetLODCount())
return 0;
// (re)set the default if no fourth argument
unsigned int dlist = lmp->dispListID;
if (lua_isnumber(L, 4)) {
CLuaDisplayLists& displayLists = CLuaHandle::GetActiveDisplayLists(L);
dlist = displayLists.GetDList(luaL_checknumber(L, 4));
}
lmp->lodDispLists[lod] = dlist;
return 0;
}
/******************************************************************************/
/******************************************************************************/
static LuaMatType ParseMaterialType(const std::string& matName)
{
const auto it = matNameMap.find(StringToLower(matName));
if (it == matNameMap.end())
return (LuaMatType) -1;
return it->second;
}
static LuaObjectMaterial* GetObjectMaterial(CSolidObject* obj, const std::string& matName)
{
LuaMatType matType = ParseMaterialType(matName);
LuaObjectMaterialData* lmd = obj->GetLuaMaterialData();
if (matType < 0)
return nullptr;
return (lmd->GetLuaMaterial(matType));
}
/******************************************************************************/
static void ParseShader(lua_State* L, int index, LuaMatShader& shader)
{
const LuaShaders& shaders = CLuaHandle::GetActiveShaders(L);
switch (lua_type(L, index)) {
case LUA_TNUMBER: {
shader.SetTypeFromID(shaders.GetProgramName(luaL_checknumber(L, index)));
} break;
case LUA_TSTRING: {
shader.SetTypeFromKey(StringToLower(lua_tostring(L, index)));
} break;
default: {
} break;
}
}
static void ParseTexture(lua_State* L, int index, LuaMatTexture& texUnit) {
if (index < 0)
index = lua_gettop(L) + index + 1;
if (lua_isstring(L, index)) {
LuaOpenGLUtils::ParseTextureImage(L, texUnit, lua_tostring(L, index));
texUnit.enable = true;
return;
}
if (!lua_istable(L, index))
return;
const int table = (index > 0) ? index : (lua_gettop(L) + index + 1);
for (lua_pushnil(L); lua_next(L, table) != 0; lua_pop(L, 1)) {
if (!lua_israwstring(L, -2))
continue;
const string key = StringToLower(lua_tostring(L, -2));
if (key == "tex") {
LuaOpenGLUtils::ParseTextureImage(L, texUnit, lua_tostring(L, -1));
texUnit.enable = true;
continue;
}
if (key == "enable") {
texUnit.enable = lua_isboolean(L, -1) && lua_toboolean(L, -1);
continue;
}
}
}
static GLuint ParseDisplayList(lua_State* L, int index)
{
if (!lua_isnumber(L, index))
return 0;
const unsigned int ilist = (unsigned int)luaL_checknumber(L, index);
const CLuaDisplayLists& displayLists = CLuaHandle::GetActiveDisplayLists(L);
return displayLists.GetDList(ilist);
}
static LuaMatRef ParseMaterial(lua_State* L, int index, LuaMatType matType) {
if (!lua_istable(L, index))
return LuaMatRef();
LuaMaterial mat(matType);
mat.Parse(L, index, &ParseShader, &ParseTexture, &ParseDisplayList);
mat.Finalize();
return (luaMatHandler.GetRef(mat));
}
/******************************************************************************/
/******************************************************************************/
int LuaObjectRenderingImpl::GetMaterial(lua_State* L)
{
const LuaMatType matType = ParseMaterialType(luaL_checkstring(L, 1));
if (!lua_istable(L, 2))
luaL_error(L, "Incorrect arguments to GetMaterial");
LuaMatRef** matRef = (LuaMatRef**) lua_newuserdata(L, sizeof(LuaMatRef*));
luaL_getmetatable(L, "MatRef");
lua_setmetatable(L, -2);
*matRef = new LuaMatRef;
**matRef = ParseMaterial(L, 2, matType);
return 1;
}
/******************************************************************************/
/******************************************************************************/
int LuaObjectRenderingImpl::SetMaterial(lua_State* L)
{
// args=<objID, lodMatNum, matName, matRef>
CSolidObject* obj = ParseSolidObject(L, __FUNCTION__, 1, GetObjectType());
if (obj == nullptr)
return 0;
const string matName = luaL_checkstring(L, 3);
const LuaMatType matType = ParseMaterialType(matName);
LuaObjectMaterial* objMat = GetObjectMaterial(obj, matName);
if (objMat == nullptr)
return 0;
LuaObjectLODMaterial* lodMat = objMat->GetMaterial(luaL_checknumber(L, 2) - 1);
if (lodMat == nullptr)
return 0;
if (lua_isuserdata(L, 4)) {
LuaMatRef** matRef = (LuaMatRef**) luaL_checkudata(L, 4, "MatRef");
if (matRef) {
lodMat->matref = **matRef;
}
} else {
lodMat->matref = ParseMaterial(L, 4, matType);
}
return 0;
}
int LuaObjectRenderingImpl::SetMaterialLastLOD(lua_State* L)
{
// args=<objID, matName, lodMatNum>
CSolidObject* obj = ParseSolidObject(L, __FUNCTION__, 1, GetObjectType());
if (obj == nullptr)
return 0;
LuaObjectMaterial* objMat = GetObjectMaterial(obj, luaL_checkstring(L, 2));
if (objMat == nullptr)
return 0;
objMat->SetLastLOD(luaL_checknumber(L, 3) - 1);
return 0;
}
int LuaObjectRenderingImpl::SetMaterialDisplayLists(lua_State* L)
{
// args=<objID, lodLevel, matName, preListID, postListID>
CSolidObject* obj = ParseSolidObject(L, __FUNCTION__, 1, GetObjectType());
if (obj == nullptr)
return 0;
LuaObjectMaterial* objMat = GetObjectMaterial(obj, luaL_checkstring(L, 3));
if (objMat == nullptr)
return 0;
LuaObjectLODMaterial* lodMat = objMat->GetMaterial(luaL_checknumber(L, 2) - 1);
if (lodMat == nullptr)
return 0;
lodMat->preDisplayList = ParseDisplayList(L, 4);
lodMat->postDisplayList = ParseDisplayList(L, 5);
return 0;
}
/******************************************************************************/
/******************************************************************************/
static int SetObjectLuaDraw(lua_State* L, CSolidObject* obj)
{
if (obj == nullptr)
return 0;
if (!lua_isboolean(L, 2))
return 0;
obj->luaDraw = lua_toboolean(L, 2);
return 0;
}
int LuaObjectRenderingImpl::SetUnitLuaDraw(lua_State* L)
{
return (SetObjectLuaDraw(L, unitHandler->GetUnit(luaL_checkint(L, 1))));
}
int LuaObjectRenderingImpl::SetFeatureLuaDraw(lua_State* L)
{
return (SetObjectLuaDraw(L, featureHandler->GetFeature(luaL_checkint(L, 1))));
}
/******************************************************************************/
/******************************************************************************/
static void PrintObjectLOD(const CSolidObject* obj, int lod)
{
const LuaObjectMaterialData* lmd = obj->GetLuaMaterialData();
const LuaObjectMaterial* mats = lmd->GetLuaMaterials();
LOG(" LOD %i:", lod);
LOG(" LodLength = %f", lmd->GetLODLength(lod));
for (int type = 0; type < LUAMAT_TYPE_COUNT; type++) {
const LuaObjectMaterial& luaMat = mats[type];
const LuaObjectLODMaterial* lodMat = luaMat.GetMaterial(lod);
const LuaMatBin* bin = lodMat->matref.GetBin();
if (bin) {
bin->Print(" ");
}
}
}
int LuaObjectRenderingImpl::Debug(lua_State* L)
{
if (lua_gettop(L) == 0) {
// no arguments, dump all bins
luaMatHandler.PrintAllBins("");
return 0;
}
// args=<objID>
const CSolidObject* obj = ParseSolidObject(L, __FUNCTION__, 1, GetObjectType());
if (obj == nullptr)
return 0;
const LuaObjectMaterialData* lmd = obj->GetLuaMaterialData();
const LuaObjectMaterial* mats = lmd->GetLuaMaterials();
LOG_L(L_DEBUG, "%s", "");
LOG_L(L_DEBUG, "ObjectID = %i", obj->id);
LOG_L(L_DEBUG, "ObjectDefID = %i", obj->GetDef()->id);
LOG_L(L_DEBUG, "ObjectDefName = %s", obj->GetDef()->name.c_str());
LOG_L(L_DEBUG, "LodCount = %i", lmd->GetLODCount());
LOG_L(L_DEBUG, "CurrentLod = %i", lmd->GetCurrentLOD());
LOG_L(L_DEBUG, "%s", "");
const LuaObjectMaterial& alphaMat = mats[LUAMAT_ALPHA];
const LuaObjectMaterial& opaqueMat = mats[LUAMAT_OPAQUE];
const LuaObjectMaterial& alphaReflMat = mats[LUAMAT_ALPHA_REFLECT];
const LuaObjectMaterial& opaqueReflMat = mats[LUAMAT_OPAQUE_REFLECT];
const LuaObjectMaterial& shadowMat = mats[LUAMAT_SHADOW];
LOG_L(L_DEBUG, "LUAMAT_ALPHA lastLOD = %i", alphaMat.GetLastLOD());
LOG_L(L_DEBUG, "LUAMAT_OPAQUE lastLOD = %i", opaqueMat.GetLastLOD());
LOG_L(L_DEBUG, "LUAMAT_ALPHA_REFLECT lastLOD = %i", alphaReflMat.GetLastLOD());
LOG_L(L_DEBUG, "LUAMAT_OPAQUE_REFLECT lastLOD = %i", opaqueReflMat.GetLastLOD());
LOG_L(L_DEBUG, "LUAMAT_SHADOW lastLOD = %i", shadowMat.GetLastLOD());
for (unsigned lod = 0; lod < lmd->GetLODCount(); lod++) {
PrintObjectLOD(obj, lod);
}
return 0;
}
/******************************************************************************/
/******************************************************************************/
|