Lab 12

Code for Creating a Button and Opening a File

After you create the 'openFile()' slot using the QT designer, add the following header to your GL widget's header file:

#include <qtfiledialog.h>

The following 2 lines should also have been added to the header file:

public slots:
    void openFile();

In the source file for the widget, the 'openFile()' function should also have been added:

void GLScene::openFile()
{
  // ... insert code to open file dialog here
}

The following line will open a file dialog:

    QString s = QFileDialog::getOpenFileName(
        QString::null,
        "Model FIles (*.md2)",
        this,
        "Choose a File");

Documentation for this function is here. The string that is returned will be the filename that was chosen in the dialog (or QString::null if it was cancelled).

You should now be able to open your file with a line like this ('md2File' is an object of type MD2 that has already been initialized):

    md2File->LoadModel(s);


Code for drawing a frame of the MD2 file

Once you have the MD2 file open, you can use the MD2 class to access the data to draw the object. A sample function that will draw one frame of the scene might look like this (note, no texture mapping or normals have been implemented):

void GLScene::drawFrame(int n){

	glBegin (GL_TRIANGLES);
	/* Draw each triangle */

	int nrTriangles = md2File->num_tris;
	int nrVertices  = md2File->num_xyz;

	vec3_t  *ptrverts;  // pointer on m_vertices
	//we moved the pointer over to the first
	//vertex of the frame we are interested in
	ptrverts = &md2File->m_vertices[nrVertices * n];

	for (int i = 0; i < nrTriangles; ++i)
	{

		//     A
		//    /  \
		//   B    C

		int vertexIndexA = md2File->tris[i].index_xyz[0];
		int vertexIndexB = md2File->tris[i].index_xyz[1];
		int vertexIndexC = md2File->tris[i].index_xyz[2];

		glVertex3fv(ptrverts[vertexIndexA]);
		glVertex3fv(ptrverts[vertexIndexB]);
		glVertex3fv(ptrverts[vertexIndexC]);
	}


	glEnd ();
}