Jump to content
  • Advertisement
Sign in to follow this  
maxalba

OpenGL Sphere 2 mouth

This topic is 4881 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

So I got this opengl code to build a simple sphere:
void RenderSolidSphere(double radius, int slices, int stacks)
{
/* Adjust z and radius as stacks are drawn. */

   double z0 = 0.0, z1 = 0.0, r0 = 0.0, r1 = 0.0;

/* Pre-computed circle. */
   double* sint1 = NULL, * cost1 = NULL;
   double* sint2 = NULL, * cost2 = NULL;
   CircleTable(&sint1, &cost1, -slices);
   CircleTable(&sint2, &cost2, stacks * 2);

/* The top stack is covered with a triangle fan. */

   z0 = 1.0;
   z1 = cost2[1];
   r0 = 0.0;
   r1 = sint2[1];

   glBegin(GL_TRIANGLE_FAN);
   glVertex3d(0.0, 0.0, radius);
   for (int j = slices; j >= 0; --j)
   {
      glNormal3d(cost1[j] * r1, sint1[j] * r1, z1);
      glVertex3d(cost1[j] * r1 * radius, sint1[j] * r1 * radius, z1 * radius);
   }
   glEnd();

/* Cover each stack with a quad strip, except the top and bottom stacks. */

   for (int i = 1; i < stacks - 1; ++i)
   {
      z0 = z1; z1 = cost2[i + 1];
      r0 = r1; r1 = sint2[i + 1];

      glBegin(GL_QUAD_STRIP);
      for(int j = 0; j <= slices; ++j)
      {
         glNormal3d(cost1[j] * r1, sint1[j] * r1, z1);
         glVertex3d(cost1[j] * r1 * radius, sint1[j] * r1 * radius, z1 * radius);
         glNormal3d(cost1[j] * r0, sint1[j] * r0, z0);
         glVertex3d(cost1[j] * r0 * radius, sint1[j] * r0 * radius, z0 * radius);
      }
      glEnd();
   }

/* The bottom stack is covered with a triangle fan. */

   z0 = z1;
   r0 = r1;

   glBegin(GL_TRIANGLE_FAN);
   glVertex3d(0.0, 0.0, -radius);
   for (int j = 0; j <= slices; ++j)
   {
      glNormal3d(cost1[j] * r0, sint1[j] * r0, z0);
      glVertex3d(cost1[j] * r0 * radius, sint1[j] * r0 * radius, z0 * radius);
   }
   glEnd();

/* Release sin and cos tables. */
   free(sint1);
   free(cost1);
   free(sint2);
   free(cost2);

   return;
}


And I was using it to render a simple 3D yellow "Pac Man". Now with the game logic complete, I was looking to improve the graphics. Is it possible to "cut out" a wedge from the sphere, thus forming pac man's mouth??? If so, how would I go about this? Example code? Thanks in advance...

Share this post


Link to post
Share on other sites
Advertisement
Either model a mesh in a 3d package (you are going to want to do that if you want to model the eyes or to put legs on pacman or whatever anway)

If you are going to really just make him a sphere with a wedge cutout and use texture maps only for the eyes, then you should be able to pretty easily run the sphere code and then simply alter the existing shape after making it. Try altering your rendering code to put the verticies into a buffer. Also, remove the sin/cos table making, it's really not needed on modern CPUs and you will make a HUGE performance increase by caching the results of the actually sphere generation rather than the trig functions. Then simply modify the verticies of the mouth by scaling them about the center of the sphere.

Share this post


Link to post
Share on other sites
Of course I am using an opengl list!!! And little little little spheres for the eyes!

list[4] = list[0] + 4;
glNewList(list[4], GL_COMPILE);
RenderSolidSphere(0.2, 16, 16);
glEndList();

I just want to cut out a wedge for the mouth.

Share this post


Link to post
Share on other sites
the library of glut or aux has some function to slice sphere, if I remember correctly. You should look into the info.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!