YASARA View—molecular graphics for all devices—from smartphones to workstations

Summary: Today's graphics processing units (GPUs) compose the scene from individual triangles. As about 320 triangles are needed to approximate a single sphere—an atom—in a convincing way, visualizing larger proteins with atomic details requires tens of millions of triangles, far too many for smooth interactive frame rates. We describe a new approach to solve this ‘molecular graphics problem’, which shares the work between GPU and multiple CPU cores, generates high-quality results with perfectly round spheres, shadows and ambient lighting and requires only OpenGL 1.0 functionality, without any pixel shader Z-buffer access (a feature which is missing in most mobile devices). Availability and implementation: YASARA View, a molecular modeling program built around the visualization algorithm described here, is freely available (including commercial use) for Linux, MacOS, Windows and Android (Intel) from www.YASARA.org. Contact: elmar@yasara.org Supplementary information: Supplementary data are available at Bioinformatics online.

1.1.2.1 Loop over the three different atom shifts along the X axis: The atom is now raytraced three times, horizontally shifted by 0, -s and +s. The first zero shift is used normally, and the latter two shifts are used for stereoscopic 3D graphics, where the images for left and right eye need different sphere graphics, so that spheres appear plastic and not flat.
1.1.2.1.1 Run POVRay to trace the sphere with color yellow and size 256x256 pixels, given the current light-source position and horizontal camera shift. For 4K and Apple retina displays, the sphere size should be increased to 512x512 pixels, and the steps below adjusted. 1.1.2.1.5.2 Find the first non-empty pixel from the right and set all empty pixels on the right to its color. The last two steps make sure that the background color is removed from the sphere bitmap and does not accidentally appear during rendering with OpenGL.
1.1.2.1.6 To save space, compress the sphere bitmap from 24-bit true-color to an 8-bit intensity (considering the red and blue color channels only, the green channel equals the red, since the sphere was ray-traced in yellow).
1.1.2.1.7 Compress the 8-bit intensity further with Zlib 1.1.2.1.8 Store the Zlib compressed 256x256 8-bit intensity bitmap in file 'sphere_styleS.dat' 2 On application startup: set the default parameters for the graphics engine, for example by loading them from an *.ini file that stores the user's preferences 2.1 ply_lightdir -The direction (unit-)vector from the origin to the light-source for all PoLYgons.
2.2 atv_size -ATom Visualization Size -The size of atoms when they touch (and are clipped by) the view plane, in percent of the maximum size supported by the graphics engine [0..1].
Changing this size is equivalent to moving the near clipping plane.  Figure 1B), where the precalculated sphere images will be stored (or 2048*2048 for high-res screens). If stereoscopic 3D is enabled, allocate another texture to store sphere images as seen from the other eye (atv_viewtex2).
4.4 Create a table 'defradtab' of defined sphere radii (in pixels) that will be present in atv_viewtex ( Figure 1B), we use [1,2,3,4,5,6,7,8,9,10,11,12,14,16,18,20,22,24,26,28,30,32,64,127], so the largest atom that can be displayed (without zooming the texture and reducing the resolution) has a radius of 127 pixels. , the number of standard colors available, we use ATV_COLORS=7: blue, magenta, red, yellow, green, cyan and grey (Fig.1B). Atoms with non-standard colors can be created by blending two standard colors using multi-texturing and a variable blending factor. 4.6 Loop over the sphere radii r in defradtab: 4.6.1 Calculate the alpha mask for a filled circle of radius r (which will later be filled with the 2D image of the atom sphere). This is just a bitmap of 8-bit values, 2r*2r pixels large, with values indicating the fraction of the pixel covered by the filled circle (ranging from 0 (pixel is totally outside circle) to 255 (pixel is totally inside circle)). 4.6.2 Loop over the ATV_COLORS, and for each find the next empty spot in texture atv_viewtex, copy the alpha mask to the texture's alpha channel at this spot (and also to atv_viewtex2 if present). 4.10 Precalculate the atom densities for ambient lighting. A very low-res 8-bit 3D density grid (2.62 Å spacing) of the scene will be created to determine the amount of light reaching each atom from the six main directions (+X,-X,+Y,-Y,+Z,-Z). Densities to fill this 3D grid are precalculated for the three atom styles 'space filling', 'balls&sticks' and 'sticks', with radii of 1.2 Å, 0.95 Å and 0.85 Å, respectively (chosen empirically to give a convincing visual result).

Define ATV_COLORS
Since atoms are thus smaller than the grid spacing of 2.62 Å, the density needs to be added with 'sub-grid-cell' accuracy. We define this 'sub-grid-cell' accuracy with 2 bits, i.e. four slightly shifted densities along each axis are precalculated for each of the three atom styles, yielding 4*4*4*3 = 192 different atom densities (the atom density index 'ADIdx' can then be calculated from the atom coordinate and atom style using simple shifts and logical operations as described in detail later). The actual density values are obtained by calculating the fraction of the grid cell that is inside the atom and multiplying the result with 112. Since an atom with radius 1.2 Å and volume 7.24 Å 3 occupies at most 7.24/2.62 3 = 40% of a grid cell, the maximum density added is thus 112*0.4 = 45. So if more than 255/45 = ~5.7 atoms are present in a cell, the limit imposed by the 8-bit data-type is reached, the grid cell becomes completely light blocking. As described later, the densities will be integrated along the six main directions, finally ambient light reaching each atom will be interpolated from the integrated grid.
4.11 Precalculate the atom circle masks for shadow calculation. A low-res 16-bit Z-buffer of the scene (with a resolution of 0.164*0.164 Å per pixel) will be drawn as seen from the direction of the light-source ( Figure 1D). For each atom, one can then count the fraction of pixels with a Z-value smaller or equal than the atom's own Z-value, which is the fraction of light that reaches the atom. The shape of the atoms drawn in the Z-buffer is stored in three circle masks (16-bit bitmaps that contain 0 for pixels outside the circle and 0xffff for pixels inside the circle and are used as an AND mask when filling the Z-buffer (described later)). The sizes of the three circle masks are 15*15 pixels (used for space filling atoms), 11*11 pixels (for ball&sticks atoms), and 7*7 pixels (for atoms shown as sticks). So an 'X' corresponds to 0xffff, and a '.' to 0:  Figure 1D, with Z-buffer value 0 mapped to white, and 32767 mapped to black.   is not true, then the intersection curve has changed direction so much, that it can no longer be approximated by a straight line. We therefore need to emit four vertices to draw a part of the sphere ( Figure 1C, hydrogen 2), as described above for drawing a non- .GlobalPos.z))) and draw it, potentially with a text indicating the chemical element, which thus becomes visible when the atom is cut open.

5.12
If stereoscopic 3D is active, repeat the drawing procedure using the second texture atv_viewtex2 and horizonally shifted atom coordinates.