Skip to content

Instantly share code, notes, and snippets.

@kenpower
Created March 6, 2013 12:57
Show Gist options
  • Select an option

  • Save kenpower/5099146 to your computer and use it in GitHub Desktop.

Select an option

Save kenpower/5099146 to your computer and use it in GitHub Desktop.
OpenGL Torus
void MyTorus(float u,float v,GLfloat fv[]){
const float TWO_PI= 3.141f*2;
float r=0.4f;
float R=1.3f;
float x_u=r*(float)cos(TWO_PI*u)+R;
fv[0]=x_u*(float)cos(TWO_PI*v); //x-coordinate
fv[1]=r*(float)sin(TWO_PI*u); //y-coordinate
fv[2]=x_u*(float)sin(TWO_PI*v); //z-coordinate
}
void main(){
...
GLfloat vertex1[3];
GLfloat vertex2[3];
GLfloat vertex3[3];
GLfloat vertex4[3];
float u,v, delta_u=0.05f,delta_v=0.05f;
glBegin(GL_QUADS); // each pair of glVertex will form a line
// draw body of cylinder
for(u=0;u<1;u+=delta_u){
for(v=0;v<1;v+=delta_v){
glColor3f(u,v,1-u*v);
MyTorus(u,v,vertex1);
MyTorus(u+delta_u,v,vertex2);
MyTorus(u+delta_u,v+delta_v,vertex3);
MyTorus(u,v+delta_v,vertex4);
glVertex3fv(vertex1);
glVertex3fv(vertex2);
glVertex3fv(vertex3);
glVertex3fv(vertex4);
}
}
glEnd();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment