D3DX11CreateShaderResourceViewFromFile(md3dDevice, L"Textures/water2.dds", 0, 0, &mWavesMapSRV, 0 )


이것을 아래함수로 변경


ID3D11RESOURCE* texResource;

CreateDDSTextureFromFile(md3dDevice, L"Textures/grass.dds", &texResource, &mGrassMapSRV )

ReleaseCOM(texResource);



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
bool TexturedHillsAndWavesApp::Init()
{
    if(!D3DApp::Init())
        return false;
 
    mWaves.Init(1601601.0f, 0.03f, 3.25f, 0.4f);
 
    // Must init Effects first since InputLayouts depend on shader signatures.
    Effects::InitAll(md3dDevice);
    InputLayouts::InitAll(md3dDevice);
 
    ID3D11Resource* texResource;
        
    HR(CreateDDSTextureFromFile(md3dDevice, 
        L"Textures/grass.dds", nullptr, &mGrassMapSRV ));
    //ReleaseCOM(texResource);
 
    HR(CreateDDSTextureFromFile(md3dDevice,
        L"Textures/water2.dds"&texResource, &mWavesMapSRV ));
    ReleaseCOM(texResource);
    
    ID3D11Texture2D* tex;
    mGrassMapSRV->GetResource((ID3D11Resource**)&tex);
    D3D11_TEXTURE2D_DESC texDesc;
    tex->GetDesc(&texDesc);
    
    BuildLandGeometryBuffers();
    BuildWaveGeometryBuffers();
    
    return true;
}
 
cs


위에

 HR(CreateDDSTextureFromFile(md3dDevice, L"Textures/grass.dds", nullptr, &mGrassMapSRV ));

처럼 ID3D11Resource를 nullptr을 넣어줘도 컴파일 잘됨 이게 도데체 뭔 용도인지를 모르겠음


텍스처의 정보를 볼려면 셰이더리소스뷰에서 얻으면됨


압축을 명시적으로 하고싶으나 아직은 못 찾았음

신고

'DirectX' 카테고리의 다른 글

D3DX11CreateShaderResourceViewFromFile 과 CreateDDSTextureFromFile  (0) 2017.04.17
챕터6 연습문제 2번  (0) 2017.04.04
DirectX11, VS2015, WindowsSDK  (0) 2017.04.04
Posted by 아딸가격아빡

#include "d3dApp.h"

#include "d3dx11Effect.h"

#include "MathHelper.h"

#include <d3dcompiler.h>


/*struct Vertex

{

XMFLOAT3 Pos;

XMFLOAT4 Color;

};*/


struct VertexPos

{

XMFLOAT3 Pos;

};


struct VertexColor

{

XMFLOAT4 Color;

};


class BoxApp : public D3DApp

{

public:

BoxApp(HINSTANCE hInstance);

~BoxApp();


bool Init();

void OnResize();

void UpdateScene(float dt);

void DrawScene();


void OnMouseDown(WPARAM btnState, int x, int y);

void OnMouseUp(WPARAM btnState, int x, int y);

void OnMouseMove(WPARAM btnState, int x, int y);


private:

void BuildGeometryBuffers();

void BuildFX();

void BuildVertexLayout();


private:

ID3D11Buffer* mBoxVBPos;

ID3D11Buffer* mBoxVBColor;

ID3D11Buffer* mBoxIB;


ID3DX11Effect* mFX;

ID3DX11EffectTechnique* mTech;

ID3DX11EffectMatrixVariable* mfxWorldViewProj;


ID3D11InputLayout* mInputLayout;


XMFLOAT4X4 mWorld;

XMFLOAT4X4 mView;

XMFLOAT4X4 mProj;


float mTheta;

float mPhi;

float mRadius;


POINT mLastMousePos;

};


int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE prevInstance, PSTR cmdLine, int showCmd)

