bool WinWindow::Initialize() { RETURN_FALSE_IF_FALSE(IWindow::Initialize()); //register window class WNDCLASSEX wcex; HINSTANCE instance = GetModuleHandle(nullptr); wcex.cbSize = sizeof(WNDCLASSEX); wcex.style = CS_HREDRAW | CS_VREDRAW | CS_OWNDC&~WS_CAPTION&~WS_SYSMENU; wcex.lpfnWndProc = _WndProc; wcex.cbClsExtra = 0; wcex.cbWndExtra = 0; wcex.hInstance = instance; wcex.hIcon = nullptr; wcex.hCursor = LoadCursor(nullptr, IDC_ARROW); wcex.hbrBackground = nullptr; wcex.lpszMenuName = nullptr; wcex.lpszClassName = L"MedusaGameWindow"; wcex.hIconSm = nullptr; if (!RegisterClassEx(&wcex)) { /*DWORD err= GetLastError();*/ //log error MEDUSA_ASSERT_FAILED(""); } ////get window rect WHeapString windowName = StringParser::ToW(mName); //int windowStyle=mParentWindowHandle==nullptr?WS_CAPTION | WS_POPUPWINDOW | WS_MINIMIZEBOX:WS_CHILDWINDOW|WS_VISIBLE; int windowStyle = mParentWindowHandle == nullptr ? WS_CAPTION | WS_OVERLAPPEDWINDOW | WS_MINIMIZEBOX : WS_CHILDWINDOW | WS_VISIBLE; mWindowHandle = CreateWindowEx(WS_EX_APPWINDOW | WS_EX_WINDOWEDGE, L"MedusaGameWindow", windowName.c_str(), windowStyle, 0, 0, (int)mSize.Width, (int)mSize.Height, mParentWindowHandle, nullptr, instance, nullptr); RegisterTouchWindow(mWindowHandle, 0); //ImmAssociateContext(mWindowHandle, nullptr); //disable ime if (!mWindowHandle) { /*DWORD err= GetLastError();*/ //log error MEDUSA_ASSERT_FAILED(""); } //SetForegroundWindow(mWindowHandle); MakeCenter(); Resize(mSize); ShowWindow(mWindowHandle, SW_SHOW); UpdateWindow(mWindowHandle); return true; }
Matrix3 Matrix3::CreateInverse() { float det=Determinant(); if (Math::IsEqual(det,0.f)) { MEDUSA_ASSERT_FAILED("Matrix has no inverse: singular matrix"); } det=1.f/det; return Matrix3(( M22 * M33 - M32 * M23 ) * det,- ( M12 * M33 - M32 * M13 ) * det, ( M12 * M23 - M22 * M13 ) * det, - ( M21 * M33 - M31 * M23 ) * det,( M11 * M33 - M31 * M13 ) * det,- ( M11 * M23 - M21 * M13 ) * det, ( M21 * M32 - M31 * M22 ) * det,- ( M11 * M32 - M31 * M12 ) * det,( M11 * M22 - M21 * M12 ) * det); }
Share<IShader> ShaderFactory::CreateShader( const FileIdRef& fileId,const List<HeapString>* defines/*=nullptr*/,ResourceShareType shareType /*= ResourceShareType::Share*/) { Share<IShader> result = nullptr; if (shareType != ResourceShareType::None) { result = Find(fileId); RETURN_SELF_IF_NOT_NULL(result); } result=ShaderCreater::Instance().Create(fileId.Name,fileId); if (result==nullptr) { switch(FileInfo::ExtractType(fileId.Name)) { case FileType::fsh: result=new BasePixelShader(fileId); break; case FileType::vsh: result=new BaseVertexShader(fileId); break; default: MEDUSA_ASSERT_FAILED("Unsupported shader file type"); return nullptr; } } MemoryData data= FileSystem::Instance().ReadAllData(fileId); if (data.IsNull()) { Log::FormatError("Cannot find:{}-{}", fileId.Name, fileId.Order); return nullptr; } StringRef str(data.Cast<char>()); if(result->Initialize(str,defines)) { Add(result, shareType); return result; } else { result = nullptr; } return nullptr; }
void FrameBuffer::AttachRenderBuffer( GraphicsAttachment attachment,RenderBuffer* renderBuffer ) { if(mRenderBuffers.ContainsKey(attachment)||mTextures.ContainsKey(attachment)) { MEDUSA_ASSERT_FAILED("Duplicate attachment"); return; } mRenderBuffers.Add(attachment,renderBuffer); Bind(true); Render::Instance().AttachRenderBufferToFrameBuffer(attachment,renderBuffer->Id()); Bind(false); UpdateClearMask(attachment,true); }
void FrameBuffer::AttachTexture( GraphicsAttachment attachment,GraphicsTextureTarget textureTarget, const Share<ITexture>& texture,int level ) { if(mRenderBuffers.ContainsKey(attachment)||mTextures.ContainsKey(attachment)) { MEDUSA_ASSERT_FAILED("Duplicate attachment"); return; } mTextures.Add(attachment,texture); Bind(true); Render::Instance().AttachTextureToFrameBuffer(attachment,textureTarget,texture->Texture(),level); Bind(false); UpdateClearMask(attachment,true); }
bool ShaderConstantInitializer::UpdateLightSpecular( ShaderConstant& uniform ) { ILight* light=LightFactory::Instance().GetCurrentLight(); if (light!=nullptr) { uniform.Invalidate(); uniform.Set(light->SpecularColor()); return true; } else { MEDUSA_ASSERT_FAILED("No light for LightSpecular"); return false; } }
RenderBuffer* FrameBuffer::DetachRenderBuffer(GraphicsAttachment attachment) { RenderBuffer* renderBuffer=mRenderBuffers.GetOptional(attachment,nullptr); if (renderBuffer==nullptr) { MEDUSA_ASSERT_FAILED("Cannot find attachment"); return nullptr; } mRenderBuffers.RemoveKey(attachment); Bind(true); Render::Instance().AttachRenderBufferToFrameBuffer(attachment,0); Bind(false); UpdateClearMask(attachment,false); return renderBuffer; }
void ShaderConstantInitializer::Register(RenderingStep step,StringRef name,FuncType func ) { InitializerType* initializer= mInitializerDict.TryGetValueWithFailed(step,nullptr); if (initializer==nullptr) { initializer=new InitializerType(); mInitializerDict.Add(step,initializer); } if(initializer->ContainsKey(name)) { MEDUSA_ASSERT_FAILED("Duplicate register"); } initializer->Add(name,func); }
bool ShaderConstantInitializer::UpdateWorldLightPosition( ShaderConstant& uniform ) { ILight* light=LightFactory::Instance().GetCurrentLight(); if (light!=nullptr) { //IRenderBatch* batch=RenderingContext::Instance().GetBatch(); Point3F position=light->Position(); uniform.Invalidate(); uniform.Set(position); return true; } else { MEDUSA_ASSERT_FAILED("No light for WorldLightPosition"); return false; } }
bool ShaderConstantInitializer::UpdateWorldLightDirection( ShaderConstant& uniform) { ILight* light=LightFactory::Instance().GetCurrentLight(); if (light!=nullptr) { //IRenderBatch* batch=RenderingContext::Instance().GetBatch(); Point3F direction=light->Direction(); uniform.Invalidate(); uniform.Set(direction); return true; } else { MEDUSA_ASSERT_FAILED("No light for WorldLightDirection"); //lightDirectionUniform->Set(Point3F(0.f,0.f,-1.f)); //default light return false; } }
void EffectTechnique::AddPass( IRenderPass* pass ) { IRenderPass* result=GetPassByName(pass->Name()); if (result!=nullptr) { MEDUSA_ASSERT_FAILED("Duplicate add pass"); } if (!pass->Name().IsEmpty()) { mRenderPassDict.Add(pass->Name(),pass); } mRenderPasses.Add(pass); pass->Retain(); pass->SetTechnique(this); UpdateFlags(); }
Share<ITexture> FrameBuffer::DetachTexture( GraphicsAttachment attachment ) { auto texture=mTextures.GetOptional(attachment,nullptr); if (texture==nullptr) { MEDUSA_ASSERT_FAILED("Cannot find attachment"); return nullptr; } bool isShared=texture->IsShared(); mTextures.RemoveKey(attachment); Bind(true); Render::Instance().AttachTextureToFrameBuffer(attachment,GraphicsTextureTarget::Texture2D,0,0); //GraphicsTextureTarget::Texture2D is ignored inside Bind(false); UpdateClearMask(attachment,false); return isShared?texture:nullptr; }
bool ShaderConstantInitializer::UpdateLightViewProjectMatrix( ShaderConstant& uniform) { ILight* light=LightFactory::Instance().GetCurrentLight(); if (light!=nullptr&&light->LightType()==GraphicsLightType::Spot) { SpotLight* spotLight=(SpotLight*)light; uniform.Invalidate(); const Matrix& matrix=spotLight->ViewProjectMatrix(); uniform.SetMatrix(matrix); return true; } else { MEDUSA_ASSERT_FAILED("No spot light for LightViewProjectMatrix"); return false; } }
bool ShaderConstantInitializer::UpdateModelLightPosition( ShaderConstant& uniform ) { ILight* light=LightFactory::Instance().GetCurrentLight(); if (light!=nullptr) { IRenderBatch* batch=RenderingContext::Instance().Batch(); Matrix modelMatrix=batch->GetModelMatrix(); modelMatrix.Inverse(); Point3F position=light->Position(); position=modelMatrix.Transform(position); uniform.Invalidate(); uniform.Set(position); return true; } else { MEDUSA_ASSERT_FAILED("No light for ModelLightPosition"); return false; } }
bool WinWindow::Uninitialize() { RETURN_TRUE_IF_NULL(mWindowHandle); if (DestroyWindow(mWindowHandle) != 0) { //DWORD error= GetLastError(); mWindowHandle = nullptr; } mWindowHandle = nullptr; HINSTANCE instance = GetModuleHandle(nullptr); if (!UnregisterClass(L"MedusaGameWindow", instance)) { /*DWORD err= GetLastError();*/ //log error MEDUSA_ASSERT_FAILED(""); } return true; }
SamplerRenderState* SamplerRenderState::Current() { IRender& render = Render::Instance(); GraphicsTextureUnits activeTexture = (GraphicsTextureUnits)render.GetInteger(GraphicsIntegerName::ActiveTexture); SamplerRenderState* samplerState = new SamplerRenderState(); samplerState->SetTextureUnit(activeTexture); uint cubeMapTexture = render.GetInteger(GraphicsIntegerName::CubeMapTextureBinding); uint texture2D = render.GetInteger(GraphicsIntegerName::TextureBinding); //uint texture = 0; GraphicsTextureType textureType = GraphicsTextureType::Texture2D; if (cubeMapTexture != 0) { if (texture2D != 0) { Log::Error("Error texture binding"); MEDUSA_ASSERT_FAILED("Error texture binding"); } else { textureType = GraphicsTextureType::TextureCubeMap; samplerState->SetTexture(cubeMapTexture); //texture = cubeMapTexture; } } else { textureType = GraphicsTextureType::Texture2D; samplerState->SetTexture(texture2D); //texture = texture2D; } samplerState->SetTextureUnit((GraphicsTextureUnits)render.GetInteger(GraphicsIntegerName::ActiveTexture)); samplerState->SetMagFilter(GraphicsTextureMagFilter(render.GetTextureParamter(textureType, GraphicsTextureParameter::MagFilter))); samplerState->SetMinFilter(GraphicsTextureMinFilter(render.GetTextureParamter(textureType, GraphicsTextureParameter::MinFilter))); samplerState->SetWrapS(GraphicsTextureWrapMode(render.GetTextureParamter(textureType, GraphicsTextureParameter::WrapS))); samplerState->SetWrapT(GraphicsTextureWrapMode(render.GetTextureParamter(textureType, GraphicsTextureParameter::WrapT))); return samplerState; }
bool ShaderConstantInitializer::UpdateLightWorldViewProjectMatrix( ShaderConstant& uniform) { ILight* light=LightFactory::Instance().GetCurrentLight(); if (light!=nullptr&&light->LightType()==GraphicsLightType::Spot) { IRenderBatch* batch=RenderingContext::Instance().Batch(); Matrix modelMatrix=batch->GetModelMatrix(); SpotLight* spotLight=(SpotLight*)light; uniform.Invalidate(); const Matrix& matrix=spotLight->ViewProjectMatrix(); modelMatrix*=matrix; uniform.SetMatrix(modelMatrix); return true; } else { MEDUSA_ASSERT_FAILED("No spot light for LightViewProjectMatrix"); return false; } }
Matrix43 Matrix43::CreateInverse() { // Should be [A 0] // [B 1] float det=Determinant(); if (Math::IsEqual(det,0.f)) { MEDUSA_ASSERT_FAILED("Matrix has no inverse: singular matrix"); } det=1.f/det; Matrix43 result; /* mat=A*B => (A*B)-1=B-1*A-1=mat-1 */ //inverse(A)=adj(A)/det; result.M11 = ( M22 * M33 - M32 * M23 ) * det; result.M12 = - ( M12 * M33 - M32 * M13 ) * det; result.M13 = ( M12 * M23 - M22 * M13 ) * det; result.M21 = - ( M21 * M33 - M31 * M23 ) * det; result.M22 = ( M11 * M33 - M31 * M13 ) * det; result.M23 = - ( M11 * M23 - M21 * M13 ) * det; result.M31 = ( M21 * M32 - M31 * M22 ) * det; result.M32 = - ( M11 * M32 - M31 * M12 ) * det; result.M33 = ( M11 * M22 - M21 * M12 ) * det; //inverse(B)*inverse(A) //inverse(B)=-B //mat-1=-B*inverse(A) result.M41 = - ( M41 * result.M11 + M42 * result.M21 + M43 * result.M31 ); result.M42 = - ( M41 * result.M12 + M42 * result.M22 + M43 * result.M32 ); result.M43 = - ( M41 * result.M13 + M42 * result.M23 + M43 * result.M33 ); return result; }