This is a noob question, but I wasn't sure if it belonged in the beginners or graphics forums.
I'm learning DirectX and trying to get textures figured out, but when I load my texture, it just duplicates the first pixel over my entire surface.
To let you know in advance, I am using the most recent SDK and Visual Studio 2005 standard edition.
Here is my code. "graphics" the first parameter, is the name of my Direct3d device:
Bitmap bmp = new Bitmap("red.bmp");texture =
new Microsoft.DirectX.Direct3D.Texture(graphics, bmp, 0, Microsoft.DirectX.Direct3D.Pool.Managed);And later when I try rendering:
graphics.SetTexture(0, texture);
graphics.VertexFormat = Direct3D.
CustomVertex.TransformedTextured.Format;graphics.DrawUserPrimitives(Direct3D.
PrimitiveType.TriangleStrip, 2, vertexes);
Thanks in advance for any help.

Texture simply duplicates the first pixel
Tom Rahav
Maybe this is a result of different FSAA settings in the control panel for the hardware. That was the reason why I am notice the half pixel offset. Without AA and points sampling you don’t need it but as soon as you use AA bad things can happen if you don’t care about the pixel center.
francois974
Managed DirectX is only a small layer over the C++ version. Because of this everything that is defined for C++ is still in place if you use C#. The texture coordinates don’t get this offset. You have to apply it to the position.
ActiveBrian
I copied your code with very minimal changes into the EmptyProject sample from the SDK and it worked perfectly. So that leads me to think there is something up elsewhere in your code.
1st obvious thing to check is that your are loading the bitmap you think you are. A bitmap file called 'red' makes me think it might all be red :-) Since you have no paths it will be loading the file from the start directory which will default to bin/debug
Are there any texture states or render states set anywhere in the code
Jude A
Sorry to say the texture appears perfectly on my PC.
So what is left to try
1. Try creating the device using the reference rasterizer and see if your texture appears
2. Try running some other texture code and see if that works http://www.c-unit.com/tutorials/mdirectx/ t=37 would be a good one.
3. Upgrade/downgrade your video card driver to a WHQL certified one and see if that makes a difference.
As for the half pixel offset, that may be a problem you have to deal with later. Its normal effect is to make your texture appear blurry - I've never seen it turn a whole texture one color before but Ralf thinks it may be an interaction with FSAA - so try looking at your graphics card settings in control panel to see if you have any odd settings turn on, or high performance etc.
dglane
Well I added those renderstates to my code, still works. Maybe put the zip the whole project and the bitmap and put it on a site somewhere (or email it to me). I don't see anything obviously wrong.
Tiago Nobrega
Interesting - that looks fine to me. Or, more importantly, it doesn't look like it'd have any obvious visual artifacts...
The only other thing that I've had cause these sorts of problems is a mismatched vertex declaration (or FVF). If you get those wrong then the GPU will read the incorrect bit of raw-binary as a texture coordinate and you'll get all sorts of funky results.
I think this might be your problem - you're using TransformedTextured, which at a guess is (D3DFVF_XYZRHW|D3DFVF_TEX1) to people like me (
). That would mean you'd have to have a 'w' component in your vertex. Without that you'll get the situation I just described - it might be reading 'tu' as the 'w' coordinate and 'tv' as the 'u' coordinate and then the 'x' of the next vertex as 'tv' - or something suitably broken.
hth
Jack
Cognitronic
I have render states for alpha blending (from a previous part of the tutorial). The texture I'm trying to load is a big block of red with black scribbles and a single green pixel in the upper left, and naturally, the entire thing is green.
I'll add the other two methods that deal with graphics. Thanks in advance.
public
bool InitializeGraphics(){
try { // Now let's setup the Direct3D stuffDirect3D.
PresentParameters presentParams = new Direct3D.PresentParameters(); if (windowed == true){
presentParams.Windowed =
true;}
else{
presentParams.Windowed =
false;Direct3D.
Format current = Direct3D.Manager.Adapters[0].CurrentDisplayMode.Format;presentParams.BackBufferCount = 1;
presentParams.BackBufferFormat = current;
presentParams.BackBufferWidth = screenWidth;
presentParams.BackBufferHeight = screenHeight;
}
presentParams.SwapEffect = Direct3D.
SwapEffect.Discard; // Create the graphicsgraphics =
new Direct3D.Device(0, Direct3D.DeviceType.Hardware, this, Direct3D.CreateFlags.SoftwareVertexProcessing, presentParams);graphics.RenderState.CullMode = Direct3D.
Cull.None;graphics.RenderState.AlphaBlendEnable =
true;graphics.RenderState.AlphaBlendOperation = Direct3D.
BlendOperation.Add;graphics.RenderState.DestinationBlend = Microsoft.DirectX.Direct3D.
Blend.InvSourceAlpha;graphics.RenderState.SourceBlend = Microsoft.DirectX.Direct3D.
Blend.SourceAlpha; // Setup the event handlers for the graphicsgraphics.DeviceLost +=
new EventHandler(this.InvalidateDeviceObjects);graphics.DeviceReset +=
new EventHandler(this.RestoreDeviceObjects);graphics.Disposing +=
new EventHandler(this.DeleteDeviceObjects);graphics.DeviceResizing +=
new CancelEventHandler(this.EnvironmentResizing); return true;}
catch (DirectXException) { return false;}
}
protected
virtual void Render(){
if (graphics != null){
if (graphicsLost){
try{
graphics.TestCooperativeLevel();
}
catch (Direct3D.DeviceLostException){
// device cannot be reaquired yet, just return return;}
catch (Direct3D.DeviceNotResetException){
// device has not been reset, but it can be reaquired nowgraphics.Reset(graphics.PresentationParameters);
}
graphicsLost =
false;}
try{
graphics.Clear(Direct3D.
ClearFlags.Target, Color.Black, 1.0f, 0);graphics.BeginScene();
graphics.SetTexture(0, texture);
graphics.VertexFormat = Direct3D.
CustomVertex.TransformedTextured.Format;graphics.DrawUserPrimitives(Direct3D.
PrimitiveType.TriangleStrip, 2, vertexes); // TODO : Scene renderinggraphics.EndScene();
graphics.Present();
}
catch (Direct3D.DeviceLostException){
graphicsLost =
true;}
}
David Johnson MCSD
Sent it. Thanks for the help.
BTW, Ralf, are you certain that C# uses the half-pixel offset. All of the documentation I've found indicates that texture coordinates are a scalar between 0 and 1. I know C++ uses it (via the link you gave me), but I don't think c# does.
jaedge
Looks like you are missing to subtract the half pixel offset to compensate the different pixel center that Direct3D use:
Http://msdn.microsoft.com/library/en-us/directx9_c/Directly_Mapping_Texels_to_Pixels.asp frame=true
rockybooth
Smurble
Well, that makes things more difficult.
Lots of that post is over my head, even after a bit of googling, but responding to what I do understand:
Other texture's work (including the specific tutorial you gave me).
Regarding high performance mode, if I look at my adapter settings (display properties, settings, advanced) The troubleshoot tab has the hardware acceleration that can be set between none and full (I incrementally lowered this until my program crashed). I don't know if this is what you mean.
The only other possibly enlightening thing I saw was under a tab called "PowerPlay" (which I think is an ATI-specific tab), it says that the graphics adapter is running in high performance mode, but I couldn't find anything to change that. If I lowered the Hardware acceleration bar too far, the tab would vanish, but until then, it said the same thing.
If you know a way to change it from high performance mode, I can try that, otherwise, I think I'm at the end of the line.
KarlH
Hello everyone, i have exactly the same problem here. In fact it's the same code.
The code is from the book "Beginning C# Game Programming" and i get only the first pixel drawn. This book was written when DirectX 9.b was in use. Even the author said that there were some differences to consider between his code and the new version (at that time) DirectX 9.c.
Just like Takalth, i'm using an ATI video card (mine is a Mobility card). May the default configuration from ATI cards be the problem I have seen a lot of projects on the web that run wonderfully on my machine, using textures (really complex code compared to the code from the book), but my code doesn't work.
I almost forget. I'm using DirectX 9.c to build my code. What version did everyone who succeed use
Greetings
ihsansolak
If you want the whole texture to be displayed, you need to be specifying values from 0.0 to 1.0 depending on the shape of your vertices/triangles.
Incorrect texture coordinates can cause all sorts of unexpected texturing results
hth
Jack
AnaXyd
Here is the code. I'm rendering onto a large square. The square renders as all one color, and it's always the first pixel in my bitmap. Thanks
vertexes =
new Direct3D.CustomVertex.TransformedTextured[4];
// top-left vertex:vertexes[0].X = screenWidth / 4.0f;
vertexes[0].Y = screenHeight / 4.0f;
vertexes[0].Z = 0.0f;
vertexes[0].Tu = 0.0f;
vertexes[0].Tv = 0.0f;
// top-right vertex:vertexes[1].X = (screenWidth / 4.0f) * 3.0f;
vertexes[1].Y = screenHeight / 4.0f;
vertexes[1].Z = 0.0f;
vertexes[1].Tu = 1.0f;
vertexes[1].Tv = 0.0f;
// bottom-left vertex:vertexes[2].X = screenWidth / 4.0f;
vertexes[2].Y = (screenHeight / 4.0f) * 3.0f;
vertexes[2].Z = 0.0f;
vertexes[2].Tu = 0.0f;
vertexes[2].Tv = 1.0f;
// bottom-right vertex:vertexes[3].X = (screenWidth / 4.0f) * 3.0f;
vertexes[3].Y = (screenHeight / 4.0f) * 3.0f;
vertexes[3].Z = 0.0f;
vertexes[3].Tu = 1.0f;
vertexes[3].Tv = 1.0f;