# How to convert this geometry opengl code to three.js?

**URL:** <https://discourse.threejs.org/t/how-to-convert-this-geometry-opengl-code-to-three-js/5708>\
**Category:** Questions\
**Tags:** geometry\
**Created:** [January 14, 2019, 12:37am UTC](https://discourse.threejs.org/t/how-to-convert-this-geometry-opengl-code-to-three-js/5708 "2019-01-14T00:37:16Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Tintinabulator\_Zea](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.threejs.org/tintinabulator_zea/32/4949_2.png) [@Tintinabulator\_Zea](https://discourse.threejs.org/u/Tintinabulator_Zea)\
**Post date:** [January 14, 2019, 12:37am UTC](https://discourse.threejs.org/t/how-to-convert-this-geometry-opengl-code-to-three-js/5708/1 "2019-01-14T00:37:16Z")

</div>

Hi there, I am trying to follow this tutorial, where they show how to make a bending cylinder in C and opengl (at Edit2)

> <https://stackoverflow.com/questions/54029401/how-to-bend-a-cylinder-in-three-js>

Would anyone who knows C be willing to turn this into a three.js codepen? Or explain how to do it in three.js

---

<div class="post-metadata">

**Author:** ![marquizzo](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.threejs.org/marquizzo/32/25192_2.png) [@marquizzo](https://discourse.threejs.org/u/marquizzo)\
**Post date:** [January 14, 2019, 3:21am UTC](https://discourse.threejs.org/t/how-to-convert-this-geometry-opengl-code-to-three-js/5708/2 "2019-01-14T03:21:04Z")

</div>

@Tintinabulator_Zea out of curiosity, is this like a college homework assignment or something? I think I’ve seen @prisoner849 answering this same question about three times in the last few weeks in StackOverflow.

---

<div class="post-metadata">

**Author:** ![Tintinabulator\_Zea](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.threejs.org/tintinabulator_zea/32/4949_2.png) [@Tintinabulator\_Zea](https://discourse.threejs.org/u/Tintinabulator_Zea)\
**Post date:** [January 14, 2019, 6:39am UTC](https://discourse.threejs.org/t/how-to-convert-this-geometry-opengl-code-to-three-js/5708/3 "2019-01-14T06:39:56Z")

</div>

@marquizzo it’s not homework, i’m not in school. It’s for a tool i’m working on, but i am stuck with some of the math language.

---

<div class="post-metadata">

**Author:** ![prisoner849](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.threejs.org/prisoner849/32/535_2.png) [@prisoner849](https://discourse.threejs.org/u/prisoner849)\
**Post date:** [January 14, 2019, 6:43am UTC](https://discourse.threejs.org/t/how-to-convert-this-geometry-opengl-code-to-three-js/5708/4 "2019-01-14T06:43:42Z")

</div>

@Tintinabulator_Zea will you work with such tubular-like geometries only?

---

<div class="post-metadata">

**Author:** ![Tintinabulator\_Zea](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.threejs.org/tintinabulator_zea/32/4949_2.png) [@Tintinabulator\_Zea](https://discourse.threejs.org/u/Tintinabulator_Zea)\
**Post date:** [January 14, 2019, 6:58am UTC](https://discourse.threejs.org/t/how-to-convert-this-geometry-opengl-code-to-three-js/5708/5 "2019-01-14T06:58:39Z")

</div>

@prisoner849 yes only tubular geometries:)

---

<div class="post-metadata">

**Author:** ![prisoner849](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.threejs.org/prisoner849/32/535_2.png) [@prisoner849](https://discourse.threejs.org/u/prisoner849)\
**Post date:** [January 14, 2019, 8:49am UTC](https://discourse.threejs.org/t/how-to-convert-this-geometry-opengl-code-to-three-js/5708/6 "2019-01-14T08:49:56Z")

</div>

@Tintinabulator_Zea Another idea from the twilight zone of my mind is: maybe an easier way is just to use `THREE.TubeBufferGeometry()` then? With some modifications, of course 🙂

> <https://github.com/mrdoob/three.js/blob/70438028775dd4d539ebdfdaf1aafd6fbcac43c7/src/geometries/TubeGeometry.js#L168>

Here, `radius` should be depending on the function (sin, cos etc.) of an angle, which is `var v = j / radialSegments * Math.PI * 2;` (see it above in the source code)

---

<div class="post-metadata">

**Author:** ![prisoner849](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.threejs.org/prisoner849/32/535_2.png) [@prisoner849](https://discourse.threejs.org/u/prisoner849)\
**Post date:** [January 14, 2019, 1:40pm UTC](https://discourse.threejs.org/t/how-to-convert-this-geometry-opengl-code-to-three-js/5708/7 "2019-01-14T13:40:10Z")

</div>

I went the path of less resistance 😄

Just created a tube geometry and used its source code as a base to distort it (with some modifications and simplifications) 🙂

https://codepen.io/prisoner849/embed/preview/qLLwWK?height=300&slug-hash=qLLwWK&default-tabs=js,result&host=https://codepen.io

All the magic is in these lines:

```javascript
      var radius = geometry.parameters.radius;
      radius += Math.abs(Math.sin(v * 2.5)); // radial half-waves
      radius += Math.sin(pointAt * Math.PI * 4); // wave along the path
      normal.applyAxisAngle(geometry.tangents[i], pointAt * Math.PI * 2); // twisting

```
