Jump to content

  • Log In with Google      Sign In   
  • Create Account


Textures over lapping and causing FPS drops, help.


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
2 replies to this topic

#1 Xooch   Members   -  Reputation: 179

Like
0Likes
Like

Posted 07 November 2012 - 09:39 AM

Hey there,

I've recently tried to texture a cube in DirectX11, although I've encountered a problem and Im not sure how to fix it. The problem is I only want the texture to apply to one of my two cubes, and it applies to both and seems to be constantly overlapping the texture, and not keeping it as 1 solid texture.

here is what the application currently looks like when its ran: http://img525.imageshack.us/img525/8264/directx11bug.png

and here is what the texture should look like: http://www.directxtutorial.com/Images/Textures/Wood.png

This is my render function will draws the applies translation etc to the cubes

Object.cpp
void Object::Render(ID3D11DeviceContext * pImmediateContext)
{
/* VARIABLES */
//Load Constant Buffer Struct
CBUFFER cBuffer;
//Setup HLSL lighting
cBuffer.LightVector  = D3DXVECTOR4(1.0f, 1.0f, 1.0f, 0.0f);
cBuffer.LightColor  = D3DXCOLOR(0.5f, 0.5f, 0.5f, 1.0f);
cBuffer.AmbientColor = D3DXCOLOR(0.2f, 0.2f, 0.2f, 1.0f);
//simple animation
static float Time = 0.0f;
  Time += 0.0001f;

/* SETUP MATRIX */
//Create the view matrix
D3DXMatrixLookAtLH(&matView,
	  &D3DXVECTOR3(0.0f, 5.0f, 10.0f),  //Camera position
	  &D3DXVECTOR3(0.0f, 0.0f, 0.0f),  //the look-at position
	  &D3DXVECTOR3(0.0f, 1.0f, 0.0f)  //the up direction
	  );
//Create the projection matrix
D3DXMatrixPerspectiveFovLH(&matProjection,
	    (FLOAT)D3DXToRadian(45),  //Field of View
	    (FLOAT)1280 / (FLOAT) 720,  //aspect ratio
	    1.0f,	   //near view-plane
	    100.0f);	  //far view-plane
// select which vertex buffer to display
    UINT stride = sizeof(VERTEX);
    UINT offset = 0;
    pImmediateContext->IASetVertexBuffers(0, 1, &g_pVertexBuffer, &stride, &offset);
pImmediateContext->IASetIndexBuffer(g_pIBuffer, DXGI_FORMAT_R32_UINT, 0);
// select which primtive type we are using
    pImmediateContext->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST);
/* CUBE 1 */
//Create the rotation matrix, spin on the Y axis at 0.001f per frame
D3DXMatrixRotationY(&matRotate, Time);
//Move the shape
D3DXMatrixTranslation(&matTranslate, 5.0f, 2.0f, 0.0f);
//Create the final transform
cBuffer.Final = matRotate * matTranslate * matView * matProjection;
cBuffer.Rotation = matRotate;
    //Load in Shaders
VertexShader(pImmediateContext);
PixelShader(pImmediateContext);
//Draw
    pImmediateContext->UpdateSubresource(g_pCBuffer, 0, 0, &cBuffer, 0, 0);
pImmediateContext->DrawIndexed(36, 0, 0);
/* CUBE 2 */
//Create the rotation matrix, spin on the Y axis at 0.001f per frame
D3DXMatrixRotationX(&matRotate, Time);
//Move the shape
D3DXMatrixTranslation(&matTranslate, -5.0f, 2.0f, 0.0f);
//Create the final transform
cBuffer.Final = matRotate * matTranslate * matView * matProjection;
cBuffer.Rotation = matRotate;
//Draw
    pImmediateContext->UpdateSubresource(g_pCBuffer, 0, 0, &cBuffer, 0, 0);
pImmediateContext->PSSetShaderResources(0, 1, &woodTexture);   //add texture
pImmediateContext->DrawIndexed(36, 0, 0);
}

