Skip to main content
GameDev.net gamedev.net
🔒 Locked

Bizarre graphical anomalies, please help!

Started by Calneon Dec 3, 2010 at 1:56 PM 12 replies 2.2k views
Original Post
Calneon
Calneon
I'm learning DX10, and following a series of great tutorials on Bobby Anguelov's blog. Been going fine until I wanted to apply textures to the meshes I made by following his tutorial. You can recreate the exact thing I get by downloading the tutorial here (http://takinginitiative.net/2009/02/24/directx-10-tutorial-5-basic-meshes/), and adding any variable to the vertex structure in "vertexTypes.h". For example, edit the file so it says;

D3DXVECTOR3 pos;
D3DXVECTOR4 color;
D3DXVECTOR2 texCoord;

and then compile and run it. You will see some very strange graphical glitches and I have no idea why or how to fix it. Can anyone help? Thanks.

If you don't want to load up the project, this is what you see: http://imgur.com/W4Qbj.png

It's meant to be a cube.
smasherprog
smasherprog
Is that meant to be a single cube? or multiple cubes?
Wisdom is knowing when to shut up, so try it.
--Game Development http://nolimitsdesigns.com: Reliable UDP library, Threading library, Math Library, UI Library. Take a look, its all free.
16bit_port
16bit_port
Quote:
adding any variable to the vertex structure in "vertexTypes.h". For example, edit the file so it says;

D3DXVECTOR3 pos;
D3DXVECTOR4 color;
D3DXVECTOR2 texCoord;


Did you modify the input declaration to handle color and/or texture coordinates?
Calneon
Calneon
Quote:
Original post by 16bit_port
Quote:
adding any variable to the vertex structure in "vertexTypes.h". For example, edit the file so it says;

D3DXVECTOR3 pos;
D3DXVECTOR4 color;
D3DXVECTOR2 texCoord;


Did you modify the input declaration to handle color and/or texture coordinates?

No, I added the variable that's it. Not declaring it or using it anywhere. I have tried modifying the rest of the code to enable textures but get the same thing. I managed to remove code step by step and found that adding this variable caused the problem.

It's multiple cubes yes, sorry. Rotating.
Erik Rufelt
Erik Rufelt
You definitely need to modify the stride when setting the vertex buffer, as each vertex is larger if you add something to the structure, and in order to use the new parameter you need to modify the vertex declaration and the vertex shader input structure.
Calneon
Calneon
Quote:
Original post by Erik Rufelt
You definitely need to modify the stride when setting the vertex buffer, as each vertex is larger if you add something to the structure, and in order to use the new parameter you need to modify the vertex declaration and the vertex shader input structure.

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.
Erik Rufelt
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.
DickieChen
DickieChen
Quote:
Original post by Calneon
I'm learning DX10, and following a series of great tutorials on Bobby Anguelov's blog. Been going fine until I wanted to apply textures to the meshes I made by following his tutorial. You can recreate the exact thing I get by downloading the tutorial here (http://takinginitiative.net/2009/02/24/directx-10-tutorial-5-basic-meshes/), and adding any variable to the vertex structure in "vertexTypes.h". For example, edit the file so it says;

D3DXVECTOR3 pos;
D3DXVECTOR4 color;
D3DXVECTOR2 texCoord;

and then compile and run it. You will see some very strange graphical glitches and I have no idea why or how to fix it. Can anyone help? Thanks.

If you don't want to load up the project, this is what you see: http://imgur.com/W4Qbj.png

It's meant to be a cube.


well,the solution is simple,just turn on z-buffer:
d3dpp.EnableAutoDepthStencil=TRUE;
d3dpp.AutoDepthStencilFormat=D3DFMT_D16;//D16 standard
d3ddev->SetRenderState( D3DRS_ZENABLE, D3DZB_TRUE );

and when render():
d3ddev->Clear(0, 0, D3DCLEAR_TARGET|D3DCLEAR_ZBUFFER, RGB(255, 255, 255), 1.0f, 0L);
DickieChen
DickieChen
also read this:
http://www.toymaker.info/Games/html/z_buffer.html
for more information about z-buffer
Calneon
Calneon
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);
Coldon
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
"In theory, theory and practice are the same. In Practice, they never are."
My Technical Blog : http://www.takinginitiative.net/
Calneon
Calneon
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.
Calneon
Calneon
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]

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.