Tag: rendering

Entries for tag "rendering", ordered from most recent. Entry count: 158.

Warning! Some information on this page is older than 3 years now. I keep it for reference, but it probably doesn't reflect my current knowledge and beliefs.

Pages: > 1 2 3 4 5 ... 20 >

22:47
Mon
07
May 2012

Direct2D - Unboxing

Just as some bloggers record unboxing of products, I'd like to share my first impressions from playing around with Direct2D library. What is it? As you can guess from its name, it is a library from Microsoft for efficient, hardware-accelerated drawing of immediate-mode 2D graphics. It is successor of the old GDI, as well as GDI+. It is native library, object-oriented, based on COM. It works on Windows 7, as well as Windows Vista (after installing SP2 and Platform Update) and Windows 2008. The API seems modern, clean and very elegant, much like Direct3D 10/11. But of course it could be just a matter of taste. You can find its documentation, as well as headers and libraries in DirectX SDK. Docs say something about Windows 7 SDK, so probably it's there too.

What can it interop with? You can create a render target that will send the graphics to either a window (using HWND), a GDI canvas (HDC), DXGI surface (interop with Direct3D 10/11) or WIC Bitmap. Interop with GDI is also possible in the opposite way - you can obtain a HDC from D2D render target. Microsoft advices using WIC (Windows Imaging Component) for loading bitmap files. Another important library that cooperates with Direct2D is DirectWrite, which enables text rendering.

I didn't find any good tutorial about Direct2D, but I haven't look for it very hard either. I think the official documentation is good enough teaching all aspects of the library. After reviewing the documentation, I'm very impressed not only by the looks of the API, but also by the list of features is supports, like:

Things I don't like so far about the library:

My first test, drawn using following code:

using namespace D2D1;
const D2D1_SIZE_F rtSize = m_RenderTarget->GetSize();
m_RenderTarget->BeginDraw();

m_RenderTarget->Clear(ColorF(ColorF::White));

m_Brush->SetColor(ColorF(ColorF::Silver));
const float gridStep = 24.f;
for(float x = gridStep; x < rtSize.width; x += gridStep)
    m_RenderTarget->DrawLine(Point2F(x, 0.f), Point2F(x, rtSize.height), m_Brush, 1.f);
for(float y = gridStep; y < rtSize.height; y += gridStep)
    m_RenderTarget->DrawLine(Point2F(0.f, y), Point2F(rtSize.width, y), m_Brush, 1.f);

m_Brush->SetColor(ColorF(ColorF::Navy));
m_RenderTarget->DrawRectangle(RectF(200.f, 400.f, 400.f, 550.f), m_Brush);
m_RenderTarget->DrawRectangle(RectF(240.f, 450.f, 280.f, 500.f), m_Brush);
m_RenderTarget->DrawRectangle(RectF(320.f, 450.f, 360.f, 550.f), m_Brush));
m_RenderTarget->DrawLine(Point2F(200.f, 400.f), Point2F(300.f, 300.f), m_Brush, 1.f);
m_RenderTarget->DrawLine(Point2F(300.f, 300.f), Point2F(400.f, 400.f), m_Brush, 1.f);

m_RenderTarget->EndDraw();

Comments (2) | Tags: rendering direct2d | Author: Adam Sawicki | Share

15:35
Wed
19
Oct 2011

Easiest Way to Generate a Bitmap

I wanted to visualize results of some mathematical computations so I wondered what is the easiest way to generate and then show or save a bitmap? I considered Scilab, PHP with GD library, C++ with WinAPI GDI, C++ with some library that have image file formats support like FreeImage... Finally I've came up with the following solution, using only C++ and WinAPI to fill appropriate data structures and save the image as 24-bit BMP file.

// Width and height of the bitmap
unsigned size_x = 1280, size_y = 720;
// Rows top-down,
// for each row pixels from left to right,
// for each pixel components B, G, R = 0..255.
unsigned data_size = size_x * size_y * 3;
unsigned char *data = new unsigned char[data_size];

// Here fill the data, for example:
for (unsigned y = 0, i = 0; y < size_y; ++y) {
  for (unsigned x = 0; x < size_x; ++x) {
    data[i++] = 255; // G
    data[i++] = 255; // B
    data[i++] = 255; // R
  }
}

