예제 #1
0
/*
***************************************************************
*
* Draws the face normals on the weapon model
*
***************************************************************
*/
void GLWidget::drawWeaponFaceNormals()
{
    glDisable(GL_LIGHTING);
    glColor3f(0.0, 0.0, 1.0);
    for(int i = 0; i < weaponReader_.numberOfTriangles(); i++)
    {
        int indexOne = 0;
        int indexTwo = 0;
        int indexThree = 0;
        weaponReader_.retrieveTriangleVertexIndicies(i, &indexOne,
                &indexTwo, &indexThree);

        MathVector* faceNormals = weaponReader_.faceNormals()->at(i);


        VertexCoordinate vertexOne = weaponReader_.retrieveVertexCoordinatesAt(indexOne);
        VertexCoordinate vertexTwo = weaponReader_.retrieveVertexCoordinatesAt(indexTwo);
        VertexCoordinate vertexThree = weaponReader_.retrieveVertexCoordinatesAt(indexThree);

        VertexCoordinate middleOfTriangle;
        middleOfTriangle.x = ((vertexOne.x + vertexTwo.x + vertexThree.x)/3);
        middleOfTriangle.y = ((vertexOne.y + vertexTwo.y + vertexThree.y)/3);
        middleOfTriangle.z = ((vertexOne.z + vertexTwo.z + vertexThree.z)/3);

        glBegin(GL_LINES);
        glVertex3f(middleOfTriangle.x, middleOfTriangle.y,
                   middleOfTriangle.z);
        glVertex3f((faceNormals->x()*2)+middleOfTriangle.x, (faceNormals->y()*2)+middleOfTriangle.y,
                   (faceNormals->z()*2)+middleOfTriangle.z);
        glEnd();
    }
    glEnable(GL_LIGHTING);
}
예제 #2
0
파일: tri_box.cpp 프로젝트: bsumirak/ugcore
///
//	TriangleBoxIntersection()
//
//		Determine if a bounding box and triangle intersect
//
bool TriangleBoxIntersection(const MathVector<3>& p0, const MathVector<3>& p1,
							 const MathVector<3>& p2,
							 const MathVector<3>& boxMin, const MathVector<3>& boxMax)
{
	vector3 Trans;
	vector3 Scale(1.0, 1.0, 1.0);
	vector3 TransMax;
	TRI		TestTri;

	///
	//	Compute the scale and transform required to make BBox
	//	a voxel
	//
	Trans.x() = (boxMax.x() + boxMin.x()) / 2;
	Trans.y() = (boxMax.y() + boxMin.y()) / 2;
	Trans.z() = (boxMax.z() + boxMin.z()) / 2;

	VecSubtract(TransMax, boxMax, Trans);
	
	if(TransMax.x() != 0)
		Scale.x() = 0.5f / TransMax.x();
	if(TransMax.y() != 0)
		Scale.y() = 0.5f / TransMax.y();
	if(TransMax.z() != 0)
		Scale.z() = 0.5f / TransMax.z();

	///
	//	Put the triangle in voxel space
	//	
	TestTri.m_P[0].x() = (p0.x() - Trans.x()) * Scale.x();
	TestTri.m_P[0].y() = (p0.y() - Trans.y()) * Scale.y();
	TestTri.m_P[0].z() = (p0.z() - Trans.z()) * Scale.z();

	TestTri.m_P[1].x() = (p1.x() - Trans.x()) * Scale.x();
	TestTri.m_P[1].y() = (p1.y() - Trans.y()) * Scale.y();
	TestTri.m_P[1].z() = (p1.z() - Trans.z()) * Scale.z();
	
	TestTri.m_P[2].x() = (p2.x() - Trans.x()) * Scale.x();
	TestTri.m_P[2].y() = (p2.y() - Trans.y()) * Scale.y();
	TestTri.m_P[2].z() = (p2.z() - Trans.z()) * Scale.z();

	///
	//	Test against the voxel
	//
	return(TriCubeIntersection(TestTri) == INSIDE);	
}
예제 #3
0
/*
***************************************************************
*
* Draws the vertex normals on the weapon model
*
***************************************************************
*/
void GLWidget::drawWeaponVertexNormals()
{
    glDisable(GL_LIGHTING);
    glColor3f(1.0, 0.0, 0.0);
    for(int i = 0; i < weaponReader_.numberOfVertices(); i++)
    {
        MathVector* vertexNormal = weaponReader_.vertexNormals()->at(i);
        VertexCoordinate vertexCoordinate = weaponReader_.retrieveVertexCoordinatesAt(i);

        glBegin(GL_LINES);
        glVertex3f(vertexCoordinate.x, vertexCoordinate.y,
                   vertexCoordinate.z);
        glVertex3f((vertexNormal->x()*2)+vertexCoordinate.x, (vertexNormal->y()*2)+vertexCoordinate.y,
                   (vertexNormal->z()*2)+vertexCoordinate.z);
        glEnd();
    }
    glEnable(GL_LIGHTING);
}
예제 #4
0
/*
***************************************************************
*
*  Draws the weapon model that is currently opened in the weaponReader_
*
***************************************************************
*/
void GLWidget::drawWeapon()
{
    if(textureLoadedForWeapon_)
    {
        glEnable(GL_TEXTURE_2D);
        glBindTexture(GL_TEXTURE_2D, weaponTexture_);
    }
    else
    {
        glColor3f(1.0, 0.0, 0.0);
    }

    glBegin(GL_TRIANGLES);

    for(int currentTriangle = 0;
            currentTriangle < weaponReader_.numberOfTriangles(); currentTriangle++)
    {
        int indexOne = 0;
        int indexTwo = 0;
        int indexThree = 0;
        weaponReader_.retrieveTriangleVertexIndicies(currentTriangle, &indexOne,
                &indexTwo, &indexThree);

        int texOne = 0;
        int texTwo = 0;
        int texThree = 0;
        weaponReader_.retrieveTriangleTextureIndicies(currentTriangle, &texOne,
                &texTwo, &texThree);

        VertexCoordinate vertexOne = weaponReader_.retrieveVertexCoordinatesAt(indexOne);
        VertexCoordinate vertexTwo = weaponReader_.retrieveVertexCoordinatesAt(indexTwo);
        VertexCoordinate vertexThree = weaponReader_.retrieveVertexCoordinatesAt(indexThree);

        TextureCoordinate textureOne = weaponReader_.retrieveTextureCoordinateAt(texOne);
        TextureCoordinate textureTwo = weaponReader_.retrieveTextureCoordinateAt(texTwo);
        TextureCoordinate textureThree = weaponReader_.retrieveTextureCoordinateAt(texThree);

        //Point One.
        MathVector* vector;
        if(displayMode_== DrawingDefines::FLAT_SHADING)
        {
            vector = weaponReader_.faceNormals()->at(currentTriangle);
        }
        else
        {
            vector = weaponReader_.vertexNormals()->at(indexOne);
        }
        glNormal3f(vector->x(), vector->y(), vector->z());
        glTexCoord2f((float) textureOne.u/weaponReader_.skinWidth(),
                     (float) textureOne.v/weaponReader_.skinHeight());
        glVertex3f(vertexOne.x, vertexOne.y, vertexOne.z);

        //Point Two
        if(displayMode_== DrawingDefines::FLAT_SHADING)
        {
            vector = weaponReader_.faceNormals()->at(currentTriangle);
        }
        else
        {
            vector = weaponReader_.vertexNormals()->at(indexTwo);
        }
        glNormal3f(vector->x(), vector->y(), vector->z());
        glTexCoord2f((float) textureTwo.u/weaponReader_.skinWidth(),
                     (float) textureTwo.v/weaponReader_.skinHeight());
        glVertex3f(vertexTwo.x, vertexTwo.y, vertexTwo.z);

        //Point Three
        if(displayMode_== DrawingDefines::FLAT_SHADING)
        {
            vector = weaponReader_.faceNormals()->at(currentTriangle);
        }
        else
        {
            vector = weaponReader_.vertexNormals()->at(indexThree);
        }
        vector = weaponReader_.vertexNormals()->at(indexThree);
        glNormal3f(vector->x(), vector->y(), vector->z());
        glTexCoord2f((float) textureThree.u/weaponReader_.skinWidth(),
                     (float) textureThree.v/weaponReader_.skinHeight());
        glVertex3f(vertexThree.x, vertexThree.y, vertexThree.z);
    }
    glEnd();

    glDisable(GL_TEXTURE_2D);
}
예제 #5
0
/*
***************************************************************
*
*  Draws the model based on the opened MD2 model file
*
***************************************************************
*/
void GLWidget::drawModel()
{
    switch(displayMode_)
    {
    case DrawingDefines::WIREFRAME:
    {
        glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
        break;
    }
    case DrawingDefines::FLAT_SHADING:
    {
        glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
        glShadeModel(GL_FLAT);
        break;
    }
    case DrawingDefines::SMOOTH_SHADING:
    {
        glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
        glShadeModel(GL_SMOOTH);
        break;
    }
    }

    if(textureLoadedForMd2Model_)
    {
        glEnable(GL_TEXTURE_2D);
        glBindTexture(GL_TEXTURE_2D, modelTexture_);
    }
    else
    {
        glColor3f(0.0, 1.0, 0.0);
    }

    glBegin(GL_TRIANGLES);

    for(int currentTriangle = 0;
            currentTriangle < modelReader_.numberOfTriangles(); currentTriangle++)
    {
        int indexOne = 0;
        int indexTwo = 0;
        int indexThree = 0;
        modelReader_.retrieveTriangleVertexIndicies(currentTriangle, &indexOne,
                &indexTwo, &indexThree);

        int texOne = 0;
        int texTwo = 0;
        int texThree = 0;
        modelReader_.retrieveTriangleTextureIndicies(currentTriangle, &texOne,
                &texTwo, &texThree);

        VertexCoordinate vertexOne = modelReader_.retrieveVertexCoordinatesAt(indexOne);
        VertexCoordinate vertexTwo = modelReader_.retrieveVertexCoordinatesAt(indexTwo);
        VertexCoordinate vertexThree = modelReader_.retrieveVertexCoordinatesAt(indexThree);

        TextureCoordinate textureOne = modelReader_.retrieveTextureCoordinateAt(texOne);
        TextureCoordinate textureTwo = modelReader_.retrieveTextureCoordinateAt(texTwo);
        TextureCoordinate textureThree = modelReader_.retrieveTextureCoordinateAt(texThree);

        //Point One.
        MathVector* vector;
        if(displayMode_== DrawingDefines::FLAT_SHADING)
        {
            vector = modelReader_.faceNormals()->at(currentTriangle);
        }
        else
        {
            vector = modelReader_.vertexNormals()->at(indexOne);
        }
        glNormal3f(vector->x(), vector->y(), vector->z());
        glTexCoord2f((float) textureOne.u/modelReader_.skinWidth(),
                     (float) textureOne.v/modelReader_.skinHeight());
        glVertex3f(vertexOne.x, vertexOne.y, vertexOne.z);

        //Point Two
        if(displayMode_== DrawingDefines::FLAT_SHADING)
        {
            vector = modelReader_.faceNormals()->at(currentTriangle);
        }
        else
        {
            vector = modelReader_.vertexNormals()->at(indexTwo);
        }
        glNormal3f(vector->x(), vector->y(), vector->z());
        glTexCoord2f((float) textureTwo.u/modelReader_.skinWidth(),
                     (float) textureTwo.v/modelReader_.skinHeight());
        glVertex3f(vertexTwo.x, vertexTwo.y, vertexTwo.z);

        //Point Three
        if(displayMode_== DrawingDefines::FLAT_SHADING)
        {
            vector = modelReader_.faceNormals()->at(currentTriangle);
        }
        else
        {
            vector = modelReader_.vertexNormals()->at(indexThree);
        }
        glNormal3f(vector->x(), vector->y(), vector->z());
        glTexCoord2f((float) textureThree.u/modelReader_.skinWidth(),
                     (float) textureThree.v/modelReader_.skinHeight());
        glVertex3f(vertexThree.x, vertexThree.y, vertexThree.z);
    }
    glEnd();

    glDisable(GL_TEXTURE_2D);

    if(weaponLoaded_)
    {
        drawWeapon();
    }
}