I am trying to create a rectangle and colour the inverse (area outside that rectangle) area.
Gdiplus::Region *cropRegion = new Gdiplus::Region(cropRectF);
Gdiplus::Rect gdiCropRect(0.0f, 0.0f, 1000.0f, 1000.0f);
Gdiplus::Rect imageContainer(0.0f, 0.0f, 1000.0f, 1000.0f);
Gdiplus::Region *completeRegion = new Gdiplus::Region(imageContainer);
completeRegion->Intersect(cropRegion);
gdiGraphics.SetClip(completeRegion, Gdiplus::CombineModeXor);
Gdiplus::GraphicsPath *cropRectPath = new Gdiplus::GraphicsPath();
cropRectPath->AddRectangle(cropRectF);
Gdiplus::Pen* myPen = new Gdiplus::Pen(Gdiplus::Color::White);
myPen->SetWidth(1);
gdiGraphics.DrawPath(myPen, cropRectPath);
Gdiplus::SolidBrush dimmingBrush(Gdiplus::Color::MakeARGB(50, 0, 0, 0));
gdiGraphics.FillRegion(&dimmingBrush, completeRegion);
The code till here works perfectly fine,
However when I try to move this rectangle using mouse movements in MouseMove CallBack, it moves but it leaves white traces behind.
I tried to call InvalidateRect in the end but then my rectangle doesn't moves at all.
Any Idea how can old rectangle can be cleared.
I am using GDI+ and C++.
Thanks
Related
I am using gdiplus inside my mfc application. I want to draw an arc ended with a cap. When I use a TranslateTransform (in this example I want to swap the positive angle direction), the end cap is crossed by a line.
I used only this simple code inside the OnPaint method:
CPaintDC dc(this);
CRect rect;
GetClientRect(rect);
Gdiplus::Graphics gdip(dc.GetSafeHdc());
gdip.SetSmoothingMode(Gdiplus::SmoothingModeHighQuality);
Gdiplus::SolidBrush brush(Gdiplus::Color(255, 255, 255));
gdip.FillRectangle(&brush, rect.left, rect.top, rect.Width(), rect.Height());
Gdiplus::Pen pen(Gdiplus::Color(0, 0, 0), 1.0f);
Gdiplus::AdjustableArrowCap endArrow(10.0f, 10.0f);
pen.SetCustomEndCap(&endArrow);
int radius = 100;
gdip.TranslateTransform(rect.CenterPoint().x, rect.CenterPoint().y);
gdip.ScaleTransform(1.0f, -1.0f);
gdip.DrawArc(&pen, -radius, -radius, 2 * radius, 2 * radius, 0.0f, 90.0f);
If I remove the "gdip.TranslateTransform..." line the direction is opposite but the end cap is ok. Is this a known issue? Does anybody know how to set the angle orientation correctly so as the end caps stay ok?
With below instructions, I can strafe camera left-right, up-down and can rotate pitch+yaw using mouse and keyboard but I couldnt move forward or backwards:
Starting of drawing:
GL11.glMatrixMode(GL11.GL_PROJECTION);
GL11.glLoadIdentity();
GL11.glTranslatef((-(float)horizontalstrafe)*0.15f, (-(float)verticalstrafe)*0.15f, 0);
GL11.glTranslatef(0, 0, ((float)-forward)*0.15f);// does not move!
GL11.glRotatef((float)mouseAngleY, -0.02f, 0, 0);
GL11.glRotatef((float)mouseAngleX, 0, 0.02f, 0);
Why doesnt it move? When I press the necessary button, it just disappears after sometime and get visible again if I press opposite button for enough time.
Init part:
GL11.glMatrixMode(GL11.GL_MATRIX_MODE);
GL11.glLoadIdentity();
GL11.glOrtho(-2.0 * aspect, 2.0 * aspect, -2.0, 2.0, 0.1f, 3.0f);
Note: the object I need to see has vertices between the boundaries of x:[-1,1], y:[-1,1], z:[-1,1]
I need to draw something in DirectX 9.
I am interested to work in a way that the coordinates will be adjusted to pixels, while (0,0) will be the TopLeft corner. Here is what I do:
RECT clientRect;
::GetClientRect(m_hWIN, &clientRect);
D3DXMATRIX matOrtho2D, matIdentity;
D3DXMatrixOrthoOffCenterLH(&matOrtho2D, 0, clientRect.right, 0, clientRect.bottom, 0.0f, 1.0f);
D3DXMatrixIdentity(&matIdentity);
g_pDirect3D_Device->SetTransform(D3DTS_PROJECTION, &matOrtho2D);
g_pDirect3D_Device->SetTransform(D3DTS_WORLD, &matIdentity);
g_pDirect3D_Device->SetTransform(D3DTS_VIEW, &matIdentity);
This works fine, except the (0,0) is the BottomLeft corner. How can I change it?
Thanks!
This should be add to code:
D3DXMATRIX matTranslate, matFlip;
D3DXMatrixTranslation(&matTranslate, 0, clientRect.bottom, 0.0f);
D3DXMatrixRotationX(&matFlip, D3DXToRadian(180));
D3DXMATRIX matCoordTransform = matFlip * matTranslate;
g_pDirect3D_Device->SetTransform(D3DTS_WORLD, &matCoordTransform);
I'm new to directx and finally I managed to load an image which I want to display as background image/texture
Defining image
void setBGImage(std::string path)
{
D3DXCreateTextureFromFileA(m_Device, path.c_str(), &m_BGImage);
m_BGImageCenter = D3DXVECTOR3(450.0f, 250.0f, 0.0f); // Image is 900x500
}
Drawing image
void DrawBackground()
{
m_Sprite->Begin(D3DXSPRITE_OBJECTSPACE|D3DXSPRITE_DONOTMODIFY_RENDERSTATE);
// Texture tiling
/*
D3DXMATRIX texScaling;
D3DXMatrixScaling(&texScaling, 1.0f, 1.0f, 0.0f);
m_Device->SetTransform(D3DTS_TEXTURE0, &texScaling);*/
//D3DXMATRIX T, S, R;
//D3DXMatrixTranslation(&T, 0.0f, 0.0f, 0.0f);
//D3DXMatrixScaling(&S, 1.0f, 1.0f, 0.0f);
//D3DXMatrixRotationZ(&R, 0.45f);
//m_Sprite->SetTransform(&(S*T));
m_Sprite->Draw(m_BGImage, 0, &m_BGImageCenter, 0, D3DCOLOR_XRGB(255, 255, 255));
m_Sprite->Flush();
m_Sprite->End();
}
onResetDevice (is getting called at startup)
void onResetDevice()
{
m_Sprite->OnResetDevice();
// Sets up the camera 640 units back looking at the origin.
D3DXMATRIX V;
D3DXVECTOR3 pos(0.0f, 0.0f, -640.0f); // This distance is just a test value to get only the image and no white background/border as soon as I have it centered I will adjust it.
D3DXVECTOR3 up(0.0f, 1.0f, 0.0f);
D3DXVECTOR3 target(0.0f, 0.0f, 0.0f);
D3DXMatrixLookAtLH(&V, &pos, &target, &up);
m_Device->SetTransform(D3DTS_VIEW, &V);
// The following code defines the volume of space the camera sees.
D3DXMATRIX P;
RECT R;
GetClientRect(m_hWnd, &R);
float width = (float)R.right;
float height = (float)R.bottom;
D3DXMatrixPerspectiveFovLH(&P, D3DX_PI*0.25f, width/height, 0.0f, 1.0f);
m_Device->SetTransform(D3DTS_PROJECTION, &P);
// This code sets texture filters, which helps to smooth out disortions when you scale a texture.
m_Device->SetSamplerState(0, D3DSAMP_MAGFILTER, D3DTEXF_LINEAR);
m_Device->SetSamplerState(0, D3DSAMP_MINFILTER, D3DTEXF_LINEAR);
m_Device->SetSamplerState(0, D3DSAMP_MIPFILTER, D3DTEXF_LINEAR);
// This line of code disables Direct3D lighting.
m_Device->SetRenderState(D3DRS_LIGHTING, false);
// The following code specifies an alpha test and reference value.
m_Device->SetRenderState(D3DRS_ALPHAREF, 10);
m_Device->SetRenderState(D3DRS_ALPHAFUNC, D3DCMP_GREATER);
// The following code is used to setup alpha blending.
m_Device->SetTextureStageState(0, D3DTSS_ALPHAARG1, D3DTA_TEXTURE);
m_Device->SetTextureStageState(0, D3DTSS_ALPHAOP, D3DTOP_SELECTARG1);
m_Device->SetRenderState(D3DRS_SRCBLEND, D3DBLEND_SRCALPHA);
m_Device->SetRenderState(D3DRS_DESTBLEND, D3DBLEND_INVSRCALPHA);
// Indicates that we are using 2D texture coordinates.
m_Device->SetTextureStageState(0, D3DTSS_TEXTURETRANSFORMFLAGS, D3DTTFF_COUNT2);
}
When I use these methods to render my background image it get's displayed upside down, not centered (little bit to the right) and I've got the feeling the height width ratio isn't correct (image is kinda blurry and it feels like the image is not as high as it's supposed to be).
What did I try?
I tried to adjust various coordinates in order to look at the image from the other side and then rotate it but whatever I tried it turned into a white/non-existing background.
Centering the image is not such a big deal I can just move the camera position but I'm confused since the m_BGImageCenter was supposed to do that for me in the Draw method and it seems to not work 100% correct.
My Questions (I guess it's ok to ask multiple questions in this context):
How can I get the image not upside down (I know how you would do it in theory but I somehow don't get it right, so please give me the coordinates).
Why is the image not centered?
Is it possible that D3DXMatrixPerspectiveFovLH is wrapping my image since it looks a little bit blurry.
I'm new to Direct3D and I was on a project taking pictures from a webcam and draw some 3D objects in front of it.
I was able to render webcam images as background using Orthogonal Projection.
//init matrix
D3DXMatrixOrthoLH(&Ortho, frameWidth, frameHeight, 0.0f, 100.0f);
//some code
D3DXVECTOR3 position = D3DXVECTOR3(0.0f, 0.0f, 100.0f);
g_pSprite->Begin(D3DXSPRITE_OBJECTSPACE);
g_pSprite->Draw(g_pTexture,NULL,¢er,&position,0xFFFFFFFF);
g_pSprite->End();
Then I tried to insert a simple triangle in front of it. The Matrices are setup as follow
D3DXMATRIXA16 matWorld;
D3DXMatrixTranslation( &matWorld, 0.0f,0.0f,5.0f );
g_pd3dDevice->SetTransform( D3DTS_WORLD, &matWorld );
D3DXMATRIXA16 matProj;
D3DXMatrixPerspectiveFovLH( &matProj, D3DX_PI / 4, 1.0f, 1.0f, 100.0f );
g_pd3dDevice->SetTransform( D3DTS_PROJECTION, &matProj );
5.0 should be < 100.0 and the triangle is supposed to be appear in front of the images. However it does not appear unless set the z position to 0. At position 0, i can see the triangle but background is blank.
Do you guys have any suggestions?
I would not draw the webcam image in the object space (D3DXSPRITE_OBJECTSPACE) if you intend to use your image solely for background purpose; something like
D3DXVECTOR3 backPos (0.f, 0.f, 0.f);
pBackgroundSprite->Begin(D3DXSPRITE_ALPHABLEND);
pBackgroundSprite->Draw (pBackgroundTexture,
0,
0,
&backPos,
0xFFFFFFFF);
pBackgroundSprite->End();
should hopefully do what you're looking for.
As a quick fix you could disable depth testing as follows;
g_pd3dDevice->SetRenderState(D3DRS_ZENABLE, D3DZB_FALSE);
This way the z-index of the primitives being drawn should reflect the order in which they are drawn.
Also, try using the PIX debugging tool (this is bundled with the DirectX SDK). This is always my first port of call for drawing discrepancies as it allows you to debug each Draw call separately with access to the depth buffer and transformed vertices.