{

// Enable run-time memory check for debug builds.

#if defined(DEBUG) | defined(_DEBUG)

_CrtSetDbgFlag(_CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF);

#endif


BoxApp theApp(hInstance);


if (!theApp.Init())

return 0;


return theApp.Run();

}



BoxApp::BoxApp(HINSTANCE hInstance)

: D3DApp(hInstance), mBoxVBPos(0), mBoxVBColor(0), mBoxIB(0), mFX(0), mTech(0),

mfxWorldViewProj(0), mInputLayout(0),

mTheta(1.5f*MathHelper::Pi), mPhi(0.25f*MathHelper::Pi), mRadius(5.0f)

{

mMainWndCaption = L"Box Demo";


mLastMousePos.x = 0;

mLastMousePos.y = 0;


XMMATRIX I = XMMatrixIdentity();

XMStoreFloat4x4(&mWorld, I);

XMStoreFloat4x4(&mView, I);

XMStoreFloat4x4(&mProj, I);

}


BoxApp::~BoxApp()

{

ReleaseCOM(mBoxVBPos);

ReleaseCOM(mBoxVBColor);

ReleaseCOM(mBoxIB);

ReleaseCOM(mFX);

ReleaseCOM(mInputLayout);

}


bool BoxApp::Init()

{

if (!D3DApp::Init())

return false;


BuildGeometryBuffers();

BuildFX();

BuildVertexLayout();


return true;

}


void BoxApp::OnResize()

{

D3DApp::OnResize();


// The window resized, so update the aspect ratio and recompute the projection matrix.

XMMATRIX P = XMMatrixPerspectiveFovLH(0.25f*MathHelper::Pi, AspectRatio(), 1.0f, 1000.0f);

XMStoreFloat4x4(&mProj, P);

}


void BoxApp::UpdateScene(float dt)

{

// Convert Spherical to Cartesian coordinates.

float x = mRadius*sinf(mPhi)*cosf(mTheta);

float z = mRadius*sinf(mPhi)*sinf(mTheta);

float y = mRadius*cosf(mPhi);


// Build the view matrix.

XMVECTOR pos = XMVectorSet(x, y, z, 1.0f); //세계 공간에서의 카메라 위치

XMVECTOR target = XMVectorZero(); //카메라 방향

XMVECTOR up = XMVectorSet(0.0f, 1.0f, 0.0f, 0.0f); //상향벡터


XMMATRIX V = XMMatrixLookAtLH(pos, target, up); //시야행렬

XMStoreFloat4x4(&mView, V);

}


void BoxApp::DrawScene()

{

md3dImmediateContext->ClearRenderTargetView(mRenderTargetView, reinterpret_cast<const float*>(&Colors::LightSteelBlue));

md3dImmediateContext->ClearDepthStencilView(mDepthStencilView, D3D11_CLEAR_DEPTH | D3D11_CLEAR_STENCIL, 1.0f, 0);


md3dImmediateContext->IASetInputLayout(mInputLayout);

md3dImmediateContext->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST);


UINT stridePos = sizeof(VertexPos);

UINT strideColor = sizeof(VertexColor);

UINT offset = 0;

md3dImmediateContext->IASetVertexBuffers(0, 1, &mBoxVBPos, &stridePos, &offset);

md3dImmediateContext->IASetVertexBuffers(1, 1, &mBoxVBColor, &strideColor, &offset);

md3dImmediateContext->IASetIndexBuffer(mBoxIB, DXGI_FORMAT_R32_UINT, 0);


// Set constants

XMMATRIX world = XMLoadFloat4x4(&mWorld);

XMMATRIX view = XMLoadFloat4x4(&mView);

XMMATRIX proj = XMLoadFloat4x4(&mProj);

XMMATRIX worldViewProj = world*view*proj;


mfxWorldViewProj->SetMatrix(reinterpret_cast<float*>(&worldViewProj));


D3DX11_TECHNIQUE_DESC techDesc;

mTech->GetDesc(&techDesc);