// Prepare data structures
DWORD data_offset = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER);
DWORD file_size = data_offset + data_size;
BITMAPFILEHEADER file_header = {
  0x4D42, // bfType = "BM"
  file_size, // bfSize
  0, 0, // bfReserved1, bfReserved2
  data_offset, // bfOffBits
};
BITMAPINFOHEADER info_header = {
  sizeof(BITMAPINFOHEADER), // biSize
  (LONG)size_x, -(LONG)size_y, // biWidth, biHeight
  1, // biPlanes
  24, // biBitCount
  BI_RGB, // biCompression
  0, // biSizeImage
  72, 72, // biXPelsPerMeter, biYPelsPerMeter
  0, 0, // biClrUsed, biClrImportant
};

// Save file
FILE *file = fopen("file_name.bmp", "wb");
fwrite(&file_header, sizeof(file_header), 1, file);
fwrite(&info_header, sizeof(info_header), 1, file);
fwrite(&data[0], 1, data_size, file);
fclose(file);

// Finalize
delete [] data;

Comments (1) | Tags: rendering winapi c++ | Author: Adam Sawicki | Share

20:02
Mon
11
Jul 2011

Social Graph - Application on Facebook

If you are active Facebook user and you like data visualisation just like me, then I strongly recommend you look at Social Graph application. When you run in, a Flash applet draws circles representing all your friends and connections between them. You are not there because, well, these are your friends so you are connected to all of them. You can only see how are they connected to each other. What's most interesting though is that they form some groups, which are clearly depicted thanks to graph visualisation algorithm used in this app. For example, my graph looks like this:

Facebook Social Graph

Several persons are not connected to anyone other I know. They are drawn somewhere outside the graph in a far distance. These are people who I know from some unusual place or who do not participate actively in Facebook and do not collect friends.

Rest of them forms clearly distinctive groups. The group on the right are people from my home town - mostly my friends from high school or university. The biggest group in the middle are passionate game developers from warsztat.gd community. Small group at the bottom consisting of four circles are contacts from my current job. On the left of Warsztat there is a group of professional game developers from Polish game industry. And finally the smaller group on the left consists of people I know from psytrance parties :)

What's also interesting is that there are some people who play role of a "center" of some group - are connected with almost everyone else in that group. Others are "connectors" that join two groups.

Comments (3) | Tags: rendering web | Author: Adam Sawicki | Share

19:12
Mon
14
Feb 2011

MRI Rendering

Having injured arm is not a good thing, but for this reason I had MRI (Magnetic Resonance Imaging) and got results on a CD, so I've decided to try to render them in 3D as soon as I could move my arm a bit. Here is the final result:

Read full entry > | Comments (3) | Tags: directx medicine rendering | Author: Adam Sawicki | Share

20:27
Fri
27
Aug 2010

Parallelizing Algorithms with Intel TBB and C++ Lambdas

My demo for RiverWash is practically finished. I still could polish it or even make some big changes, because I know it's not great, but that's another story. What I want to write about today is how easily an algorithm can be changed to run in parallel on multicore processors when you use Intel Threading Building Blocks and C++ lambdas.

First, here is an algorithm. In one of my graphic effects I fill a 256x512 texture on CPU every frame. For each pixel I calculate a color based on some input data, which are constant during this operation. So the code looks like this:

void SaveToTexture(const D3DLOCKED_RECT &lockedRect)
{
  uint x, y;
  char *rowPtr = (char*)lockedRect.pBits;
  for (y = 0; y < TEXTURE_SIZEY; ++y)
  {
    XMCOLOR *pixelPtr = (XMCOLOR*)rowPtr;
    for (x = 0; x < TEXTURE_SIZEX; ++x)
    {
      *pixelPtr = CalcColorForPixel(x, y);
      ++pixelPtr;
    }
    rowPtr += lockedRect.Pitch;
  }
}

How to parallelize such loop? First, some theoretical background. Intel TBB is a free C++ library for high-level parallel programming. It has nice interface that makes extensive use of C++ language features but is very clean and simple. It provides many useful classes, from different kinds of mutexes and atomic operations, through thread-safe, scalable containers and memory allocators, till sophisticated task scheduler. But for my problem it was sufficient to use simple parallel_for function that utilizes the task scheduler internally. To start using TBB, I've just had to download and unpack this library, add appropriate paths as Include and Library directories in my Visual C++ and add this code:

#include <tbb/tbb.h>

