SHARE
TWEET

Untitled

a guest Dec 19th, 2018 65 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. GeometryData Geometry::createCylinderGeometry(unsigned int segments, float height, float radius)
  2. {
  3.     GeometryData data;
  4.  
  5.     // center vertices
  6.     data.positions.push_back(glm::vec3(0, -height / 2.0f, 0));
  7.     data.normals.push_back(glm::vec3(0.0f, -1, 0.0f));
  8.  
  9.     data.positions.push_back(glm::vec3(0, height / 2.0f, 0));
  10.     data.normals.push_back(glm::vec3(0.0f, 1, 0.0f));
  11.    
  12.     // circle top and bottom
  13.     float angle_step = 2.0f * glm::pi<float>() / float(segments);
  14.     //side faces
  15.     for (unsigned int i = 0; i < segments; i++) {
  16.         glm::vec3 circlePos = glm::vec3(
  17.             glm::cos(i * angle_step) * radius,
  18.             -height / 2.0f,
  19.             glm::sin(i * angle_step) * radius
  20.         );
  21.         // bottom ring vertex
  22.         data.positions.push_back(circlePos);
  23.  
  24.         // top ring vertex
  25.         circlePos.y = height / 2.0f;
  26.         data.positions.push_back(circlePos);
  27.  
  28.         //needs to be calculated from the circle -> y = 0
  29.         circlePos.y = 0;
  30.         data.normals.push_back(glm::normalize(circlePos));
  31.         data.normals.push_back(glm::normalize(circlePos));
  32.  
  33.         data.indices.push_back(2 + i * 2);
  34.         data.indices.push_back(i == segments - 1 ? 3 : 3 + (i + 1) * 2);
  35.         data.indices.push_back(i == segments - 1 ? 2 : 2 + (i + 1) * 2);
  36.  
  37.         data.indices.push_back(i == segments - 1 ? 3 : 3 + (i + 1) * 2);
  38.         data.indices.push_back(2 + i * 2);
  39.         data.indices.push_back(3 + i * 2);
  40.  
  41.     }
  42.  
  43.     for (unsigned int i = 0; i < segments; i++) {
  44.         glm::vec3 circlePos = glm::vec3(
  45.             glm::cos(i * angle_step) * radius,
  46.             -height / 2.0f,
  47.             glm::sin(i * angle_step) * radius
  48.         );
  49.        
  50.         // bottom ring vertex
  51.         data.positions.push_back(circlePos);
  52.         data.normals.push_back(glm::vec3(0.0f, -1, 0.0f));
  53.  
  54.         // top ring vertex
  55.         circlePos.y = height / 2.0f;
  56.         data.positions.push_back(circlePos);
  57.         data.normals.push_back(glm::vec3(0.0f, 1, 0.0f));
  58.        
  59.         int offset = segments*2;
  60.         // bottom face
  61.         data.indices.push_back(0);
  62.         data.indices.push_back(2+ offset + i * 2);
  63.         data.indices.push_back(i == segments - 1 ? 2+ offset : 2+ offset + (i + 1) * 2);
  64.  
  65.         // top face
  66.         data.indices.push_back(1);
  67.         data.indices.push_back(i == segments - 1 ? 3+ offset : 3+ offset + (i + 1) * 2);
  68.         data.indices.push_back(3+ offset + i * 2);
  69.     }
  70.  
  71.     return std::move(data);
  72. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top