All Blog Entries

All blog entries, ordered from most recent. Entry count: 1077.

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

# Immediate Mode GUI - Theory and Example - Slides

21:06
Tue
24
Oct 2017

Today I gave a talk at Warsaw GameDev Meetup. Topic of my presentation was: "Immediate Mode GUI - Theory and Example". You can download slides here:

Comments | #gui #teaching #events Share

# Lost clicks and key presses on low FPS

13:34
Sun
22
Oct 2017

There is a problem with handling input from mouse and keyboard in games and other interactive applications that I just solved. I would like to share my code for the solution. When your app uses a loop that constantly calculates and renders frames, like games usually do, it may seem natural to just read current state of every mouse and keyboard key (whether it's down or up) on each frame. You may then caculate derived information, like whether a button has just been pressed on released, by comparing new state to the state from previous frame. This is how Dear ImGui library works. So first solution could look like this:

void UpdateFrame()
{
    // Fill ImGui::GetIO().DeltaTime, KeyCtrl, KeyShift, KeyAlt etc.
    ImGui::GetIO().MouseDown[0] = (GetKeyState(VK_LBUTTON) & 0x8000) != 0;
    ImGui::GetIO().MouseDown[1] = (GetKeyState(VK_RBUTTON) & 0x8000) != 0;
    ImGui::GetIO().MouseDown[2] = (GetKeyState(VK_MBUTTON) & 0x8000) != 0;
    for(uint32_t i = 0; i < 512; ++i)
        ImGui::GetIO().KeysDown[i] = (GetKeyState(i) & 0x8000) != 0;
    
    ImGui::NewFrame();
    
    if(ImGui::IsKeyPressed('A'))
        // Do something...
}

There is one problem with this approach. If user presses and releases a key for a very short time, so that both press and release happens between two frame, it will go unnoticed. This is very annoying. It happens especially when:

First step towards solving this is to react to "real" events that are sent by the operating system:

Read full entry > | Comments | #gui #winapi #windows Share

# VK_KHR_dedicated_allocation unofficial manual

22:36
Mon
16
Oct 2017

I wrote a short article that explains how to use Vulkan extenion "VK_KHR_dedicated_allocation". It may be interesting to you if you are a programmer and you code graphics using Vulkan.

Go to article: VK_KHR_dedicated_allocation unofficial manual

Comments | #graphics #vulkan Share

# How to check if an integer number is a power of 10?

23:30
Wed
04
Oct 2017

I was recently asked a test question to write a function that would check whether a given integer number is a power of ten. I didn't know the answer immediately because I never solved this puzzle before, so I needed to think about a solution. I quickly came to a conclusion that there are many possible solutions, so I later decided to write them down and share with you.

Number is a power of 10 if it's equal to 10, 100, 1000 etc. 1 is also 0-th power of 10. Other numbers like 2, 3, 11, 12 etc. are not powers of 10. 0 and all negative numbers should also be rejected.

Below you can find 5 different solutions - all of them correct, but some of them more efficient than others. Example code is in C, but you can easily implement the same algorithms in Java or any other programming language.

First thing that came to my mind was to convert the number to a string and check whether it contains '1' followed by '0'-s.

int IsLog10_v1(int32_t x)
{
// Convert x to string.
char buf[12];
itoa(x, buf, 10);
const size_t bufLen = strlen(buf);

// Check if string contains '1' followed by '0'-s.
if(buf[0] != '1')
return 0;
for(size_t i = 1; i < bufLen; ++i)
{
if(buf[i] != '0')
return 0;
}
return 1;
}

Another option is to convert the number to floating-point format, use "real" log10 function, and check if the result is an integer. Note that double must be used here, because float has too small precision and would incorrectly return 1 for inputs like: 999999999, 1000000001.

int IsLog10_v2(int32_t x)
{
// Convert x to double. Calculate log10 of it.
double x_d = (double)x;
double x_log10 = log10(x_d);
// Check if result is integer number - has zero fractional part.
return x_log10 - floor(x_log10) == 0.0;
}

If we want to operate on integer numbers only, we may come up with a recursive algorithm that checks whether the number is greater than zero, divisible by 10 and then calls the same function for x/10.

int IsLog10_v3(int32_t x)
{
if(x <= 0)
return 0;
if(x == 1)
return 1;
if(x % 10 != 0)
return 0;
// Recursion.
return IsLog10_v3(x / 10);
}

Because it's a tail recursion, it's easy to convert it to iterative algorithm (use loop instead of recursive call).

int IsLog10_v4(int32_t x)
{
if(x <= 0)
return 0;
// Same algorithm as v3, but converted from recursion to loop.
for(;;)
{
if(x == 1)
return 1;
if(x % 10 != 0)
return 0;
x /= 10;
}
}

Finally, the most efficient solution is to notice that there are only few possible powers of 10 in the range of 32-bit integers, so we can just enumerate them all.

int IsLog10_v5(int32_t x)
{
// Just enumerate all possible results.
return
x == 1 ||
x == 10 ||
x == 100 ||
x == 1000 ||
x == 10000 ||
x == 100000 ||
x == 1000000 ||
x == 10000000 ||
x == 100000000 ||
x == 1000000000;
}

You can find full source code as a simple, complete C program together with tests on my GitHub, as file: IsLog10.c.

Comments | #c #algorithms Share

# Vulkan Memory Allocator 2.0.0-alpha.3

13:42
Wed
13
Sep 2017

I just published new version of Vulkan Memory Allocator 2.0.0-alpha.3. I'm quite happy with the quality of this code. Documentation is also updated, so if nothing else, please just go see User guide. I still marked it as "alpha" because I would like to ask for feedback and I may still change everything.

I would like to discuss proposed terminology. Naming things in code is a hard problem in general, and especially as English is not my native language, so please fill free to contact me and propose more elegant names to what I called: allocator, allocation, pool, block, stats, free range, used/unused bytes, own memory, persistently mapped memory, pointer to mapped data, lost allocation (becoming lost, making other lost), defragmentation, and used internally: suballocation, block vector.

Comments | #vulkan #productions #libraries #graphics Share

# What is Samsung phone doing to photos?! (sharpening a lot)

23:15
Mon
11
Sep 2017

I now use Samsung Galaxy S7 smartphone and I'm quite happy with it, except for the camera. I noticed that all the photos taken with it look bad. There is clearly something wrong with them. When I zoomed in, I noticed that the device applies insane amount of sharpening. Every photo looks like it was first filtered by bilateral filter (a kind of edge-preserving blur that is used for noise reduction) and then sharpening with intensity set to maximum, which causes annoying ringing artifacts around the edges.

I decided to make an experiment. I gathered all the devices I had access to that can take photos and I brought them to a place where I could photograph a building that has many sharp edges, plus some tram cables. It was the middle of a sunny day, so lighting brightness and contrast was high and devices didn't have a reason to apply too much processing to the photos taken. I configured all of them to fully automatic mode, maximum resolution and JPEG as output format (except Canon camera, where I forgot about it, so I actually made CR2 RAW that I later converted to JPEG). Devices I used for comparison were (click on each link to access original photo file):

When you zoom in to the building, you can clearly see that both Samsung phones applied very strong sharpening. It turns out this is a known problem. There is a discussion on Reddit, as well as YouTube video about it. Sony phone and DSLR don't have this effect.

Samsung Galaxy S6:

Samsung Galaxy S7:

Sony Xperia Z2:

Canon PowerShot G7X Mark II:

Nikon D90:

What's interesting is that the Canon camera also applied some sharpening, and did it even in RAW! (How can they call it RAW then?!) Fortunately in this camera it can be disabled: While in photo shooting mode, press MENU button, go to tab 6, select "Picture Style" and set it to "Neutral", so that the first parameter in the sequence of numbers (meaning "Sharpness Strength") is 0.

In Samsung phones this filter cannot be disabled :( The only way to take pictures without it is to use RAW, where it's not applied. To do it, while in photo shooting mode: swipe left, choose "Professional", enter configuration, select "Image size" and there enable "Save RAW and JPEG files". You need to enter "Professional" mode every time you want to take a photo. Then of course you need to process the image on your PC and convert it to JPEG, e.g. in Adobe Lightroom or other similar program, but there you can decide how much sharpening do you need (or none).

Comments | #photography #hardware Share

# Changes on My Website

08:29
Mon
28
Aug 2017

I've dedicated my free time last week to improving this website. I made it myself from scratch more than 10 years ago, using some old version of PHP, MySQL, and HTML 4.01, so it definitely needed some refresh. I introduced many changes to the code, most notably:

There are still things to do. The website, as well as database, still uses ISO-8859-2 codepage instead of UTF-8 (it was a standard for encoding Polish characters before Unicode became popular). The scripts still generate the page using lots of print()-s instead of some template system. But maybe I will fix that next time :)

Comments | #webdev Share

# How to Restrict Access to Apache Server to Local Machine?

21:12
Tue
22
Aug 2017

I wanted to do some web development locally, so I installed Apache 2.2, PHP, and MySQL on my Windows 10 machine. When configuring it, I wanted to restrict access to the Apache server to two machines only - local one and another one in my local network.

The way to do it is to enable and use mod_authz_host module. In file C:\Apache2\conf\httpd.conf I needed to make sure that following line is not commented:

LoadModule authz_host_module modules/mod_authz_host.so

Then I could add appropriate directives to <Directory ...> section of this file, or alternatively use them in .htaccess file located next to files of my website.

To deny access from all addresses except my two computers, I started from this:

Order deny,allow
Deny from all
Allow from 192.168.0.21
Allow from 192.168.0.23

After restarting Apache (needed to apply any changes in configuration), I found out that I could access my website from the other computer, but not from the local one. I quickly recalled that connections to the same machine go through special loopback interface and use special address: localhost, which has IP 127.0.0.1. So I changed my configuration to this:

Order deny,allow
Deny from all
Allow from 192.168.0.21
Allow from 192.168.0.23
Allow from 127.0.0.1

It didn't work either. That's when I started to search for address where the local connection comes from, using Process Hacker - Network tab, as well as Apache log in file C:\Apache2\logs\access.log. What I found out is that the loopback connection uses IPv6, where address of localhost is: "::1" - however strange it may seem. Explanation of this format can be found here: IPv6 at Wikipedia.

Apache accepts this form of address, so following configuration finally allowed me to connect from my local computer, as well as the other computer from my network:

Order deny,allow
Deny from all
Allow from 192.168.0.21
Allow from 192.168.0.23
Allow from 127.0.0.1
Allow from ::1

Comments | #webdev #networking Share

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

STAT NO AD
[Stat] [STAT NO AD] [Download] [Dropbox] [pub] [Mirror]
Copyright © 2004-2018