moebius strip glut

Hi :smiley:
at first: English isn’t my first language, so please excuse any mistakes ;>

from a few days i try to draw a 3D moebius strip in visual studio

my first version:
[ATTACH=CONFIG]1426[/ATTACH]


for( double a = 0; a < 2 * PI; a += 0.2 )
{
    for( r = - 1; r <= 1; r += 0.5 )
    {
        glBegin( GL_LINE_STRIP );
       
        x = cos( a ) *( 2 +( r / 2 * cos( a / 2 ) ) );
        y = sin( a ) *( 2 +( r / 2 * cos( a / 2 ) ) );
        z = r / 2 * sin( a / 2 );
       
        glVertex3f( x, y, z );
       
       
    }
    glEnd();
}



for( r = - 1; r <= 1; r += 0.5 )
{
   
    for( double a = 0; a < 2 * PI; a += 0.2 )
    {
        glBegin( GL_LINE_STRIP );
       
        x = cos( a ) *( 2 +( r / 2 * cos( a / 2 ) ) );
        y = sin( a ) *( 2 +( r / 2 * cos( a / 2 ) ) );
        z = r / 2 * sin( a / 2 );
       
        glVertex3f( x, y, z );
       
    }
    glEnd();
   
}

my program at this moment:
[ATTACH=CONFIG]1425[/ATTACH]


glBegin(GL_QUAD_STRIP);

		for (double a = 0; a < 2 * PI; a += 0.2)
			{
				for (r = -1; r <= 1; r += 0.5)
				{
		
				x = cos(a) *(1 + (r / 2 * cos(a / 2)));
				y = sin(a) *(1 + (r / 2 * cos(a / 2)));
				z = r / 2 * sin(a / 2);

				glVertex3f(x, y, z);


				}
			}	
	glEnd();

	
	glBegin(GL_LINE_STRIP);

		for (r = -1; r <= 1; r += 0.5)
			{
				for (double a = 0; a < 2 * PI; a += 0.2)
				{
			
				x = cos(a) *(1 + (r / 2 * cos(a / 2)));
				y = sin(a) *(1 + (r / 2 * cos(a / 2)));
				z = r / 2 * sin(a / 2);

				glVertex3f(x, y, z);

				}
			}
	glEnd();

im beginner and i have no idea whats wrong ;c can you help me and give some advices?

thx.

Hello Reserved -

Took a look at your code today and tweaked it to make it work. The results are below. Tried to keep the programming style close to yours so you’d understand how it works. Your equations to generate points on the surface of a Mobius strip are correct. I used your equations in my code. But I reorganized things in order to be able to generate a solid surface. If you cut and paste my code into your project, it should compile and render an image similar to the one below. A few comments.

There is a crack in your surface, i.e. the first spoke and the last spoke of vertices don’t match up. I’ll let you figure out how to fix that.

Generally speaking, glBegin and glEnd commands should appear in pairs. You break that rule by having glBegin inside a loop and glEnd outside. You were lucky your code still generated an image. Your big problem was with the use of the GL_QUAD_STRIP option to glBegin. You can’t simply change from GL_LINE_STRIP to GL_QUAD_STRIP and hope to generate a correct surface. Read the documentation on GL_QUAD_STRIP again. I’m guessing it doesn’t make sense to you from looking at your code. Note that I used GL_QUADS instead of GL_QUAD_STRIP. It seemed a little simpler to me that way.

Your code recomputes the vertex coordinates each time you draw the scene. This is not necessary. Note that in my code, the vertex coordinates are only computed once and stored in an array which is used to draw points, lines, and surfaces.

Note that we are using the old style ‘fixed function’ type of OpenGL. The purists might get on your case about this. Modern GL is faster, but it’s a moot point in a program as small as this.

Let me know if you are able to get my code to work or if you have any questions. Have fun.

[ATTACH=CONFIG]1428[/ATTACH]


void Mobius_Strip (void)
{
    int v, q, s, b;
    float r, x, y, z;
    double a;

    static int nv = 0, np = 0, ns = 0, first_time = 1;
    static float Vert[MAXVRT][3];

    if (first_time)  {
       first_time = 0;
       for (a = 0; a < 2 * M_PI; a += 0.2 )  {
          ++ns;
          for (r = -1; r <= 1; r += 0.5)  {
             Vert[nv][0] = cos(a) * (2 + (r/2 * cos(a/2)));
             Vert[nv][1] = sin(a) * (2 + (r/2 * cos(a/2)));
             Vert[nv][2] =                r/2 * sin(a/2  );
             if (++nv > MAXVRT)  {
                printf ("\007
  nv = %d.  Execution stopped.
", nv);
                exit (0);
             }
          }
       }
       printf ("
   %d vertices generated on %d spokes.
", nv, ns);
    }


    // Yellow points.

    glColor3f (0.90, 0.90, 0.30);

    glBegin (GL_POINTS);
       for (v = 0; v < nv; v++)  glVertex3fv (Vert[v]);
    glEnd ();


    // Blue quads front side.

    glCullFace (GL_BACK);
    glColor3f (0.30, 0.30, 0.95);
    glEnable (GL_CULL_FACE);
    glPolygonMode (GL_FRONT_AND_BACK, GL_FILL);

    for (s = 0; s < ns-1; s++)  {
       v = s * 5;
       for (q = 0; q < 4; q++)  {
          b = v + q;
          glBegin (GL_QUADS);
             glVertex3fv (Vert[b  ]); glVertex3fv (Vert[b+1]);
             glVertex3fv (Vert[b+6]); glVertex3fv (Vert[b+5]);
          glEnd ();
       }
    }


    // Green quads back side.

    glCullFace (GL_FRONT);
    glColor3f (0.20, 0.50, 0.20);

    for (s = 0; s < ns-1; s++)  {
       v = s * 5;
       for (q = 0; q < 4; q++)  {
          b = v + q;
          glBegin (GL_QUADS);
             glVertex3fv (Vert[b  ]); glVertex3fv (Vert[b+1]);
             glVertex3fv (Vert[b+6]); glVertex3fv (Vert[b+5]);
          glEnd ();
       }
    }


    // White wireframe quads.

    glLineWidth (1.0);
    glColor3f (0.80, 0.80, 0.80);
    glDisable (GL_CULL_FACE);
    glPolygonMode (GL_FRONT_AND_BACK, GL_LINE);

    for (s = 0; s < ns-1; s++)  {
       v = s * 5;
       for (q = 0; q < 4; q++)  {
          b = v + q;
          glBegin (GL_QUADS);
             glVertex3fv (Vert[b  ]); glVertex3fv (Vert[b+1]);
             glVertex3fv (Vert[b+6]); glVertex3fv (Vert[b+5]);
          glEnd ();
       }
    }
}