Advertisement
Guest User

Cylinder Mesh Example

a guest
Apr 30th, 2012
277
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.06 KB | None | 0 0
  1. #include "ofApp.h"
  2.  
  3. void ofApp::setup() {
  4.     ofSetVerticalSync(true);
  5.     int rings = 32, resolution = 32;
  6.     float length = 256, maxRadius = 128;
  7.     for(int i = 0; i < rings; i++) {
  8.         float radius = ofNoise(i / 4.) * maxRadius;
  9.         ofVec3f offset(0, 0, ofMap(i, 0, rings, -length, length) / 2);
  10.         for(int j = 0; j < resolution; j++) {
  11.             float theta = ofMap(j, 0, resolution, 0, 360);
  12.             ofVec2f cur(radius, 0);
  13.             cur.rotate(theta);
  14.             mesh.addVertex(offset + cur);
  15.             mesh.addColor(ofColor((i * j) % 2 == 0 ? 255 : 0));
  16.         }
  17.     }
  18.    
  19.     for(int i = 0; i < rings - 1; i++) {
  20.         for(int j = 0; j < resolution; j++) {
  21.             int sw = i * resolution + j, se = sw + 1;
  22.             if(j + 1 == resolution) {
  23.                 se -= resolution;
  24.             }
  25.             int nw = sw + resolution, ne = se + resolution;
  26.             mesh.addTriangle(sw, se, nw);
  27.             mesh.addTriangle(nw, se, ne);
  28.         }
  29.     }
  30.    
  31.     mesh.setMode(OF_PRIMITIVE_TRIANGLES);
  32. }
  33.  
  34. void ofApp::update() {
  35.    
  36. }
  37.  
  38. void ofApp::draw() {
  39.     ofBackground(0);
  40.     cam.begin();
  41.     glEnable(GL_DEPTH_TEST);
  42.     ofSetColor(255);
  43.     ofRotateY(90);
  44.     mesh.draw();
  45.     cam.end();
  46. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement