1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196
|
#include "Types.h"
#include "Vertex.h"
#include "CameraController.h"
#include "Material.h"
#include "Quad.h"
/************************************************************************/
Quad::Quad(void)
{
}
/************************************************************************/
Quad::Quad(Vector3D position, Vector3D rotation, int width, int height, int verts, DWORD colour)
{
CreateQuad(position,rotation,width,height,verts,colour);
}
/************************************************************************/
Quad::~Quad(void)
{
if(m_material)
{
delete m_material;
}
m_vertexBuffer->Release();
m_texture->Release();
}
/************************************************************************/
void
Quad::CreateQuad(Vector3D position, Vector3D rotation, int width, int height, int verts,DWORD colour)
{
m_position = position;
m_rotation = rotation;
m_width = width;
m_height = height;
m_colour = colour;
//LOAD TEXTURE
m_texture = NULL;
m_textureFileName = "..\\road.bmp";
LoadTexture();
if(verts <= 0)
{
ErrorMessage("Cannot create a quad with no verts");
}
m_numberOfVerts = verts * verts * 6;
const float delta = 1.0f/((float)verts);
m_D3DDevice->CreateVertexBuffer(m_numberOfVerts * sizeof(VertexPositionTexCoord),D3DUSAGE_WRITEONLY,0,D3DPOOL_DEFAULT,&m_vertexBuffer,0);
VertexPositionTexCoord* vertex;
m_vertexBuffer->Lock(0,0,(void**)&vertex,0);
/*vertex[0] = VertexPositionTexCoord(0.0f, 0.0f,-1.0f, 0.0f,1.0f);
vertex[1] = VertexPositionTexCoord( 0.0f, 1.0f,-1.0f, 0.0f,0.0f);
vertex[2] = VertexPositionTexCoord(1.0f,1.0f,-1.0f, 1.0f,0.0f);
vertex[3] = VertexPositionTexCoord( 0.0f,0.0f,-1.0f, 0.0f,1.0f);
vertex[4] = VertexPositionTexCoord(1.0f, 1.0f, -1.0f, 1.0f,0.0f);
vertex[5] = VertexPositionTexCoord(1.0f,0.0f, -1.0f, 1.0f,1.0f);*/
for (int i = 0 ; i < m_numberOfVerts; i++)
{
vertex[i] = VertexPositionTexCoord();
//vertex[i].normal.x = 0;
//vertex[i].normal.y = 0;
//vertex[i].normal.z = -1;
vertex[i].textureCoord.x = 0.0f;
vertex[i].textureCoord.y = 0.0f;
}
int counter = 0;
for (int y = 0 ; y < verts; y++)
{
for (int x = 0; x < verts ; x++)
{
//create the first poly
vertex[counter].pos.x = (width * x * delta) - width /2 ;
vertex[counter].pos.y = (height * y * delta) - height /2;
counter++;
vertex[counter].pos.x = (width * x * delta)- width /2 ;
vertex[counter].pos.y = (height * (y+1) * delta) - height /2;
counter++;
vertex[counter].pos.x = (width * (x+1) * delta) - width /2;
vertex[counter].pos.y = (height * (y+1) * delta) - height /2;
counter++;
vertex[counter].pos.x = (width * x * delta )- width /2;
vertex[counter].pos.y = (height * y * delta) - height /2;
counter++;
vertex[counter].pos.x = (width * (x+1) * delta) - width /2;
vertex[counter].pos.y = (height * (y+1) * delta) - height /2;
counter++;
vertex[counter].pos.x = (width * (x+1) * delta) - width /2;
vertex[counter].pos.y = (height * y * delta) - height /2;
counter++;
}
}
m_vertexBuffer->Unlock();
m_material = new Material((DWORD)0,(DWORD)0,(DWORD)0,0);
}
/************************************************************************/
void
Quad::SetPosition(Vector3D position)
{
m_position.x = position.x;
m_position.y = position.y;
m_position.z = position.z;
}
/************************************************************************/
void
Quad::SetRotation(Vector3D rotation)
{
m_rotation.x = rotation.x;
m_rotation.y = rotation.y;
m_rotation.z = rotation.z;
}
/************************************************************************/
void
Quad::SetWidth(int width)
{
m_vertexBuffer->Release();
CreateQuad(m_position,m_rotation,width,m_height,m_numberOfVerts,m_colour);
}
/************************************************************************/
void
Quad::SetHeight(int height)
{
m_vertexBuffer->Release();
CreateQuad(m_position,m_rotation,m_width,height,m_numberOfVerts,m_colour);
}
/************************************************************************/
void
Quad::SetVerts(int verts)
{
m_vertexBuffer->Release();
CreateQuad(m_position,m_rotation,m_width,m_height,verts,m_colour);
}
/************************************************************************/
void
Quad::Render()
{
Matrix positionMatrix;
Matrix rotationMatrix;
Matrix world;
//create matrix to move quad in world;
MatrixTranslation(&positionMatrix,m_position.x,m_position.y,m_position.z);
MatrixRotationYawPitchRoll(&rotationMatrix,D3DXToRadian(m_rotation.y),D3DXToRadian(m_rotation.x),D3DXToRadian(m_rotation.z));
world = rotationMatrix * positionMatrix;
CameraController::GetInstance()->SetWorld(world);
m_D3DDevice->SetStreamSource(0, m_vertexBuffer, 0 , sizeof(VertexPositionTexCoord));
m_D3DDevice->SetMaterial(m_material->GetDXMaterial());
m_D3DDevice->SetVertexDeclaration(VertexPositionTexCoord::Decl);
m_D3DDevice->SetRenderState(D3DRS_FILLMODE, D3DFILL_SOLID);
//m_D3DDevice->SetRenderState(D3DRS_FILLMODE, D3DFILL_WIREFRAME);
m_D3DDevice->SetTexture(0, m_texture);
m_D3DDevice->DrawPrimitive( D3DPT_TRIANGLELIST, 0, m_numberOfVerts/3 );
CameraController::GetInstance()->ResetWorld();
m_D3DDevice->SetTexture(0, NULL);
}
/************************************************************************/
void
Quad::LoadTexture()
{
D3DXCreateTextureFromFile(m_D3DDevice,m_textureFileName,&m_texture);
m_D3DDevice->SetSamplerState(0, D3DSAMP_MINFILTER, D3DTEXF_LINEAR);
m_D3DDevice->SetSamplerState(0, D3DSAMP_MAGFILTER, D3DTEXF_LINEAR);
}
/*******************************************************************/
|