c++ - Algorithm to draw a sphere using quadrilaterals -
i attempting draw sphere scratch using opengl. function must defined void drawsphere(float radius, int nsegments, int nslices)
, must centred @ (0, 0, 0) origin , must created using gl_quads
.
firstly, "slices" sort of tapered cylinder shapes stacked on top of each other create sphere, , "segments" quads generated in circle generate wall/side of each of these tapered cylinder slices?
secondly, cannot seem find algorithms or examples of how make calculations generate sphere using quadrilaterals - example seem generated triangles instead.
edit
here have tried, in right direction, coordinate calculations off somewhere:
void drawsphere(float radius, int nsegments, int nslices) { /* * todo * draw sphere centered @ origin using gl_quads * compute , set normal vectors each vertex ensure proper shading * set texture coordinates */ (float slice = 0.0; slice < nslices; slice += 1.0) { float lat0 = m_pi * (((slice - 1) / nslices) - 0.5); float z0 = sin(lat0); float zr0 = cos(lat0); float lat1 = m_pi * ((slice / nslices) - 0.5); float z1 = sin(lat1); float zr1 = cos(lat1); glbegin(gl_quads); (float segment = 0.0; segment < nsegments; segment += 1.0) { float long0 = 2 * m_pi * ((segment -1 ) / nsegments); float x0 = cos(long0); float y0 = sin(long0); float long1 = 2 * m_pi * (segment / nsegments); float x1 = cos(long1); float y1 = sin(long1); glvertex3f(x0 * zr0, y0 * zr0, z0); glvertex3f(x1 * zr1, y1 * zr1, z0); glvertex3f(x0 * zr0, y0 * zr0, z1); glvertex3f(x1 * zr1, y1 * zr1, z1); } glend(); } }
i'm not seeing radius
being used. simple omission. let's assume rest of computation radius 1. should generate 4 values using 0-1 on both (x, y), , (z, zr), not mix within tuples.
so x1 * zr1, y1 * zr1, z0
not right because you're mixing zr1
, z0
. can see norm of vector not 1 anymore. 4 values should be
x0 * zr0, y0 * zr0, z0 x1 * zr0, y1 * zr0, z0 x0 * zr1, y0 * zr1, z1 x1 * zr1, y1 * zr1, z1
i'm not sure order since don't use quads triangles.
Comments
Post a Comment