运行时检查失败#2 - 围绕堆栈变量“指标”已损坏 [英] Run-Time Check Failure #2 - Stack around the variable 'indices' was corrupted
问题描述
嗯,我想我知道问题是什么。我只是有一个很难调试它。我用了DirectX API的工作,我想沿着生成根据一本书,我有X轴和Z轴的平面上。当我创建我的指数的问题是。我想我的设置值出指数数组的边界。我只是有一个很难搞清楚我做错了什么。我不熟悉产生一个平面的这种方法。所以它有点为难我。下面是我的code。就以指数环重视。
我一直都检讨了。这是指数是如何工作的。
INT curVertex = X +(Z * NUM_VERTSX);
这总是得到开始顶点。所以说,我们有在x轴17的顶点和在z轴17的顶点,我们是在x轴和z轴的第一环路
curVertx = 0 +(0 * 17)
curVertx = 0 + 0 = 0
说我们是在z轴的第一环路和x轴
的第二环路 curVertx = 1 +(0 * 17)
curVertx = 1 + 0 = 1
指数[curIndex] = curVertex;
指数[curIndex + 1] = curVertex + NUM_VERTSX;
指数[curIndex + 2] = curVertex + 1; 指数[curIndex + 3] = curVertex + 1;
指数[curIndex + 4] = curVertex + NUM_VERTSX;
指数[curIndex + 5] = curVertex + NUM_VERTSX + 1;
如果我们在第一个
循环指数[curIndex] = curVertex;
这等于第一个顶点= 0。
指数[curIndex + 1] = curVertex + NUM_VERTSX;
这等于第二行的顶点(它总是起始顶点下方的顶点
X X X X
[X] X X X
的#includeMyGame.h
//#包括CubeVector.h
/ *此$ C $账套投影和显示一个转折点立方体。什么已经被添加为项目,旋转和
一个光栅来改变立方体的光栅化。这是怎么回事的问题是一些与特效文件
这是导致不正确呈现的顶点。* /
typedef结构
{
ID3D10Effect * pEffect;
ID3D10EffectTechnique * pTechnique; //顶点信息
ID3D10Buffer * pVertexBuffer;
ID3D10Buffer * pIndicesBuffer;
ID3D10InputLayout * pVertexLayout; UINT numVertices;
UINT numIndices;
} ModelObject;ModelObject modelObject;
//世界矩阵
D3DXMATRIX WorldMatrix;
//查看矩阵
D3DXMATRIX ViewMatrix;
//投影矩阵
D3DXMATRIX ProjectionMatrix;
ID3D10EffectMatrixVariable * pProjectionMatrixVariable = NULL;//网格信息
#定义16 NUM_COLS
#定义NUM_ROWS 16#定义CELL_WIDTH 32
#定义CELL_HEIGHT 32#定义NUM_VERTSX(NUM_COLS + 1)
#定义NUM_VERTSY(NUM_ROWS + 1)
布尔MyGame :: InitDirect3D()
{
如果(!DX3dApp :: InitDirect3D())
{
返回false;
} D3D10_RASTERIZER_DESC rastDesc;
rastDesc.FillMode = D3D10_FILL_WIREFRAME;
rastDesc.CullMode = D3D10_CULL_FRONT;
rastDesc.FrontCounterClockwise = TRUE;
rastDesc.DepthBias = FALSE;
rastDesc.DepthBiasClamp = 0;
rastDesc.SlopeScaledDepthBias = 0;
rastDesc.DepthClipEnable = FALSE;
rastDesc.ScissorEnable = FALSE;
rastDesc.MultisampleEnable = FALSE;
rastDesc.AntialiasedLineEnable = FALSE; ID3D10RasterizerState * g_pRasterizerState;
mpD3DDevice-> CreateRasterizerState(安培; rastDesc,&安培; g_pRasterizerState);
mpD3DDevice-> RSSetState(g_pRasterizerState); //设置世界矩阵
D3DXMatrixIdentity(安培; WorldMatrix);
D3DXMatrixLookAtLH(安培; ViewMatrix,新D3DXVECTOR3(0.0,10.0f,-20.0f),新D3DXVECTOR3(0.0,0.0,0.0),新D3DXVECTOR3(0.0,1.0F,0.0));
//设置投影矩阵
D3DXMatrixPerspectiveFovLH(安培; ProjectionMatrix,(浮点)D3DX_PI * 0.5F,(浮点)mWidth /(浮点)mHeight,0.1F,100.0f); 如果(!的CreateObject())
{
返回false;
} 返回true;
}//这些是发生缓冲器的结算之后和present前行动
无效MyGame :: GameDraw()
{ 静浮rotationAngle = 0.0; //创建使用旋转角度的旋转矩阵
D3DXMatrixRotationY(安培; WorldMatrix,rotationAngle);
rotationAngle + =(浮点)D3DX_PI * 0.0; //设置输入布局
mpD3DDevice-> IASetInputLayout(modelObject.pVertexLayout); //设置顶点缓冲
UINT跨距= sizeof的(VertexPos);
UINT偏移= 0;
mpD3DDevice-> IASetVertexBuffers(0,1,&安培; modelObject.pVertexBuffer,&放大器;步幅,&放大器;偏移);
mpD3DDevice-> IASetIndexBuffer(modelObject.pIndicesBuffer,DXGI_FORMAT_R32_UINT,0); //设置原始拓扑
mpD3DDevice-> IASetPrimitiveTopology(D3D10_PRIMITIVE_TOPOLOGY_TRIANGLELIST); //合并和最终的矩阵发送到着色器
D3DXMATRIX finalMatrix =(WorldMatrix * ViewMatrix * ProjectionMatrix);
pProjectionMatrixVariable-> SetMatrix((浮点*)及finalMatrix);
//确保modelObject是有效的
//渲染模型对象
D3D10_TECHNIQUE_DESC techniqueDescription;
modelObject.pTechnique-> GetDesc(安培; techniqueDescription); //循环遍历技术传递
为(UINT p值= 0; P&下; techniqueDescription.Passes ++ p)的
{
modelObject.pTechnique-> GetPassByIndex(对) - >应用(0); //用画全部36个顶点和12个三角形的立方体
mpD3DDevice-> DrawIndexed(modelObject.numIndices,0,0);
}
}//渲染实际上incapsulates Gamedraw,所以你可以调用数据之前,你真正清除缓冲区或之后
// present数据
无效MyGame ::渲染()
{
DX3dApp ::渲染();
}布尔MyGame ::的CreateObject()
{
VertexPos顶点[NUM_VERTSX * NUM_VERTSY]。
对于(INT Z = 0; z,其中,NUM_VERTSY ++ Z)
{
为(中间体X = 0; X&下; NUM_VERTSX ++ x)的
{
顶点[X + Z * NUM_VERTSX] .pos.x =(浮点)X * CELL_WIDTH;
顶点[X + Z * NUM_VERTSX] .pos.z =(浮点)Z * CELL_HEIGHT; 顶点[X + Z * NUM_VERTSX] .pos.y = 0.0; 顶点[X + Z * NUM_VERTSX]。颜色= D3DXVECTOR4(1.0,0.0,0.0,0.0);
}
} DWORD指数[NUM_VERTSX * NUM_VERTSY]。
INT curIndex = 0; 对于(INT Z = 0; z,其中,NUM_ROWS ++ Z)
{
对于(INT X = 0; X< NUM_COLS ++ X)
{
INT curVertex = X +(Z * NUM_VERTSX);
指数[curIndex] = curVertex;
指数[curIndex + 1] = curVertex + NUM_VERTSX;
指数[curIndex + 2] = curVertex + 1; 指数[curIndex + 3] = curVertex + 1;
指数[curIndex + 4] = curVertex + NUM_VERTSX;
指数[curIndex + 5] = curVertex + NUM_VERTSX + 1; curIndex + = 6;
}
} //创建布局
D3D10_INPUT_ELEMENT_DESC布局[] = {
{位置,0,DXGI_FORMAT_R32G32B32_FLOAT,0,0,D3D10_INPUT_PER_VERTEX_DATA,0},
{彩色,0,DXGI_FORMAT_R32G32B32A32_FLOAT,0,12,D3D10_INPUT_PER_VERTEX_DATA,0}
}; UINT numElements个=(sizeof的(布局)/的sizeof(布局[0]));
modelObject.numVertices = sizeof的(顶点)/的sizeof(VertexPos); //创建缓冲区倒序
D3D10_BUFFER_DESC bufferDesc;
bufferDesc.Usage = D3D10_USAGE_DEFAULT;
bufferDesc.ByteWidth = sizeof的(VertexPos)* modelObject.numVertices;
bufferDesc.BindFlags = D3D10_BIND_VERTEX_BUFFER;
bufferDesc.CPUAccessFlags = 0;
bufferDesc.MiscFlags = 0; D3D10_SUBRESOURCE_DATA initData;
initData.pSysMem =顶点;
//创建缓冲区 HRESULT HR = mpD3DDevice-> CreateBuffer(安培; bufferDesc,&安培; initData,&安培; modelObject.pVertexBuffer);
如果(FAILED(HR))
返回false; modelObject.numIndices = sizeof的(指数)/的sizeof(DWORD); bufferDesc.ByteWidth = sizeof的(DWORD)* modelObject.numIndices;
bufferDesc.BindFlags = D3D10_BIND_INDEX_BUFFER; initData.pSysMem =指数; HR = mpD3DDevice-> CreateBuffer(安培; bufferDesc,&安培; initData,&安培; modelObject.pIndicesBuffer);
如果(FAILED(HR))
返回false;
////////////////////////////////////////////////// ///////////////////////////
//设置FX文件
LPCWSTR effectFilename = Leffect.fx
modelObject.pEffect = NULL; HR = D3DX10CreateEffectFromFile(effectFilename,
空值,
空值,
fx_4_0
D3D10_SHADER_ENABLE_STRICTNESS,
0,
mpD3DDevice,
空值,
空值,
&安培; modelObject.pEffect,
空值,
空值); 如果(FAILED(HR))
返回false; pProjectionMatrixVariable = modelObject.pEffect-> GetVariableByName(投影) - > AsMatrix();
//不要出汗的技术。得到它!
LPCSTR effectTechniqueName =渲染; modelObject.pTechnique = modelObject.pEffect-> GetTechniqueByName(effectTechniqueName);
如果(modelObject.pTechnique == NULL)
返回false;
//创建顶点布局
D3D10_PASS_DESC passDesc;
modelObject.pTechnique-> GetPassByIndex(0) - > GetDesc(安培; passDesc); HR = mpD3DDevice-> CreateInputLayout(布局,numElements个,
passDesc.pIAInputSignature,
passDesc.IAInputSignatureSize,
&安培; modelObject.pVertexLayout);
如果(FAILED(HR))
返回false; 返回true;
}
您指数
数组包含每个细胞6项(因为你正在绘制两个三角形每个),因此它应该被声明为
DWORD指数[NUM_ROWS * * NUM_COLS 6]
您得到的错误告诉你,你写的指数的边界之外
,这通常是朝着错误的声明暗示(或者一个错误的指数计算)。
well I think I know what the problem is. I am just having a hard time debugging it. I am working with the directx api and I am trying to generate a plane along the x and z axis according to a book I have. The problem is when I am creating my indices. I think I am setting values out of the bounds of the indices array. I am just having a hard time figuring out what I did wrong. I am unfamiliar with the this method of generating a plane. so its a little difficult for me. below is my code. Take emphasis on the indices loop.
[edit] Ive been reviewing it. This is how the indices works
int curVertex = x + (z * NUM_VERTSX);
This always gets the beginning vertices. so say we have 17 vertices on the x axis and 17 vertices on the z axis and we are on the first loop of the x and z axis
curVertx = 0 + (0 * 17) curVertx = 0 + 0 = 0
say we are on the first loop of the z axis and second loop of the x axis
curVertx = 1 + (0 * 17) curVertx = 1+ 0 = 1
indices[curIndex] = curVertex;
indices[curIndex + 1] = curVertex + NUM_VERTSX;
indices[curIndex + 2] = curVertex + 1;
indices[curIndex + 3] = curVertex + 1;
indices[curIndex + 4] = curVertex + NUM_VERTSX;
indices[curIndex + 5] = curVertex + NUM_VERTSX + 1;
if we are on the first
loop indices[curIndex] = curVertex;
this equals the first vertex = 0.
indices[curIndex + 1] = curVertex + NUM_VERTSX;
this equals the second row vertices (its always the vertices below the starting vertices
x x x x
[x] x x x
#include "MyGame.h"
//#include "CubeVector.h"
/* This code sets a projection and shows a turning cube. What has been added is the project, rotation and
a rasterizer to change the rasterization of the cube. The issue that was going on was something with the effect file
which was causing the vertices not to be rendered correctly.*/
typedef struct
{
ID3D10Effect* pEffect;
ID3D10EffectTechnique* pTechnique;
//vertex information
ID3D10Buffer* pVertexBuffer;
ID3D10Buffer* pIndicesBuffer;
ID3D10InputLayout* pVertexLayout;
UINT numVertices;
UINT numIndices;
}ModelObject;
ModelObject modelObject;
// World Matrix
D3DXMATRIX WorldMatrix;
// View Matrix
D3DXMATRIX ViewMatrix;
// Projection Matrix
D3DXMATRIX ProjectionMatrix;
ID3D10EffectMatrixVariable* pProjectionMatrixVariable = NULL;
//grid information
#define NUM_COLS 16
#define NUM_ROWS 16
#define CELL_WIDTH 32
#define CELL_HEIGHT 32
#define NUM_VERTSX (NUM_COLS + 1)
#define NUM_VERTSY (NUM_ROWS + 1)
bool MyGame::InitDirect3D()
{
if(!DX3dApp::InitDirect3D())
{
return false;
}
D3D10_RASTERIZER_DESC rastDesc;
rastDesc.FillMode = D3D10_FILL_WIREFRAME;
rastDesc.CullMode = D3D10_CULL_FRONT;
rastDesc.FrontCounterClockwise = true;
rastDesc.DepthBias = false;
rastDesc.DepthBiasClamp = 0;
rastDesc.SlopeScaledDepthBias = 0;
rastDesc.DepthClipEnable = false;
rastDesc.ScissorEnable = false;
rastDesc.MultisampleEnable = false;
rastDesc.AntialiasedLineEnable = false;
ID3D10RasterizerState *g_pRasterizerState;
mpD3DDevice->CreateRasterizerState(&rastDesc, &g_pRasterizerState);
mpD3DDevice->RSSetState(g_pRasterizerState);
// Set up the World Matrix
D3DXMatrixIdentity(&WorldMatrix);
D3DXMatrixLookAtLH(&ViewMatrix, new D3DXVECTOR3(0.0f, 10.0f, -20.0f), new D3DXVECTOR3(0.0f, 0.0f, 0.0f), new D3DXVECTOR3(0.0f, 1.0f, 0.0f));
// Set up the projection matrix
D3DXMatrixPerspectiveFovLH(&ProjectionMatrix, (float)D3DX_PI * 0.5f, (float)mWidth/(float)mHeight, 0.1f, 100.0f);
if(!CreateObject())
{
return false;
}
return true;
}
//These are actions that take place after the clearing of the buffer and before the present
void MyGame::GameDraw()
{
static float rotationAngle = 0.0f;
// create the rotation matrix using the rotation angle
D3DXMatrixRotationY(&WorldMatrix, rotationAngle);
rotationAngle += (float)D3DX_PI * 0.0f;
// Set the input layout
mpD3DDevice->IASetInputLayout(modelObject.pVertexLayout);
// Set vertex buffer
UINT stride = sizeof(VertexPos);
UINT offset = 0;
mpD3DDevice->IASetVertexBuffers(0, 1, &modelObject.pVertexBuffer, &stride, &offset);
mpD3DDevice->IASetIndexBuffer(modelObject.pIndicesBuffer, DXGI_FORMAT_R32_UINT, 0);
// Set primitive topology
mpD3DDevice->IASetPrimitiveTopology(D3D10_PRIMITIVE_TOPOLOGY_TRIANGLELIST);
// Combine and send the final matrix to the shader
D3DXMATRIX finalMatrix = (WorldMatrix * ViewMatrix * ProjectionMatrix);
pProjectionMatrixVariable->SetMatrix((float*)&finalMatrix);
// make sure modelObject is valid
// Render a model object
D3D10_TECHNIQUE_DESC techniqueDescription;
modelObject.pTechnique->GetDesc(&techniqueDescription);
// Loop through the technique passes
for(UINT p=0; p < techniqueDescription.Passes; ++p)
{
modelObject.pTechnique->GetPassByIndex(p)->Apply(0);
// draw the cube using all 36 vertices and 12 triangles
mpD3DDevice->DrawIndexed(modelObject.numIndices,0,0);
}
}
//Render actually incapsulates Gamedraw, so you can call data before you actually clear the buffer or after you
//present data
void MyGame::Render()
{
DX3dApp::Render();
}
bool MyGame::CreateObject()
{
VertexPos vertices[NUM_VERTSX * NUM_VERTSY];
for(int z=0; z < NUM_VERTSY; ++z)
{
for(int x = 0; x < NUM_VERTSX; ++x)
{
vertices[x + z * NUM_VERTSX].pos.x = (float)x * CELL_WIDTH;
vertices[x + z * NUM_VERTSX].pos.z = (float)z * CELL_HEIGHT;
vertices[x + z * NUM_VERTSX].pos.y = 0.0f;
vertices[x + z * NUM_VERTSX].color = D3DXVECTOR4(1.0, 0.0f, 0.0f, 0.0f);
}
}
DWORD indices[NUM_VERTSX * NUM_VERTSY];
int curIndex = 0;
for(int z=0; z < NUM_ROWS; ++z)
{
for(int x = 0; x < NUM_COLS; ++x)
{
int curVertex = x + (z * NUM_VERTSX);
indices[curIndex] = curVertex;
indices[curIndex + 1] = curVertex + NUM_VERTSX;
indices[curIndex + 2] = curVertex + 1;
indices[curIndex + 3] = curVertex + 1;
indices[curIndex + 4] = curVertex + NUM_VERTSX;
indices[curIndex + 5] = curVertex + NUM_VERTSX + 1;
curIndex += 6;
}
}
//Create Layout
D3D10_INPUT_ELEMENT_DESC layout[] = {
{"POSITION",0,DXGI_FORMAT_R32G32B32_FLOAT, 0 , 0, D3D10_INPUT_PER_VERTEX_DATA, 0},
{"COLOR",0,DXGI_FORMAT_R32G32B32A32_FLOAT, 0 , 12, D3D10_INPUT_PER_VERTEX_DATA, 0}
};
UINT numElements = (sizeof(layout)/sizeof(layout[0]));
modelObject.numVertices = sizeof(vertices)/sizeof(VertexPos);
//Create buffer desc
D3D10_BUFFER_DESC bufferDesc;
bufferDesc.Usage = D3D10_USAGE_DEFAULT;
bufferDesc.ByteWidth = sizeof(VertexPos) * modelObject.numVertices;
bufferDesc.BindFlags = D3D10_BIND_VERTEX_BUFFER;
bufferDesc.CPUAccessFlags = 0;
bufferDesc.MiscFlags = 0;
D3D10_SUBRESOURCE_DATA initData;
initData.pSysMem = vertices;
//Create the buffer
HRESULT hr = mpD3DDevice->CreateBuffer(&bufferDesc, &initData, &modelObject.pVertexBuffer);
if(FAILED(hr))
return false;
modelObject.numIndices = sizeof(indices)/sizeof(DWORD);
bufferDesc.ByteWidth = sizeof(DWORD) * modelObject.numIndices;
bufferDesc.BindFlags = D3D10_BIND_INDEX_BUFFER;
initData.pSysMem = indices;
hr = mpD3DDevice->CreateBuffer(&bufferDesc, &initData, &modelObject.pIndicesBuffer);
if(FAILED(hr))
return false;
/////////////////////////////////////////////////////////////////////////////
//Set up fx files
LPCWSTR effectFilename = L"effect.fx";
modelObject.pEffect = NULL;
hr = D3DX10CreateEffectFromFile(effectFilename,
NULL,
NULL,
"fx_4_0",
D3D10_SHADER_ENABLE_STRICTNESS,
0,
mpD3DDevice,
NULL,
NULL,
&modelObject.pEffect,
NULL,
NULL);
if(FAILED(hr))
return false;
pProjectionMatrixVariable = modelObject.pEffect->GetVariableByName("Projection")->AsMatrix();
//Dont sweat the technique. Get it!
LPCSTR effectTechniqueName = "Render";
modelObject.pTechnique = modelObject.pEffect->GetTechniqueByName(effectTechniqueName);
if(modelObject.pTechnique == NULL)
return false;
//Create Vertex layout
D3D10_PASS_DESC passDesc;
modelObject.pTechnique->GetPassByIndex(0)->GetDesc(&passDesc);
hr = mpD3DDevice->CreateInputLayout(layout, numElements,
passDesc.pIAInputSignature,
passDesc.IAInputSignatureSize,
&modelObject.pVertexLayout);
if(FAILED(hr))
return false;
return true;
}
Your indices
array contains 6 entries per 'cell' (since you're drawing two triangles for each), therefore it should be declared as
DWORD indices[NUM_ROWS * NUM_COLS * 6]
The error you get tells you, that you write outside the boundaries of indices
, this is usually either a hint towards a wrong declaration (or a wrong index calculation).
这篇关于运行时检查失败#2 - 围绕堆栈变量“指标”已损坏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!