#include #include #include #include "stl.h" #define WIDTH 500 #define HEIGHT 500 stl_t * stl = NULL; GLuint stldl = 0; GLfloat default_color[] = {1, 1, 1, 1}; GLuint buildDL(void); void init(void); void display(void); void reshape(GLsizei w, GLsizei h); void init(void) { glClearColor (0.0, 0.0, 0.0, 0.0); glEnable(GL_DEPTH_TEST); glEnable(GL_LIGHTING); glEnable(GL_LIGHT0); glShadeModel(GL_SMOOTH); } void display(void) { glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); if (stldl) glCallList(stldl); SDL_GL_SwapBuffers(); } void reshape(GLsizei w, GLsizei h) { glViewport(0, 0, w, h); glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluPerspective(60.0, 1.0*(GLfloat)w/(GLfloat)h, 1.0, 10000.0); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); glTranslatef(0.0, 0.0, -3); } int main(int argc, char *argv[]) { if (argc != 2) { fprintf(stderr, "Usage: %s \n", argv[0]); return -1; } stl = stl_load(argv[1]); if (stl == NULL) { fprintf(stderr, "Error loading '%s'\n", argv[1]); return -2; } if (SDL_Init(SDL_INIT_VIDEO)) { fprintf(stderr, "Failed to initialize SDL!\n"); return 1; } atexit(SDL_Quit); SDL_Surface *screen; SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1); if (!(screen = SDL_SetVideoMode(WIDTH, HEIGHT, 16, SDL_OPENGL))) { fprintf(stderr, "Failed to set video mode!\n"); return 2; } SDL_WM_SetCaption(argv[0], argv[0]); stldl = buildDL(); init(); reshape(WIDTH, HEIGHT); display(); SDL_Event event; while (SDL_WaitEvent(&event)) { if (event.type == SDL_QUIT) break; else if (event.type == SDL_KEYDOWN) { if (event.key.keysym.sym == SDLK_ESCAPE) break; else if } else if (event.type == SDL_MOUSEDOWN) { } else if (event.type == SDL_MOUSEUP) { } else if (event.type == SDL_MOUSEMOTION) { } } free(stl); } GLuint buildDL(void) { unsigned int n_faces = stl_num_faces(stl); unsigned int i; int dfltClrAct = 1; GLfloat color[4]; float normal[3]; color[3] = 0.0f; GLuint dl = glGenLists(1); glNewList(dl, GL_COMPILE); glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, default_color); glBegin(GL_TRIANGLES); for (i = 0; i < n_faces; i++) { #if 0 if (stl_attr_color_valid(stl_face(stl, i).attribute)) { color[0] = stl_attr_color_r(stl_face(stl, i).attribute) / (float) 255; color[1] = stl_attr_color_g(stl_face(stl, i).attribute) / (float) 255; color[2] = stl_attr_color_b(stl_face(stl, i).attribute) / (float) 255; glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, color); dfltClrAct = 0; } else if (!dfltClrAct) glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, default_color); #endif stl_get_face_normal(stl, i, normal); // printf("normal: %f, %f, %f\n", // normal[0], normal[1], normal[2]); glNormal3fv(normal); int j; for (j = 0; j < 3; j++) { // printf("vertex: %f, %f, %f\n", // stl_face(stl, i).vertices[j][0], // stl_face(stl, i).vertices[j][1], // stl_face(stl, i).vertices[j][2]); glVertex3fv(stl_face(stl, i).vertices[j]); } } glEnd(); glEndList(); return dl; }