#ifdef _DEBUG
#pragma comment(lib, "tbb_debug.lib")
#else
#pragma comment(lib, "tbb.lib")
#endif

Second topic I want to cover here are lambdas - new, great language feature from C++0x standard, available since Visual C++ 2010. Lambdas are simply unnamed functions defined inline inside some code. What's so great about them is they can capture the context of the caller. Selected variables can be passed by value or by reference, as well as this pointer or even "everything". It makes them ideal replacement for ugly functors that had to be used in C++ before.

Summing it all together, parallelized version of my algorithm is not much more complicated than the serial version:

void SaveToTexture(const D3DLOCKED_RECT &lockedRect)
{
  tbb::parallel_for(
    tbb::blocked_range<uint>(0, TEXTURE_SIZEY),
    [this, &lockedRect](const tbb::blocked_range<uint> &range)
  {
    uint x, y;
    char *rowPtr = (char*)lockedRect.pBits + lockedRect.Pitch * range.begin();
    for (y = range.begin(); y != range.end(); ++y)
    {
      XMCOLOR *pixelPtr = (XMCOLOR*)rowPtr;
      for (x = 0; x < TEXTURE_SIZEX; ++x)
      {
        *pixelPtr = CalcColorForPixel(x, y);
        ++pixelPtr;
      }
      rowPtr += lockedRect.Pitch;
    }
  } );
}

This simple change made all my 4 CPU cores busy for 90+% and gave almost 4x speedup in terms of frame time, which is good result. So as you can see, coding parallel applications is not necessarily difficult :)

Comments (10) | Tags: libraries rendering c++ | Author: Adam Sawicki | Share

19:31
Fri
20
Aug 2010

Demo for RiverWash 2010

I code a demo for RiverWash 2010 demoscene party. I had a lot of doubt on this, but the more is done, the more I feel convinced I'm going to show it on the party, despite I know it's not of the highest quality, both because no super-advanced technology is involved and because I made all the graphics myself :) It will be my first demo made for such competition. All in all I'm more from Warsztat than demoscene community. Still I like the scenish atmosphere and after each big party, like Assembly or Breakpoint, I download and watch the productions. I've also been at the RiverWash last year and I had a lot of fun there.

I probably shouldn't show my demo before the party, so I'll do something inspired by what my former employers did on the website of their new game development studio - 11 bit studios - I'll show only small parts of some screenshots :)

Comments (2) | Tags: rendering events demoscene productions | Author: Adam Sawicki | Share

18:03
Mon
02
Aug 2010

SIGGRAPH 2010

On July 25-29th the 37th SIGGRAPH 2010 conference took place, subtitled "The People Behind the Pixels" this year. As always, many interesting papers were presented, varying from purely scientific, not-so-practical and working with SIGGRAPH-Interative-Frame-Rate (TM) :) through advances in movies and photography technology till what's most interesting for me - the real-time, hardware-accelerated graphics rendering related to game development. Now, after the event, many papers and slides are available for free to download. If you are interested, here are some links:

There is also a very active #siggraph hashtag on Twitter.

Comments (0) | Tags: rendering events | Author: Adam Sawicki | Share

23:24
Wed
14
Jul 2010

Music Analysis - Spectrogram

I've started learning about sound analysis. I have some deficiencies in education when it comes to digital signal processing (greetings for the professor who taught this subject at our university ;) but Wikipedia comes to the rescue. As a starting point, here is a spectrogram I've made from one of my recent favourite songs: Sarge Devant feat. Emma Hewitt - Take Me With You.

Now I'm going to exaplain in details how I've done this by showing some C++ code. First I had to figure out how to decode an MP3, OGG or other compressed sound format. FMOD is my favourite sound library and I knew it can play many file formats. It took me some time though to find functions for fast decoding uncompressed PCM data from a song without actually playing it for all 3 minutes. I've found on the FMOD forum that Sound::seekData and Sound::readData can do the job. Finally I've finished with this code (all code shown here is stripped from error checking which I actually do everywhere):

Read full entry > | Comments (5) | Tags: math libraries music rendering dsp algorithms | Author: Adam Sawicki | Share

Pages: > 1 2 3 4 5 ... 20 >

STAT NO AD [Stat] [Admin] [STAT NO AD] [pub] [Mirror] Copyright © 2004-2017 Adam Sawicki
Copyright © 2004-2017 Adam Sawicki