Quantcast
Viewing all articles
Browse latest Browse all 2703

acces violation whit texture



I added code for loading a texture and got an access violation.It breaks in xnamathmatrix. When i start whitout debugging it works,i get the same when i use a pointer for the cameraclass//d3dclass.h::*/


#ifndef D3DCLASS_H_
#define D3DClASS_H_



#pragma comment(lib, "d3d11.lib")
#pragma comment(lib, "d3dx11.lib")
#pragma comment(lib, "d3dx10.lib")






#include <d3d11.h>
#include <d3dx11.h>
#include <d3dx10.h>




#include <xnamath.h>



struct vertex //Overloaded Vertex Structure
{
vertex(){}
vertex(float x, float y, float z,
float u, float v)
: pos(x,y,z), texCoord(u, v){}
XMFLOAT3 pos;
XMFLOAT2 texCoord;
};




class D3Dclass

{
public:

struct cube
{
XMMATRIX WVP;

};




public:

D3Dclass();
~D3Dclass();
bool initialized11(int screenHeight,int screenWidth,HWND hwnd);
void render(float,float,float,float,XMMATRIX& camview,XMMATRIX& camprojection,XMMATRIX& camworld);
void model(int,int);
void update();




ID3D11Device* mdevice;
ID3D11DeviceContext* mdevicecon;
DXGI_SWAP_CHAIN_DESC swapchaindesc;
IDXGISwapChain* swapchain;
ID3D11RenderTargetView* rendertargetview;
ID3D11Texture2D* backbuffer;
ID3D11Texture2D* depthstencilbuffer;
ID3D11DepthStencilView* depthstencilview;
D3D11_VIEWPORT viewport;

//model
ID3D11Buffer* mvertexbuffer;
ID3D11Buffer* indexbuffer;
ID3D11Buffer* mbox;
ID3D11VertexShader* VS;
ID3D11PixelShader* PS;
ID3D10Blob* vsbuffer;
ID3D10Blob* psbuffer;
ID3D11InputLayout* vertlayout;

// texture

ID3D11ShaderResourceView* CubesTexture;
ID3D11SamplerState* CubesTexSamplerState;
//camera
cube box;
XMMATRIX WVP;
XMMATRIX World;
XMMATRIX camviewp;
XMMATRIX camprojectionp;



XMVECTOR camposition;
XMVECTOR camtarget;
XMVECTOR camup;






};



#endif




//d3dclass code::

#include"d3dclass.h"


D3Dclass::D3Dclass()
{

}


D3Dclass::~D3Dclass ()
{
}

bool D3Dclass::initialized11(int screenHeight,int screenWidth,HWND hwnd)
{

CubesTexture=0;
CubesTexSamplerState=0;

D3D11_TEXTURE2D_DESC depthstencildesc;

D3D_FEATURE_LEVEL featurelevel;

featurelevel=D3D_FEATURE_LEVEL_11_0;

D3D11CreateDevice(0,D3D_DRIVER_TYPE_HARDWARE,0,D3D 11_CREATE_DEVICE_DEBUG,0,0,D3D11_SDK_VERSION,&mdev ice,NULL,&mdevicecon);

swapchaindesc.BufferDesc.Width =screenWidth;
swapchaindesc.BufferDesc.Height=screenHeight;
swapchaindesc.BufferDesc.RefreshRate.Numerator=60;
swapchaindesc.BufferDesc.RefreshRate .Denominator=1;
swapchaindesc.BufferDesc.Format=DXGI_FORMAT_R8G8B8 A8_UNORM;
swapchaindesc.BufferDesc.ScanlineOrdering =DXGI_MODE_SCANLINE_ORDER_UNSPECIFIED;
swapchaindesc.BufferDesc.Scaling =DXGI_MODE_SCALING_UNSPECIFIED;


swapchaindesc.SampleDesc.Count=1;
swapchaindesc.SampleDesc.Quality =0;
swapchaindesc.BufferUsage =DXGI_USAGE_RENDER_TARGET_OUTPUT;

swapchaindesc.BufferCount=1;
swapchaindesc.OutputWindow =hwnd;
swapchaindesc.Windowed =true;
swapchaindesc.SwapEffect =DXGI_SWAP_EFFECT_DISCARD;
swapchaindesc.Flags =0;

IDXGIDevice* dxgiDevice = 0;
mdevice->QueryInterface(__uuidof(IDXGIDevice), (void**)&dxgiDevice);

IDXGIAdapter* dxgiAdapter = 0;
dxgiDevice->GetParent(__uuidof(IDXGIAdapter), (void**)&dxgiAdapter);

IDXGIFactory* dxgiFactory = 0;
dxgiAdapter->GetParent(__uuidof(IDXGIFactory), (void**)&dxgiFactory);



dxgiFactory->CreateSwapChain (mdevice,&swapchaindesc,&swapchain);



swapchain->GetBuffer(0,__uuidof(ID3D11Texture2D),reinterpret _cast<void**> backbuffer));

