Posts

Forward, Deferred and Raytracing rendering in openFrameworks and web

Image
Currently, there are three basic approaches for rendering computer graphics scenes: forward rendering, deferred rendering, raytracing. 1. Forward rendering It's that OpenGL does, you create vertices, then make faces, and rasterize them, all passing via shaders. The rendering process is straightworward and fast, but rendering shadows, making degree-of-field effect and drawing clouds is hard here. Most of openFrameworks examples use forward rendering. 2. Deferred rendering It's the same as forward rendering, but additionally some extra data is rendered for computing shadows. The lighting pass is performed after the scene geometry is drawn. See detailed explanation here: https://learnopengl.com/Advanced-Lighting/Deferred-Shading Shadows, degree-of-field effect and some kind of reflections are possible with deferred rendering. Unreal Engine 4 and Unity currently (2019) are based on deferred rendering. openFrameworks: https://github.com/perevalovds...

Random number generator on GPU

Pseudo-random number generation on CPU is simple - we need to store previous state and use it to generate the next number. On a GPU, it's need more attention, we need to generate random numbers in each instance independently. That is, we need to keep for each instance a state, randomly initialized on CPU, and then update the state, using corresponding functions. The good explanation and particular implementation of good random generator is discussed here: https://math.stackexchange.com/questions/337782/pseudo-random-number-generation-on-the-gpu#answers-header It's about method explained in NVidia's book "GPU Gems", Chapter 37. Efficient Random Number Generation and Application Using CUDA https://developer.nvidia.com/gpugems/GPUGems3/gpugems3_ch37.html So, here is my code: //-------------------------------------------------------------------------- //Random generator for GPU //Based on //https://math.stackexchange.com/questions/337782/pse...

Create Blueprint Library, print to log and using windows.h in a Unreal Engine C++ project

When you need to use some specific Windows API functions, such as LoadLibrary for loading DLL, or CreateFileMapping for using shared memory, you need to use #include <windows.h> in your project's C++ code. To be sure all building successfully, please insert this with "WIN32_LEAN_AND_MEAN" define, and use it as early as possible (I use it as the second #include in CPP code): #include "MyBlueprintFunctionLibrary.h" #define WIN32_LEAN_AND_MEAN #include <windows.h> For advanced capabilites, such as using GEngine class, it's required to include "Engine.h" . It's required to do before including <windows.h> : #include "MyBlueprintFunctionLibrary.h" #include "Engine.h" #define WIN32_LEAN_AND_MEAN #include <windows.h> Here is example how to load DLL using Windows API in Blueprint Function Library C++ class. Also, it demonstrates print to screen and to log in Unreal Engine C+...

Using compute shaders for reading/writing to textures and FBO

To see how to use compute shaders for pixelwise working with images, see openFrameworks example examples/gl/computeShaderTextureExample . Also, I modified this example on using FBO (ofFbo) instead textures (ofTexture). As I checked, that FBO's textures (fbo.getTextureReference) also works with shader for reading and writing. So, FBOs can be used in compute shaders as an universal "drawable" textures: it's possible to read and write to them pixelwise, and it's possible to draw in them using OpenGL rendering (fbo.begin()...fbo.end()) Additional links: https://www.khronos.org/opengl/wiki/Image_Load_Store https://www.khronos.org/opengl/wiki/Buffer_Object#Buffer_Object_Usage

Creating particles with compute shaders in openFrameworks

In openFrameworks it's easy to create particles system with compute shaders, that is, particles physics is updates using GPU. So, you may manipulate millions particles realtime, compared with hundred thousand using CPU. The example to start with is examples/gl/computeShaderParticlesExample . It's based on declaring Particle structure - just like normal CPU particle definition with position, speed and color, and defining array vector<Particle> particles; which holds all particles. But, the next big step is using ofBufferObject which maps this vector to GPU memory. particlesBuffer.allocate(particles,GL_DYNAMIC_DRAW); ( About GL_DYNAMIC_DRAW - see details here ) particlesBuffer2 is similar and is used as previous particles state during computations, and is filled with command particlesBuffer.copyTo(particlesBuffer2); The position and color is used in ofVbo vbo object vbo.setVertexBuffer(particlesBuffer,4,sizeof(Particle)); vbo.setColorBuffer(particl...

Raymarching - a way to liberation of generative graphics

Image
I published this article on openFrameworks forum also. Recently I was so fascinating to work with raymarching technology in oF, that I decide to write this detailed post about it. Such technology allows to render non-rigid objects such as clouds, create dynamic and truly geneative volumes without meshes, and, additionally, very simply render in in VR or 360-degrees panoramic images. It was not so fast to collect all information, so I sure it will be useful for somebody desired to work with in in a modern oF/GLSL. We (art-duet Endless Attractions Museum ) recently released VR art project "Night sleep was taken from my eyes, V.2". In the project we are thinking, how are our memories formed? Is it possible to visualize the amnesia process? What will the storage look like in the future? Wearing the VR HMD, a viewer found himself flying in a cloudy space. Some clouds contain inside transformed panoramic photos illustrating one day in the life of...

Creating panoramic images with raymarching

If we have raymarch shader, it's easy to create a panoramic image with it. Here I will explain how to do it by modifying a GLSL fragment shader which performs raymarching (raycasting, raytracing). 1) In the vertex shader, generate normalized 2D coordinates  pos_normalized at [-1,1]x[-1,1] of the rendered pixel: //Declare variable which will be interpolated and passed into fragment shader     out vec2  pos_normalized; //position [-1,1]x[-1,1] //Inside main() shader's function:     gl_Position = modelViewProjectionMatrix * position; //... some normal code     vec2 pos_normalized = gl_Position.xy/gl_Position.w; 2) In the fragment shader, add uniform for passing center of panoram and also optional rotation of panoram horizontally in degrees; and of course pos_normalized input:     uniform vec3 head_position = vec3(0,0,0);     uniform float panoramic_render_angle...