retabbed
This commit is contained in:
parent
bead7f9690
commit
54e3970362
264
wfobj-view.cc
264
wfobj-view.cc
@ -15,173 +15,173 @@ using namespace std;
|
|||||||
class Viewer
|
class Viewer
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
Viewer(const char * filename);
|
Viewer(const char * filename);
|
||||||
void run();
|
void run();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
void initgl();
|
void initgl();
|
||||||
void display();
|
void display();
|
||||||
void setProjection();
|
void setProjection();
|
||||||
|
|
||||||
WFObj m_obj;
|
WFObj m_obj;
|
||||||
GLuint m_list;
|
GLuint m_list;
|
||||||
float m_rotationMatrix[16];
|
float m_rotationMatrix[16];
|
||||||
int m_startx, m_starty;
|
int m_startx, m_starty;
|
||||||
bool m_dragging;
|
bool m_dragging;
|
||||||
float m_dist;
|
float m_dist;
|
||||||
};
|
};
|
||||||
|
|
||||||
/* The program's main entry point */
|
/* The program's main entry point */
|
||||||
int main(int argc, char * argv[])
|
int main(int argc, char * argv[])
|
||||||
{
|
{
|
||||||
if (argc < 2)
|
if (argc < 2)
|
||||||
{
|
{
|
||||||
cerr << "Usage: " << argv[0] << " <filename>" << endl;
|
cerr << "Usage: " << argv[0] << " <filename>" << endl;
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (SDL_Init(SDL_INIT_VIDEO))
|
if (SDL_Init(SDL_INIT_VIDEO))
|
||||||
{
|
{
|
||||||
printf("Failed to initialize SDL!\n");
|
printf("Failed to initialize SDL!\n");
|
||||||
return -2;
|
return -2;
|
||||||
}
|
}
|
||||||
|
|
||||||
atexit(SDL_Quit);
|
atexit(SDL_Quit);
|
||||||
|
|
||||||
SDL_Surface * screen;
|
SDL_Surface * screen;
|
||||||
SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
|
SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
|
||||||
if (!(screen = SDL_SetVideoMode(WIDTH, HEIGHT, 32, SDL_OPENGL)))
|
if (!(screen = SDL_SetVideoMode(WIDTH, HEIGHT, 32, SDL_OPENGL)))
|
||||||
{
|
{
|
||||||
printf("Failed to set video mode!\n");
|
printf("Failed to set video mode!\n");
|
||||||
SDL_Quit();
|
SDL_Quit();
|
||||||
return -3;
|
return -3;
|
||||||
}
|
}
|
||||||
SDL_WM_SetCaption(TITLE, TITLE);
|
SDL_WM_SetCaption(TITLE, TITLE);
|
||||||
|
|
||||||
Viewer v(argv[1]);
|
Viewer v(argv[1]);
|
||||||
v.run();
|
v.run();
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
Viewer::Viewer(const char * filename)
|
Viewer::Viewer(const char * filename)
|
||||||
{
|
{
|
||||||
m_dist = 5.0;
|
m_dist = 5.0;
|
||||||
m_dragging = false;
|
m_dragging = false;
|
||||||
if (!m_obj.load(filename))
|
if (!m_obj.load(filename))
|
||||||
{
|
{
|
||||||
cerr << "Error loading " << filename << endl;
|
cerr << "Error loading " << filename << endl;
|
||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Print out the object's size */
|
/* Print out the object's size */
|
||||||
const float * aabb = m_obj.getAABB();
|
const float * aabb = m_obj.getAABB();
|
||||||
cout << "Object width: " << (aabb[3]-aabb[0]) << endl;
|
cout << "Object width: " << (aabb[3]-aabb[0]) << endl;
|
||||||
cout << "Object depth: " << (aabb[4]-aabb[1]) << endl;
|
cout << "Object depth: " << (aabb[4]-aabb[1]) << endl;
|
||||||
cout << "Object height: " << (aabb[5]-aabb[2]) << endl;
|
cout << "Object height: " << (aabb[5]-aabb[2]) << endl;
|
||||||
}
|
}
|
||||||
|
|
||||||
void Viewer::initgl()
|
void Viewer::initgl()
|
||||||
{
|
{
|
||||||
glClearColor(0.0, 0.0, 0.0, 0.0);
|
glClearColor(0.0, 0.0, 0.0, 0.0);
|
||||||
glEnable(GL_DEPTH_TEST);
|
glEnable(GL_DEPTH_TEST);
|
||||||
glShadeModel(GL_SMOOTH);
|
glShadeModel(GL_SMOOTH);
|
||||||
glEnable(GL_LIGHTING);
|
glEnable(GL_LIGHTING);
|
||||||
glEnable(GL_LIGHT0);
|
glEnable(GL_LIGHT0);
|
||||||
glEnable(GL_CULL_FACE);
|
glEnable(GL_CULL_FACE);
|
||||||
glViewport(0, 0, WIDTH, HEIGHT);
|
glViewport(0, 0, WIDTH, HEIGHT);
|
||||||
setProjection();
|
setProjection();
|
||||||
glLoadIdentity();
|
glLoadIdentity();
|
||||||
glGetFloatv(GL_MODELVIEW_MATRIX, m_rotationMatrix);
|
glGetFloatv(GL_MODELVIEW_MATRIX, m_rotationMatrix);
|
||||||
// float pos[] = {0.0, -1.0, 0.0, 0.0};
|
// float pos[] = {0.0, -1.0, 0.0, 0.0};
|
||||||
// glLightfv(GL_LIGHT0, GL_POSITION, pos);
|
// glLightfv(GL_LIGHT0, GL_POSITION, pos);
|
||||||
GLfloat lightAmbient[] = {0.2, 0.2, 0.2, 1};
|
GLfloat lightAmbient[] = {0.2, 0.2, 0.2, 1};
|
||||||
glLightfv(GL_LIGHT0, GL_AMBIENT, lightAmbient);
|
glLightfv(GL_LIGHT0, GL_AMBIENT, lightAmbient);
|
||||||
GLfloat ambient[] = {0.0, 0.0, 0.0, 1.0};
|
GLfloat ambient[] = {0.0, 0.0, 0.0, 1.0};
|
||||||
glLightModelfv(GL_LIGHT_MODEL_AMBIENT, ambient);
|
glLightModelfv(GL_LIGHT_MODEL_AMBIENT, ambient);
|
||||||
}
|
}
|
||||||
|
|
||||||
void Viewer::setProjection()
|
void Viewer::setProjection()
|
||||||
{
|
{
|
||||||
glMatrixMode(GL_PROJECTION);
|
glMatrixMode(GL_PROJECTION);
|
||||||
glLoadIdentity();
|
glLoadIdentity();
|
||||||
gluPerspective(60.0, (GLfloat)WIDTH/(GLfloat)WIDTH, 0.01, 10000.0);
|
gluPerspective(60.0, (GLfloat)WIDTH/(GLfloat)WIDTH, 0.01, 10000.0);
|
||||||
// gluLookAt(0, m_dist, 0, 0, 0, 0, 0, 0, 1);
|
// gluLookAt(0, m_dist, 0, 0, 0, 0, 0, 0, 1);
|
||||||
glMatrixMode(GL_MODELVIEW);
|
glMatrixMode(GL_MODELVIEW);
|
||||||
}
|
}
|
||||||
|
|
||||||
void Viewer::display()
|
void Viewer::display()
|
||||||
{
|
{
|
||||||
glClear(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT);
|
glClear(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT);
|
||||||
glLoadIdentity();
|
glLoadIdentity();
|
||||||
gluLookAt(0, -m_dist, 0, 0, 0, 0, 0, 0, 1);
|
gluLookAt(0, -m_dist, 0, 0, 0, 0, 0, 0, 1);
|
||||||
glMultMatrixf(m_rotationMatrix);
|
glMultMatrixf(m_rotationMatrix);
|
||||||
m_obj.draw();
|
m_obj.draw();
|
||||||
SDL_GL_SwapBuffers();
|
SDL_GL_SwapBuffers();
|
||||||
}
|
}
|
||||||
|
|
||||||
void Viewer::run()
|
void Viewer::run()
|
||||||
{
|
{
|
||||||
initgl();
|
initgl();
|
||||||
display();
|
display();
|
||||||
SDL_Event event;
|
SDL_Event event;
|
||||||
while (SDL_WaitEvent(&event))
|
while (SDL_WaitEvent(&event))
|
||||||
{
|
{
|
||||||
if (event.type == SDL_QUIT)
|
if (event.type == SDL_QUIT)
|
||||||
break;
|
break;
|
||||||
else if (event.type == SDL_KEYDOWN)
|
else if (event.type == SDL_KEYDOWN)
|
||||||
{
|
{
|
||||||
if (event.key.keysym.sym == SDLK_ESCAPE)
|
if (event.key.keysym.sym == SDLK_ESCAPE)
|
||||||
break;
|
break;
|
||||||
if (event.key.keysym.sym == SDLK_q)
|
if (event.key.keysym.sym == SDLK_q)
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
else if (event.type == SDL_MOUSEBUTTONDOWN)
|
else if (event.type == SDL_MOUSEBUTTONDOWN)
|
||||||
{
|
{
|
||||||
if (event.button.button == SDL_BUTTON_LEFT)
|
if (event.button.button == SDL_BUTTON_LEFT)
|
||||||
{
|
{
|
||||||
if (event.button.state == SDL_PRESSED)
|
if (event.button.state == SDL_PRESSED)
|
||||||
{
|
{
|
||||||
m_dragging = true;
|
m_dragging = true;
|
||||||
m_startx = event.button.x;
|
m_startx = event.button.x;
|
||||||
m_starty = event.button.y;
|
m_starty = event.button.y;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else if (event.button.button == 4)
|
else if (event.button.button == 4)
|
||||||
{
|
{
|
||||||
m_dist *= 0.85;
|
m_dist *= 0.85;
|
||||||
if (m_dist < 1.0f)
|
if (m_dist < 1.0f)
|
||||||
m_dist = 1.0f;
|
m_dist = 1.0f;
|
||||||
setProjection();
|
setProjection();
|
||||||
display();
|
display();
|
||||||
}
|
}
|
||||||
else if (event.button.button == 5)
|
else if (event.button.button == 5)
|
||||||
{
|
{
|
||||||
m_dist *= 1.2;
|
m_dist *= 1.2;
|
||||||
setProjection();
|
setProjection();
|
||||||
display();
|
display();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else if (event.type == SDL_MOUSEBUTTONUP)
|
else if (event.type == SDL_MOUSEBUTTONUP)
|
||||||
{
|
{
|
||||||
if (event.button.button == SDL_BUTTON_LEFT)
|
if (event.button.button == SDL_BUTTON_LEFT)
|
||||||
{
|
{
|
||||||
m_dragging = false;
|
m_dragging = false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else if (event.type == SDL_MOUSEMOTION)
|
else if (event.type == SDL_MOUSEMOTION)
|
||||||
{
|
{
|
||||||
if (m_dragging)
|
if (m_dragging)
|
||||||
{
|
{
|
||||||
glLoadIdentity();
|
glLoadIdentity();
|
||||||
glRotatef(event.motion.y - m_starty, 1, 0, 0);
|
glRotatef(event.motion.y - m_starty, 1, 0, 0);
|
||||||
glRotatef(event.motion.x - m_startx, 0, 0, 1);
|
glRotatef(event.motion.x - m_startx, 0, 0, 1);
|
||||||
glMultMatrixf(m_rotationMatrix);
|
glMultMatrixf(m_rotationMatrix);
|
||||||
glGetFloatv(GL_MODELVIEW_MATRIX, m_rotationMatrix);
|
glGetFloatv(GL_MODELVIEW_MATRIX, m_rotationMatrix);
|
||||||
m_startx = event.motion.x;
|
m_startx = event.motion.x;
|
||||||
m_starty = event.motion.y;
|
m_starty = event.motion.y;
|
||||||
display();
|
display();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user