mdevice->CreateRenderTargetView (backbuffer,0,&rendertargetview);

backbuffer->Release();
backbuffer=0;


depthstencildesc.Width =screenWidth;
depthstencildesc.Height =screenHeight;
depthstencildesc.MipLevels=1;
depthstencildesc.ArraySize =1;
depthstencildesc.Format =DXGI_FORMAT_D24_UNORM_S8_UINT;
depthstencildesc.SampleDesc.Count =1;
depthstencildesc.SampleDesc.Quality =0;
depthstencildesc.Usage=D3D11_USAGE_DEFAULT;
depthstencildesc.BindFlags =D3D11_BIND_DEPTH_STENCIL;
depthstencildesc.CPUAccessFlags=0;
depthstencildesc.MiscFlags =0;

mdevice->CreateTexture2D (&depthstencildesc,0,&depthstencilbuffer);

mdevice->CreateDepthStencilView (depthstencilbuffer,0,&depthstencilview);


mdevicecon->OMSetRenderTargets (1,&rendertargetview,depthstencilview);

viewport.TopLeftX=0.0f;
viewport.TopLeftY =0.0f;
viewport.Width=(float)screenWidth;
viewport.Height =(float)screenHeight;
viewport.MinDepth=0.0f;
viewport.MaxDepth =1.0f;

mdevicecon->RSSetViewports (1,&viewport);




return true;


}

void D3Dclass::render (float red,float green,float blue, float alpha,XMMATRIX& camview,XMMATRIX& camprojection,XMMATRIX& camworld)
{
float color[4];



color[0] = red;
color[1] = green;
color[2] = blue;
color[3] = alpha;


camworld=camworld;
camviewp=camview;

camprojection=camprojection;

mdevicecon->ClearRenderTargetView (rendertargetview,color);

mdevicecon->ClearDepthStencilView (depthstencilview,D3D11_CLEAR_DEPTH,1.0f,0);

WVP=camworld*camviewp*camprojection;
box.WVP=XMMatrixTranspose (WVP);

mdevicecon->UpdateSubresource (mbox,0,NULL,&box,0,0);
mdevicecon->VSSetConstantBuffers (0,1,&mbox);

mdevicecon ->PSSetShaderResources( 0, 1, &CubesTexture );
mdevicecon->PSSetSamplers( 0, 1, &CubesTexSamplerState );


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

swapchain->Present (0,0);

}

