162 lines
3.3 KiB
C
162 lines
3.3 KiB
C
|
|
#include <SDL/SDL.h>
|
|
#include <GL/gl.h>
|
|
#include <GL/glu.h>
|
|
#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, -10);
|
|
}
|
|
|
|
int main(int argc, char *argv[])
|
|
{
|
|
if (argc != 2)
|
|
{
|
|
fprintf(stderr, "Usage: %s <stl-file>\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;
|
|
int dragging = 0;
|
|
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 (event.type == SDL_MOUSEBUTTONDOWN)
|
|
{
|
|
if (event.button.button == 1)
|
|
dragging = 1;
|
|
}
|
|
else if (event.type == SDL_MOUSEBUTTONUP)
|
|
{
|
|
if (event.button.button == 1)
|
|
dragging = 0;
|
|
}
|
|
else if (event.type == SDL_MOUSEMOTION)
|
|
{
|
|
if (dragging)
|
|
{
|
|
glRotatef(event.motion.xrel, 0, 1, 0);
|
|
glRotatef(event.motion.yrel, 0, 0, 1);
|
|
display();
|
|
}
|
|
}
|
|
}
|
|
|
|
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 (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);
|
|
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]);
|
|
}
|
|
// printf("attribute: 0x%x\n", stl_face(stl, i).attribute);
|
|
}
|
|
glEnd();
|
|
glEndList();
|
|
return dl;
|
|
}
|