for (UINT p = 0; p < techDesc.Passes; ++p)

{

mTech->GetPassByIndex(p)->Apply(0, md3dImmediateContext);


// 36 indices for the box.

md3dImmediateContext->DrawIndexed(36, 0, 0);

}


HR(mSwapChain->Present(0, 0));

}


void BoxApp::OnMouseDown(WPARAM btnState, int x, int y)

{

mLastMousePos.x = x;

mLastMousePos.y = y;


SetCapture(mhMainWnd);

}


void BoxApp::OnMouseUp(WPARAM btnState, int x, int y)

{

ReleaseCapture();

}


void BoxApp::OnMouseMove(WPARAM btnState, int x, int y)

{

if ((btnState & MK_LBUTTON) != 0)

{

// Make each pixel correspond to a quarter of a degree.

float dx = XMConvertToRadians(0.25f*static_cast<float>(x - mLastMousePos.x));

float dy = XMConvertToRadians(0.25f*static_cast<float>(y - mLastMousePos.y));


// Update angles based on input to orbit camera around box.

mTheta += dx;

mPhi += dy;


// Restrict the angle mPhi.

mPhi = MathHelper::Clamp(mPhi, 0.1f, MathHelper::Pi - 0.1f);

}

else if ((btnState & MK_RBUTTON) != 0)

{

// Make each pixel correspond to 0.005 unit in the scene.

float dx = 0.005f*static_cast<float>(x - mLastMousePos.x);

float dy = 0.005f*static_cast<float>(y - mLastMousePos.y);


// Update the camera radius based on input.

mRadius += dx - dy;//원점으로부터의 거리


  // Restrict the radius.

mRadius = MathHelper::Clamp(mRadius, 3.0f, 15.0f);

}


mLastMousePos.x = x;

mLastMousePos.y = y;

}


void BoxApp::BuildGeometryBuffers()

{

// Create vertex buffer

VertexPos verticesPos[] =

{

XMFLOAT3(-1.0f, -1.0f, -1.0f),

XMFLOAT3(-1.0f, +1.0f, -1.0f),

XMFLOAT3(+1.0f, +1.0f, -1.0f),

XMFLOAT3(+1.0f, -1.0f, -1.0f),

XMFLOAT3(-1.0f, -1.0f, +1.0f),

XMFLOAT3(-1.0f, +1.0f, +1.0f),

XMFLOAT3(+1.0f, +1.0f, +1.0f),

XMFLOAT3(+1.0f, -1.0f, +1.0f) 

};


VertexColor verticesColor[] =

{

XMFLOAT4((const float*)&Colors::White),

XMFLOAT4((const float*)&Colors::Black),

XMFLOAT4((const float*)&Colors::Red),

XMFLOAT4((const float*)&Colors::Green),

XMFLOAT4((const float*)&Colors::Blue),

XMFLOAT4((const float*)&Colors::Yellow),

XMFLOAT4((const float*)&Colors::Cyan),

XMFLOAT4((const float*)&Colors::Magenta)

};


D3D11_BUFFER_DESC vbd;

vbd.Usage = D3D11_USAGE_IMMUTABLE;

vbd.ByteWidth = sizeof(VertexPos) * 8;

vbd.BindFlags = D3D11_BIND_VERTEX_BUFFER;

vbd.CPUAccessFlags = 0;

vbd.MiscFlags = 0;

vbd.StructureByteStride = 0;

D3D11_SUBRESOURCE_DATA vinitData;

vinitData.pSysMem = verticesPos;


D3D11_BUFFER_DESC vbdC;

vbdC.Usage = D3D11_USAGE_IMMUTABLE;

vbdC.ByteWidth = sizeof(VertexColor) * 8;

vbdC.BindFlags = D3D11_BIND_VERTEX_BUFFER;

vbdC.CPUAccessFlags = 0;

vbdC.MiscFlags = 0;

vbdC.StructureByteStride = 0;

D3D11_SUBRESOURCE_DATA vinitDataC;

vinitDataC.pSysMem = verticesColor;


HR(md3dDevice->CreateBuffer(&vbd, &vinitData, &mBoxVBPos));

HR(md3dDevice->CreateBuffer(&vbdC, &vinitDataC, &mBoxVBColor));


// Create the index buffer


UINT indices[] = {

// front face

0, 1, 2,

0, 2, 3,


// back face

4, 6, 5,

4, 7, 6,


// left face

4, 5, 1,

4, 1, 0,


// right face

3, 2, 6,

3, 6, 7,


// top face

1, 5, 6,

1, 6, 2,


// bottom face

4, 0, 3,

4, 3, 7

};


D3D11_BUFFER_DESC ibd;

ibd.Usage = D3D11_USAGE_IMMUTABLE;

ibd.ByteWidth = sizeof(UINT) * 36;

ibd.BindFlags = D3D11_BIND_INDEX_BUFFER;

ibd.CPUAccessFlags = 0;

ibd.MiscFlags = 0;

ibd.StructureByteStride = 0;

D3D11_SUBRESOURCE_DATA iinitData;

iinitData.pSysMem = indices;

HR(md3dDevice->CreateBuffer(&ibd, &iinitData, &mBoxIB));

}


void BoxApp::BuildFX()

{

std::ifstream fin("color.fxo", std::ios::binary);


fin.seekg(0, std::ios_base::end);

int size = (int)fin.tellg();

fin.seekg(0, std::ios_base::beg);

std::vector<char> compiledShader(size);


fin.read(&compiledShader[0], size);

fin.close();


HR(D3DX11CreateEffectFromMemory(&compiledShader[0], size, 0, md3dDevice, &mFX));


mTech = mFX->GetTechniqueByName("ColorTech");

mfxWorldViewProj = mFX->GetVariableByName("gWorldViewProj")->AsMatrix();

/*DWORD shaderFlags = 0;

#if defined(DEBUG) || defined(_DEBUG)

shaderFlags |= D3D10_SHADER_DEBUG;

shaderFlags |= D3D10_SHADER_SKIP_OPTIMIZATION;

#endif

ID3D10Blob* compiledShader = 0;

ID3D10Blob* compilationMsgs = 0;

//HRESULT hr = D3DX11CompileFromFile(L"FX/color.fx", 0, 0, 0, "fx_5_0", shaderFlags, 0, 0, &compiledShader, &compilationMsgs);

HRESULT hr = D3DCompileFromFile(L"color.fx",0, 0, 0, "fx_5_0", shaderFlags, 0, &compiledShader, &compilationMsgs);


if (compilationMsgs != 0)

{

MessageBox(0, (LPCWCHAR)compilationMsgs->GetBufferPointer(), 0, 0);

ReleaseCOM(compilationMsgs);

}


if (FAILED(hr))

{

DXTrace(__FILEW__, (DWORD)__LINE__, hr, L"D3DX11CompileFromFile", true);

}


HR(D3DX11CreateEffectFromMemory(compiledShader->GetBufferPointer(), compiledShader->GetBufferSize(), 0, md3dDevice, &mFX));

ReleaseCOM(compiledShader);


mTech = mFX->GetTechniqueByName("ColorTech");

mfxWorldViewProj = mFX->GetVariableByName("gWorldViewProj")->AsMatrix();*/

}


void BoxApp::BuildVertexLayout()

{

// Create the vertex input layout.

D3D11_INPUT_ELEMENT_DESC vertexDesc[] =

{

{ "POSITION", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 0, D3D11_INPUT_PER_VERTEX_DATA, 0 },

{ "COLOR",    0, DXGI_FORMAT_R32G32B32A32_FLOAT, 1, 0, D3D11_INPUT_PER_VERTEX_DATA, 0 }

};


// Create the input layout

D3DX11_PASS_DESC passDesc;

mTech->GetPassByIndex(0)->GetDesc(&passDesc);

HR(md3dDevice->CreateInputLayout(vertexDesc, 2, passDesc.pIAInputSignature,

passDesc.IAInputSignatureSize, &mInputLayout));

}