void D3Dclass::model (int screenWidth,int screenHeight)
{
HRESULT hr;

D3D11_INPUT_ELEMENT_DESC vertexDesc []=
{
{"POSITION",0,DXGI_FORMAT_R32G32B32_FLOAT,0,0,D3D1 1_INPUT_PER_VERTEX_DATA,0},
{ "TEXCOORD", 0, DXGI_FORMAT_R32G32_FLOAT, 0, 12, D3D11_INPUT_PER_VERTEX_DATA, 0 },
};

UINT numelements =ARRAYSIZE (vertexDesc) ;

D3DX10CompileFromFile (L"effects.fx",0,0,"VS","vs_4_0",0,0,0,&vsbuffer,0 ,0);
D3DX10CompileFromFile (L"effects.fx",0,0,"PS","ps_4_0",0,0,0,&psbuffer,0 ,0);

hr= mdevice->CreateVertexShader(vsbuffer->GetBufferPointer(), vsbuffer->GetBufferSize(), NULL, &VS);
hr= mdevice->CreatePixelShader(psbuffer->GetBufferPointer(), psbuffer->GetBufferSize(), NULL, &PS);

mdevicecon->VSSetShader(VS, 0, 0);
mdevicecon->PSSetShader(PS, 0, 0);


D3D11_BUFFER_DESC vertexbufferdesc;
D3D11_BUFFER_DESC indexbufferdesc;
vertex triang []=
{
// Front Face
// Front Face
vertex(-1.0f, -1.0f, -1.0f, 0.0f, 1.0f),
vertex(-1.0f, 1.0f, -1.0f, 0.0f, 0.0f),
vertex( 1.0f, 1.0f, -1.0f, 1.0f, 0.0f),
vertex( 1.0f, -1.0f, -1.0f, 1.0f, 1.0f),

// Back Face
vertex(-1.0f, -1.0f, 1.0f, 1.0f, 1.0f),
vertex( 1.0f, -1.0f, 1.0f, 0.0f, 1.0f),
vertex( 1.0f, 1.0f, 1.0f, 0.0f, 0.0f),
vertex(-1.0f, 1.0f, 1.0f, 1.0f, 0.0f),

// Top Face
vertex(-1.0f, 1.0f, -1.0f, 0.0f, 1.0f),
vertex(-1.0f, 1.0f, 1.0f, 0.0f, 0.0f),
vertex( 1.0f, 1.0f, 1.0f, 1.0f, 0.0f),
vertex( 1.0f, 1.0f, -1.0f, 1.0f, 1.0f),

// Bottom Face
vertex(-1.0f, -1.0f, -1.0f, 1.0f, 1.0f),
vertex( 1.0f, -1.0f, -1.0f, 0.0f, 1.0f),
vertex( 1.0f, -1.0f, 1.0f, 0.0f, 0.0f),
vertex(-1.0f, -1.0f, 1.0f, 1.0f, 0.0f),

// Left Face
vertex(-1.0f, -1.0f, 1.0f, 0.0f, 1.0f),
vertex(-1.0f, 1.0f, 1.0f, 0.0f, 0.0f),
vertex(-1.0f, 1.0f, -1.0f, 1.0f, 0.0f),
vertex(-1.0f, -1.0f, -1.0f, 1.0f, 1.0f),

// Right Face
vertex( 1.0f, -1.0f, -1.0f, 0.0f, 1.0f),
vertex( 1.0f, 1.0f, -1.0f, 0.0f, 0.0f),
vertex( 1.0f, 1.0f, 1.0f, 1.0f, 0.0f),
vertex( 1.0f, -1.0f, 1.0f, 1.0f, 1.0f),
// Bottom Face

// Back Face

};

DWORD indices[] = {

// Front Face
0, 1, 2,
0, 2, 3,

// Back Face
4, 5, 6,
4, 6, 7,

// Top Face
8, 9, 10,
8, 10, 11,

// Bottom Face
12, 13, 14,
12, 14, 15,

// Left Face
16, 17, 18,
16, 18, 19,

// Right Face
20, 21, 22,
20, 22, 23



};


indexbufferdesc.Usage =D3D11_USAGE_DEFAULT;
indexbufferdesc.ByteWidth=sizeof(DWORD) *12 *3;
indexbufferdesc.BindFlags=D3D11_BIND_INDEX_BUFFER;
indexbufferdesc.CPUAccessFlags =0;
indexbufferdesc.MiscFlags =0;

D3D11_SUBRESOURCE_DATA iindices;

iindices.pSysMem =indices;

mdevice->CreateBuffer(&indexbufferdesc,&iindices,&indexbuf fer);

mdevicecon->IASetIndexBuffer(indexbuffer,DXGI_FORMAT_R32_UINT ,0);


vertexbufferdesc.ByteWidth =sizeof(vertex)*24;
vertexbufferdesc.Usage =D3D11_USAGE_DEFAULT;
vertexbufferdesc.BindFlags =D3D11_BIND_VERTEX_BUFFER;
vertexbufferdesc.MiscFlags=0;
vertexbufferdesc.CPUAccessFlags=0;
vertexbufferdesc.StructureByteStride;


D3D11_SUBRESOURCE_DATA subdata;

subdata.pSysMem =triang;

mdevice->CreateBuffer(&vertexbufferdesc ,&subdata,&mvertexbuffer );

UINT stride =sizeof(vertex);
UINT offset=0;

mdevicecon->IASetVertexBuffers (0,1,&mvertexbuffer,&stride,&offset);


mdevice->CreateInputLayout (vertexDesc ,numelements ,vsbuffer->GetBufferPointer(),vsbuffer->GetBufferSize (),&vertlayout );

mdevicecon->IASetInputLayout (vertlayout );

mdevicecon->IASetPrimitiveTopology (D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST );

D3D11_BUFFER_DESC boxdesc;

boxdesc.Usage =D3D11_USAGE_DEFAULT;
boxdesc.ByteWidth =sizeof(cube);
boxdesc.BindFlags=D3D11_BIND_CONSTANT_BUFFER;
boxdesc.CPUAccessFlags =0;
boxdesc.MiscFlags =0;

mdevice->CreateBuffer (&boxdesc,NULL,&mbox);

D3DX11CreateShaderResourceViewFromFile( mdevice, L"../directx11basicshapes /data/kunst.jpg",
NULL, NULL, &CubesTexture, NULL );



// Describe the Sample State
D3D11_SAMPLER_DESC sampDesc;
ZeroMemory( &sampDesc, sizeof(sampDesc) );
sampDesc.Filter = D3D11_FILTER_MIN_MAG_MIP_LINEAR;
sampDesc.AddressU = D3D11_TEXTURE_ADDRESS_WRAP;
sampDesc.AddressV = D3D11_TEXTURE_ADDRESS_WRAP;
sampDesc.AddressW = D3D11_TEXTURE_ADDRESS_WRAP;
sampDesc.ComparisonFunc = D3D11_COMPARISON_NEVER;
sampDesc.MinLOD = 0;
sampDesc.MaxLOD = D3D11_FLOAT32_MAX;

//Create the Sample State
mdevice->CreateSamplerState( &sampDesc, &CubesTexSamplerState );





}


