codesamples >> crossplatform primitive class

Crossplatform: Primitive class

Project:

Crossplatform application (Windows & PS3)

Engine:

Custom

Code language:

C++

Class description:

The base class for all the geometry for Windows and PS3.

Primitive.cpp

#include "Primitive.h"
#include "Camera.h"

Primitive::Primitive(): 
	m_AngleX(0.0f), m_AngleY(0.0f), m_AngleZ(0.0f), 
	m_PosX(0.0f), m_PosY(0.0f), m_PosZ(0.0f),
	m_Vertices(0), m_Indices(0)
{
}

Primitive::~Primitive()
{
}

// General functions
void Primitive::SetPosition( float x, float y, float z)
{
	m_PosX = x; 
	m_PosY = y; 
	m_PosZ = z;
}

void Primitive::Update(Camera* camera, float time)
{
	m_AngleY += 1.0f * time;

	float distance = camera->GetDistanceToPoint(m_PosX, m_PosY, m_PosZ);

	// Display object highly detailed
	if(distance < 10)
	{
		SetLOD(5);
	}
	// Display object normal detailed
	else if(distance < 20)
	{
		SetLOD(4);
	}
	// Display object lowly detailed
	else if(distance < 30)
	{
		SetLOD(3);
	}
	// Display object lowly detailed
	else if(distance < 50)
	{
		SetLOD(2);
	}
	// Display object very lowly detailed
	else if(distance < 100)
	{
		SetLOD(1);
	}
	// Display object very lowly detailed
	else
	{
		SetLOD(0);
	}
}

void Primitive::SetAngleY(float angleY)
{
	m_AngleY = angleY;
}

Primitive.h

#ifndef _PRIMITIVE_H
#define _PRIMITIVE_H

// Crossplatform: Includes
#include <vector>
#include <assert.h>
#include <string.h>

// PS3 Specific: Defines & Includes
#if defined(PS3)
#define __CELL_ASSERT__

#include "gcmutil.h"

using namespace cell::Gcm;
#endif

// Windows Specific: Includes
#if defined(WIN32)
#include <windows.h>
#include <d3d11.h>
#include <d3dx11.h>
#include <d3dcompiler.h>
#endif

class Camera;

class Primitive
{
public:
	// Crossplatform: Functions
	Primitive();
	virtual ~Primitive();
	virtual void SetPosition(float x, float y, float z);
	virtual void Update(Camera* camera, float time);
	virtual void SetAngleY(float angleY);
	virtual void SetLOD(int lod) = 0;

#if defined(WIN32)
	// Windows Specific: Functions
	virtual void Render(Camera* camera, ID3D11DeviceContext* deviceContext, ID3D11VertexShader* vertexShader, 
	ID3D11PixelShader* pixelShader, float time) = 0;
#elif(PS3)
	// PS3 Specific: Functions
	virtual void Render(Camera* camera, CGprogram cGVertexProgram, CGprogram cGFragmentProgram, 
	void* vertexProgramUCode, void* fragmentProgramUCode) = 0;
#endif
	

protected:
	// Crossplatform: Datamembers
	float m_AngleX;
	float m_AngleY;
	float m_AngleZ;

	float m_PosX;
	float m_PosY;
	float m_PosZ;

	int m_Vertices;
	int m_Indices;
};

#endif