void BVHTest() { Common::Timer timer; Random rand = Random(1); #define NUM_INSERTIONS 100000 Box* pBoxes = (Box*)_aligned_malloc(NUM_INSERTIONS * sizeof(Box), 16); for (int i = 0; i < NUM_INSERTIONS; i++) { Vector pos = rand.GetFloat3(); pos *= 100.0f; pBoxes[i] = Box(pos - Vector(0.5f, 0.5f, 0.5), pos + Vector(0.5f, 0.5f, 0.5)); } Util::BVH bvh; timer.Start(); for (int i = 0; i < NUM_INSERTIONS; i++) { bvh.Insert(pBoxes[i], (void*)i); } double buildTime = timer.Stop(); Box testBox = Box(Vector(50, 50, 50), Vector(60, 60, 60)); //__asm int 3; }
// Start the timer when a game is booted void RerecordingStart() { g_FrameCounter = 0; ReRecTimer.Start(); // Logging //DEBUG_LOG(CONSOLE, "RerecordingStart: %i\n", g_FrameCounter); }
/* TODO: Font rendering MUST be redesigned. */ Font* GuiRendererD3D11::MakeFont(const char* pPath, int height) { Common::Timer timer; timer.Start(); FT_Face face; int texWidth = 4096; int texHeight = 4096; //create pixels array unsigned char* pTexData = (unsigned char*)malloc(texWidth * texHeight); ZeroMemory(pTexData, texWidth * texHeight); if (FT_New_Face(mFreeTypeLibrary, pPath, 0, &face) != 0) { free(pTexData); // TODO // LOG_ERROR("Failed to load font '%s'.", pPath); return 0; } //FT_Set_Pixel_Sizes(face, 2*height, 2*height); FT_Set_Char_Size(face, 0, height * 64, 96, 96); Font* pFont = new Font; /* FT_Matrix Transform; Transform.xx = (FT_Fixed)(1.0f * 0x10000L); Transform.xy = (FT_Fixed)(0.0f * 0x10000L); Transform.yx = (FT_Fixed)(0.0f * 0x10000L); Transform.yy = (FT_Fixed)(1.0f * 0x10000L); FT_Set_Transform(face, &Transform, 0); */ int Width; int Height; int OffsetX = 0; int OffsetY = 0; pFont->height = height; pFont->charCount = 65536; pFont->characters = (CharacterInfo*)malloc(sizeof(CharacterInfo) * pFont->charCount); int Index = 0; for (int ChrId = 0; ChrId < (65536); ChrId++) { // Load The Glyph For Our Character. unsigned int GlyphIndex = FT_Get_Char_Index(face, ChrId); if (GlyphIndex == 0) { pFont->characters[ChrId].exists = false; continue; } FT_Load_Glyph(face, GlyphIndex, FT_LOAD_DEFAULT); // Move The Face's Glyph Into A Glyph Object. FT_Glyph glyph; FT_Get_Glyph(face->glyph, &glyph); // Convert The Glyph To A Bitmap. FT_Glyph_To_Bitmap(&glyph, FT_RENDER_MODE_NORMAL, 0, 1); FT_BitmapGlyph bitmap_glyph = (FT_BitmapGlyph)glyph; // This Reference Will Make Accessing The Bitmap Easier. FT_Bitmap& bitmap = bitmap_glyph->bitmap; Width = bitmap.width; Height = bitmap.rows; //char won't fit to texture if (OffsetX + Width + 2 > texWidth) { OffsetX = 0; OffsetY += 2 * height; } for (int y = 0; y < Height; y++) { for (int x = 0; x < Width; x++) { unsigned char Value = bitmap.buffer[x + Width * y]; int PxOffset = (y + OffsetY) * texWidth + x + OffsetX; pTexData[PxOffset] = Value; } } pFont->characters[ChrId].exists = true; pFont->characters[ChrId].top = (short)bitmap_glyph->top; pFont->characters[ChrId].left = (short)bitmap_glyph->left; pFont->characters[ChrId].height = (short)Height; pFont->characters[ChrId].width = (short)Width; pFont->characters[ChrId].u = (short)OffsetX; pFont->characters[ChrId].v = (short)OffsetY; pFont->characters[ChrId].spacing = (short)(face->glyph->advance.x >> 6) + 1; FT_Done_Glyph(glyph); OffsetX += pFont->characters[ChrId].width + 1; Index++; } //close font FT_Done_Face(face); OffsetY += 2 * height; // Used texture height texHeight = CeilToPowerOf2(OffsetY); Common::Image fontImage; fontImage.SetData(pTexData, texWidth, texHeight, Common::ImageFormat::A_UBYTE); // pFont->texture = pRenderer->CreateTexture(&fontImage, false); pFont->texture = new RendererTextureD3D11; pFont->texture->FromImage(fontImage); pFont->texHeight = texHeight; pFont->texWidth = texWidth; free(pTexData); // TODO // LOG_SUCCESS("Font '%s', size %i loaded in %.3lf seconds.", pPath, height, timer.Stop()); return pFont; }
int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) { SetUpCurrentDirectory(); //create window CustomWindow* pWindow = new CustomWindow; pWindow->setSize(800, 600); pWindow->setTitle(L"NFEngine Demo - Initializing engine..."); pWindow->open(); //initialize engine if (EngineInit() != Result::OK) return 1; Demo_InitEditorBar(); //create scene and camera g_pScene = EngineCreateScene(); // -------------------------------- // Build scene // -------------------------------- #ifdef DEMO_SEC_CAMERA_ON InitSecondaryCamera(); #endif //set ambient & background color EnviromentDesc envDesc; envDesc.ambientLight = Vector(0.02f, 0.03f, 0.04f, 0.0f); //envDesc.m_BackgroundColor = Vector(0.04f, 0.05f, 0.07f, 0.03f); envDesc.backgroundColor = Vector(0.02f, 0.03f, 0.04f, 0.01f); g_pScene->SetEnvironment(&envDesc); CollisionShape* pFloorShape = ENGINE_GET_COLLISION_SHAPE("shape_floor"); pFloorShape->SetCallbacks(OnLoadCustomShapeResource, NULL); pFloorShape->Load(); pFloorShape->AddRef(); CollisionShape* pFrameShape = ENGINE_GET_COLLISION_SHAPE("shape_frame"); pFrameShape->SetCallbacks(OnLoadCustomShapeResource, NULL); pFrameShape->Load(); pFrameShape->AddRef(); CollisionShape* pBoxShape = ENGINE_GET_COLLISION_SHAPE("shape_box"); pBoxShape->SetCallbacks(OnLoadCustomShapeResource, NULL); pBoxShape->Load(); pBoxShape->AddRef(); CollisionShape* pBarrelShape = ENGINE_GET_COLLISION_SHAPE("shape_barrel"); pBarrelShape->SetCallbacks(OnLoadCustomShapeResource, NULL); pBarrelShape->Load(); pBarrelShape->AddRef(); CollisionShape* pChamberShape = ENGINE_GET_COLLISION_SHAPE("chamber_collision_shape.nfcs"); pChamberShape->Load(); pChamberShape->AddRef(); pWindow->InitCamera(); pWindow->setTitle(L"NFEngine Demo"); #ifdef SCENE_MINECRAFT // SUNLIGHT Entity* pDirLightEnt = g_pScene->CreateEntity(); XOrientation orient; orient.x = Vector(0.0f, -0.0f, -0.0f, 0.0f); orient.z = Vector(-1.5f, -1.0f, 0.5f, 0.0f); orient.y = Vector(0.0f, 1.0f, 0.0f, 0.0f); pDirLightEnt->SetOrientation(&orient); DirLightDesc dirLight; dirLight.m_Far = 100.0f; dirLight.m_Splits = 4; dirLight.m_LightDist = 1000.0f; LightComponent* pDirLight = new LightComponent(pDirLightEnt); pDirLight->SetDirLight(&dirLight); pDirLight->SetColor(Float3(2.2, 2, 1.8)); pDirLight->SetShadowMap(1024); // MINECRAFT Entity* pEnt = g_pScene->CreateEntity(); pEnt->SetPosition(Vector(0, -70.0f, 0)); MeshComponent* pMesh = new MeshComponent(pEnt); pMesh->SetMeshResource("minecraft.nfm"); #endif #ifdef SCENE_SPONZA // SPONZA Entity* pEnt = g_pScene->CreateEntity(); pEnt->SetPosition(Vector(0, 0, 0)); MeshComponent* pMesh = new MeshComponent(pEnt); pMesh->SetMeshResource("sponza.nfm"); CollisionShape* pSponzaShape = ENGINE_GET_COLLISION_SHAPE("sponza_collision_shape.nfcs"); //pSponzaShape->Load(); BodyComponent* pFloorBody = new BodyComponent(pEnt); pFloorBody->EnablePhysics(pSponzaShape); pFloorBody->SetMass(0.0); pEnt = g_pScene->CreateEntity(); pEnt->SetPosition(Vector(0.0f, 3.5f, 0.0f)); LightComponent* pLight = new LightComponent(pEnt); OmniLightDesc omni; omni.m_ShadowFadeStart = 12.0f; omni.m_ShadowFadeEnd = 120.0f; omni.m_Radius = 90.0f; pLight->SetOmniLight(&omni); pLight->SetColor(Float3(50, 50, 50)); pLight->SetShadowMap(512); /* // SUNLIGHT Entity* pDirLightEnt = g_pScene->CreateEntity(); XOrientation orient; orient.x = Vector(0.0f, -0.0f, -0.0f, 0.0f); orient.z = Vector(0.1, -2.3f, 1.05, 0.0f); orient.y = Vector(0.0f, 1.0f, 0.0f, 0.0f); pDirLightEnt->SetOrientation(&orient); DirLightDesc dirLight; dirLight.m_Far = 100.0f; dirLight.m_Splits = 4; dirLight.m_LightDist = 1000.0; LightComponent* pDirLight = new LightComponent(pDirLightEnt); pDirLight->SetDirLight(&dirLight); pDirLight->SetColor(Float3(2.2, 1.3, 0.8)); pDirLight->SetShadowMap(2048); */ #endif //performance test (many objects and shadowmaps) #ifdef SCENE_SEGMENTS_PERF_TEST for (int x = -4; x < 5; x++) { for (int z = -4; z < 5; z++) { Entity* pEntity = g_pScene->CreateEntity(); pEntity->SetPosition(12.0f * Vector((float)x, 0, (float)z)); MeshComponent* pMesh = new MeshComponent(pEntity); pMesh->SetMeshResource("chamber.nfm"); BodyComponent* pBody = new BodyComponent(pEntity); pBody->EnablePhysics(pChamberShape); LightComponent* pLight; OmniLightDesc omni; pEntity = g_pScene->CreateEntity(); pEntity->SetPosition(12.0f * Vector(x, 0, z) + Vector(0.0f, 3.5f, 0.0f)); pLight = new LightComponent(pEntity); omni.shadowFadeStart = 80.0f; omni.shadowFadeEnd = 120.0f; omni.radius = 8.0f; pLight->SetOmniLight(&omni); pLight->SetColor(Float3(50, 50, 50)); pLight->SetShadowMap(32); pEntity = g_pScene->CreateEntity(); pEntity->SetPosition(12.0f * Vector(x, 0, z) + Vector(6.0f, 1.8f, 0.0f)); pLight = new LightComponent(pEntity); omni.radius = 3.0f; pLight->SetOmniLight(&omni); pLight->SetColor(Float3(5.0f, 0.5f, 0.25f)); pEntity = g_pScene->CreateEntity(); pEntity->SetPosition(12.0f * Vector(x, 0, z) + Vector(0.0f, 1.8f, 6.0f)); pLight = new LightComponent(pEntity); omni.radius = 3.0f; pLight->SetOmniLight(&omni); pLight->SetColor(Float3(5.0f, 0.5f, 0.25f)); /* for (int i = -3; i<=3; i++) { for (int j = 0; j<4; j++) { for (int k = -3; k<=3; k++) { Entity* pCube = g_pScene->CreateEntity(); pCube->SetPosition(12.0f * Vector(x,0,z) + 0.6f * Vector(i,j,k) + Vector(0.0f, 0.25f, 0.0f)); MeshComponent* pMesh = new MeshComponent(pCube); pMesh->SetMeshResource("cube.nfm"); BodyComponent* pBody = new BodyComponent(pCube); pBody->SetMass(0.0f); pBody->EnablePhysics((CollisionShape*)Engine_GetResource(Mesh::COLLISION_SHAPE, "shape_box")); } } }*/ } } #endif // infinite looped scene #ifdef SCENE_SEGMENTS BufferOutputStream segmentDesc; Matrix mat = MatrixRotationNormal(Vector(0, 1, 0), NFE_MATH_PI / 4.0f); // create segments description buffer { OmniLightDesc omni; LightComponent* pLight; Entity entity; entity.SetPosition(Vector()); //pEntity->SetMatrix(mat); MeshComponent* pMesh = new MeshComponent(&entity); pMesh->SetMeshResource("chamber.nfm"); BodyComponent* pBody = new BodyComponent(&entity); pBody->EnablePhysics(pChamberShape); entity.Serialize(&segmentDesc, Vector()); entity.RemoveAllComponents(); entity.SetPosition(Vector(0.0f, 3.5f, 0.0f)); /* pLight = new LightComponent(&entity); omni.m_ShadowFadeEnd = 12.0f; omni.m_ShadowFadeStart = 8.0f; omni.m_Radius = 9.0f; pLight->SetOmniLight(&omni); pLight->SetColor(Float3(50, 50, 50)); pLight->SetShadowMap(512); entity.Serialize(&segmentDesc, Vector()); */ entity.RemoveAllComponents(); entity.SetPosition(Vector(6.0f, 1.8f, 0.0f)); pLight = new LightComponent(&entity); omni.m_Radius = 3.0f; pLight->SetOmniLight(&omni); pLight->SetColor(Float3(5.0f, 0.5f, 0.25f)); entity.Serialize(&segmentDesc, Vector()); entity.RemoveAllComponents(); entity.SetPosition(Vector(0.0f, 1.8f, 6.0f)); pLight = new LightComponent(&entity); omni.m_Radius = 3.0f; pLight->SetOmniLight(&omni); pLight->SetColor(Float3(5.0f, 0.5f, 0.25f)); entity.Serialize(&segmentDesc, Vector()); } #define SEG_AXIS_NUM 12 Segment* pSegments[SEG_AXIS_NUM][SEG_AXIS_NUM]; // create segments array for (int i = 0; i < SEG_AXIS_NUM; i++) { for (int j = 0; j < SEG_AXIS_NUM; j++) { char segName[32]; sprintf_s(segName, "seg_%i_%i", i, j); pSegments[i][j] = g_pScene->CreateSegment(segName, Vector(5.99f, 1000.0f, 5.99f)); pSegments[i][j]->AddEntityFromRawBuffer(segmentDesc.GetData(), segmentDesc.GetSize()); } } // create links for (int x = 0; x < SEG_AXIS_NUM; x++) { for (int z = 0; z < SEG_AXIS_NUM; z++) { //make inifinite loop for (int depth = 1; depth <= 5; depth++) { g_pScene->CreateLink(pSegments[x][z], pSegments[(x + depth) % SEG_AXIS_NUM][z], Vector(depth * 12.0f, 0.0f, 0.0f)); g_pScene->CreateLink(pSegments[x][z], pSegments[x][(z + depth) % SEG_AXIS_NUM], Vector(0.0, 0.0f, depth * 12.0f)); } } } // Set focus g_pScene->SetFocusSegment(pSegments[0][0]); #endif /* for (int i = -4; i<4; i++) { for (int j = -10; j<10; j++) { for (int k = -4; k<4; k++) { Entity* pCube = g_pScene->CreateEntity(); pCube->SetPosition(0.75f * Vector(i,j,k)); MeshComponent* pMesh = new MeshComponent(pCube); pMesh->SetMeshResource("cube.nfm"); BodyComponent* pBody = new BodyComponent(pCube); pBody->SetMass(1.0f); pBody->EnablePhysics((CollisionShape*)Engine_GetResource(Mesh::COLLISION_SHAPE, "shape_box")); } } } //set ambient & background color envDesc.m_AmbientLight = Vector(0.001f, 0.001f, 0.001f, 0.0f); envDesc.m_BackgroundColor = Vector(0.0f, 0.0f, 0.0f, 0.0f); g_pScene->SetEnvironment(&envDesc); // SUNLIGHT Entity* pDirLightEnt = g_pScene->CreateEntity(); XOrientation orient; orient.x = Vector(0.0f, -0.0f, -0.0f, 0.0f); orient.z = Vector(-0.5f, -1.1f, 1.2f, 0.0f); orient.y = Vector(0.0f, 1.0f, 0.0f, 0.0f); pDirLightEnt->SetOrientation(&orient); DirLightDesc dirLight; dirLight.m_Far = 100.0f; dirLight.m_Splits = 4; dirLight.m_LightDist = 1000.0f; LightComponent* pDirLight = new LightComponent(pDirLightEnt); pDirLight->SetDirLight(&dirLight); pDirLight->SetColor(Float3(2.2, 2, 1.8)); pDirLight->SetShadowMap(1024); pFirstWindow->cameraEntity->SetPosition(Vector(0.0f, 1.6f, -20.0f, 0.0f)); */ // message loop DrawRequest drawRequests[2]; Common::Timer timer; timer.Start(); while (!pWindow->isClosed()) { //measure delta time g_DeltaTime = (float)timer.Stop(); timer.Start(); UpdateRequest updateReq; updateReq.pScene = g_pScene; updateReq.deltaTime = g_DeltaTime; pWindow->processMessages(); pWindow->UpdateCamera(); drawRequests[0].deltaTime = g_DeltaTime; drawRequests[0].pView = pWindow->view; drawRequests[1].deltaTime = g_DeltaTime; drawRequests[1].pView = g_pSecondaryCameraView; EngineAdvance(drawRequests, 2, &updateReq, 1); ProcessSceneEvents(); // print focus segment name wchar_t str[128]; Segment* pFocus = g_pScene->GetFocusSegment(); swprintf(str, L"NFEngine Demo (%S) - focus: %S", PLATFORM_STR, (pFocus != 0) ? pFocus->GetName() : "NONE"); pWindow->setTitle(str); } // for testing purposes Common::FileOutputStream test_stream("test.xml"); g_pScene->Serialize(&test_stream, SerializationFormat::Xml, false); EngineDeleteScene(g_pScene); delete pWindow; EngineRelease(); //detect memory leaks #ifdef _DEBUG _CrtDumpMemoryLeaks(); #endif return 0; }