loaded a textured .obj!

git-svn-id: svn://anubis/misc/wfobj-view@32 bd8a9e45-a331-0410-811e-c64571078777
This commit is contained in:
josh 2008-01-30 04:14:50 +00:00
parent 9db9725187
commit 681dbf4ab6
2 changed files with 99 additions and 82 deletions

View File

@ -4,6 +4,8 @@
#include <GL/gl.h> #include <GL/gl.h>
#include <GL/glu.h> #include <GL/glu.h>
#include <iostream> #include <iostream>
#include "wfobj/WFObj.hh"
#include "TextureCache/TextureCache.hh"
using namespace std; using namespace std;
/* Some definitions */ /* Some definitions */
@ -11,53 +13,45 @@ using namespace std;
#define HEIGHT 800 #define HEIGHT 800
#define TITLE "Josh's Wavefront Object Viewer" #define TITLE "Josh's Wavefront Object Viewer"
/* Some global variables */ TextureCache textureCache;
float rotationMatrix[16];
int startx, starty;
bool dragging = false;
float dist = 5.0;
void initgl(void) GLuint loadTexture(const char * filename)
{ {
glClearColor(0.0, 0.0, 0.0, 0.0); return textureCache.load(filename);
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);
} }
void display(void) class Viewer
{ {
glClear(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT); public:
glLoadIdentity(); Viewer(const char * filename);
glMultMatrixf(rotationMatrix); void run();
glBegin(GL_QUADS);
glVertex3f(0.5, -0.5, 0.0); private:
glVertex3f(0.5, 0.5, 0.0); void initgl();
glVertex3f(-0.5, 0.5, 0.0); void display();
glVertex3f(-0.5, -0.5, 0.0); void setProjection();
glEnd();
SDL_GL_SwapBuffers(); 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 */ /* The program's main entry point */
int main(int argc, char * argv[]) int main(int argc, char * argv[])
{ {
if (argc < 2)
{
cerr << "Usage: " << argv[0] << " <filename>" << endl;
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 1; return -2;
} }
atexit(SDL_Quit); atexit(SDL_Quit);
@ -68,10 +62,61 @@ int main(int argc, char * argv[])
{ {
printf("Failed to set video mode!\n"); printf("Failed to set video mode!\n");
SDL_Quit(); SDL_Quit();
return 2; return -3;
} }
SDL_WM_SetCaption(TITLE, TITLE); 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(); initgl();
display(); display();
SDL_Event event; SDL_Event event;
@ -90,31 +135,23 @@ int main(int argc, char * argv[])
{ {
if (event.button.state == SDL_PRESSED) if (event.button.state == SDL_PRESSED)
{ {
dragging = true; m_dragging = true;
startx = event.button.x; m_startx = event.button.x;
starty = event.button.y; m_starty = event.button.y;
} }
} }
else if (event.button.button == 4) else if (event.button.button == 4)
{ {
dist *= 0.85; m_dist *= 0.85;
if (dist < 1.0f) if (m_dist < 1.0f)
dist = 1.0f; m_dist = 1.0f;
glMatrixMode(GL_PROJECTION); setProjection();
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);
display(); display();
} }
else if (event.button.button == 5) else if (event.button.button == 5)
{ {
dist *= 1.2; m_dist *= 1.2;
glMatrixMode(GL_PROJECTION); setProjection();
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);
display(); display();
} }
} }
@ -122,25 +159,23 @@ int main(int argc, char * argv[])
{ {
if (event.button.button == SDL_BUTTON_LEFT) if (event.button.button == SDL_BUTTON_LEFT)
{ {
dragging = false; m_dragging = false;
} }
} }
else if (event.type == SDL_MOUSEMOTION) else if (event.type == SDL_MOUSEMOTION)
{ {
if (dragging) if (m_dragging)
{ {
glLoadIdentity(); glLoadIdentity();
glRotatef(event.motion.y - starty, 1, 0, 0); glRotatef(event.motion.y - m_starty, 1, 0, 0);
glRotatef(event.motion.x - startx, 0, 0, 1); glRotatef(event.motion.x - m_startx, 0, 0, 1);
glMultMatrixf(rotationMatrix); glMultMatrixf(m_rotationMatrix);
glGetFloatv(GL_MODELVIEW_MATRIX, rotationMatrix); glGetFloatv(GL_MODELVIEW_MATRIX, m_rotationMatrix);
startx = event.motion.x; m_startx = event.motion.x;
starty = event.motion.y; m_starty = event.motion.y;
display(); display();
} }
} }
} }
return 0;
} }

View File

@ -14,7 +14,7 @@
using namespace std; using namespace std;
#define WHITESPACE " \n\r\t\v" #define WHITESPACE " \n\r\t\v"
#define DEBUGGL // #define DEBUGGL
/****** static functions ******/ /****** static functions ******/
@ -376,24 +376,6 @@ bool WFMtl::load(const string & filename, WFObj::loadTextureFunc_t loadTexture)
processInputLine(input); processInputLine(input);
} }
/* DEBUG */
map<string, vector< vector<string> > >::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(); ifs.close();
m_loadTexture = loadTexture; m_loadTexture = loadTexture;
m_fileName = filename; m_fileName = filename;