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/glu.h>
#include <iostream>
#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] << " <filename>" << 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;
}

View File

@ -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<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();
m_loadTexture = loadTexture;
m_fileName = filename;