Message
Message
Message
#define _modelo
//#define _XM_NO_INTRINSICS_
#include <d3d11.h>
#include <d3dx11.h>
#include <DxErr.h>
#include <D3Dcompiler.h>
#include <d3dx10math.h>
#include <conio.h>
#include <vector>
#include <iostream>
#include "loadModel.h"
class ModeloRR {
private:
struct VertexComponent
{
D3DXVECTOR3 pos;
D3DXVECTOR2 UV;
D3DXVECTOR3 normal;
};
struct VertexCollide
{
D3DXVECTOR3 pos;
};
struct vector3 {
float x, y, z;
};
struct vector2 {
float u, v;
};
ID3D11VertexShader* VertexShaderVS;
ID3D11PixelShader* solidColorPS;
ID3D11InputLayout* inputLayout;
ID3D11Buffer* vertexBuffer;
ID3D11Buffer* indexBuffer;
ID3D11ShaderResourceView* colorMap;
ID3D11ShaderResourceView* specMap;
ID3D11SamplerState* colorMapSampler;
ID3D11Buffer* viewCB;
ID3D11Buffer* projCB;
ID3D11Buffer* worldCB;
D3DXMATRIX viewMatrix;
D3DXMATRIX projMatrix;
ID3D11Buffer* cameraPosCB;
XMFLOAT3 camPos;
ID3D11Buffer* specForceCB;
float specForce;
ID3D11Device* d3dDevice;
ID3D11DeviceContext* d3dContext;
CObjParser m_ObjParser;
float posX;
float posZ;
//////////
// matriz para rotacion de la camara
//////////
D3DXVECTOR3 rotCam;
public:
ModeloRR(ID3D11Device* D3DDevice, ID3D11DeviceContext* D3DContext, char*
ModelPath, WCHAR* colorTexturePath, WCHAR* specularTexturePath, float _posX, float
_posZ)
{
//copiamos el device y el device context a la clase terreno
d3dContext = D3DContext;
d3dDevice = D3DDevice;
posX = _posX;
posZ = _posZ;
~ModeloRR()
{
//libera recursos
UnloadContent();
}
float getPosX() {
return this->posX;
}
float getPosZ() {
return this->posZ;
}
//COLISIONES
float* getCollisionSphere(float radio) {
collision[0] = posX;
collision[1] = posZ;
collision[2] = radio;
return collision;
}
//////////
// funciones set X y Z
//////////
void setPosX(float posX) {
this->posX = posX;
}
void setPosZ(float posZ) {
this->posZ = posZ;
}
//////////
// funcion rotacion de la camara
//////////
void setRotCamp(D3DXVECTOR3 rotCam) {
this->rotCam = rotCam;
}
ID3DBlob* errorBuffer = 0;
HRESULT result;
if (errorBuffer != 0)
errorBuffer->Release();
return true;
}
ID3DBlob* vsBuffer = 0;
if (vsBuffer)
vsBuffer->Release();
return false;
}
d3dResult = d3dDevice->CreateInputLayout(solidColorLayout,
totalLayoutElements,
vsBuffer->GetBufferPointer(), vsBuffer->GetBufferSize(),
&inputLayout);
vsBuffer->Release();
if (FAILED(d3dResult))
{
return false;
}
ID3DBlob* psBuffer = 0;
// de los vertices pasamos al pixel shader, note que el nombre del
shader es el mismo
//ahora buscamos al pixel shader llamado PS_Main
compileResult = CompileD3DShader(L"Modelo.fx", "PS_Main", "ps_4_0",
&psBuffer);
if (compileResult == false)
{
return false;
}
//ya compilado y sin error lo ponemos en la memoria
d3dResult = d3dDevice->CreatePixelShader(psBuffer->GetBufferPointer(),
psBuffer->GetBufferSize(), 0, &solidColorPS);
psBuffer->Release();
if (FAILED(d3dResult))
{
return false;
}
D3D11_SUBRESOURCE_DATA resourceData;
ZeroMemory(&resourceData, sizeof(resourceData));
resourceData.pSysMem = m_ObjParser.m_pVertex;
if (FAILED(d3dResult))
{
MessageBox(0, L"Error", L"Error al crear vertex buffer", MB_OK);
return false;
}
if (FAILED(d3dResult))
{
return false;
}
if (FAILED(d3dResult))
{
return false;
}
//creamos los buffers para el shader para poder pasarle las matrices
D3D11_BUFFER_DESC constDesc;
ZeroMemory(&constDesc, sizeof(constDesc));
constDesc.BindFlags = D3D11_BIND_CONSTANT_BUFFER;
constDesc.ByteWidth = sizeof(D3DXMATRIX);
constDesc.Usage = D3D11_USAGE_DEFAULT;
//de vista
d3dResult = d3dDevice->CreateBuffer(&constDesc, 0, &viewCB);
if (FAILED(d3dResult))
{
return false;
}
//de proyeccion
d3dResult = d3dDevice->CreateBuffer(&constDesc, 0, &projCB);
if (FAILED(d3dResult))
{
return false;
}
//de mundo
d3dResult = d3dDevice->CreateBuffer(&constDesc, 0, &worldCB);
if (FAILED(d3dResult))
{
return false;
}
constDesc.ByteWidth = sizeof(XMFLOAT4);
d3dResult = d3dDevice->CreateBuffer(&constDesc, 0, &cameraPosCB);
if (FAILED(d3dResult))
{
return false;
}
//posicion de la camara
D3DXVECTOR3 eye = D3DXVECTOR3(0.0f, 100.0f, 200.0f);
//a donde ve
D3DXVECTOR3 target = D3DXVECTOR3(0.0f, 0.0f, 0.0f);
D3DXVECTOR3 up = D3DXVECTOR3(0.0f, 1.0f, 0.0f);
return true;
}
bool UnloadContent()
{
if (colorMapSampler)
colorMapSampler->Release();
if (colorMap)
colorMap->Release();
if (specMap)
specMap->Release();
if (VertexShaderVS)
VertexShaderVS->Release();
if (solidColorPS)
solidColorPS->Release();
if (inputLayout)
inputLayout->Release();
if (vertexBuffer)
vertexBuffer->Release();
if (viewCB)
viewCB->Release();
if (projCB)
projCB->Release();
if (worldCB)
worldCB->Release();
if (cameraPosCB)
cameraPosCB->Release();
if (specForceCB)
specForceCB->Release();
colorMapSampler = 0;
colorMap = 0;
specMap = 0;
VertexShaderVS = 0;
solidColorPS = 0;
inputLayout = 0;
vertexBuffer = 0;
indexBuffer = 0;
viewCB = 0;
projCB = 0;
worldCB = 0;
cameraPosCB = 0;
specForceCB = 0;
}
camPos.x = posCam.x;
camPos.y = posCam.y;
camPos.z = posCam.z;
d3dContext->PSSetSamplers(0, 1, &colorMapSampler);
//////////
// separar de la camara cuando es tercera persona
//////////
D3DXMATRIX translationRotCam;
if (tipoCamara) {
D3DXMatrixTranslation(&translationRotCam, 0.0, 0.0, -0.4);
}
else {
D3DXMatrixTranslation(&translationRotCam, 0.0, 0.0, 0.0);
}
D3DXMATRIX scaleMat;
D3DXMatrixScaling(&scaleMat, scale, scale, scale); //SACALA NORMAL 1
PERO SE PUEDE ESCALAR
//validar movacam
D3DXMATRIX worldMat;
if (movCam) {
worldMat = rotationMat * scaleMat * translationMat *
translationRotCam;
}
else {
worldMat = rotationMat * scaleMat * translationMat;
}
d3dContext->Draw(m_ObjParser.m_nVertexCount, 0);
}
};
#endif