/* Modélisation d'un tore                       */
/*                                              */
/* Auteur: Nicolas JANEY                        */
/* nicolas.janey@univ-fcomte.fr                 */
/* Mars 2021                                    */

#include <math.h>

#include <GL/glut.h>
#include <GL/gl.h>
#include <GL/glu.h>

#ifndef M_PI
#define M_PI 3.14159265358979323846264338327950288
#endif

void solidTore(double ri,double re,int nbe,int nbi) {
  for ( int i = 0 ; i < nbi ; i++ ) {
    float alphai = 2*M_PI*i/nbi;
    float alphaj = alphai+2*M_PI/nbi;
    float cosalphai = cos(alphai);
    float sinalphai = sin(alphai);
    float cosalphaj = cos(alphaj);
    float sinalphaj = sin(alphaj);
    glBegin(GL_QUAD_STRIP);
    for ( int j = 0 ; j <= nbe ; j++ ) {
      float beta = 2*M_PI*j/nbe;
      float cosbeta = cos(beta);
      float sinbeta = sin(beta);
      float x1 = (re+ri*cosbeta)*cosalphai;
      float y1 = (re+ri*cosbeta)*sinalphai;
      float z1 = ri*sinbeta;
      glTexCoord2f((double) i/nbi,(double) j/nbe);
      glNormal3f(cosbeta*cosalphai,cosbeta*sinalphai,sinbeta);
      glVertex3f(x1,y1,z1);
      float x2 = (re+ri*cosbeta)*cosalphaj;
      float y2 = (re+ri*cosbeta)*sinalphaj;
      float z2 = ri*sinbeta;
      glTexCoord2f((double) (i+1)/nbi,(double) j/nbe);
      glNormal3f(cosbeta*cosalphaj,cosbeta*sinalphaj,sinbeta);
      glVertex3f(x2,y2,z2); }
    glEnd(); }
}