how to create a sphere ( for deform it by playing on radius )

Hi
I try to create a sphere, for deform it by playing on radius of different point.
I use this script for calculate positions of point :

  
	float radJ=ofDegToRad(i);  
				float radI=ofDegToRad(j);  
				  
				posX=rayon*cos(radI)*cos(radJ);  
				posY=rayon*cos(radI)*sin(radJ);  
				posZ=rayon*sin(radI);  

i obtain a sphere but the wireframe look like this :
http://d.pr/i/m1ld-
all my vertex of outside 're connected to one vertex in center of sphere
If someone have an idea how i can obtaina basic sphere, or maybe some code
I looked around on google, but not found something very interesting

thanks for reading

my whole code :

  
#include "testApp.h"  
  
//--------------------------------------------------------------  
void testApp::setup(){  
	ofBackground(0, 0, 0, 0);  
	ofSetVerticalSync(true);  
	ofSetFrameRate(60);  
	ofSetWindowTitle("trigo3d");  
	//ofNoFill();  
	ofSetColor(255, 255, 255);  
	rayon=250;  
	disque.setMode(OF_PRIMITIVE_TRIANGLE_STRIP);  
	  
	  
}  
//--------------------------------------------------------------  
void testApp::update(){  
	float posX;  
	float posY;  
	float posZ;  
	disque.clear();  
	  
	for(int i =0;i<360;i+=5){  
		if (i%2==0) {  
			  
		for (int j =0; j<360; j+=10) {  
			float radI=ofDegToRad(i);  
			float radJ=ofDegToRad(j);  
			// radj = mouvement "non"  
			// radi = mouvement "oui"  
			posX=rayon*cos(radI)*cos(radJ);  
			posY=rayon*cos(radI)*sin(radJ);  
			posZ=rayon*sin(radI);  
			ofVec3f vert(posX,posY,posZ);  
			  
			radI=ofDegToRad(i+5);  
			radJ=ofDegToRad(j);  
			// radj = mouvement "non"  
			// radi = mouvement "oui"  
			posX=rayon*cos(radI)*cos(radJ);  
			posY=rayon*cos(radI)*sin(radJ);  
			posZ=rayon*sin(radI);  
			 ofVec3f vert1(posX,posY,posZ);  
			ofVec3f vertN=vert.getPerpendicular(vert1);  
			disque.addVertex(vert);  
			disque.addVertex(vertN);  
			disque.addVertex(vert);  
			disque.addVertex(vertN);  
		}  
		}else {  
			for (int j =360; j>0; j-=10) {  
				float radJ=ofDegToRad(i);  
				float radI=ofDegToRad(j);  
				// radj = mouvement "non"  
				// radi = mouvement "oui"  
				posX=rayon*cos(radI)*cos(radJ);  
				posY=rayon*cos(radI)*sin(radJ);  
				posZ=rayon*sin(radI);  
				ofVec3f vert(posX,posY,posZ);  
				  
				radI=ofDegToRad(i);  
				radJ=ofDegToRad(j+10);  
				// radj = mouvement "non"  
				// radi = mouvement "oui"  
				posX=rayon*cos(radI)*cos(radJ);  
				posY=rayon*cos(radI)*sin(radJ);  
				posZ=rayon*sin(radI);  
				ofVec3f vert1(posX,posY,posZ);  
				ofVec3f vertN=vert.getPerpendicular(vert1);  
				disque.addVertex(vert);  
				disque.addVertex(vertN);  
				disque.addVertex(vert);  
				disque.addVertex(vertN);  
			}  
		}  
  
	}  
	  
  
}  
  
//--------------------------------------------------------------  
void testApp::draw(){  
		ofTranslate(ofGetWidth()/2,ofGetHeight()/2);  
	ofRotateX(ofGetFrameNum()*0.5);  
	ofRotateY(ofGetFrameNum()*0.7);  
  
	  
		disque.drawWireframe();  
	  
	  
  
}  

In this Cinder post I have some code to draw a hemisphere, hope it helps

http://forum.libcinder.org/topic/how-to-calculate-vbo-mesh-normals

thanks , i will look at it.

Something like this also works for creating a VBO mesh:

  
  
  
void testApp::createSphere(ofVboMesh *vbo, float radius, unsigned int rings, unsigned int sectors)  
{  
    ofPrimitiveMode p;  
    vbo->setMode(OF_PRIMITIVE_TRIANGLE_FAN);  
      
    float sphereCompleteness = 0.5; // 1.0 for whole sphere, 0.5 for hemisphere  
    float const R = 1./(float)(rings-1) * sphereCompleteness;  
    float const S = 1./(float)(sectors-1);  
    int r, s;  
      
    for(r = 0; r < rings; r++) for(s = 0; s < sectors; s++) {  
        float const y = sin( -M_PI_2 + M_PI * r * R );  
        float const x = cos(2*M_PI * s * S) * sin( M_PI * r * R );  
        float const z = sin(2*M_PI * s * S) * sin( M_PI * r * R );  
        vbo->addTexCoord( ofVec2f(s*S, r*R) );  
        vbo->addVertex(ofVec3f(x * radius, y * radius, z * radius));  
        vbo->addNormal(ofVec3f(x,y,z));  
    }  
      
      
    for(r = 0; r < rings; r++) {  
        for(s = 0; s < sectors; s++) {  
              
            vbo->addIndex( r * sectors + s );  
            vbo->addIndex( r * sectors + (s+1));  
            vbo->addIndex( (r+1) * sectors + (s+1));  
            vbo->addIndex( (r+1) * sectors + s);  
        }  
    }  
}  
  

1 Like

If you want to deform it you can use perlin noise when you are calculating the x/y/z. Something like posX=rayon*cos(radI)*cos(radJ) + ofNoise(blah);

It is basically what I did for this

http://vimeo.com/40801569

Instead of using a vbo or whatever, I just have an array that keeps track of the various points I want to draw. So that video has 30 “lines” with 45 points in each line (each point is the little sphere drawn with ofSphere), with the perlin noise added to the radius. I also use some perlin noise for your radI and radJ angles.

Instead of iterating through the angle from 0 to 360 I loop through each point in the array and each loop increment radI/radJ by some fixed amount (basically 360/#of points in a line). The code is a bit of a mess, but once I had the basic “points on a sphere” stuff working i just messed around with how much noise to add till I got something I liked.

1 Like