//
// "$Id: qtcube.cxx,v 1.3 2003/06/22 23:44:47 mike Exp $"
//
// OpenGL example using Qt.
//
// Copyright 2003 by Michael Sweet, All Rights Reserved.
//


//
// Include necessary headers...
//

#include <qapplication.h>
#include <qgl.h>


//
// OpenGL subclass for drawing the rotating cube...
//

class Cube : public QGLWidget
{
  float		rotation[3],		// Rotation of cube
		rate[3];		// Rotation rates
  int		mouse_button,		// Starting mouse button
		mouse_x,		// Starting mouse X
		mouse_y;		// Starting mouse Y
  int		width,			// Width of window
		height;			// Height of window
  GLuint	font;			// List base for text font

  public:

  Cube(QWidget *parent, const char *name);
  ~Cube();

  protected:

  void initializeGL();
  void paintGL();
  void resizeGL(int w, int h);
};


//
// 'main()' - Main entry for example program.
//

int					// O - Exit status
main(int  argc,				// I - Number of command-line args
     char *argv[])			// I - Command-line args
{
  QApplication	app(argc, argv);	// Application window


  if (!QGLFormat::hasOpenGL())
    qFatal("Sorry, can't run qtcube without OpenGL!");

  Cube		cube(0, "QtCube");	// Cube window

  app.setMainWidget(&cube);

  cube.resize(774, 588);
  cube.show();

  return (app.exec());
}


//
// 'Cube::Cube()' - Create a cube window.
//

Cube::Cube(QWidget    *parent,		// I - Parent widget
           const char *name)		// I - Name of widget
  : QGLWidget(parent, name)
{
  width       = 400;
  height      = 400;
  rotation[0] = 45.0f;
  rotation[1] = 45.0f;
  rotation[2] = 45.0f;
  rate[0]     = 1.0f;
  rate[1]     = 1.0f;
  rate[2]     = 1.0f;
}


//
// 'Cube::~Cube()' - Destroy a cube window.
//

Cube::~Cube()
{
}


//
// 'Cube::intiializeGL()' - Initialization method.
//

void
Cube::initializeGL()
{
}


//
// 'Cube::paintGL()' - Display method.
//

void
Cube::paintGL()
{
  int			i, j;		// Looping vars
  static const GLfloat	corners[8][3] =	// Corner vertices
			{
			  {  1.0f,  1.0f,  1.0f },	// Front top right
			  {  1.0f, -1.0f,  1.0f },	// Front bottom right
			  { -1.0f, -1.0f,  1.0f },	// Front bottom left
			  { -1.0f,  1.0f,  1.0f },	// Front top left
			  {  1.0f,  1.0f, -1.0f },	// Back top right
			  {  1.0f, -1.0f, -1.0f },	// Back bottom right
			  { -1.0f, -1.0f, -1.0f },	// Back bottom left
			  { -1.0f,  1.0f, -1.0f }	// Back top left
			};
  static const int	sides[6][4] =	// Sides
			{
			  { 0, 1, 2, 3 },		// Front
			  { 4, 5, 6, 7 },		// Back
			  { 0, 1, 5, 4 },		// Right
			  { 2, 3, 7, 6 },		// Left
			  { 0, 3, 7, 4 },		// Top
			  { 1, 2, 6, 5 }		// Bottom
			};
  static const GLfloat	colors[6][3] =	// Colors
			{
			  { 1.0f, 0.0f, 0.0f },		// Red
			  { 0.0f, 1.0f, 0.0f },		// Green
			  { 1.0f, 1.0f, 0.0f },		// Yellow
			  { 0.0f, 0.0f, 1.0f },		// Blue
			  { 1.0f, 0.0f, 1.0f },		// Magenta
			  { 0.0f, 1.0f, 1.0f }		// Cyan
			};


  // Clear the window...
  glViewport(0, 0, width, height);
  glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
  glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

  // Setup the matrices...
  glMatrixMode(GL_PROJECTION);
  glLoadIdentity();
  glOrtho(-2.0f, 2.0f,
          -2.0f * height / width, 2.0f * height / width,
          -2.0f, 2.0f);

  glMatrixMode(GL_MODELVIEW);
  glLoadIdentity();
  glRotatef(rotation[0], 1.0f, 0.0f, 0.0f);
  glRotatef(rotation[1], 0.0f, 1.0f, 0.0f);
  glRotatef(rotation[2], 0.0f, 0.0f, 1.0f);

  // Draw the cube...
  glEnable(GL_DEPTH_TEST);

  glBegin(GL_QUADS);

  for (i = 0; i < 6; i ++)
  {
    glColor3fv(colors[i]);
    for (j = 0; j < 4; j ++)
      glVertex3fv(corners[sides[i][j]]);
  }

  glEnd();
}


//
// 'Cube::resizeGL()' - Resize method.
//

void
Cube::resizeGL(int w,			// I - New width
               int h)			// I - New height
{
  // Save the current width and height...
  width  = w;
  height = h;
}


//
// End of "$Id: qtcube.cxx,v 1.3 2003/06/22 23:44:47 mike Exp $".
//
