@RobbertGroenendijk wrote:
Hi everyone,
I’ve been looking into generating supershapes, and started by constructing a sphere with mesh vertexes and connecting those as triangle strips. I’ve had some strange results from connecting the triangle strips the wrong way, which seem to be fixed now. Though I wonder if someone who has more experience in this could look at my way of putting the vertices to indices and how the mesh is generated from there. Is this the ‘right’ way of doing it?
Thanks in advance
#include "ofApp.h" //-------------------------------------------------------------- void ofApp::setup(){ ofEnableDepthTest(); ofSetVerticalSync(true); ofSetSmoothLighting(true); colorOffset = 0; ofSetBackgroundColor(255); camera.setDistance(300); lightColor = ofColor(246, 255, 178); int lightCount = 3; for (int i = 0; i < lightCount; i ++) { lightVector.push_back(*new ofLight()); } pointLight.setPointLight(); pointLight.setPosition(0, 500, 300); pointLight.setSpecularColor(ofColor(255)); pointLight.setDiffuseColor(ofColor(255)); pointLight.setAmbientColor(ofColor(100)); mesh.setMode(OF_PRIMITIVE_TRIANGLE_STRIP); mesh.enableColors(); mesh.enableIndices(); } //-------------------------------------------------------------- void ofApp::update(){ mesh.clear(); colorOffset += 0.5; meshRes = 200; float radius = 200; for (int i = 0; i < meshRes + 1; i++) { float lat = ofMap(i, 0, meshRes, 0, PI); for (int j = 0; j < meshRes + 1; j++) { float lon = ofMap(j,0,meshRes,0,TWO_PI); float x = radius * sin(lat) * cos(lon); float y = radius * sin(lat) * sin(lon); float z = radius * cos(lat); mesh.addVertex(ofVec3f(x,y,z)); mesh.addColor(ofColor::fromHsb(int(i+colorOffset)%255, 255, 255)); } } int vertexCount = mesh.getNumVertices(); for (int i = 0; i < vertexCount; i++) { mesh.addIndex(i); mesh.addIndex(i+1); mesh.addIndex(i+meshRes+1); mesh.addIndex(i+1); mesh.addIndex(i+meshRes+2); } } //-------------------------------------------------------------- void ofApp::draw(){ pointLight.enable(); pointLight.getPosition(); camera.begin(); // Add sphere at pointLight location ofSetColor(lightColor); ofDrawSphere(pointLight.getPosition().x, pointLight.getPosition().y, 5); // Drawing lines for XYZ axis ofSetLineWidth(3); ofSetColor(255, 0, 0); ofDrawLine(0, 0, 500, 0); ofSetColor(0, 255, 0); ofDrawLine(0, 0, 0, 500); ofSetColor(0, 0, 255); ofDrawLine(0, 0, 0, 0, 0, 500); // Drawing mesh ofSetColor(100); mesh.draw(); camera.end(); pointLight.disable(); }
Posts: 1
Participants: 1