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?
Related
This is the code as it is but I'm having trouble making SDL_Rect work or cairo move to / line to. It produces a blank black window. I found out that it is possible for cairo to draw on an SDL2 window but don't know how I would go about making it work. Majority of the code I see uses GTK+.
SDL_Window* mainWindow;
SDL_Renderer* mainRenderer;
SDL_CreateWindowAndRenderer(1280, 960, SDL_WINDOW_SHOWN, &mainWindow, &mainRenderer);
cairo_surface_t* surface;
cairo_t* cr;
surface = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, 1200, 900);
cr = cairo_create(surface);
cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
cairo_set_line_width(cr, 25);
cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
cairo_move_to(cr, 100.0, 100.0);
cairo_line_to(cr, 500, 500);
cairo_stroke(cr);
unsigned char* data;
data = cairo_image_surface_get_data(surface);
SDL_Texture* texture;
SDL_Rect rect = {0, 0, 100, 100};
texture = SDL_CreateTexture(mainRenderer, SDL_PIXELFORMAT_ABGR8888,
SDL_TEXTUREACCESS_STREAMING, 100, 200);
SDL_UpdateTexture(texture, &rect, data, 400);
// Main program loop
while (1)
{
SDL_Event event;
if (SDL_PollEvent(&event))
{
if (event.type == SDL_QUIT)
{
SDL_DestroyRenderer(mainRenderer);
SDL_DestroyWindow(mainWindow);
SDL_Quit();
break;
}
}
SDL_RenderClear(mainRenderer);
SDL_RenderCopy(mainRenderer, texture, NULL, NULL);
SDL_RenderPresent(mainRenderer);
}
// Cleanup and quit
cairo_destroy(cr);
cairo_surface_destroy(surface);
Your texture is 100x200 and you're only updating its (0,0)(100,100) rectangle from cairo image data, but with cairo you only started drawing at (100,100), so entire area is black. In addition, your pitch when updating texture is incorrect - it is byte length of source data line; your cairo image have width of 1200 and its format requires 4 bytes per pixel; neglecting padding it is 1200*4, not 400 (note - if format is different, e.g. 3 bytes per pixel, padding may be important - refer to cairo documentation to check if it pads its rows if you're going to use that format). So there are two solutions:
Use cairo to produce full image you want, e.g. don't use (100,100) offset with move_to, or copy entire image to SDL texture. Then only correcting pitch is enough.
Copy part of cairo data to texture,
e.g.
const unsigned int bpp = 4;
const unsigned int pitch = 1200*bpp;
SDL_UpdateTexture(texture, &rect,
data // offset pointer to start at 'first' pixel you want to copy
+ 100*pitch // skip first 100 rows
+ 100*bpp, // and first 100 pixels
pitch // pitch is the same - it refers to source image, not destination
);
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);
When my window is resized, i don't want the contents to scale but just to increase the view port size. I found this while searching on stackoverflow (http://stackoverflow.com/questions/5894866/resize-viewport-crop-scene) which is pretty much the same as my problem. However I'm confused as to what to set the Zoom to and where, i tried it with 1.0f but then nothing was shown at all :s
This is resize function code at the moment which does scaling:
void GLRenderer::resize() {
RECT rect;
int width, height;
GLfloat aspect;
GetClientRect(hWnd, &rect);
width = rect.right;
height = rect.bottom;
if (height == 0) {
height = 1;
}
aspect = (GLfloat) width / height;
glViewport(0, 0, width, height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45.0, aspect, 0.1, 100.0);
glMatrixMode(GL_MODELVIEW);
}
And my function to render a simple triangle:
void GLRenderer::render() {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
glTranslated(0, 0, -20);
glBegin(GL_TRIANGLES);
glColor3d(1, 0, 0);
glVertex3d(0, 1, 0);
glVertex3d(1, -1, 0);
glVertex3d(-1, -1, 0);
glEnd();
SwapBuffers(hDC);
}
You can change the zoom in y (height) with the "field of view" parameter to gluPerspective. The one that is 45 degrees in your code. As it is currently always 45 degrees, you will always get the same view angle (in y). How to change this value as a function of the height of the window is not obvious. A linear relation would fail for big values (180 degrees and up). I would try to use arctan(height/k), where 'k' is something like 500.
Notice also that when you extend the window in x, you will already get what you want (the way your source code currently is). That is, you get a wider field of view. That is because you change the aspect (second argument) to a value depending on the ratio between x and y.
Height and Width is measured in pixels, so a value of 1 is not good.
Notice that you are using deprecated legacy OpenGL. See Legacy OpenGL for more information.
How can i draw a lightened border like this with gdi/gdi+:
Anyone can give me a train of thought?Thanks.
Using GDI+, I would recommend you use a PathGradientBrush. It allows you to fill a region with series of colors around the edge that all blend toward a center color. You probably only need 1 edge color in this case. Create a GraphicsPath for a rounded rectangle and use FillPath() to fill it with a PathGradientBrush:
GraphicsPath graphicsPath;
//rect - for a bounding rect
//radius - for how 'rounded' the glow will look
int diameter = radius * 2;
graphicsPath.AddArc(Rect(rect.X, rect.Y, diameter, diameter) 180.0f, 90.0f);
graphicsPath.AddArc(Rect(rect.X + rect.Width - diameter, rect.Y, diameter, diameter), 270.0f, 90.0f);
graphicsPath.AddArc(Rect(rect.X + rect.Width - diameter, rect.Y + rect.Height - diameter, diameter, diameter), 0.0f, 90.0f);
graphicsPath.AddArc(Rect(rect.X, rect.Y + rect.Height - diameter, diameter, diameter), 90.0f, 90.0f);
graphicsPath.CloseFigure();
PathGradientBrush brush(&graphicsPath);
brush.SetCenterColor(centerColor); //would be some shade of blue, following your example
int colCount = 1;
brush.SetSurroundColors(surroundColor, &colCount); //same as your center color, but with the alpha channel set to 0
//play with these numbers to get the glow effect you want
REAL blendFactors[] = {0.0, 0.1, 0.3, 1.0};
REAL blendPos[] = {0.0, 0.4, 0.6, 1.0};
//sets how transition toward the center is shaped
brush.SetBlend(blendFactors, blendPos, 4);
//sets the scaling on the center. you may want to have it elongated in the x-direction
brush.SetFocusScales(0.2f, 0.2f);
graphics.FillPath(&brush, &graphicsPath);
Draw the border into an image slightly larger than the border itself.
Blur it.
Erase the inside of the border.
Draw the border over the blurred image.
Draw that image to the destination.
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