Пример #1
0
BoundingBoxClass::BoundingBoxClass(String a_sInstanceName)
{
	m_pMeshOBB = nullptr;
	m_v3CentroidOBB = vector3(0.0f, 0.0f, 0.0f);
	m_v3ColorOBB = MEBLUE;
	m_m4ModelToWorldOBB = matrix4(1.0f);
	m_bVisibleOBB = false;

	m_pMeshAABB = nullptr;
	m_v3CentroidAABB = vector3(0.0f,0.0f,0.0f);
	m_v3ColorAABB = MEWHITE;
	m_m4ModelToWorldAABB = matrix4(1.0f);
	m_bVisibleAABB = false;

	m_pModelMngr = ModelManagerClass::GetInstance();
	m_sInstance = a_sInstanceName;

	int nInstance = m_pModelMngr->IdentifyInstance(m_sInstance);
	if (nInstance == -1)
		return;

	CalculateOBB(m_sInstance);
	m_m4ModelToWorldOBB = m_pModelMngr->GetModelMatrix(m_sInstance);

	m_pMeshOBB = new PrimitiveWireClass();
	m_pMeshOBB->GenerateCube(1.0f, MEBLUE);
	SetModelMatrixOBB(m_m4ModelToWorldOBB);

	CalculateAABB(m_sInstance);
	m_m4ModelToWorldAABB = m_pModelMngr->GetModelMatrix(m_sInstance);

	m_pMeshAABB = new PrimitiveWireClass();
	m_pMeshAABB->GenerateCube(1.0f, MEWHITE);
	SetModelMatrixAABB(m_m4ModelToWorldAABB);
}
Пример #2
0
matrix4 float4::transposeMult(const float4 &other) const
{
#ifdef __SSE__
    return matrix4(_mm_mul_ps(v, _mm_shuffle_ps(other.v, other.v, _MM_SHUFFLE(0, 0, 0, 0))),
                   _mm_mul_ps(v, _mm_shuffle_ps(other.v, other.v, _MM_SHUFFLE(1, 1, 1, 1))),
                   _mm_mul_ps(v, _mm_shuffle_ps(other.v, other.v, _MM_SHUFFLE(2, 2, 2, 2))),
                   _mm_mul_ps(v, _mm_shuffle_ps(other.v, other.v, _MM_SHUFFLE(3, 3, 3, 3))));
#else
    return matrix4(*this*other.x, *this*other.y, *this*other.z, *this*other.w);
#endif
}
Пример #3
0
void AppClass::Update(void)
{
	//This matrices will just orient the objects to the camera (which is looking from
	matrix4 rotateX = glm::rotate(matrix4(IDENTITY), 90.0f, vector3(1.0f, 0.0f, 0.0f));
	matrix4 rotateY = glm::rotate(matrix4(IDENTITY), 90.0f, vector3(0.0f, 1.0f, 0.0f));

	//First person camera movement
	if (m_bFPC == true)
		CameraRotation();

	printf("Earth Day: %.3f, Moon Day: %.3f\r", m_fEarthTimer, m_fMoonTimer);//print the Frames per Second
}
Пример #4
0
void AppClass::Display(void)
{
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // clear the window

	m_pGrid->Render(); //renders the XY grid with a 100% scale

	m_pMeshMngr->Render(); //renders the render list

	m_pTorus->Render(glm::translate(matrix4(1.0f), m_v3Torus), REGREEN);
	m_pCone->Render(glm::translate(matrix4(1.0f), m_v3Cone), REBLUE);
	

	m_pGLSystem->GLSwapBuffers(); //Swaps the OpenGL buffers
}
Пример #5
0
matrix4 matrix4::affineInverse() const
{
    return matrix4(float4(x.x, y.x, z.x, 0),
                   float4(x.y, y.y, z.y, 0),
                   float4(x.z, y.z, z.z, 0),
                   float4(-w*x, -w*y, -w*z, 1.0));
}
//--- Non Standard Singleton Methods
void BoundingBoxManagerSingleton::GenerateBoundingBox(matrix4 a_mModelToWorld, String a_sInstanceName)
{
	MeshManagerSingleton* pMeshMngr = MeshManagerSingleton::GetInstance();
	//Verify the instance is loaded
	if(pMeshMngr->IsInstanceCreated(a_sInstanceName))
	{//if it is check if the Box has already been created
		int nBox = IdentifyBox(a_sInstanceName);
		if(nBox == -1)
		{
			//Create a new bounding Box
			BoundingBoxClass* pBB = new BoundingBoxClass();
			//construct its information out of the instance name
			pBB->GenerateOrientedBoundingBox(a_sInstanceName);
			//Push the Box back into the list
			m_lBox.push_back(pBB);
			//Push a new matrix into the list
			m_lMatrix.push_back(matrix4(IDENTITY));
			//Specify the color the Box is going to have
			m_lColor.push_back(vector3(1.0f));
			//Increase the number of Boxes
			m_nBoxs++;
		}
		else //If the box has already been created you will need to check its global orientation
		{
			m_lBox[nBox]->GenerateAxisAlignedBoundingBox(a_mModelToWorld);
		}
		nBox = IdentifyBox(a_sInstanceName);
		m_lMatrix[nBox] = a_mModelToWorld;
	}
}
Пример #7
0
void CRespawn::Render()
{
    position2d<s32> pos;
    for ( int i = 0; i < points.size(); i++ )
    {
      WideString wstr = "(S) ";
      wstr += points[i]->getActorName().c_str();
      pos = IRR.smgr->getSceneCollisionManager()->getScreenCoordinatesFrom3DPosition( points[i]->getPosition(), IRR.smgr->getActiveCamera() );
      IRR.gui->getBuiltInFont()->draw( wstr.c_str(), core::rect<s32>( pos.X, pos.Y, pos.X + 100, pos.Y + 50 ), irr::video::SColor( 255, 15, 85, 10 ), false, true );
    }

    // draw 3d stuff
    IRR.video->setTransform( ETS_WORLD, matrix4() );

    SMaterial m; 
    m.Lighting = false; 
    m.BackfaceCulling = false;
    IRR.video->setMaterial( m );
    vector3df vP;
    for ( int i = 0; i < points.size(); i++ )
    {
      vP = points[i]->getPosition();
      IRR.video->draw3DBox( aabbox3df( vP - vector3df( points[i]->radius, points[i]->radius, points[i]->radius ), vP + vector3df( points[i]->radius, points[i]->radius, points[i]->radius ) ), SColor( 255, 105, 22, 90 ) );
    }
}
Пример #8
0
matrix4 matrix4::diagonal(const float4 &diagonal)
{
    return matrix4(float4(diagonal.x, 0.0f, 0.0f, 0.0f),
                   float4(0.0f, diagonal.y, 0.0f, 0.0f),
                   float4(0.0f, 0.0f, diagonal.z, 0.0f),
                   float4(0.0f, 0.0f, 0.0f, diagonal.w));
}
Пример #9
0
matrix4 matrix4::transposed() const
{
#ifdef __SSE__
    __m128 tmp3, tmp2, tmp1, tmp0;
    tmp0 = _mm_unpacklo_ps(x.v, y.v);
    tmp2 = _mm_unpacklo_ps(z.v, w.v);
    tmp1 = _mm_unpackhi_ps(x.v, y.v);
    tmp3 = _mm_unpackhi_ps(z.v, w.v);

    return matrix4(_mm_movelh_ps(tmp0, tmp2), _mm_movehl_ps(tmp2, tmp0), _mm_movelh_ps(tmp1, tmp3), _mm_movehl_ps(tmp3, tmp1));
#else
    return matrix4(float4(x.x, y.x, z.x, w.x),
                   float4(x.y, y.y, z.y, w.y),
                   float4(x.z, y.z, z.z, w.z),
                   float4(x.w, y.w, z.w, w.w));
#endif
}
Пример #10
0
Float32List toMatrix4(const SkMatrix& sk_matrix)
{
    Float32List matrix4(Dart_NewTypedData(Dart_TypedData_kFloat32, 16));
    for (intptr_t i = 0; i < 9; ++i)
        matrix4[kSkMatrixIndexToMatrix4Index[i]] = sk_matrix[i];
    matrix4[10] = 1.0; // Identity along the z axis.
    return matrix4;
}
Пример #11
0
void BoundingBoxClass::RenderOBB(vector3 a_v3Color)
{
	if (!m_bVisibleOBB)
		return;

	vector3 v3Color = a_v3Color == MEDEFAULT ? m_v3ColorOBB : a_v3Color;

	m_pMeshOBB->Render(matrix4(1.0f), v3Color);
}
Пример #12
0
void MatrixTest::testMatrixOperations ()
{
  // Test computing determinant
  Matrix<3,3,int> matrix;
  assignList(matrix) = 1, 2, 3,
                       4, 5, 6,
                       7, 8, 9;
  validateEquals (0, det3x3(matrix));
  assignList(matrix) = -1, 2, 3,
                       4, -5, 2,
                       -2, 3, 1;
  validateEquals (1, det3x3(matrix)); // computed by octave

  // Test streaming
  Matrix<2,2,int> matrix2;
  assignList(matrix2) = 1, 2, 3, 4;
  std::ostringstream stream;
  stream << matrix2;
  validateEquals (stream.str(), std::string("1, 2; 3, 4"));
  // Test matrix multiply scalar
  matrix2 =matrix2*2;
  validateEquals(matrix2(0,0),2);
  validateEquals(matrix2(0,1),4);
  validateEquals(matrix2(1,0),6);
  validateEquals(matrix2(1,1),8);
  // Test matrix add matrix
//  Matrix<2,2,int> matrix3;
//  assignList(matrix3) = 1, 2, 3, 4;
//  matrix3=matrix3+matrix2;
//  validateEquals(matrix3(0,0),3);
//  validateEquals(matrix3(0,1),6);
//  validateEquals(matrix3(1,0),9);
//  validateEquals(matrix3(1,1),12);
  // Test matrix square
  Matrix<2,2,double> matrix4;
  assignList(matrix4) = 4.0, 9.0, 16.0, 25.0;
  matrix4=sqrt(matrix4);
  validateEquals(matrix4(0,0),2.0);
  validateEquals(matrix4(0,1),3.0);
  validateEquals(matrix4(1,0),4.0);
  validateEquals(matrix4(1,1),5.0);
}
Пример #13
0
void BoundingBoxClass::RenderAABB(vector3 a_v3Color)
{
	if (!m_bVisibleAABB)
		return;

	vector3 v3Color = a_v3Color == MEDEFAULT ? m_v3ColorAABB : a_v3Color;

	// Not completed, need box manager to test if things are rendering properly

	m_pMeshAABB->Render(matrix4(1.0f), v3Color);
}
Пример #14
0
void AppClass::InitVariables(void)
{
	// Set the camera into the initial position
	m_pCamera->SetUpVector(vector3(0.0f, 0.0f, -1.0f));
	m_pCamera->SetPositionAndView(vector3(0.0f, 25.0f, 0.0f), vector3(0.0f, 0.0f, 0.0f));

	//Starting the values of the transformation matricies to the identity (defined under MEDEFINIONS.h)
	m_m4Sun = matrix4(IDENTITY); 
	m_m4Earth = matrix4(IDENTITY);
	m_m4Moon = matrix4(IDENTITY);

	//Starting the new primitive objects as PrimitiveClass objects (like MyPrimitive but inside ReEng.dll)
	m_pSun = new PrimitiveClass();
	m_pEarth = new PrimitiveClass();
	m_pMoon = new PrimitiveClass();

	//Initializing the primitives
	m_pSun->GenerateSphere(5.936f, 5, REYELLOW);
	m_pEarth->GenerateTube(0.524f, 0.45f, 0.3f, 10, REBLUE);
	m_pMoon->GenerateTube(0.524f * 0.27f, 0.45f * 0.27f, 0.3f * 0.27f, 10, REWHITE);
}
Пример #15
0
void AppClass::Display(void)
{
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // clear the window

	m_pGrid->Render(); //renders the XY grid with a 100% scale

	m_pMeshMngr->Render(); //renders the render list

	m_pTorus->Render(glm::translate(matrix4(1.0f), m_v3Torus), REGREEN);

	m_pCone->Render(glm::translate(matrix4(1.0f), m_v3Cone), REBLUE);

	// render spheres
	if (!bs_Torus->IsColliding(bs_Cone))
	{
		bs_Torus->m_pSphere->Render(glm::translate(matrix4(1.0f), bs_Torus->center), REWHITE);
		bs_Cone->m_pSphere->Render(glm::translate(matrix4(1.0f), bs_Cone->center), REWHITE);
	}
	else
	{
		bs_Torus->m_pSphere->Render(glm::translate(matrix4(1.0f), bs_Torus->center), RERED);
		bs_Cone->m_pSphere->Render(glm::translate(matrix4(1.0f), bs_Cone->center), RERED);
	}

	m_pGLSystem->GLSwapBuffers(); //Swaps the OpenGL buffers
}
Пример #16
0
void MyCameraSingleton::Init(void)
{
	m_bFPS = true;

	m_nMode = CAMERAMODE::CAMPERSP;

	m_fFOV = 45.0f;

	m_v2NearFar = vector2(0.001f, 1000.0f);

	m_v3Position = vector3(0.0f, 0.0f, 5.0f);
	m_v3Target = vector3(0.0f, 0.0f, 0.0f);
	m_v3Top = vector3(0.0f, 1.0f, 0.0f);

	m_v3Forward = vector3(0.0f, 0.0f, -1.0f);
	m_v3Upward = vector3(0.0f, 1.0f, 0.0f);
	m_v3Rightward = vector3(1.0f, 0.0f, 0.0f);

	m_v3PitchYawRoll = vector3(0.0f);

	m_m4Projection = matrix4(1.0f);
	m_m4View = matrix4(1.0f);
}
Пример #17
0
void AppClass::InitVariables(void)
{
	//Set the camera at a position other than the default
	m_pCameraMngr->SetPositionTargetAndView(vector3(0.0f, 10.0f, 25.0f), vector3(0.0f, 0.0f, 0.0f), REAXISY);

	std::vector<vector3> vVectors;
	m_nCubes = 100;
	for (int n = 0; n < m_nCubes; n++)
	{
		vVectors.push_back(vector3(0.0f, 2 * n + 2, -0.1 * n));
		m_pMeshMngr->LoadModel("Sorted\\Polycube.obj", "Cube", false, glm::translate(matrix4(1.0f), vVectors[n]));
	}

	//Setup world, Broadphase, collision configuration, solver
	broadphase = new btDbvtBroadphase();
	collisionConfiguration = new btDefaultCollisionConfiguration();
	dispatcher = new btCollisionDispatcher(collisionConfiguration);
	solver = new btSequentialImpulseConstraintSolver;
	dynamicsWorld = new btDiscreteDynamicsWorld(dispatcher, broadphase, solver, collisionConfiguration);
	dynamicsWorld->setGravity(btVector3(0, -10, 0));

	//Ground
	groundShape = new btStaticPlaneShape(btVector3(0, 1, 0), 0);
	groundMotionState = new btDefaultMotionState(btTransform(btQuaternion(0, 0, 0, 1), btVector3(0, 0, 0)));
	btRigidBody::btRigidBodyConstructionInfo groundRigidBodyCI(0, groundMotionState, groundShape, btVector3(0, 0, 0));
	groundRigidBody = new btRigidBody(groundRigidBodyCI);
	dynamicsWorld->addRigidBody(groundRigidBody);

	//Cube
	fallShape = new btBoxShape(btVector3(0.5, 0.5, 0.5));
	btScalar mass = 1;
	btVector3 fallInertia(0, 0, 0);
	fallShape->calculateLocalInertia(mass, fallInertia);
	for (int n = 0; n < m_nCubes; n++)
	{
		fallMotionState.push_back(
			new btDefaultMotionState(
				btTransform(
					btQuaternion(0, 0, 0, 1),
					btVector3(
						vVectors[n].x,
						vVectors[n].y,
						vVectors[n].z)))
			);
		btRigidBody::btRigidBodyConstructionInfo fallRigidBodyCI(mass, fallMotionState[n], fallShape, fallInertia);
		fallRigidBody.push_back(new btRigidBody(fallRigidBodyCI));
		dynamicsWorld->addRigidBody(fallRigidBody[n]);
	}
}
Пример #18
0
void ApplicationClass::InitAppVariables()
{
	// Create a new grid initializing its properties and compiling it
	m_pGrid = new GridClass(MEAXIS::XY | MEAXIS::YZ);
	m_pGrid->CompileGrid();

	m_m4Mesh = matrix4(IDENTITY);

	m_pMesh = new MyPrimitiveClass();
	//m_pMesh->GenerateCube( 1.0f, MERED);
	//m_pMesh->GenerateCone( 1.0f, 1.0f, 8, MEBLUE);
	//m_pMesh->GenerateCylinder( 1.0f, 2.0f, 8, MEGREEN); //originally 5
	m_pMesh->GenerateTube( 1.0f, 0.6f, 2.0f, 8, MERED); //INNER radius is 0.7f
	//m_pMesh->GenerateSphere( 1.0f, 6, MERED);
}
Пример #19
0
void MyMesh::RenderList(float* a_fMatrixArray, int a_nInstances)
{
	if (!m_bBinded)
		return;

	// Use the buffer and shader
	GLuint nProgram = m_pShaderMngr->GetShaderID("BasicColor");
	glUseProgram(nProgram);

	// Get the GPU variables by their name and hook them to CPU variables
	GLuint MVP = glGetUniformLocation(nProgram, "MVP");
	GLuint m4ModelToWorld = glGetUniformLocation(nProgram, "m4ModelToWorld");

	GLuint v4Position = glGetAttribLocation(nProgram, "Position_b");
	GLuint v4Color = glGetAttribLocation(nProgram, "Color_b");

	GLuint CameraPosition = glGetUniformLocation(nProgram, "CameraPosition");

	GLuint gl_nInstances = glGetUniformLocation(nProgram, "nElements");

	GLuint m4ToWorld = glGetUniformLocation(nProgram, "m4ToWorld");

	//ToWorld matrix
	glUniformMatrix4fv(m4ModelToWorld, 1, GL_FALSE, glm::value_ptr(matrix4(1.0f)));

	//Final Projection of the Camera
	glUniformMatrix4fv(MVP, 1, GL_FALSE, glm::value_ptr(m_pCamera->GetVP()));

	glUniform1i(gl_nInstances, a_nInstances);
	glUniformMatrix4fv(m4ToWorld, a_nInstances, GL_FALSE, a_fMatrixArray);

	glUniform3f(CameraPosition, m_pCamera->GetPosition().x, m_pCamera->GetPosition().y, m_pCamera->GetPosition().z);

	//position
	glEnableVertexAttribArray(v4Position);
	glBindBuffer(GL_ARRAY_BUFFER, m_VertexBuffer);
	glVertexAttribPointer(v4Position, 3, GL_FLOAT, GL_FALSE, 0, (void*)0);

	//Color
	glEnableVertexAttribArray(v4Color);
	glBindBuffer(GL_ARRAY_BUFFER, m_ColorBuffer);
	glVertexAttribPointer(v4Color, 3, GL_FLOAT, GL_FALSE, 0, (void*)0);

	glDrawArraysInstanced(GL_TRIANGLES, 0, m_nVertexCount, a_nInstances);

	glDisableVertexAttribArray(v4Position);
	glDisableVertexAttribArray(v4Color);
}
Пример #20
0
void AppClass::Update(void)
{
	//Update the system's time
	m_pSystem->UpdateTime();

	//Update the mesh manager's time without updating for collision detection
	m_pMeshMngr->Update();

	//First person camera movement
	if (m_bFPC == true)
		CameraRotation();

	//Call the arcball method
	ArcBall();

	dynamicsWorld->stepSimulation(1 / 60.0f, 1);

	btTransform trans;
	btVector3 position;
	btQuaternion orientation;
	vector3 v3Position;
	quaternion qOrientation;
	matrix4 mToWorld;
	for (int n = 0; n < m_nCubes; n++)
	{
		fallRigidBody[n]->getMotionState()->getWorldTransform(trans);
		position = trans.getOrigin();
		orientation = trans.getRotation();
		v3Position = vector3(position.getX(), position.getY(), position.getZ());
		qOrientation = quaternion(orientation.getW(), orientation.getX(), orientation.getY(), orientation.getZ());
		mToWorld = glm::translate(matrix4(1.0f), v3Position) * glm::mat4_cast(qOrientation);
		m_pMeshMngr->SetModelMatrix(mToWorld, n);
	}

	//Adds all loaded instance to the render list
	m_pMeshMngr->AddInstanceToRenderList("ALL");

	// m_pMeshMngr->AddPlaneToQueue(glm::rotate(IDENTITY_M4, 90.0f, REAXISX) * glm::scale(vector3(100.0f)), REBLACK);

	//Indicate the FPS
	int nFPS = m_pSystem->GetFPS();
	//print info into the console
	//printf("FPS: %d            \r", nFPS);//print the Frames per Second
	//Print info on the screen
	m_pMeshMngr->PrintLine(m_pSystem->GetAppName(), REYELLOW);
	m_pMeshMngr->Print("FPS:");
	m_pMeshMngr->Print(std::to_string(nFPS), RERED);
}
//--- Non Standard Singleton Methods
void BoundingBoxManagerSingleton::GenerateBoundingBox(String a_sInstanceName)
{
	MeshManagerSingleton* pMeshMngr = MeshManagerSingleton::GetInstance();
	//Verify the instance is loaded
	if(pMeshMngr->IsInstanceCreated(a_sInstanceName))
	{//if it is check if the Box has already been created
		if(IdentifyBox(a_sInstanceName) == -1)
		{
			//Create a new bounding Box
			BoundingBoxClass* pBS = new BoundingBoxClass();
			//construct its information out of the instance name
			pBS->GenerateBoundingBox(a_sInstanceName);
			//Push the Box back into the list
			m_lBox.push_back(pBS);
			//Push a new matrix into the list
			m_lMatrix.push_back(matrix4(IDENTITY));
			//Specify the color the Box is going to have
			m_lColor.push_back(vector3(1.0f));
			//Increase the number of Boxs
			m_nBoxs++;
		}
	}
}
Пример #22
0
/********************************************************************
 *         Advanced 3D Game Programming using DirectX 9.0			*
 ********************************************************************
 * copyright (c) 2003 by Peter A Walsh and Adrian Perez				*
 * See license.txt for modification and distribution information	*
 ********************************************************************/

