Author Archives: Sergey K.

OpenGL: storing index/vertex data together inside a single buffer

With modern OpenGL, it is possible to store index and vertex data together in a single buffer. Here is a short code snipped showing how to do this (Direct State Access for OpenGL 4.5+).

Lets assume our data is initially loaded in these two vectors.

vector<vec3> positions;
vector<unsigned int> indices;
...

First, let’s create a new buffer and upload everything into it.

const size_t sizeI = sizeof(unsigned int) * indices.size();
const size_t sizeV = sizeof(vec3) * positions.size();

GLuint buf;
glCreateBuffers(1, &buf);
glNamedBufferStorage(buf, sizeI + sizeV, nullptr, GL_DYNAMIC_STORAGE_BIT);
glNamedBufferSubData(buf, 0, sizeI, indices.data());
glNamedBufferSubData(buf, sizeI, sizeV, positions.data());

Create and set up a new VAO.

GLuint vao;
glCreateVertexArrays( 1, &VAO);

Now we can tell OpenGL that our indices should be read from the same buffer and the offset of our vertex data from the beginning of the buffer is equal to sizeI.

glVertexArrayElementBuffer(vao, buf);
glVertexArrayVertexBuffer(vao, 0, buf, sizeI, sizeof(vec3));
glEnableVertexArrayAttrib(vao, 0);
glVertexArrayAttribFormat(vao, 0, 3, GL_FLOAT, GL_FALSE, 0);
glVertexArrayAttribBinding(vao, 0, 0);

That’s it. Rendering with glDrawElements(GL_TRIANGLES, indices.size(), GL_UNSIGNED_INT, nullptr); just works as expected.

Dreaming of an image loading/saving/manipulation library

For decades I was using FreeImage as a “backend” images loading/saving library which I wrapped into my own Bitmap class providing additional image manipulation functionality on top: convert colors, blit, resize, convolve, move channels around, cut & paste, and so on and so forth. The Bitmap class has been evolving for almost 15 years to become what it is now, a true abomination combining different coding styles and looking like a patched stovepipe with numerous ugly fixes and hacks. While it is still doing its job, it is becoming more and more difficult to support and maintain it. A full-featured clean image manipulation library would be really nice to have. Here is my features wish list for it.

  • Support 1D, 2D, 3D and Cube images. I’m doing graphics and using this class as a staging ground to upload textures to GPU. 1D image can be a particular case of 2D image with the height of 1, 2D image can be a case of 3D image with the depth of 1, and Cube image can be a 3D image with the depth equal to 6. Should be able to convert equirectangular projection to a cube image and back. Should be able to convert vertical/horizontal cross image to a cube image and back. Should be able to extract a 2D slice from a 3D image (and write it back as well).
  • Support different pixel formats. 8/16/32-bit unsigned/signed integer. 16/32-bit floating point. Arbitrary number of channels, R/RG/RGB/RGBA. Basic image operations like GetPixel, SetPixel, Resize, Flip, etc should work with all these basic formats. GetPixel should support bilinear and trilinear (for 3D textures) filtering. Cube image lookup should support seamless mode.
  • Some notion of channels semantics. All flavours of RGB-BGR, RGBA-BGRA, ARGB-RGBA, etc conversions. Different color spaces (CMYK, Lab) desirable but are not a show stopper.
  • Conversion from sRGB to linear color and back.
  • Support of some esoteric formats to store compressed image data and upload it later to GPU. ETC2/ETC2_EAC is a minimum here. Basic image operations can skip all work with these formats. That’s it, just storage.
  • Blending operations. Normal, Lighten, Darken, Multiply, Average, Add, Subtract, Difference, Negation, Screen, Exclusion, SoftLight, HardLight, VividLight, PinLight, LinearLight, HardMix, ColodDodge, ColorBurn, LinearDodge, LinearBurn, etc.
  • The library should not be too pessimized on performance if compiled with -O0 optimization (many C++14/17/20 libraries have this). Should be able to run the code in debug builds reasonably well.
  • Add/remove scanline stride.
  • Load and save different image formats. JPEG, PNG, HDR, KTX, RAW (uncompressed pixels, mostly for 3D textures) loading and saving are vital necessary. EXR loading is necessary as well, however, saving is optional.
  • Load and save from/to files and memory.
  • Move channels around. “Take the 2nd channel from this image and put it into the 1st channel of this one”.
  • Some rudimentary drawing operations: ClearColor, FillBox, MakeXorPattern, line drawing, etc.
  • Store mipmap levels (just having a NextImage pointer will do). Treat mipmaps well while loading and saving image formats which support them (KTX).
  • Calculate normal maps from heigh maps and height maps from normal maps.
  • Should have configurable dependencies. All supported image formats should be optional and configurable at will.
  • Support multiple image loading/saving backends switchable at compile time. For example, libpng/libjpeg vs stb_image.
  • Simple to compile. Two files would be ideal: .cpp and .hpp
  • Anyone?