void D3Dclass::update()
{

}


************************************************** *************

//the camera class header::

#ifndef _CAMERA_H_
#define CAMERA_H_


#pragma comment(lib, "d3d11.lib")
#pragma comment(lib, "d3dx11.lib")
#pragma comment(lib, "d3dx10.lib")





#include <d3d11.h>
#include <d3dx11.h>
#include <d3dx10.h>




#include <xnamath.h>










class camera
{
public:

camera();


void inicamera(int ,int);

XMMATRIX getcamprojection();
XMMATRIX getcamview();
XMMATRIX getworld();

void moveright();
void moveleft();
void moveforeward();
void moveback();
void moveup();
void movedown();

void setrotz();
void updatecam();

public:

XMMATRIX world;
XMMATRIX camview;
XMMATRIX camprojection;

XMMATRIX rotationx;
XMMATRIX rotationz;

XMMATRIX rotation;
XMMATRIX translation;
XMMATRIX scale;


XMVECTOR camposition;
XMVECTOR camup;
XMVECTOR camtarget;

float movex;
float movey;
float movez;
float rotx;
float rotz;
float scaleX;
float scaley;
double rot;

};


#endif


camera code::
//

#include "camera.h"


camera::camera ()
{
movex=0.0f;
movey=0.0f;
movez=0.0f;
rotx=0.01f;

rotz=0.8f;
rot=0.01f;
scaleX=1.0f;
scaley=1.0f;
}


void camera::inicamera (int screenWidth,int screenHeight)


{



camposition=XMVectorSet (0.0f,3.0f,-8.0f,0.0f);
camtarget = XMVectorSet( 0.0f, 0.0f, 0.0f, 0.0f );
camup = XMVectorSet( 0.0f, 1.0f, 0.0f, 0.0f );

camview = XMMatrixLookAtLH( camposition, camtarget, camup );

camprojection = XMMatrixPerspectiveFovLH( 0.4f*3.14f, (float)screenWidth /screenHeight,1.0f, 1000.0f);
world = XMMatrixIdentity();

}

XMMATRIX camera::getworld()
{

return world;
}

XMMATRIX camera::getcamprojection ()

{
return camprojection ;
}

XMMATRIX camera::getcamview ()
{
return camview ;
}

void camera::setrotz()
{
rot=rot += 0.1f;
}


void camera::moveright()
{
movex = movex +=0.001f;
}

void camera:: moveleft()
{
movex= movex -=0.001f;
}

