void ShipType::Init() { static bool isInitted = false; if (isInitted) return; isInitted = true; std::vector<LmrModel*> ship_models; LmrGetModelsWithTag("ship", ship_models); lua_State *L = LmrGetLuaState(); int num = 0; for (std::vector<LmrModel*>::iterator i = ship_models.begin(); i != ship_models.end(); ++i) { LmrModel *model = *i; model->PushAttributeToLuaStack("ship_defs"); if (lua_isnil(L, -1)) { Error("Model %s is tagged as ship but has no ship_defs.", model->GetName()); } else if (lua_istable(L, -1)) { // multiple ship-defs for 1 model for (unsigned int i=0; i<lua_objlen(L,-1); i++) { lua_pushinteger(L, i+1); lua_gettable(L, -2); define_ship(L, model->GetName()); num++; lua_pop(L, 1); } } else { Error("Model %s: ships_def is malformed", model->GetName()); } lua_pop(L, 1); } printf("%d ship types.\n", num); }
static void _define_ships(const char *tag, ShipType::Tag stag, std::vector<ShipType::Type> &list) { std::vector<LmrModel*> ship_models; LmrGetModelsWithTag(tag, ship_models); lua_State *L = LmrGetLuaState(); int num = 0; for (std::vector<LmrModel*>::iterator i = ship_models.begin(); i != ship_models.end(); ++i) { LmrModel *model = *i; model->PushAttributeToLuaStack("ship_defs"); if (lua_isnil(L, -1)) { Error("Model %s is tagged as ship but has no ship_defs.", model->GetName()); } else if (lua_istable(L, -1)) { // multiple ship-defs for 1 model for (unsigned int j=0; j<lua_objlen(L,-1); j++) { lua_pushinteger(L, j+1); lua_gettable(L, -2); _define_ship(L, model->GetName(), list, stag); num++; lua_pop(L, 1); } } else { Error("Model %s: ships_def is malformed", model->GetName()); } lua_pop(L, 1); } printf("ShipType: %d ships with tag '%s'\n", num, tag); }
void Viewer::MainLoop() { Uint32 lastTurd = SDL_GetTicks(); Uint32 t = SDL_GetTicks(); int numFrames = 0; Uint32 lastFpsReadout = SDL_GetTicks(); g_campos = vector3f(0.0f, 0.0f, m_cmesh->GetBoundingRadius()); g_camorient = matrix4x4f::Identity(); matrix4x4f modelRot = matrix4x4f::Identity(); printf("Geom tree build in %dms\n", SDL_GetTicks() - t); Render::State::SetZnearZfar(1.0f, 10000.0f); for (;;) { PollEvents(); Render::PrepareFrame(); if (g_keyState[SDLK_LSHIFT] || g_keyState[SDLK_RSHIFT]) { if (g_keyState[SDLK_UP]) g_camorient = g_camorient * matrix4x4f::RotateXMatrix(g_frameTime); if (g_keyState[SDLK_DOWN]) g_camorient = g_camorient * matrix4x4f::RotateXMatrix(-g_frameTime); if (g_keyState[SDLK_LEFT]) g_camorient = g_camorient * matrix4x4f::RotateYMatrix(-g_frameTime); if (g_keyState[SDLK_RIGHT]) g_camorient = g_camorient * matrix4x4f::RotateYMatrix(g_frameTime); if (g_mouseButton[3]) { float rx = 0.01f*g_mouseMotion[1]; float ry = 0.01f*g_mouseMotion[0]; g_camorient = g_camorient * matrix4x4f::RotateXMatrix(rx); g_camorient = g_camorient * matrix4x4f::RotateYMatrix(ry); if (g_mouseButton[1]) { g_campos = g_campos - g_camorient * vector3f(0.0f,0.0f,1.0f) * 0.01 * m_model->GetDrawClipRadius(); } } } else { if (g_keyState[SDLK_UP]) modelRot = modelRot * matrix4x4f::RotateXMatrix(g_frameTime); if (g_keyState[SDLK_DOWN]) modelRot = modelRot * matrix4x4f::RotateXMatrix(-g_frameTime); if (g_keyState[SDLK_LEFT]) modelRot = modelRot * matrix4x4f::RotateYMatrix(-g_frameTime); if (g_keyState[SDLK_RIGHT]) modelRot = modelRot * matrix4x4f::RotateYMatrix(g_frameTime); if (g_mouseButton[3]) { float rx = 0.01f*g_mouseMotion[1]; float ry = 0.01f*g_mouseMotion[0]; modelRot = modelRot * matrix4x4f::RotateXMatrix(rx); modelRot = modelRot * matrix4x4f::RotateYMatrix(ry); } } if (g_keyState[SDLK_EQUALS]) g_campos = g_campos - g_camorient * vector3f(0.0f,0.0f,1.0f); if (g_keyState[SDLK_MINUS]) g_campos = g_campos + g_camorient * vector3f(0.0f,0.0f,1.0f); if (g_keyState[SDLK_PAGEUP]) g_campos = g_campos - g_camorient * vector3f(0.0f,0.0f,1.0f); if (g_keyState[SDLK_PAGEDOWN]) g_campos = g_campos + g_camorient * vector3f(0.0f,0.0f,1.0f); // geom->MoveTo(modelRot, vector3d(0.0,0.0,0.0)); glMatrixMode(GL_PROJECTION); glLoadIdentity(); float fracH = g_height / (float)g_width; glFrustum(-1, 1, -fracH, fracH, 1.0f, 10000.0f); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); glClearColor(0,0,0,0); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); SetSbreParams(); int beforeDrawTriStats = LmrModelGetStatsTris(); if (g_renderType == 0) { glPushAttrib(GL_ALL_ATTRIB_BITS); matrix4x4f m = g_camorient.InverseOf() * matrix4x4f::Translation(-g_campos) * modelRot.InverseOf(); if (g_doBenchmark) { for (int i=0; i<1000; i++) m_model->Render(m, ¶ms); } else { m_model->Render(m, ¶ms); } glPopAttrib(); } else if (g_renderType == 1) { glPushMatrix(); matrix4x4f m = g_camorient.InverseOf() * matrix4x4f::Translation(-g_campos) * modelRot.InverseOf(); glMultMatrixf(&m[0]); render_coll_mesh(m_cmesh); glPopMatrix(); } else { matrix4x4f tran = modelRot * g_camorient;//.InverseOf(); vector3d forward = tran * vector3d(0.0,0.0,-1.0); vector3d up = tran * vector3d(0.0,1.0,0.0); raytraceCollMesh(modelRot * g_campos, up, forward, m_space); } Render::State::UseProgram(0); Render::UnbindAllBuffers(); { char buf[128]; Aabb aabb = m_cmesh->GetAabb(); snprintf(buf, sizeof(buf), "%d triangles, collision mesh size: %.1fx%.1fx%.1f (radius %.1f)", (g_renderType == 0 ? LmrModelGetStatsTris() - beforeDrawTriStats : m_cmesh->m_numTris), aabb.max.x-aabb.min.x, aabb.max.y-aabb.min.y, aabb.max.z-aabb.min.z, aabb.GetBoundingRadius()); m_trisReadout->SetText(buf); } Render::PostProcess(); Gui::Draw(); glError(); Render::SwapBuffers(); numFrames++; g_frameTime = (SDL_GetTicks() - lastTurd) * 0.001f; lastTurd = SDL_GetTicks(); if (SDL_GetTicks() - lastFpsReadout > 1000) { int numTris = LmrModelGetStatsTris(); LmrModelClearStatsTris(); printf("%d fps, %.3f Million tris/sec\n", numFrames, numTris/1000000.0f); numFrames = 0; lastFpsReadout = SDL_GetTicks(); } //space->Collide(onCollision); } }