PDA

View Full Version : drawing icosahedron problem (following redBook)

miroslav_karpis
11-28-2008, 03:01 AM
Hi All,
I'm following redBook and I'm having a problem with drawing the icosahedron. I'm getting only a black screen.

Below is the source. Please do you have some tips, that what can be wrong? I have tried also to draw a normal polygon and that worked, so I do not know. :p

#include <GL/glut.h>
#include <stdlib.h>

static GLfloat spin = 0.0;
#define X .525731112119133606
#define Z .850650808352039932

//-----------------------------------------------------------------------

static GLfloat vdata[12][3] = {
{-X, 0.0, Z}, {X, 0.0, Z}, {-X, 0.0, -Z}, {X, 0.0, -Z},
{0.0, Z, X}, {0.0, Z, -X}, {0.0, -Z, X}, {0.0, -Z, -X},
{Z, X, 0.0}, {-Z, X, 0.0}, {Z, -X, 0.0}, {-Z, -X, 0.0}
};

static GLuint tindices [20][3] = {
{1, 4, 0}, {4, 9, 0}, {4, 5, 9}, {8, 5, 4}, {1, 8, 4},
{1, 10, 8}, {10, 3, 8}, {8, 3, 5}, {3, 2, 5}, {3, 7, 2},
{3, 10, 7}, {10, 6, 7}, {6, 11, 7}, {6, 0, 11}, {6, 1, 0},
{10, 1, 6}, {11, 0, 9}, {2, 11, 9}, {5, 2, 9}, {11, 2, 7},
};

//-----------------------------------------------------------------------

void display(void)
{
glClear(GL_COLOR_BUFFER_BIT);
glPushMatrix();
glColor3f(1.0, 0.0, 0.0);

glBegin(GL_POLYGON);
glVertex3f(0.25,0.25,0);
glVertex3f(0.75,0.25,0);
glVertex3f(0.75,0.75,0);
glVertex3f(0.25,0.75,0);
glEnd();

/*
int i;
glBegin(GL_TRIANGLES);
for(i=0; i<20;i++) {
glColor3f(1.0,0.0,0.0);
glVertex3fv(&amp;vdata[tindices[i][0]][0]);
glVertex3fv(&amp;vdata[tindices[i][0]][0]);
glVertex3fv(&amp;vdata[tindices[i][0]][0]);
}
glEnd();
*/
glPopMatrix();
glutSwapBuffers();
//glFlush();
}

void init(void)
{
glClearColor (0.0, 0.0, 0.0, 0.0);
}

void reshape(int w, int h)
{
glViewport (0, 0, (GLsizei) w, (GLsizei) h);
glMatrixMode(GL_PROJECTION);
glOrtho(0.0, 1.0, 0.0, 1.0, -1.0, 1.0);
glMatrixMode(GL_MODELVIEW);
}

int main(int argc, char** argv)
{
glutInit(&amp;argc, argv);
glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGB);
glutInitWindowSize (500,500);
glutInitWindowPosition (100, 100);
glutCreateWindow (argv[0]);
init ();
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutMainLoop();
return 0;
}

Nicolai de Haan Brøgger
11-28-2008, 04:54 AM
I think your indices are all wrong (check the book again). Try and use this code below:

int i;
glBegin(GL_TRIANGLES);
for(i=0; i<20;i++) {
glColor3f(1.0,0.0,0.0);
glVertex3fv(&amp;vdata[tindices[i][0]][0]);
glVertex3fv(&amp;vdata[tindices[i][1]][0]);
glVertex3fv(&amp;vdata[tindices[i][2]][0]);
}
glEnd();

If that doesn't help, maybe you have face culling enabled? Because I think the icosahedron faces are specified in clockwise order. You can reverse these with

GL11.glFrontFace(GL11.GL_CW);
<draw icosahedron>
GL11.glFrontFace(GL11.GL_CCW);

miroslav_karpis
11-28-2008, 05:23 AM
many thanks!!!..next time should double check book and source!