vendredi 27 février 2015

Trying to make a sphere with triangle strips on OpenGL


I'm trying to make an sphere with the OpenGl primitive GL_TRIANGLE_STRIP from OpenGL but it seems I'm missing something. The program starts from the botton of the sphere and from there it goes upwards.


Both divO and divA are the number of subdivions that the sphere has both verticaly and horizontaly.


Here is the code I tried.



void hacerEsfera (float radio, int divO, int divA) {

float px, py,pz;
int i,j;
float incO = 2*M_PI / divO;
float incA = M_PI /divA;

glClear(GL_COLOR_BUFFER_BIT);
glColor3i(1,0,0);

//Depende del polo en el que empezemos
for (i= 0 ; i<= divO; i++){
for (j = 0; j<=divA ; j++) {

glBegin(GL_TRIANGLE_STRIP);

if (i % 2 == 0){

pz = cos (M_PI-(incA*j))*radio;
py = sin (M_PI-(incA*j))*sin (incO*i)*radio;
px = sin (M_PI-(incA*j))*cos (incO*i)*radio;

glVertex3f (px, py, pz);

pz = cos (M_PI-(incA*j))*radio;
py = sin (M_PI-(incA*j))*sin (incO*(i+1))*radio;
px = sin (M_PI-(incA*j))*cos (incO*(i+1))*radio;

glVertex3f (px, py, pz);
}

else {
pz = cos (incA*j)*radio;
py = sin (M_PI-(incA*j))*sin (incO*i)*radio;
px = sin (M_PI-(incA*j))*cos (incO*i)*radio;

glVertex3f (px, py, pz);

pz = cos (incA*j)*radio;
py = sin (incA*j)*sin (incO*(i+1))*radio;
px = sin (incA*j)*cos (incO*(i+1))*radio;

glVertex3f (px, py, pz);
}
}
}
glEnd();
glFlush();


}




Aucun commentaire:

Enregistrer un commentaire