Public feed #379387

Unnamed

public text v1 · immutable
Raw Edit
·published 2007-03-03 06:08 UTC
rendered paste body
/**
 * File viewer.cpp
 * Brief Implementation of viewer
 * This is a simple QGLWidget displaying an openGL wireframe box
 *
 * The OpenGL code is mostly borrowed from Brian Pauls "spin" example
 * in the Mesa distribution.
 * $ID$
 */

#include "viewer.h"


/**
 * Constructor that creates a viewer widget
 */
viewer::viewer( QWidget* parent, const char* name )
  : QGLWidget( parent, name )
{
   xRot = yRot = zRot = 0.0;
   scale = 1.25;
   object = 0;
}

viewer::~viewer()
{  glDeleteLists( object, 1 );
} // end destructor

/*--------------------------------------------------------------------------*/
/**
 * Paint the box. The actual openGL commands for drawing the box are
 * performed here.
 */
void viewer::paintGL()
{
  //glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  //glEnable( GL_DEPTH_TEST ); 
  //glLoadIdentity();
  //glTranslatef(0.0, 0.0, -10.0);
  // set the zoom according to the scale variable
  //glScalef(scale, scale, scale);
  // set the image rotation up according to xRot, yRot, zRot
  //glRotatef( xRot, 1.0, 0.0, 0.0);
  //glRotatef( yRot, 0.0, 1.0, 0.0);
  //glRotatef( zRot, 0.0, 0.0, 1.0);

  glCallList(object);
}

GLuint viewer::makeObject()
{  GLuint list;
   list = glGenLists(1);
   glNewList(list, GL_COMPILE);
     // set the initial color
     glColor3f( 1.0, 1.0, 1.0 );
     // points for triangle to draw Sierpinski Gasket
     drawModel();
   glEndList();
   return list;
} // end make object


/*--------------------------------------------------------------------------*/
/**
 * Set up the OpenGL rendering state, and define display list
 */

void viewer::initializeGL()
{
    
    glClearColor( 0.0, 0.0, 0.0, 0.0 ); // Let OpenGL clear to black
  object = makeObject(); // generate an OpenGL display list
  glShadeModel( GL_SMOOTH ); // we want smooth shading . . . try GL_FLAT if you like

}

/*--------------------------------------------------------------------------*/
/**
 * Set up the OpenGL view port, matrix mode, etc.
 */
void viewer::resizeGL( int w, int h )
{
  glViewport( 0, 0, (GLint)w, (GLint)h );
  glMatrixMode( GL_PROJECTION );
  glLoadIdentity();
  glOrtho(-10, 10, -10, 10, -5, 5);
  glMatrixMode( GL_MODELVIEW );
}

//*********************************************************
// Functions to rotate the model along the x, y, & z axes.
void viewer::setXRotation(int degrees)
{  xRot=(GLfloat)(degrees % 360);
   updateGL();
}

void viewer::setYRotation(int degrees)
{  yRot=(GLfloat)(degrees % 360);
   updateGL();
}

void viewer::setZRotation(int degrees)
{  zRot=(GLfloat)(degrees % 360);
   updateGL();
}

void viewer::loadFile()
{
 QFileDialog * n = new QFileDialog(this, "file dialog", TRUE);
 n->setFilter("MD2 Model (*.md2)");
 n->setMode(QFileDialog::ExistingFile);
 n->setViewMode(QFileDialog::Detail);
 n->show();
 if(n->exec() == QDialog::Accepted) {
  loader = MD2();
  n->hide();
  loader.LoadModel(n->selectedFile());
  //MD2 parsing
  object = makeObject();
 }

}

//**********************************************************
// Write Your Own Function To Set The Scale Here!!!!
//void viewer::setScale(int newscale){ }

//**********************************************************
// Draws a triangle
void viewer::drawFace(int index_a, int index_b, int index_c)
{  
  glClear(GL_COLOR_BUFFER_BIT);
  glColor3f(1, 1, 1);
  glBegin(GL_LINE_LOOP);
  glVertex3fv (loader.m_vertices[index_a]);
  glVertex3fv (loader.m_vertices[index_b]);
  glVertex3fv (loader.m_vertices[index_c]);
 glEnd();
 glFlush();
} // end draw triangle

//*******************************************************
// Function from class to draw the Sierpinski fractal
//   Recursion is FUN!
void viewer::drawModel()
{
 int index_a, index_b, index_c;
 for(int i = 0; i < loader.num_tris; i++)
 {
  index_a = loader.tris[i].index_xyz[0];
  index_b = loader.tris[i].index_xyz[1];
  index_c = loader.tris[i].index_xyz[2];   
  drawFace(index_a, index_b, index_c);
 }
} // end draw Sierpinski