Visual Studio 2015 and lambda parameters

Was writing this code in Visual Studio 2015. Note the ‘auto’ specifier in the lambda parameter list.

template <typename Container, typename Entity>
void Remove( Container& c, Entity e )
{
	auto Iter = std::remove_if(
		c.begin(), c.end(),
		[ e ]( const auto& Ent ){ return Ent == e; }
	);
	c.erase( Iter, c.end() );
}

Ended up with this code to stay compatible with Visual Studio 2013.

template <typename Container, typename Entity>
void Remove( Container& c, Entity e )
{
	auto Iter = std::remove_if(
		c.begin(), c.end(),
		[ e ]( const typename Container::value_type& Ent ) { return Ent == e; }
	);
	c.erase( Iter, c.end() );
}

Dual panel orthodox portable file manager: Wal Commander GitHub Edition

As a zealous Windows fan and developer, I use Far Manager to do all file managing and significant amount of coding. When it comes to Linux or OS X, I feel like a fish dragged ashore. That is it: no familiar UI and hotkeys to dig through my files and type my code. It is painful because I need to switch between Windows/Linux/OSX and cannot live on a single system. Somewhat over a year ago, I found a portable open-source file manager called Wal Commander, which was pretty close to what Far Manager is on Windows. I started using it and found some nice features missing, which I was able to add. The original author was very slow on development and integration of patches, so I decided to put up a GitHub repository and stockpile all my contributions there.

Since then, several people contributed to the project to improve it and make it usable on FreeBSD. The project has evolved too far from the original source code (working FreeBSD port, tons of new features, step-by-step refactoring and code improvement) and I renamed it to Wal Commander GitHub Edition.

Wal Commander GitHub Edition

Here is a complete list of new features in the current development branch (master-0.16.2):

  • Added: share search text between all search and search-n-replace dialogs
  • Added: indicator of the current sorting order in the panel
  • Added: taskbar hints for the Alt key
  • Added: option ‘Select folders’
  • Added: F3 calculates the size of multiple selected folders
  • Added: command line autocomplete
  • Added: save window size & position
  • Added: save path in panels
  • Hotket: Shift+Enter to start command without the terminal
  • Hotkey: Ctrl+Tab to switch between editor/viewer and panels
  • Hotkey: Ctrl+O in editor/viewer shows the terminal terminal
  • Hotkey: ‘5’ on the numpad calculates the size of the selected files
  • Hotkey: Ctrl+U swap panels
  • Hotkey: Ctrl+Del in the editor
  • Hotkey: Ctrl+BackSpace in the editor
  • Hotkey: Ctrl+[ and Ctrl+] paste a path from the left/right panel to the command line
  • Hotkey: Ctrl+Enter in Alt-search moved to the next file
  • Hotkey: Ctrl+G applies command to the selected files
  • Hotkey: Ctrl+Insert copies the current file name to the clipboard
  • Hotkey: Ctrl+Insert copies contents of the non-empty command line to the clipboard
  • Hotkey: Ctrl+Enter on .. pastes the current path into the command line
  • Hotkey: ESC clears the command line before toggling the panels
  • Hotkey: ESC cancels Alt+… panel search before toggling the panels
  • Hotkey: DEL deletes files
  • Hotkey: Ctrl+PgDown to enter directories
  • Hotkey: BackSpace to go up dir (configurable, off by default)
  • Improved: Alt-search editline repaints correctly
  • Improved: mouse wheel support
  • Improved: FreeBSD support
  • Improved: editor cursor is now Far-like and blinking
  • Improved: save per-file scrolling positions in the editor
  • Improved: inaccessible paths bring you to the root folder or to the drive selection dialog
  • Improved: panel footer text (less garbage, show free space)
  • Improved: show other panel’s full path in the drive selection dialog
  • Fixed: vertical lines in Full panel modes
  • Fixed: command line is now black
  • Fixed: numpad Enter
  • Fixed: cursor position in the panel after deleting files
  • Fixed: switching language while in Alt-search closes the search editline
  • Fixed: bug with the first letter in Alt-search
  • Fixed: save/restore panel paths on Windows
  • Fixed: save the configuration on exit
  • Fixed: don’t cancel Alt+… panel search on Shift, Alt, and Ctrl
  • Added CMake and Code::Blocks configs
  • Disabled most of the 3D UI
  • Disabled auto indent in the editor
  • Default font is LiberationMono-Regular 14pt
  • Default tab size is 3
  • Case sensitive sorting is disabled by default
  • Fast path selection on the other panel

Wal Commanger GitHub Edition - Autocomplete

The project is growing and looking for volunteers to establish a working OS X port.

https://github.com/corporateshark/WalCommander