Sphere calculation in opengl

I want to calculate all the necessary vertices and connect them with the lines, so I will finally come up with a sphere. How many ways to do this? And also the lines between the vertices will be straight; how can i make them "curved". I know I can use glutWireSphere (), but I'm interested in the actual vertex calculation. The way I thought about this was to put all the vertices manually in an array, but I think this is not the way it was done.

+4
source share
5 answers

Copying and pasting some code that I originally wrote in Creating a 3D Sphere in Opengl with Visual C ++

class SolidSphere { protected std::vector<GLfloat> vertices; std::vector<GLfloat> normals; std::vector<GLfloat> texcoords; std::vector<GLushort> indices; public: void SolidSphere(float radius, unsigned int rings, unsigned int sectors) { float const R = 1./(float)(rings-1); float const S = 1./(float)(sectors-1); int r, s; sphere_vertices.resize(rings * sectors * 3); sphere_normals.resize(rings * sectors * 3); sphere_texcoords.resize(rings * sectors * 2); std::vector<GLfloat>::iterator v = sphere_vertices.begin(); std::vector<GLfloat>::iterator n = sphere_normals.begin(); std::vector<GLfloat>::iterator t = sphere_texcoords.begin(); 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 ); *t++ = s*S; *t++ = r*R; *v++ = x * radius; *v++ = y * radius; *v++ = z * radius; *n++ = x; *n++ = y; *n++ = z; } sphere_indices.resize(rings * sectors * 4); std:vector<GLushort>::iterator i = sphere_indices.begin(); for(r = 0; r < rings; r++) for(s = 0; s < sectors; s++) { *i++ = r * sectors + s; *i++ = r * sectors + (s+1); *i++ = (r+1) * sectors + (s+1); *i++ = (r+1) * sectors + s; } } void draw(GLfloat x, GLfloat y, GLfloat z) { glMatrixMode(GL_MODELVIEW); glPushMatrix(); glTranslatef(x,y,z); glEnableClientState(GL_VERTEX_ARRAY); glEnableClientState(GL_NORMAL_ARRAY); glEnableClientState(GL_TEXTURE_COORD_ARRAY); glVertexPointer(3, GL_FLOAT, 0, &sphere_vertices[0]); glNormalPointer(GL_FLOAT, 0, &sphere_normals[0]); glTexCoordPointer(2, GL_FLOAT, 0, &sphere_texcoords[0]); glDrawElements(GL_QUADS, sphere_indices.size()/4, GL_UNSIGNED_SHORT, sphere_indices); glPopMatrix(); } } 

how can i make them "curved"

You can not. All OpenGL primitives are "affine", that is, flat or straight. Curvature is emulated by drawing short straight sections with sufficient resolution.

+14
source

Paul Bourke actually has a good understanding of the realm of generation . As for the curved lines, OpenGL doesn’t have such a thing. You can make them seem curved by adding additional intermediate connection points.

+4
source

There is more than one way to do this: a) the generation of the icosphere; and b) the generation of the UV sphere. It could be more. Some googling got me this great post in the generation of the icosphere . However, I could not find a method for generating a UV sphere.

+4
source

The datenwolf answer is great, but contains some error. When you use vbo, client states should be turned off after power-on.

Add three lines to draw code

 void draw(GLfloat x, GLfloat y, GLfloat z) { glMatrixMode(GL_MODELVIEW); glPushMatrix(); glTranslatef(x,y,z); glEnableClientState(GL_VERTEX_ARRAY); glEnableClientState(GL_NORMAL_ARRAY); glEnableClientState(GL_TEXTURE_COORD_ARRAY); glVertexPointer(3, GL_FLOAT, 0, &sphere_vertices[0]); glNormalPointer(GL_FLOAT, 0, &sphere_normals[0]); glTexCoordPointer(2, GL_FLOAT, 0, &sphere_texcoords[0]); glDrawElements(GL_QUADS, sphere_indices.size()/4, GL_UNSIGNED_SHORT, sphere_indices); **glDisableClientState(GL_VERTEX_ARRAY); glDisableClientState(GL_NORMAL_ARRAY); glDisableClientState(GL_TEXTURE_COORD_ARRAY);** glPopMatrix(); } 
0
source

The iconosphere would do the trick. However, in order to create a sphere with it, you will have to separate its triangles.

0
source

All Articles