Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- void CGLRenderer::DrawShpere(float radius, int steps, int stacks)
- {
- if (SphereCoor.size() > 0) {
- SphereCoor.clear();
- }
- float dAlpha = M_PI * 2 / steps;
- float dTheta = M_PI / stacks;
- float radiusPrim, x, y, z, tempThetaAngle;
- for (float theta = -M_PI / 2; theta <= M_PI/2 + dTheta; theta += dTheta) {
- for (float alpha = 0.0f; alpha <= M_PI * 2; alpha += dAlpha) {
- radiusPrim = cos(theta) * radius;
- x = radiusPrim * sin(alpha);
- z = radiusPrim * cos(alpha);
- y = radius * sin(theta);
- SphereCoor.push_back(x); SphereCoor.push_back(y); SphereCoor.push_back(z);
- // norm
- float nx = cos(theta)*sin(alpha);
- float ny = sin(theta);
- float nz = cos(theta)*cos(alpha);
- SphereCoor.push_back(nx); SphereCoor.push_back(ny); SphereCoor.push_back(nz);
- // text
- float u = 0.5 + atan2(nz, nx) / (2 * M_PI);
- float v = 0.5 - asin(ny) / M_PI;
- SphereCoor.push_back(v); SphereCoor.push_back(u);
- tempThetaAngle = theta - dTheta;
- radiusPrim = cos(tempThetaAngle) * radius;
- x = radiusPrim * sin(alpha);
- z = radiusPrim * cos(alpha);
- y = radius * sin(tempThetaAngle);
- SphereCoor.push_back(x); SphereCoor.push_back(y); SphereCoor.push_back(z);
- // norm
- nx = cos(tempThetaAngle)*sin(alpha);
- ny = sin(tempThetaAngle);
- nz = cos(tempThetaAngle)*cos(alpha);
- SphereCoor.push_back(nx); SphereCoor.push_back(ny); SphereCoor.push_back(nz);
- //text
- u = 0.5 + atan2(nz, nx) / (2 * M_PI);
- v = 0.5 - asin(ny) / M_PI;
- SphereCoor.push_back(v); SphereCoor.push_back(u);
- // normals nx = cos(theta)*sin(alpha) ny=sin(theta) nz=cos(theta)*cos(alpha)
- }
- }
- glVertexPointer(3, GL_FLOAT, 8 * sizeof(float), SphereCoor.data());
- glNormalPointer(GL_FLOAT, 8 * sizeof(float), &SphereCoor.data()[3]);
- glTexCoordPointer(2, GL_FLOAT, 8 * sizeof(float), &SphereCoor.data()[6]);
- glEnableClientState(GL_VERTEX_ARRAY);
- glEnableClientState(GL_NORMAL_ARRAY);
- glEnableClientState(GL_TEXTURE_COORD_ARRAY);
- glDrawArrays(GL_QUAD_STRIP, 0, SphereCoor.size() / 8);
- glDisableClientState(GL_VERTEX_ARRAY);
- glDisableClientState(GL_NORMAL_ARRAY);
- glDisableClientState(GL_TEXTURE_COORD_ARRAY);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement