Here is the code, i know it's full of improper techniques as I am currently trying everything to make it work.
MDX 1.1 in C# express.
Trying to draw cube with texture using .fx file.
Very little of the texture renders, can't see any outlines or primitives rendered on the cube. The parts that do render flicker so i know i am "looking" at the cube.. Changing the number of primitives and primitive type renders different portions of the texture but still all wrong.
Cube code: primitive = new CustomVertex.PositionNormalTextured[36]; Vector3 topLeftFront = new Vector3(-0.5f, 0.5f, 0.5f); Vector3 bottomLeftFront = new Vector3(-0.5f, -0.5f, 0.5f); Vector3 topRightFront = new Vector3(0.5f, 0.5f, 0.5f); Vector3 bottomRightFront = new Vector3(0.5f, -0.5f, 0.5f); Vector3 topLeftBack = new Vector3(-0.5f, 0.5f, -0.5f); Vector3 topRightBack = new Vector3(0.5f, 0.5f, -0.5f); Vector3 bottomLeftBack = new Vector3(-0.5f, -0.5f, -0.5f); Vector3 bottomRightBack = new Vector3(0.5f, -0.5f, -0.5f);
Vector3 ZP = new Vector3(0f, 0f, 1f); Vector3 ZN = new Vector3(0f, 0f, -1f); Vector3 XP = new Vector3(1f, 0f, 0f); Vector3 XN = new Vector3(-1f, 0f, 0f); Vector3 YP = new Vector3(0f, 1f, 0f); Vector3 YN = new Vector3(0f, -1f, 0f);
Vector2 textureTopLeft = new Vector2(0.0f, 0.0f); Vector2 textureTopRight = new Vector2(1.0f, 0.0f); Vector2 textureBottomLeft = new Vector2(0.0f, 1.0f); Vector2 textureBottomRight = new Vector2(1.0f, 1.0f);
// Front face primitive[0] = new CustomVertex.PositionNormalTextured(topLeftFront, ZP, textureTopLeft.X,textureTopLeft.Y); primitive[1] = new CustomVertex.PositionNormalTextured(bottomLeftFront, ZP, textureTopRight.X, textureTopRight.Y); primitive[2] = new CustomVertex.PositionNormalTextured(topRightFront, ZP, textureBottomLeft.X, textureBottomLeft.Y); primitive[3] = new CustomVertex.PositionNormalTextured(bottomLeftFront, ZP, textureBottomLeft.X, textureBottomLeft.Y); primitive[4] = new CustomVertex.PositionNormalTextured(bottomRightFront, ZP, textureTopRight.X, textureTopRight.Y); primitive[5] = new CustomVertex.PositionNormalTextured(topRightFront, ZP, textureBottomRight.X, textureBottomRight.Y);
// Back face primitive = new CustomVertex.PositionNormalTextured(topLeftBack, ZN, textureTopLeft.X, textureTopLeft.Y); primitive[7] = new CustomVertex.PositionNormalTextured(topRightBack, ZN, textureTopRight.X, textureTopRight.Y); primitive = new CustomVertex.PositionNormalTextured(bottomLeftBack, ZN, textureBottomLeft.X, textureBottomLeft.Y); primitive[9] = new CustomVertex.PositionNormalTextured(bottomLeftBack, ZN, textureBottomLeft.X, textureBottomLeft.Y); primitive[10] = new CustomVertex.PositionNormalTextured(topRightBack, ZN, textureTopRight.X, textureTopRight.Y); primitive[11] = new CustomVertex.PositionNormalTextured(bottomRightBack, ZN, textureBottomRight.X, textureBottomRight.Y);
// Top face primitive[12] = new CustomVertex.PositionNormalTextured(topLeftFront, XP, textureTopLeft.X, textureTopLeft.Y); primitive[13] = new CustomVertex.PositionNormalTextured(topRightBack, XP, textureTopRight.X, textureTopRight.Y); primitive[14] = new CustomVertex.PositionNormalTextured(topLeftBack, XP, textureBottomLeft.X, textureBottomLeft.Y); primitive[15] = new CustomVertex.PositionNormalTextured(topLeftFront, XP, textureBottomLeft.X, textureBottomLeft.Y); primitive[16] = new CustomVertex.PositionNormalTextured(topRightFront, XP, textureTopRight.X, textureTopRight.Y); primitive[17] = new CustomVertex.PositionNormalTextured(topRightBack, XP, textureBottomRight.X, textureBottomRight.Y);
// Bottom face primitive[18] = new CustomVertex.PositionNormalTextured(bottomLeftFront, XN, textureTopLeft.X, textureTopLeft.Y); primitive[19] = new CustomVertex.PositionNormalTextured(bottomLeftBack, XN, textureTopRight.X, textureTopRight.Y); primitive[20] = new CustomVertex.PositionNormalTextured(bottomRightBack, XN, textureBottomLeft.X, textureBottomLeft.Y); primitive[21] = new CustomVertex.PositionNormalTextured(bottomLeftFront, XN, textureBottomLeft.X, textureBottomLeft.Y); primitive[22] = new CustomVertex.PositionNormalTextured(bottomRightBack, XN, textureTopRight.X, textureTopRight.Y); primitive[23] = new CustomVertex.PositionNormalTextured(bottomRightFront, XN, textureBottomRight.X, textureBottomRight.Y); // Left face primitive[24] = new CustomVertex.PositionNormalTextured(topLeftFront, YP, textureTopLeft.X, textureTopLeft.Y); primitive[25] = new CustomVertex.PositionNormalTextured(bottomLeftBack, YP, textureTopRight.X, textureTopRight.Y); primitive[26] = new CustomVertex.PositionNormalTextured(bottomLeftFront, YP, textureBottomLeft.X, textureBottomLeft.Y); primitive[27] = new CustomVertex.PositionNormalTextured(topLeftBack, YP, textureBottomLeft.X, textureBottomLeft.Y); primitive[28] = new CustomVertex.PositionNormalTextured(bottomLeftBack, YP, textureTopRight.X, textureTopRight.Y); primitive[29] = new CustomVertex.PositionNormalTextured(topLeftFront, YP, textureBottomRight.X, textureBottomRight.Y);
// Right face primitive[30] = new CustomVertex.PositionNormalTextured(topRightFront, YN, textureTopLeft.X, textureTopLeft.Y); primitive[31] = new CustomVertex.PositionNormalTextured(bottomRightFront, YN, textureTopRight.X, textureTopRight.Y); primitive[32] = new CustomVertex.PositionNormalTextured(bottomRightBack, YN, textureBottomLeft.X, textureBottomLeft.Y); primitive[33] = new CustomVertex.PositionNormalTextured(topRightBack, YN, textureBottomLeft.X, textureBottomLeft.Y); primitive[34] = new CustomVertex.PositionNormalTextured(topRightFront, YN, textureTopRight.X, textureTopRight.Y); primitive[35] = new CustomVertex.PositionNormalTextured(bottomRightBack, YN, textureBottomRight.X, textureBottomRight.Y);
Render Code:
device.VertexFormat = CustomVertex.PositionNormalTextured.Format; device.RenderState.CullMode = Cull.Clockwise; vb = new VertexBuffer(typeof(CustomVertex.PositionNormalTextured), primitiveToRender.Length, device, Usage.WriteOnly, CustomVertex.PositionNormalTextured.Format, Pool.Managed);
effect.Technique = "Simple";
int passes = effect.Begin(FX.None);
for (int i = 0; i < passes; i++) { effect.BeginPass(i); effect.CommitChanges(); device.DrawUserPrimitives(PrimitiveType.TriangleFan, 12, primitiveToRender); effect.EndPass(); } effect.End();
//calculate fps frameCount += 1; lastFpsShow += elapsedTime;
if (lastFpsShow >= 1) { Console.WriteLine((float)frameCount); lastFps = frameCount; frameCount = 0; lastFpsShow = 0; }
DrawDebugPanel(); device.EndScene(); device.Present(); }
.fx file code
//Input variables float4x4 projection; float4x4 view; float4x4 world; texture myTexture;
struct VS_INPUT { float4 Position : POSITION0; float2 Texcoord : TEXCOORD0; };
struct VS_OUTPUT { float4 Position : POSITION0; float2 Texcoord : TEXCOORD0; };
VS_OUTPUT SimpleVS(VS_INPUT In) { VS_OUTPUT Out;
//Move to screen space Out.Position = mul(In.Position, world); Out.Texcoord = In.Texcoord; return Out; }
sampler2D baseMap = sampler_state { Texture = (myTexture); ADDRESSU = WRAP; ADDRESSV = WRAP; MINFILTER = LINEAR; MAGFILTER = LINEAR; };
struct PS_INPUT { float2 Texcoord : TEXCOORD0; };
float4 SimplePS( PS_INPUT Input ) : COLOR0 { return tex2D( baseMap, Input.Texcoord ); }
//--------------------------------------------------------------// // Technique Section for Simple screen transform //--------------------------------------------------------------// technique Simple { pass Single_Pass { VertexShader = compile vs_1_1 SimpleVS(); PixelShader = compile ps_1_1 SimplePS(); }
}
Init effect code:
private Effect InitBasicEffect(Effect effect, Device device) { //create camera matrices Matrix view = Matrix.LookAtLH(new Vector3(0.2f, 3.0f, 3.0f), new Vector3(0.0f, 0.0f, 0.0f), new Vector3(0.0f, 1.0f, 0.0f)); ;
Matrix projection = Matrix.PerspectiveFovLH( (float)Math.PI / 4.0f, // 2 PI Radians is 360 degrees, so this is 45 degrees (float)device.DepthStencilSurface.Description.Width / (float)device.DepthStencilSurface.Description.Height, 1.0f, 100.0f); ;
string s = null; string s1 = "";
effect = Effect.FromFile(device, "Content/simpleRedux.fx", null, s1, ShaderFlags.None, null,out s);
//.fx compile error results in null effect.SetValue("projection", projection); effect.SetValue("view", view); //set up texture switch (texIndex) { case 0: tex = TextureLoader.FromFile(device, "Content/texture256.jpg"); //tex.Name = "256x256"; break; case 1: tex = TextureLoader.FromFile(device, "Content/texture512.jpg"); //tex.Name = "512x412"; break; case 2: tex = TextureLoader.FromFile(device, "Content/texture1024.jpg"); //tex.Name = "1024x1024"; break; case 3: tex = TextureLoader.FromFile(device, "Content/texture2048.jpg"); //tex.Name = "2048x2048"; break; case 4: tex = TextureLoader.FromFile(device, "Content/texture4096.jpg"); //tex.Name = "4096x4096"; break; default: tex = TextureLoader.FromFile(device, "Content/texture256.jpg"); //tex.Name = "256x256"; break; }
effect.SetValue("myTexture", tex);
//debugging Texture test = effect.GetValueTexture("myTexture");
Sorry if this is vague, very tired.
| | lushdog | One obvious problem that I can see is that you are trying to render a cube as a TriangleFan primitive, that should be TriangleList.
Another note, although it's not attached to anything right now, you should not be creating vertex buffers each frame - it is far better to create them once and re-use them from frame to frame, and when you are done with them make sure you Dispose them. Otherwise the GC won't clean them up even when they eat up video memory, because the actual storage of the buffer in video memory is unmanaged and unknown to the GC. | | Robert Dunlop | Thanks for your reply.
I do realize it should be TriangleList, I was just playing around with settings to see if there were any differences with what was being rendered.
Thanks for the VertexBuffer help.
I'm not sure what is going wrong. I had similar functionality working with XNA but switching to Effect from BasicEffect is a little difficult. Before i would just set the World, View, Projection matrixes in the BasicEffect properties. But in Effect i'm not sure how they are manipulated to set the camera, translation etc. etc.
Oh well, it's a learning process :)
lushdog | | lushdog | Are you setting the world parameter somewhere with a world matrix? It's not apparent from your code, if not then it's uninitialized. Presumably you are doing that in other code that I don't see in your post? Also you are not using the view and projection matrices, only the world, but maybe you are concatenating them together into the world matrix elsewhere?
The flickering you mention would seem to point to a certain class of problem, namely that you are not getting stable results from frame to frame. This could be caused by an uninitialized shader parameter, mismatched vertex data, or not properly clearing the depth buffer, to name a few such encounters that come to mind.
Are you varying the transformation of the cube from frame to frame? If you are not, and you are seeing flickering, something would definitely seem to be awry. If you are varying the transformation (e.g. rotating the cube), try using a constant transformation and see if the flickering goes away. Some other things to try:
- Set the pixel shader return a constant color value, to eliminate the possibility that the problem is from varying output from the pixel shader.
- Disable depth buffer testing by setting the ZBufferEnable renderstate to false.
- Set the fill mode to wireframe to see how the primitives are being rendered, to make sure the geometry is intact after vertex processing (in conjunction with constant pixel shader output).
- Clear the back buffer to a color other than black or white to insure that "missing" portions of the cube are not simply the same color as background.
- Disable the pixel shader and use fixed pipe for pixel processing, while keeping the vertex shader, to isolate it's effect on rendering.
- Disable the vertex shader and use fixed pipe for vertex processing, while keeping the pixel shader, to isolate the vertex shader
| | Robert Dunlop |
|