From 681dbf4ab6f5f7e4ee97ad6e63aaa8c64aed2a44 Mon Sep 17 00:00:00 2001 From: josh Date: Wed, 30 Jan 2008 04:14:50 +0000 Subject: [PATCH] loaded a textured .obj! git-svn-id: svn://anubis/misc/wfobj-view@32 bd8a9e45-a331-0410-811e-c64571078777 --- wfobj-view.cc | 161 ++++++++++++++++++++++++++++++------------------- wfobj/WFObj.cc | 20 +----- 2 files changed, 99 insertions(+), 82 deletions(-) diff --git a/wfobj-view.cc b/wfobj-view.cc index ce11b28..57073bf 100644 --- a/wfobj-view.cc +++ b/wfobj-view.cc @@ -4,6 +4,8 @@ #include #include #include +#include "wfobj/WFObj.hh" +#include "TextureCache/TextureCache.hh" using namespace std; /* Some definitions */ @@ -11,53 +13,45 @@ using namespace std; #define HEIGHT 800 #define TITLE "Josh's Wavefront Object Viewer" -/* Some global variables */ -float rotationMatrix[16]; -int startx, starty; -bool dragging = false; -float dist = 5.0; +TextureCache textureCache; -void initgl(void) +GLuint loadTexture(const char * filename) { - glClearColor(0.0, 0.0, 0.0, 0.0); - glEnable(GL_DEPTH_TEST); - glShadeModel(GL_SMOOTH); - glEnable(GL_LIGHTING); - glEnable(GL_LIGHT0); - float pos[] = {0.0, -1.0, 0.0, 0.0}; - glLightfv(GL_LIGHT0, GL_POSITION, pos); - glEnable(GL_CULL_FACE); - glViewport(0, 0, WIDTH, HEIGHT); - glMatrixMode(GL_PROJECTION); - glLoadIdentity(); - gluPerspective(60.0, (GLfloat)WIDTH/(GLfloat)WIDTH, 0.01, 10000.0); - gluLookAt(0, -dist, 0, 0, 0, 0, 0, 0, 1); - glMatrixMode(GL_MODELVIEW); - glLoadIdentity(); - glGetFloatv(GL_MODELVIEW_MATRIX, rotationMatrix); + return textureCache.load(filename); } -void display(void) +class Viewer { - glClear(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT); - glLoadIdentity(); - glMultMatrixf(rotationMatrix); - glBegin(GL_QUADS); - glVertex3f(0.5, -0.5, 0.0); - glVertex3f(0.5, 0.5, 0.0); - glVertex3f(-0.5, 0.5, 0.0); - glVertex3f(-0.5, -0.5, 0.0); - glEnd(); - SDL_GL_SwapBuffers(); -} +public: + Viewer(const char * filename); + void run(); + +private: + void initgl(); + void display(); + void setProjection(); + + WFObj m_obj; + GLuint m_list; + float m_rotationMatrix[16]; + int m_startx, m_starty; + bool m_dragging; + float m_dist; +}; /* The program's main entry point */ int main(int argc, char * argv[]) { + if (argc < 2) + { + cerr << "Usage: " << argv[0] << " " << endl; + return -1; + } + if (SDL_Init(SDL_INIT_VIDEO)) { printf("Failed to initialize SDL!\n"); - return 1; + return -2; } atexit(SDL_Quit); @@ -68,10 +62,61 @@ int main(int argc, char * argv[]) { printf("Failed to set video mode!\n"); SDL_Quit(); - return 2; + return -3; } SDL_WM_SetCaption(TITLE, TITLE); + Viewer v(argv[1]); + v.run(); + + return 0; +} + +Viewer::Viewer(const char * filename) +{ + m_dist = 5.0; + m_dragging = false; + m_obj.load(filename, &loadTexture); + m_list = m_obj.render(); +} + +void Viewer::initgl() +{ + glClearColor(0.0, 0.0, 0.0, 0.0); + glEnable(GL_DEPTH_TEST); + glShadeModel(GL_SMOOTH); + glEnable(GL_LIGHTING); + glEnable(GL_LIGHT0); + float pos[] = {0.0, -1.0, 0.0, 0.0}; + glLightfv(GL_LIGHT0, GL_POSITION, pos); + glEnable(GL_CULL_FACE); + glViewport(0, 0, WIDTH, HEIGHT); + setProjection(); + glLoadIdentity(); + glGetFloatv(GL_MODELVIEW_MATRIX, m_rotationMatrix); +} + +void Viewer::setProjection() +{ + glMatrixMode(GL_PROJECTION); + glLoadIdentity(); + gluPerspective(60.0, (GLfloat)WIDTH/(GLfloat)WIDTH, 0.01, 10000.0); + gluLookAt(0, -m_dist, 0, 0, 0, 0, 0, 0, 1); + glMatrixMode(GL_MODELVIEW); +} + +void Viewer::display() +{ + glClear(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT); + glLoadIdentity(); + glMultMatrixf(m_rotationMatrix); + if (m_list) + glCallList(m_list); + SDL_GL_SwapBuffers(); +} + +void Viewer::run() +{ initgl(); display(); SDL_Event event; @@ -90,31 +135,23 @@ int main(int argc, char * argv[]) { if (event.button.state == SDL_PRESSED) { - dragging = true; - startx = event.button.x; - starty = event.button.y; + m_dragging = true; + m_startx = event.button.x; + m_starty = event.button.y; } } else if (event.button.button == 4) { - dist *= 0.85; - if (dist < 1.0f) - dist = 1.0f; - glMatrixMode(GL_PROJECTION); - glLoadIdentity(); - gluPerspective(60.0, (GLfloat)WIDTH/(GLfloat)WIDTH, 0.01, 10000.0); - gluLookAt(0, -dist, 0, 0, 0, 0, 0, 0, 1); - glMatrixMode(GL_MODELVIEW); + m_dist *= 0.85; + if (m_dist < 1.0f) + m_dist = 1.0f; + setProjection(); display(); } else if (event.button.button == 5) { - dist *= 1.2; - glMatrixMode(GL_PROJECTION); - glLoadIdentity(); - gluPerspective(60.0, (GLfloat)WIDTH/(GLfloat)WIDTH, 0.01, 10000.0); - gluLookAt(0, -dist, 0, 0, 0, 0, 0, 0, 1); - glMatrixMode(GL_MODELVIEW); + m_dist *= 1.2; + setProjection(); display(); } } @@ -122,25 +159,23 @@ int main(int argc, char * argv[]) { if (event.button.button == SDL_BUTTON_LEFT) { - dragging = false; + m_dragging = false; } } else if (event.type == SDL_MOUSEMOTION) { - if (dragging) + if (m_dragging) { glLoadIdentity(); - glRotatef(event.motion.y - starty, 1, 0, 0); - glRotatef(event.motion.x - startx, 0, 0, 1); - glMultMatrixf(rotationMatrix); - glGetFloatv(GL_MODELVIEW_MATRIX, rotationMatrix); - startx = event.motion.x; - starty = event.motion.y; + glRotatef(event.motion.y - m_starty, 1, 0, 0); + glRotatef(event.motion.x - m_startx, 0, 0, 1); + glMultMatrixf(m_rotationMatrix); + glGetFloatv(GL_MODELVIEW_MATRIX, m_rotationMatrix); + m_startx = event.motion.x; + m_starty = event.motion.y; display(); } } } - return 0; } - diff --git a/wfobj/WFObj.cc b/wfobj/WFObj.cc index ef511c1..c091f15 100644 --- a/wfobj/WFObj.cc +++ b/wfobj/WFObj.cc @@ -14,7 +14,7 @@ using namespace std; #define WHITESPACE " \n\r\t\v" -#define DEBUGGL +// #define DEBUGGL /****** static functions ******/ @@ -376,24 +376,6 @@ bool WFMtl::load(const string & filename, WFObj::loadTextureFunc_t loadTexture) processInputLine(input); } - /* DEBUG */ - map > >::iterator it = m_data.begin(); - while (it != m_data.end()) - { - cout << "Material '" << it->first << "':" << endl; - for (int i = 0; i < it->second.size(); i++) - { - cout << " "; - for (int j = 0; j < it->second[i].size(); j++) - { - cout << '\'' << it->second[i][j] << "' "; - } - cout << endl; - } - it++; - } - /* END DEBUG */ - ifs.close(); m_loadTexture = loadTexture; m_fileName = filename;