Jump to content

  • Log In with Google      Sign In   
  • Create Account


[slimDX] textures on the simpletriangle


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
3 replies to this topic

#1 georges123   Members   -  Reputation: 100

Like
0Likes
Like

Posted 02 January 2011 - 09:20 AM

i have been working with slimdx (direct3d11) for 2-3 months now.
i started programming about 1.5years ago, the only langue i know is c#.

thats why i chose for slimdx when i started working on my game, i have been able
to make basic triangles, squares, qube's, etc. implement rotation(on the cpu) and i made a basic game world in which i can move around.

uptil now i have used shader only to return a color, colering my world by giving each polygon another colered shader, but now i would like to use textures(image .bmp or .jpg or something like that) instead of colors for my polygons.

so my question is how do i get to use a image instead of a color to be drawn as my polygon?

i have been searching for 3days and so far i havent found any helpfull tuturial about the subject for slimdx, i have found a dx11 tuturial to place a texture on a cube. (http://msdn.microsoft.com/en-us/library/ff729724(VS.85).aspx)
and i have been trying to use it to put a texture on the simpletriangle slimdx tutorial, but the only thing i get are erors or just a plain blue background.
this i probably due to the fact that i dont understand c++ or hlsl.

so if anyone has/knows some working code/ tuturial to put a texture on a square, triangle or cube , i would realy apriciate that.

the thing i'm currently stuck on is this :

from the msdn dx11 tutrial mentioned above :


c++ code (i assume)

g_pImmediateContext->PSSetShaderResources( 0, 1, &g_pTextureRV );
g_pImmediateContext->PSSetSamplers( 0, 1, &g_pSamplerLinear );


hlsl code (i assume)
// Pixel Shader
float4 PS( PS_INPUT input) : SV_Target
{
return txDiffuse.Sample( samLinear, input.Tex ) * vMeshColor;
}
// Vertex Shader
PS_INPUT VS( VS_INPUT input )
{
PS_INPUT output = (PS_INPUT)0;
output.Pos = mul( input.Pos, World );
output.Pos = mul( output.Pos, View );
output.Pos = mul( output.Pos, Projection );
output.Tex = input.Tex;

return output;
}


i tryed transelating this code to slim dx and i seem to be able to work around most errors i get but i keep getting a txDiffuse doesent exist exeption.

exept for here (txDiffuse is the object storing our texture that we passed in from the code above, when we bound the resource view g_pTextureRV to it.)
the name txdiffuse isent mentioned any where else in the tuturial, leading me to assume that its not a varable but some kind of a keyword that exists in directx but not in slim dx.(i could be totaly rong here no clue how hlsl works just speculation.)

the g_pTextureRV thats mentioned in the "explenation" of the txdiffuse thingie,
is set here: g_pImmediateContext->PSSetShaderResources( 0, 1, &g_pTextureRV );
which i assume corresponds to this : device.ImmediateContext.PixelShader.SetShaderResource(resourceView, 0);
in slimdx but there doesent seem a way for me to pas g_pTextureRV there, this leaves me a bit confused.

i would apreciate it if anyone could offer me some help whit this problem.

Sponsor:

#2 STufaro   Members   -  Reputation: 183

Like
0Likes
Like

Posted 02 January 2011 - 12:54 PM

Your HLSL shader that you posted -- is that the complete shader? You never defined "txDiffuse," so it'll tell you that it doesn't exist when it tries to compile the shader.

You're going to want to take a look at Riemer's XNA or Managed DirectX tutorials for this problem; it's explained nicely.

This has exactly what you're looking for:
http://www.riemers.net/eng/Tutorials/XNA/Csharp/Series3/Textured_triangle.php

The basics as I understand them in DirectX 9 are this: You need to set up a shader with a texture sampler (tells the graphics card how to pick/generate pixels from the texture). You then set the texture in the shader in your C++ program, and the sampler figures out how to color the pixels from the texture you fed it and your pixel shader code. I don't know if it's exactly the same for Direct3D 11, but something tells me the HLSL interaction part should be similar.

Best,
- Steve.

#3 georges123   Members   -  Reputation: 100

Like
0Likes
Like

Posted 04 January 2011 - 12:05 PM

i finally got a texture to be painted(on a square)

the tutorial you linked to was indeed very helpfull, it gave me a better insight in how shaders function, so tank you for that.

but there seems to be some difference between xna dx9 and slimdx 11 so i still had to do a lot of puzzeling to get the syntax right.

so i'm gonna leave my working code here so the next person whith a problem like this may find it usefull.

its not realy the nicest code and i dont know if its the best (or even the right) way to do it but i got a picture to show up on the screen so for what its worth :


program.cs
using System.Windows.Forms;
using SlimDX;
using SlimDX.D3DCompiler;
using SlimDX.Direct3D11;
using SlimDX.DXGI;
using SlimDX.Windows;
using Device = SlimDX.Direct3D11.Device;
using Resource = SlimDX.Direct3D11.Resource;

namespace SimpleTriangle
{
static class Program
{
static void Main()
{
Device device;
SwapChain swapChain;
ShaderSignature inputSignature;
VertexShader vertexShader;
PixelShader pixelShader;

var form = new RenderForm("Tutorial 3: Simple Triangle");
var description = new SwapChainDescription()
{
BufferCount = 2,
Usage = Usage.RenderTargetOutput,
OutputHandle = form.Handle,
IsWindowed = true,
ModeDescription = new ModeDescription(0, 0, new Rational(60, 1), Format.R8G8B8A8_UNorm),
SampleDescription = new SampleDescription(1, 0),
Flags = SwapChainFlags.AllowModeSwitch,
SwapEffect = SwapEffect.Discard
};

Device.CreateWithSwapChain(DriverType.Hardware, DeviceCreationFlags.None, description, out device, out swapChain);

// create a view of our render target, which is the backbuffer of the swap chain we just created
RenderTargetView renderTarget;
using (var resource = Resource.FromSwapChain<Texture2D>(swapChain, 0))
renderTarget = new RenderTargetView(device, resource);

// setting a viewport is required if you want to actually see anything
var context = device.ImmediateContext;
var viewport = new Viewport(0.0f, 0.0f, form.ClientSize.Width, form.ClientSize.Height);
context.OutputMerger.SetTargets(renderTarget);
context.Rasterizer.SetViewports(viewport);
ShaderBytecode effectByteCode = ShaderBytecode.CompileFromFile("effect.fx", "Render", "fx_5_0", /*"vs_4_0_level_9_1", */ShaderFlags.None, EffectFlags.None);
Effect effect = new Effect(device,effectByteCode);


SamplerDescription a = new SamplerDescription();
a.AddressU = TextureAddressMode.Wrap;
a.AddressV = TextureAddressMode.Wrap;
a.AddressW = TextureAddressMode.Wrap;

a.Filter = Filter.MinPointMagMipLinear;

SamplerState b = SamplerState.FromDescription(device, a);

Texture2D flower = Texture2D.FromFile(device, "rainbow.bmp");

ShaderResourceView resourceView = new ShaderResourceView(device, flower);
device.ImmediateContext.PixelShader.SetShaderResource(resourceView, 0);

effect.GetVariableByName("xTexture").AsResource().SetResource(resourceView);
effect.GetVariableByName("TextureSampler").AsSampler().SetSamplerState(0, b);

context.PixelShader.SetShaderResource(resourceView, 0);
context.PixelShader.SetSampler(b,0);

using (var bytecode = ShaderBytecode.CompileFromFile("effect.fx", "vs_main", "vs_4_0", ShaderFlags.None, EffectFlags.None))
{
inputSignature = ShaderSignature.GetInputSignature(bytecode);
vertexShader = new VertexShader(device, bytecode);
}


using (var bytecode = ShaderBytecode.CompileFromFile("effect.fx", "ps_main", "ps_5_0", ShaderFlags.None, EffectFlags.None))
pixelShader = new PixelShader(device, bytecode);


var vertices = new DataStream(20 * 4, true, true);

vertices.Write(new Vector3(-0.5f, -0.5f, 0.5f));vertices.Write(new Vector2(1f, 1f));
vertices.Write(new Vector3(-0.5f, 0.5f, 0.5f)); vertices.Write(new Vector2(0f, 1f));
vertices.Write(new Vector3(0.5f, -0.5f, 0.5f)); vertices.Write(new Vector2(1f, 0f));
vertices.Write(new Vector3(0.5f, 0.5f, 0.5f)); vertices.Write(new Vector2(0f, 0f));

vertices.Position = 0;

var elements = new[] { new InputElement("POSITION", 0, Format.R32G32B32_Float, 0), new InputElement("textcoord", 0, Format.R32G32_Float, 12, 0) };
var layout = new InputLayout(device, inputSignature, elements);
var vertexBuffer = new Buffer(device, vertices,20 * 4, ResourceUsage.Default, BindFlags.VertexBuffer, CpuAccessFlags.None, ResourceOptionFlags.None, 0);

context.InputAssembler.InputLayout = layout;
context.InputAssembler.PrimitiveTopology = PrimitiveTopology.TriangleStrip;
context.InputAssembler.SetVertexBuffers(0, new VertexBufferBinding(vertexBuffer,20, 0));

context.VertexShader.Set(vertexShader);
context.PixelShader.Set(pixelShader);

MessagePump.Run(form, () =>
{

context.ClearRenderTargetView(renderTarget, new Color4(0.0f, 0.0f, 0.0f));


context.Draw(4, 0);
swapChain.Present(0, PresentFlags.None);
});

vertices.Close();
vertexBuffer.Dispose();
layout.Dispose();
inputSignature.Dispose();
vertexShader.Dispose();
renderTarget.Dispose();
swapChain.Dispose();
device.Dispose();
}
}
}




fx file :

Texture2D <float4> xTexture;
sampler TextureSampler;


struct VS_IN
{
float4 pos : POSITION;
float2 cords :textcoord;
};

struct PS_IN
{
float4 pos : SV_POSITION;
float2 cords :textcoord;
};

PS_IN vs_main(VS_IN input)
{
PS_IN output = (PS_IN)0;
output.pos = input.pos;
output.cords =input.cords;
return output;
}

float4 ps_main(PS_IN input) : SV_Target
{
float2 temp;
temp = float2(input.cords[0],input.cords[1]);

return xTexture.Sample(TextureSampler,temp );
}




#4 Xyno   Members   -  Reputation: 100

Like
0Likes
Like

Posted 10 September 2011 - 08:09 AM

Thx for the snippet georges123,

It was usefull for me, the effect part was missing in my code.

Cheers and GL for your game, we all share the same passion :)

Xyno




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