Bizarre graphical anomalies, please help!

Started by
12 comments, last by Calneon 13 years, 4 months ago
Quote:Original post by Erik Rufelt
Quote:Original post by Calneon
I don't know what the stride is, and I'm not using a vertex buffer. What you're saying makes sense, but I'm not sure what to do.


In your case the vertex buffer is inside the ID3DX10Mesh. In order to make it work, you must add the texcoord to the input-layout as well as to the vertex, as for example "TEXCOORD", 0, DXGI_FORMAT_R32G32, 0, 28, PER_VERTEX, 0. Then you need to change the creation of the input-layout to match this by setting the element count to 3 instead of 2, for D3DX10CreateMesh and CreateInputLayout, and you must also modify the effect so that the vertex shader input contains a float2 texcoord : TEXCOORD.

Ok, I've changed all that but it's not working, could you take a look at my code:

Vertex structure
*********************************

struct vertex
{
D3DXVECTOR3 pos;
D3DXVECTOR4 color;
D3DXVECTOR2 texCoords;

vertex()
{
}

vertex(D3DXVECTOR3 p, D3DXVECTOR4 c)
{
pos = p;
color = c;
}

vertex(D3DXVECTOR3 p, D3DXVECTOR4 c, D3DXVECTOR2 tex)
{
pos = p;
color = c;
texCoords = tex;
}
};

const D3D10_INPUT_ELEMENT_DESC vertexInputLayout[] =
{
{ "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 },
{ "TEXTURE", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 28, D3D10_INPUT_PER_VERTEX_DATA, 0 }
};

Shader file:
***************************************

//--------------------------------------------------------------------------------------
// basicEffect.fx
//--------------------------------------------------------------------------------------

matrix World;
matrix View;
matrix Projection;

struct PS_INPUT
{
float4 Pos : SV_POSITION;
float4 Color : COLOR;
float2 Tex : TEXCOORD;
};

struct VS_INPUT
{
float4 Pos : POSITION;
float4 Color : COLOR;
float2 Tex : TEXCOORD;
};

//--------------------------------------------------------------------------------------
// Vertex Shader
//--------------------------------------------------------------------------------------
PS_INPUT VS( VS_INPUT input )
{
PS_INPUT output;

output.Pos = mul( input.Pos, World );
output.Pos = mul( output.Pos, View );
output.Pos = mul( output.Pos, Projection );
output.Color = input.Color;
output.Tex = input.Tex;

return output;
}

//--------------------------------------------------------------------------------------
// Pixel Shader
//--------------------------------------------------------------------------------------
float4 PS( PS_INPUT input ) : SV_Target
{
return input.Color;
}

//--------------------------------------------------------------------------------------
// Techniques
//--------------------------------------------------------------------------------------
technique10 render
{
pass P0
{
SetVertexShader( CompileShader( vs_4_0, VS() ) );
SetGeometryShader( NULL );
SetPixelShader( CompileShader( ps_4_0, PS() ) );
}
}

Create input layout:
*****************************************

UINT numElements = sizeof( vertexInputLayout ) / sizeof( vertexInputLayout[0] );

//create input layout
D3D10_PASS_DESC PassDesc;
pBasicTechnique->GetPassByIndex( 0 )->GetDesc( &PassDesc );
if ( FAILED( pD3DDevice->CreateInputLayout( vertexInputLayout,
numElements,
PassDesc.pIAInputSignature,
PassDesc.IAInputSignatureSize,
&pVertexLayout ) ) ) return fatalError("Could not create Input Layout!");

// Set the input layout//
pD3DDevice->IASetInputLayout( pVertexLayout );

Creating mesh:
****************************************

vertex v[4];

v[0] = vertex( D3DXVECTOR3(-width, height, 0), D3DXVECTOR4(1,1,1,1) ); // front top left
v[1] = vertex( D3DXVECTOR3(width, height, 0), D3DXVECTOR4(1,1,1,1) ); // front top right
v[2] = vertex( D3DXVECTOR3(width, -height, 0), D3DXVECTOR4(1,1,1,1) ); // front bottom right
v[3] = vertex( D3DXVECTOR3(-width, -height, 0), D3DXVECTOR4(1,1,1,1) ); // front bottom left

// create indexes
unsigned int i[6] = {0, 1, 2,
0, 3, 2};

// create mesh
D3DX10CreateMesh( dx.getD3DDevice(), vertexInputLayout, 3, "POSITION", 4, 2, D3DX10_MESH_32_BIT, &sqMesh);
Advertisement
okay i'm gonna reply here instead of on my blog :)

your problem is simple and silly (but those are the one that usually leave you scratching your head)

in your input layout you define your texture coord as:

TEXTURE", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 28, D3D10_INPUT_PER_VERTEX_DATA, 0

or as three float values, and yet in your HLSL and vertex declaration you have it as a 2 float value...

whats happening is that the API uses the input layout to seperate the raw vertex buffer memory into seperate vertices, since your input layout states that the third element is 12 bytes large (3 floats), the seperation fails since now the first 4 byte value of the second vertex in the buffer will be added to the first vertex, and so on...

if you fix that line to state:

TEXTURE", 0, DXGI_FORMAT_R32G32_FLOAT, 0, 28, D3D10_INPUT_PER_VERTEX_DATA, 0

it should work :)

- Bobby

"In theory, theory and practice are the same. In Practice, they never are."
My Technical Blog : http://www.takinginitiative.net/

Quote:Original post by Coldon
okay i'm gonna reply here instead of on my blog :)

your problem is simple and silly (but those are the one that usually leave you scratching your head)

in your input layout you define your texture coord as:

TEXTURE", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 28, D3D10_INPUT_PER_VERTEX_DATA, 0

or as three float values, and yet in your HLSL and vertex declaration you have it as a 2 float value...

whats happening is that the API uses the input layout to seperate the raw vertex buffer memory into seperate vertices, since your input layout states that the third element is 12 bytes large (3 floats), the seperation fails since now the first 4 byte value of the second vertex in the buffer will be added to the first vertex, and so on...

if you fix that line to state:

TEXTURE", 0, DXGI_FORMAT_R32G32_FLOAT, 0, 28, D3D10_INPUT_PER_VERTEX_DATA, 0

it should work :)

- Bobby

Thanks, I got it working now. Started again starting with your code from Tutorial 5 and after a lot of tweaking got it working in my game :). Just want to say a massive thanks for writing the tutorials you have, I know for a fact that most people in my programming class at uni use them to learn DX10, and there seems to be a huge lack of good tutorials and references on the subject on the internet, so thanks again.
Any tips on giving different objects different textures? :) Seems whenever I call "pTextureSR->SetResource( dx.getTextureSRV()[1] );" in an object class, every object with a texture changes to that texture.

EDIT: Nevermind, moved it from the initialization method to the draw method.

[Edited by - Calneon on December 4, 2010 2:13:56 PM]

This topic is closed to new replies.

Advertisement