main.cpp
obj.InitObject(gfx.getDevice(), gfx.getImmediateContext());
    // Main message loop
    MSG msg = {0};
obj.InitGraphics(gfx.getDevice(),  gfx.getImmediateContext());
    while( WM_QUIT != msg.message )
    {
	    if( PeekMessage( &msg, NULL, 0, 0, PM_REMOVE ) )
	    {
		    TranslateMessage( &msg );
		    DispatchMessage( &msg );
	    }
	    else
	    {
   gfx.clearBackBuffer();	   //Clear Stage
   obj.Render(gfx.getImmediateContext());   //Draw
   gfx.present();		 //Present
	    }
    }
obj.CleanupObject(); //Cleanup Objects
    gfx.CleanupDevice(); //Cleanup Graphics 
    return ( int )msg.wParam;


shader.fx
//Created by Tim Lawton 2012
//--------------------------------------------------------------------------------------
// Vertex Shader
//--------------------------------------------------------------------------------------
//Constant Buffer
cbuffer ConstantBuffer
{
float4x4 final;  //4x4 matrix which holds the final value
float4x4 rotation; //rotation matrix
float4 lightvec; //Light's Vector
float4 lightcol; //light's colour
float4 ambientcol; //Ambient light's colour
}
Texture2D Texture;
SamplerState ss;
//Struct to return two values
struct VOut
{
float4 color : COLOR;
float2 texcoord : TEXCOORD;
float4 position : SV_POSITION;
};

VOut VShader(float4 position : POSITION, float4 normal : NORMAL, float2 texcoord : TEXCOORD)
{
    VOut output;
    output.position = mul(final, position);
    // set the ambient light
    output.color = ambientcol;
    // calculate the diffuse light and add it to the ambient light
    float4 norm = normalize(mul(rotation, normal));
    float diffusebrightness = saturate(dot(norm, lightvec));
    output.color += lightcol * diffusebrightness;
//Set texture coordinates
output.texcoord = texcoord;
    return output;
}
//--------------------------------------------------------------------------------------
// Pixel Shader
//--------------------------------------------------------------------------------------
float4 PShader(float4 color : COLOR, float2 texcoord : TEXCOORD) : SV_TARGET
{
    return color * Texture.Sample(ss, texcoord);
}





Now I don't think theres any problems coming from my .fx file, and yes I have inserts the U and V cords into the Vertex Struct to apply texture to the cube, and have the load up method and it is being released at the end.

Although I dont know why it's doing this, has anyone else encountered a problem like this before and how did they overcome it?

in OpenGL I was able to do Begin() and Flush() between each object I was drawing, is there a function similar to that in DirectX11?

Thanks!

Sponsor:

#2 papulko   Members   -  Reputation: 786

Like
0Likes
Like

Posted 09 November 2012 - 12:07 PM

Long shot, but did you create and set the ID3D11SamplerState accordingly? My best bet would still be that one/some of the UV coordinates got the wrong value.

Edited by papulko, 09 November 2012 - 12:10 PM.


#3 Adam_42   Crossbones+   -  Reputation: 2360

Like
1Likes
Like

Posted 09 November 2012 - 02:23 PM

As a simple test to see if it's the texture co-ordinates you can adjust the pixel shader to show you some data. These options should show you what's going on - comment out the lines of code to try each one:

float4 PShader(float4 color : COLOR, float2 texcoord : TEXCOORD, float4 screenSpace : SV_Position) : SV_TARGET
{
	return float4(texcoord.x, texcoord.y, 0.0f, 1.0f);	 // Display texture coords in the red and green channels of the output for debugging
	return Texture.Sample(ss, screenSpace.xy);	 // Map texture in screen space for debugging

	return color * Texture.Sample(ss, texcoord);
}

P.S. The FPS drop is probably because mip mapping is off.

Edited by Adam_42, 09 November 2012 - 02:25 PM.





Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS