I have a HUGE problem and would really appreciate if someone could tell me how to fix it (sorry I don't have the code with me but I can describe it very well):
I'm using a texture and trying to map polygons with it, but the vertex/pixel shaders are not getting the texture coordinates. I know this because I've ruled out other problems:
- It is drawing the polygons the color of a single pixel of the texture. If I hard code the texture coordinates in the pixel shader, it gives a different color if I change the coordinates, or if I change which texture I give it to draw.
- The texture coordinates are what they're supposed to be in the data structure in C# that I'm using to store points. I checked them just before drawing a mesh.
- Also I made sure the structure format is the same size it thinks it should be (14 floats), and it matches InputLayout object I have to set up with an array of contexts (like COLOR, TEXCOORD, etc.).
In the shader file I declared a cbuffer, which just contains:
- A matrix for the view transformation
- A float4 to hold the camera position (just so I can do some calculations with that)
- A Texture2D
- SamplerState.
Then in C# I made a structure that matches those with a Matrix and a Vector4. I tried defining it with sequential and then explicit layout, and it made no difference.
I have two simple shaders in the file:
The vertex shader takes a structure for an input that contains:
- float4 POSITION
- float4 NORMAL
- float4 COLOR
- float2 TEXCOORD
The normal is for lighting purposes, and the color is because sometimes I draw them as a color instead of texture mapping, but don't worry about that part, because it works perfectly.
The output from the vertex shader and input to the pixel shader is as follows:
- float4 SV_POSITION
- float4 COLOR
- float2 TEXCOORD
The vertex shader is very simple - it only does the matrix transformation (which works), then it just copies the color and texcoord to the output (it's not even using the normal at the moment).
The pixel shader just returns a sample of the image at the texture coordinates input from the vertex shader (which should automatically be incremented between vertices by the rasterizer).
Interestingly, I have a different vertex and pixel shader that does successfully texture map. The only differences are:
- The vertex shader input structure only contains a float4 POSITION and a float2 TEXCOORD, and the vertex shader output/pixel shader input structure only contains a float4 SV_POSITION and float2 TEXCOORD (so they don't have COLOR or NORMAL).
- It doesn't have the float4 camera position in the constant buffer (and actually it doesn't even use a constant buffer - it just has the matrix, Texture2D and SamplerState declared like globals. However, I think I read that I should use constant buffers for that stuff, and ironically, I'm surprised that one even works, while this other one doesn't!).
Does anyone have any ideas? I'll try just about anything.