#include #include #include "SDL.h" #include "SDL_opengl.h" #include "icosahedron.hpp" using namespace std; #define RES_X 800 #define RES_Y 600 int main ( int argc, char *argv[]) { /* Initialize the SDL library */ if ( SDL_Init(SDL_INIT_VIDEO) < 0 ) { fprintf(stderr, "Unable to init SDL: %s\n", SDL_GetError()); exit(1); } atexit(SDL_Quit); SDL_GL_SetAttribute ( SDL_GL_DOUBLEBUFFER, 1 ); SDL_Surface *screen; //screen = SDL_SetVideoMode(RES_X, RES_Y, 24, SDL_OPENGL|SDL_FULLSCREEN); screen = SDL_SetVideoMode(RES_X, RES_Y, 24, SDL_OPENGL); if ( screen == NULL ) { fprintf(stderr, "Unable to set video mode: %s\n", SDL_GetError()); exit(1); } srand(time(NULL)); glClearColor( 0, 0, 0, 0); glViewport( 0, 0, RES_X, RES_Y ); glMatrixMode(GL_PROJECTION); glLoadIdentity(); glOrtho(0, 800, 600, 0, -20.0, 20.0); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); glClear ( GL_COLOR_BUFFER_BIT ); icosahedron *ball; float origin[3] = { 400.0, 300.0, 0}; ball = new icosahedron(origin); ball->draw(); SDL_GL_SwapBuffers(); SDL_Delay(1000); delete ball; return (0); } /* icosahedron.cpp */ #include "SDL_opengl.h" #include "icosahedron.hpp" #include using namespace std; #define X .525731112119133606 #define Z .850650808352039932 static GLfloat vdata[12][13] = { {-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}, }; icosahedron::icosahedron ( float origin[3] ) { x = origin[0]; y = origin[1]; z = origin[2]; } void icosahedron::draw( ) { glPushMatrix(); glLoadIdentity(); glTranslatef(x, y, z); glBegin(GL_TRIANGLES); for (int i=0; i < 20; i++) { glColor3f( 1.0 / (i + 1), 1.0 / (i + 1), 1.0 / (i + 1)); glVertex3fv(&vdata[tindices[i][0]][0]); glVertex3fv(&vdata[tindices[i][1]][0]); glVertex3fv(&vdata[tindices[i][2]][0]); } glEnd(); glPopMatrix(); }