#include "point3.h"
#include "matrix4.h"

const matrix4 matrix4::Identity = matrix4(
	1,0,0,0,
	0,1,0,0,
	0,0,1,0,
	0,0,0,1);

//returns matrix4*matrix4
matrix4 operator*(matrix4 const &a, matrix4 const &b)
{
    matrix4 out;      // temporary matrix4 for storing result
    for (int j = 0; j < 4; j++)         // transform by columns first
        for (int i = 0; i < 4; i++)     // then by rows
            out.m[i][j] = a.m[i][0] * b.m[0][j] + a.m[i][1] * b.m[1][j] +
                           a.m[i][2] * b.m[2][j] + a.m[i][3] * b.m[3][j];
	return out;
};

// Multiplies two matrices together
void MatMult( const matrix4 &a, const matrix4& b, matrix4* result )
{
    for (int j = 0; j < 4; j++)         // transform by columns first
void ApplicationClass::ProcessKeyboard(void)
{
	bool bModifier = false;
	float fSpeed = 0.1f;

	m_m4SelectedObject = m_pMeshMngr->GetModelMatrix(m_sSelectedObject);

	if(sf::Keyboard::isKeyPressed(sf::Keyboard::Escape))
	{
		//To correctly terminate the application releasing memory
		PostMessage(m_pWindow->GetHandler(), WM_QUIT, NULL, NULL); 
		//exit(0); //will not terminate cleanly at this point
	}

	//Modifiers
#pragma region Modifiers

	if(sf::Keyboard::isKeyPressed(sf::Keyboard::LShift))
	{
		fSpeed += 1.0f;
		bModifier = true;
	}

	if(sf::Keyboard::isKeyPressed(sf::Keyboard::RShift))
	{
		fSpeed += 1.0f;
		bModifier = true;
	}
	
#pragma endregion

	//Model Positioning
#pragma region Model Positioning
	if(sf::Keyboard::isKeyPressed(sf::Keyboard::Left))
	{
		if(bModifier)
			m_m4SelectedObject = glm::rotate(m_m4SelectedObject, 1.0f, vector3(0.0f, 0.0f, 1.0f));
		else
			m_m4SelectedObject = glm::translate(matrix4(), vector3(-fSpeed, 0.0f, 0.0f)) * m_m4SelectedObject;
	}
	if(sf::Keyboard::isKeyPressed(sf::Keyboard::Right))
	{
		if(bModifier)
			m_m4SelectedObject = glm::rotate(m_m4SelectedObject, 1.0f, vector3(0.0f, 0.0f,-1.0f));
		else
			m_m4SelectedObject = glm::translate(matrix4(), vector3( fSpeed, 0.0f, 0.0f)) * m_m4SelectedObject;
	}
	if(sf::Keyboard::isKeyPressed(sf::Keyboard::Up))
	{
		if(bModifier)
			m_m4SelectedObject = glm::translate(matrix4(), vector3(0.0f, 0.0f, fSpeed - 1.0f)) * m_m4SelectedObject;
		else
			m_m4SelectedObject = glm::translate(matrix4(), vector3(0.0f, fSpeed, 0.0f)) * m_m4SelectedObject;
	}
	if(sf::Keyboard::isKeyPressed(sf::Keyboard::Down))
	{
		if(bModifier)
			m_m4SelectedObject = glm::translate(matrix4(), vector3(0.0f, 0.0f,-fSpeed + 1.0f)) * m_m4SelectedObject;
			
		else
			m_m4SelectedObject = glm::translate(matrix4(), vector3(0.0f,-fSpeed, 0.0f)) * m_m4SelectedObject;
	}
#pragma endregion
	//ModelSelection
	if(sf::Keyboard::isKeyPressed(sf::Keyboard::F1))
	{
		m_sSelectedObject = "Cow";
		m_m4SelectedObject = m_pMeshMngr->GetModelMatrix(m_sSelectedObject);
	}

	if(sf::Keyboard::isKeyPressed(sf::Keyboard::F2))
	{
		m_sSelectedObject = "Zombie";
		m_m4SelectedObject = m_pMeshMngr->GetModelMatrix(m_sSelectedObject);
	}
	
	if(sf::Keyboard::isKeyPressed(sf::Keyboard::F3))
	{
		m_sSelectedObject = "Creeper";
		m_m4SelectedObject = m_pMeshMngr->GetModelMatrix(m_sSelectedObject);
	}

	if(sf::Keyboard::isKeyPressed(sf::Keyboard::F4))
	{
		m_sSelectedObject = "Steve";
		m_m4SelectedObject = m_pMeshMngr->GetModelMatrix(m_sSelectedObject);
	}

	if(sf::Keyboard::isKeyPressed(sf::Keyboard::F5))
	{
		m_sSelectedObject = "Pig";
		m_m4SelectedObject = m_pMeshMngr->GetModelMatrix(m_sSelectedObject);
	}

	//Camera
#pragma region Camera
	if(sf::Keyboard::isKeyPressed(sf::Keyboard::W))
		m_pCamera->MoveForward(fSpeed);

	if(sf::Keyboard::isKeyPressed(sf::Keyboard::S))
		m_pCamera->MoveForward(-fSpeed);
	
	if(sf::Keyboard::isKeyPressed(sf::Keyboard::A))
		m_pCamera->MoveSideways(-fSpeed);

	if(sf::Keyboard::isKeyPressed(sf::Keyboard::D))
		m_pCamera->MoveSideways(fSpeed);
	m_pCamera->CalculateView();
#pragma endregion
}
Пример #24
0
//------------------------------------------------------------------------------
void initialize()
{
	cursorOnCowBoundingBox=false;
	// Set up OpenGL state
	glShadeModel(GL_SMOOTH);         // Set Smooth Shading
	glEnable(GL_DEPTH_TEST);         // Enables Depth Testing
	glDepthFunc(GL_LEQUAL);          // The Type Of Depth Test To Do
	// Use perspective correct interpolation if available
	glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
	// Initialize the matrix stacks
	reshape(width, height);
	// Define lighting for the scene
	float lightDirection[]   = {1.0, 1.0, 1.0, 0};
	float ambientIntensity[] = {0.1, 0.1, 0.1, 1.0};
	float lightIntensity[]   = {0.9, 0.9, 0.9, 1.0};
	glLightfv(GL_LIGHT0, GL_AMBIENT, ambientIntensity);
	glLightfv(GL_LIGHT0, GL_DIFFUSE, lightIntensity);
	glLightfv(GL_LIGHT0, GL_POSITION, lightDirection);
	glEnable(GL_LIGHT0);

	// initialize floor
	{
		// After making checker-patterned texture, use this repetitively.

		// Insert color into checker[] according to checker pattern.
		const int size = 8;
		unsigned char checker[size*size*3];
		for( int i=0; i < size*size; i++ )
		{
			if (((i/size) ^ i) & 1)
			{
				checker[3*i+0] = 200;
				checker[3*i+1] = 32;
				checker[3*i+2] = 32;
			}
			else
			{
				checker[3*i+0] = 200;
				checker[3*i+1] = 200;
				checker[3*i+2] = 32;
			}
		}

		// Make texture which is accessible through floorTexID. 
		glGenTextures( 1, &floorTexID );				
		glBindTexture(GL_TEXTURE_2D, floorTexID);		
		glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
		glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
		glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
		glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
		glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
		glTexImage2D(GL_TEXTURE_2D, 0, 3, size, size, 0, GL_RGB, GL_UNSIGNED_BYTE, checker);
	}
	// initialize cow
	{

		// Read information from cow.obj.
		cow = new WaveFrontOBJ( "cow.obj" );

		// Make display list. After this, you can draw cow using 'cowID'.
		cowID = glGenLists(1);				// Create display lists
		glNewList(cowID, GL_COMPILE);		// Begin compiling the display list using cowID
		cow->Draw();						// Draw the cow on display list.
		glEndList();						// Terminate compiling the display list. Now, you can draw cow using 'cowID'.
		glPushMatrix();						// Push the current matrix of GL into stack.
		glLoadIdentity();					// Set the GL matrix Identity matrix.
		glTranslated(0,-cow->bbmin.y,-8);	// Set the location of cow.
		glRotated(-90, 0, 1, 0);			// Set the direction of cow. These information are stored in the matrix of GL.
		// Read the modelview matrix about location and direction set above, and store it in cow2wld matrix.
		cow2wld.getCurrentOpenGLmatrix(	GL_MODELVIEW_MATRIX);

		glPopMatrix();						// Pop the matrix on stack to GL.
	}
	// initialize bethoben
	{

		// Read information from beethovan.obj.
		bet = new WaveFrontOBJ( "beethovan.obj" );

		// Make display list. After this, you can draw beethovan using 'betID'.
		betID = glGenLists(1);
		glNewList(betID, GL_COMPILE);
		bet->Draw();
		glEndList();
		glPushMatrix();
		glLoadIdentity();
		glTranslated(0,-bet->bbmin.y,8);
		glRotated(180, 0, 1, 0);
		// bet2wld will become T * R
		bet2wld.getCurrentOpenGLmatrix(GL_MODELVIEW_MATRIX);
		glPopMatrix();
	}
	// intialize camera model.
	{
		cam = new WaveFrontOBJ("camera.obj");	// Read information of camera from camera.obj.
		camID = glGenLists(1);					// Create display list of the camera.
		glNewList(camID, GL_COMPILE);			// Begin compiling the display list using camID.
		cam->Draw();							// Draw the camera. you can do this job again through camID..
		glEndList();							// Terminate compiling the display list.

		// initialize camera frame transforms.
		for (int i=0; i < cameraCount; i++ )
		{
			double* c = cameras[i];											// 'c' points the coordinate of i-th camera.
			wld2cam.push_back(matrix4());								// Insert {0} matrix to wld2cam vector.
			glPushMatrix();													// Push the current matrix of GL into stack.
			glLoadIdentity();												// Set the GL matrix Identity matrix.
			gluLookAt(c[0],c[1],c[2], c[3],c[4],c[5], c[6],c[7],c[8]);		// Setting the coordinate of camera.
			wld2cam[i].getCurrentOpenGLmatrix(GL_MODELVIEW_MATRIX);
			glPopMatrix();													// Transfer the matrix that was pushed the stack to GL.
			matrix4 invmat;
			invmat.inverse(wld2cam[i]);
			cam2wld.push_back(invmat);
		}
		cameraIndex = 0;
	}
}
void MyPrimitive::GenerateSphere(float a_fRadius, int a_nSubdivisions, vector3 a_vColor)
{
	//Sets minimum and maximum of subdivisions
	if (a_nSubdivisions < 1)
	{
		GenerateCube(a_fRadius * 2, a_vColor);
		return;
	}
	if (a_nSubdivisions > 6)
		a_nSubdivisions = 6;

	//Clean up Memory
	Release();
	Init();

	//Your Code Goes Here instead of the next three lines
	float fValue = 0.5f;
	vector3 pointA(-fValue, -fValue, fValue); //0
	vector3 pointB(fValue, -fValue, fValue); //1
	vector3 pointC(-fValue, fValue, fValue); //2

	//left to right List of vector3
	std::vector<vector3> vectorAB;
	vectorAB.push_back(pointA);
	for (int i = 0; i < a_nSubdivisions; i++)
	{
		vector3 temp(pointB - pointA);
		temp /= a_nSubdivisions + 1;
		temp *= (i + 1);
		vectorAB.push_back(temp + pointA);
	}
	vectorAB.push_back(pointB);

	//height increments
	float fHeight = pointC.y - pointA.y;
	fHeight /= a_nSubdivisions + 1;

	//List of Lists
	std::vector<std::vector<vector3>> list;
	list.push_back(vectorAB);
	for (int j = 0; j < a_nSubdivisions + 1; j++)
	{
		std::vector<vector3> temp = list[0];
		float increment = fHeight * (j + 1);
		for (int i = 0; i < a_nSubdivisions + 2; i++)
		{
			temp[i].y += increment;
		}
		list.push_back(temp);
	}

	//Creating the patch of quads
	for (int j = 0; j < a_nSubdivisions + 1; j++)
	{
		for (int i = 0; i < a_nSubdivisions + 1; i++)
		{
			AddQuad(list[j][i], list[j][i + 1], list[j + 1][i], list[j + 1][i + 1]);
		}
	}
	
	int nVertices = static_cast<int>(m_lVertexPos.size());

	//normalizing the vectors to make them round
	for (int i = 0; i < nVertices; i++)
	{
		m_lVertexPos[i] = glm::normalize(m_lVertexPos[i]);
		m_lVertexPos[i] *= a_fRadius;
	}

	//RightSideFace
	std::vector<vector3> right;
	for (int i = 0; i < nVertices; i++)
	{
		matrix4 rotation;
		rotation = glm::rotate(matrix4(1.0f), 90.0f, vector3(0.0f, 1.0f, 0.0f));
		right.push_back(static_cast <vector3>(rotation * vector4(m_lVertexPos[i], 1.0f)));
	}


	for (int i = 0; i < nVertices; i++)
	{
		AddVertexPosition(right[i]);
	}

	//LeftSideFace
	std::vector<vector3> left;
	for (int i = 0; i < nVertices; i++)
	{
		matrix4 rotation;
		rotation = glm::rotate(matrix4(1.0f), -90.0f, vector3(0.0f, 1.0f, 0.0f));
		left.push_back(static_cast <vector3>(rotation * vector4(m_lVertexPos[i], 1.0f)));
	}

	for (int i = 0; i < nVertices; i++)
	{
		AddVertexPosition(left[i]);
	}

	//BackSideFace
	std::vector<vector3> back;
	for (int i = 0; i < nVertices; i++)
	{
		matrix4 rotation;
		rotation = glm::rotate(matrix4(1.0f), 180.0f, vector3(0.0f, 1.0f, 0.0f));
		back.push_back(static_cast <vector3>(rotation * vector4(m_lVertexPos[i], 1.0f)));
	}

	for (int i = 0; i < nVertices; i++)
	{
		AddVertexPosition(back[i]);
	}

	//TopSideFace
	std::vector<vector3> top;
	for (int i = 0; i < nVertices; i++)
	{
		matrix4 rotation;
		rotation = glm::rotate(matrix4(1.0f), -90.0f, vector3(1.0f, 0.0f, 0.0f));
		top.push_back(static_cast <vector3>(rotation * vector4(m_lVertexPos[i], 1.0f)));
	}

	for (int i = 0; i < nVertices; i++)
	{
		AddVertexPosition(top[i]);
	}

	//BottomSideFace
	std::vector<vector3> bottom;
	for (int i = 0; i < nVertices; i++)
	{
		matrix4 rotation;
		rotation = glm::rotate(matrix4(1.0f), 90.0f, vector3(1.0f, 0.0f, 0.0f));
		bottom.push_back(static_cast <vector3>(rotation * vector4(m_lVertexPos[i], 1.0f)));
	}

	for (int i = 0; i < nVertices; i++)
	{
		AddVertexPosition(bottom[i]);
	}

	//Compile the object in this color and assign it this name
	CompileObject(a_vColor);
}