Drawing a sphere using for loop ( i want to draw a sphere instead of circle please help)

}
void drawCircle(double x, double y, double r) {
	
	glColor3f(nodeBGClolor.r, nodeBGClolor.g, nodeBGClolor.b);
	int iterations = 500;
	glBegin(GL_POLYGON);
	
	
	for (int i = 0; i < iterations; i++) {
		double currX = r * cos(2 * PI * (i / (iterations * 1.0)));
		double currY = r * sin(2 * PI * (i / (iterations * 1.0)));
		glVertex2f(x + currX, y + currY);
		
		
	}
	glEnd();
}

I forget where I picked this up, it draws using quads, hope it helps.

void drawSphere(GLfloat r, GLint lats, GLint longs) 
{
	int i, j;
	for(i = 0; i <= lats; i++) 
	{
		double lat0 = M_PI * (-0.5 + (double) (i - 1) / lats);
		double z0  = sin(lat0)*r;
		double zr0 =  cos(lat0)*r;
		double lat1 = M_PI * (-0.5 + (double) i / lats);
		double z1 = sin(lat1)*r;
		double zr1 = cos(lat1)*r;

		glBegin(GL_QUAD_STRIP);
		for(j = 0; j <= longs; j++)
		{
			GLfloat lng = 2 * M_PI * (double) (j - 1) / longs;
			GLfloat x = cos(lng);
			GLfloat y = sin(lng);
			glNormal3f(x * zr1, y * zr1, z1);
			glVertex3f(x * zr1, y * zr1, z1);
			glNormal3f(x * zr0, y * zr0, z0);
			glVertex3f(x * zr0, y * zr0, z0);
		}
		glEnd();
	}
}
1 Like