void camera::moveforeward()
{
movez=movez +=0.001f;
}

void camera::movedown()
{
movey=movey -=0.001f;
}

void camera::moveback()
{
movez=movez -=0.001f;
}

void camera::moveup()
{
movey=movey +=0.001f;
}
void camera::updatecam()
{





//Reset cube1World
world = XMMatrixIdentity();

//Define cube1's world space matrix
XMVECTOR rotaxis = XMVectorSet(0.0f, 1.0f, 0.0f, 0.0f);
rotation = XMMatrixRotationAxis( rotaxis, rot);
translation = XMMatrixTranslation( movex, movey, movez );
scale= XMMatrixScaling( 500.0f, 10.0f, 500.0f );
//Set cube1's world space using the transformations
world = translation *scale;
}

**********************************************
//graphicsclass header::

#ifndef _GRAPHICSCLASS_H_
#define _GRAPHICSCLASS_H_




//////////////
// INCLUDES //
//////////////
#include <windows.h>
#include "d3dclass.h"

#include "controls.h"

#include"camera.h"
#include <iostream>

using namespace std;


/////////////
// GLOBALS //
/////////////
const bool FULL_SCREEN = false;
const bool VSYNC_ENABLED = true;
const float SCREEN_DEPTH = 1000.0f;
const float SCREEN_NEAR = 0.1f;

class GraphicsClass
{
public:
GraphicsClass();
GraphicsClass(const GraphicsClass&);
~GraphicsClass();

bool Initialize(int, int,HINSTANCE , HWND);
void Shutdown();
void keyinput(HWND m_hwnd);
bool Frame();



public:
bool Render();

public:
D3Dclass* d3d;
controls* control;
camera cam;

XMMATRIX camview;
XMMATRIX camprojection;
XMMATRIX camworld;



};

#endif


graphicsclass code::
#include "graphicsclass.h"


GraphicsClass::GraphicsClass()

{

d3d=0;
control =0;
}


GraphicsClass::GraphicsClass(const GraphicsClass& other)
{
}


GraphicsClass::~GraphicsClass()
{
}


bool GraphicsClass::Initialize(int screenWidth, int screenHeight,HINSTANCE hinstance, HWND hwnd)
{

d3d=new D3Dclass;


d3d->initialized11 (screenWidth,screenHeight,hwnd);


cam.inicamera(screenWidth,screenHeight);




control=new controls;

control->inicontrols (hinstance,hwnd);





d3d->model(screenWidth,screenHeight);
return true;
}


void GraphicsClass::Shutdown()
{

return;
}

void GraphicsClass ::keyinput (HWND m_hwnd)
{

int key =0;
key=control->detectkey (key,m_hwnd);
if(key ==1)
{

cam.moveleft();
cam.updatecam ();

}

if(key==2)
{
cam.moveright();
cam.updatecam();
}

if(key==3)
{
cam.moveforeward ();
cam.updatecam();
}

if (key==4)
{
cam.moveback ();
cam.updatecam ();
}
if(key==5)
{
DestroyWindow(m_hwnd);
}

if(key==6)
{
cam.moveup();
cam.updatecam ();

}

if(key==7)
{
cam.movedown ();
cam.updatecam();
}
else key=0;
}

bool GraphicsClass::Frame()
{
camview=cam.getcamview();
camprojection=cam.getcamprojection();
camworld=cam.getworld();




d3d->render(0.0f, 0.0f, 0.0f, 0.0f,camview,camprojection,camworld);



return true;
}


bool GraphicsClass::Render()
{

return true;
}
************************************************
shader file::

cbuffer cube
{
float4x4 WVP;
};

Texture2D ObjTexture;
SamplerState ObjSamplerState;

struct VS_OUTPUT
{
float4 Pos : SV_POSITION;
float2 TexCoord : TEXCOORD;
};

VS_OUTPUT VS(float4 inPos : POSITION, float2 inTexCoord : TEXCOORD)
{
VS_OUTPUT output;

output.Pos = mul(inPos, WVP);
output.TexCoord = inTexCoord;

return output;
}

float4 PS(VS_OUTPUT input) : SV_TARGET
{
return ObjTexture.Sample( ObjSamplerState, input.TexCoord );
}






Viewing all articles
Browse latest Browse all 2703

Trending Articles