I’ve recently started a course on Coursera ( Database and Algorithms , by Princeton U, a brilliant course btw). Now quite a few times , those coding sessions can be quite strenuous. My way of letting off some steam is coding something or the other on OpenGL.

The best part about coding in 3D is the fact that you can visualize the results of your code ( which is a motivation) .

3D Programming is a powerful visualization tool. Coding the structure of DNA is an intriguing and challenging task . Challenging because of the limitations posed by OpenGL in the code. The mathematical aspects of the structure are fairly simple I believe.

**About the Code and the DNA Structure :**

- DNA is a double helix , with strips connecting both the strands at different angular intervals. I’ve used GL_QUADS for drawing the strands and strips both. For the QUADS the coordinates are obtained using :

x1 = 40*cos(angle*GL_PI/180); z1 = 40*sin(angle*GL_PI/180); x2 = 40*cos((angle+10)*GL_PI/180); z2 = 40*sin((angle+10)*GL_PI/180);

The vertices are input as (10 is the width of the strand) —

glVertex3f(x1 , yOld1 , z1); glVertex3f(x1 , yOld1+10 , z1); glVertex3f(x2 , yNew1+10 , z2); glVertex3f(x2 , yNew1 , z2);

Where the relationship b/w yOld1 and yNew1 is as follows :

`yNew1 = yOld1+3;`

2. For the strips connecting the 2 strands , I’ve used an array of a C++ struct , which stores the x , y , z values of the corresponding points on both the strands to be connected using GL_QUADS. Then it’s a simple “for” loop to connect them both.

struct strand { float x ; float y; float z; };

I’ve taken the angles to be connected to be +60 and -60 and +120 and -120 respectively. Thus whenever the angle traversed is 60 or 60+360n on the first strand , or 120+360n on the second strand , I’ve incremented my struct arrays with the corresponding values of x, y ,z.

After that a simple for loop connecting the x,y,z with GL_QUADS does the job.

If you’re wondering about the colour/lighting , it’s the materials assigned to the strands and the strips (GL_DIFFUSE).

Here are a few screenshots :

The strips can always be increased by decreasing the angles by a factor of “n” , where n is the number of strips.

Had a lot of fun with this. I think I’ll be trying to make the same DNA with Spheres (to denote the actual molecules) some day.

The entire code coming up on GitHub soon !

*Update — The code is up ! The link is :* https://github.com/soumitrasaxena/DNA