----------------------------------------------------------------------------------------------------------------------


chapter6.cpp



Position과 Color를 따로 담아낼 구조체를 만듬

ID3D11Buffer도 나눠줌


DrawScene에서

두 버퍼를 입력슬롯에 붙여야 하므로

md3dImmediateContext->IASetVertexBuffers(0, 1, &mBoxVBPos, &stridePos, &offset);

md3dImmediateContext->IASetVertexBuffers(1, 1, &mBoxVBColor, &strideColor, &offset);

첫번째 매개변수가 입력슬롯의 색인이다


정점 버퍼 초기화 자료도 따로 준비해준다

VertexPos verticesPos[] =

{

XMFLOAT3(-1.0f, -1.0f, -1.0f),

XMFLOAT3(-1.0f, +1.0f, -1.0f),

XMFLOAT3(+1.0f, +1.0f, -1.0f),

XMFLOAT3(+1.0f, -1.0f, -1.0f),

XMFLOAT3(-1.0f, -1.0f, +1.0f),

XMFLOAT3(-1.0f, +1.0f, +1.0f),

XMFLOAT3(+1.0f, +1.0f, +1.0f),

XMFLOAT3(+1.0f, -1.0f, +1.0f) 

};


VertexColor verticesColor[] =

{

XMFLOAT4((const float*)&Colors::White),

XMFLOAT4((const float*)&Colors::Black),

XMFLOAT4((const float*)&Colors::Red),

XMFLOAT4((const float*)&Colors::Green),

XMFLOAT4((const float*)&Colors::Blue),

XMFLOAT4((const float*)&Colors::Yellow),

XMFLOAT4((const float*)&Colors::Cyan),

XMFLOAT4((const float*)&Colors::Magenta)

};


D3D11_BUFFER_DESC vbd;

vbd.Usage = D3D11_USAGE_IMMUTABLE;

vbd.ByteWidth = sizeof(VertexPos) * 8;

vbd.BindFlags = D3D11_BIND_VERTEX_BUFFER;

vbd.CPUAccessFlags = 0;

vbd.MiscFlags = 0;

vbd.StructureByteStride = 0;

D3D11_SUBRESOURCE_DATA vinitData;

vinitData.pSysMem = verticesPos;


D3D11_BUFFER_DESC vbdC;

vbdC.Usage = D3D11_USAGE_IMMUTABLE;

vbdC.ByteWidth = sizeof(VertexColor) * 8;

vbdC.BindFlags = D3D11_BIND_VERTEX_BUFFER;

vbdC.CPUAccessFlags = 0;

vbdC.MiscFlags = 0;

vbdC.StructureByteStride = 0;

D3D11_SUBRESOURCE_DATA vinitDataC;

vinitDataC.pSysMem = verticesColor;


HR(md3dDevice->CreateBuffer(&vbd, &vinitData, &mBoxVBPos));

HR(md3dDevice->CreateBuffer(&vbdC, &vinitDataC, &mBoxVBColor));


신고

'DirectX' 카테고리의 다른 글

D3DX11CreateShaderResourceViewFromFile 과 CreateDDSTextureFromFile  (0) 2017.04.17
챕터6 연습문제 2번  (0) 2017.04.04
DirectX11, VS2015, WindowsSDK  (0) 2017.04.04
Posted by 아딸가격아빡

http://hogoo.tistory.com/2166

신고

'DirectX' 카테고리의 다른 글

D3DX11CreateShaderResourceViewFromFile 과 CreateDDSTextureFromFile  (0) 2017.04.17
챕터6 연습문제 2번  (0) 2017.04.04
DirectX11, VS2015, WindowsSDK  (0) 2017.04.04
Posted by 아딸가격아빡


티스토리 툴바