Sign in to follow this  
Nemesis_001

[MDX] Textured quad won't light up. Update: Solved

Recommended Posts

Update: Problem solved. Microsoft apparently changed the type of Material.Diffuse in MDX 2.0, and it now takes a Microsoft.DirectX.ColorValue type instead of System.Drawing.Color type. Ofcourse, no errors during compile, so you have to guess. Grrr. Hello, I am trying to render a simple textured quad, and having some difficulties with it. When lighting is off, all is well. Turning the lighting off and the whole thing goes black. Also, rendering the quad without a texture, using PositionNormalColored vertices instead of PositionNormalTextured works fine. Lighting turned on ofcourse. Could someone please point to what am I doing wrong? Any help would be appreciated. Source code below (Sorry for the length): Setting up:
        private void OnResetDevice(object sender, EventArgs e)
        {
            //Setup camera

            Size displaySize = DisplaySize;
            float aspect = (float)displaySize.Width / (float)displaySize.Height;

            _graphics.Device.Transform.Projection = Matrix.PerspectiveFieldOfViewLeftHanded((float)Math.PI / 4.0f, aspect, 0.1f, 100.0f);
            _graphics.Device.Transform.View = Matrix.LookAtLeftHanded(new Vector3(0.0f, 0.0f, 5.0f), new Vector3(), new Vector3(0.0f, 1.0f, 0.0f));

            //Lights

            _graphics.Device.RenderState.Lighting = true;
            _graphics.Device.RenderState.FillMode = FillMode;
            _graphics.Device.Lights[0].LightType = LightType.Directional;
            _graphics.Device.Lights[0].Position = new Vector3(0, 0, 10);
            _graphics.Device.Lights[0].Diffuse = Color.White;
            _graphics.Device.Lights[0].Direction = new Vector3(0, 0, -1);
            _graphics.Device.Lights[0].Enabled = true;

            verts = new GraphicsBuffer<PositionNormalTextured>(6);
            verts.Write(new PositionNormalTextured(-1.0f, -1.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f));
            verts.Write(new PositionNormalTextured(1.0f, -1.0f, 0.0f, 0.0f, 0.0f, 1.0f, 1.0f, 0.0f));
            verts.Write(new PositionNormalTextured(1.0f, 1.0f, 0.0f, 0.0f, 0.0f, 1.0f, 1.0f, 1.0f));
            verts.Write(new PositionNormalTextured(1.0f, 1.0f, 0.0f, 0.0f, 0.0f, 1.0f, 1.0f, 1.0f));
            verts.Write(new PositionNormalTextured(-1.0f, 1.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 1.0f));
            verts.Write(new PositionNormalTextured(-1.0f, -1.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f));
            

            rts = new RenderToSurface(_graphics.Device,renderSurfaceSize, renderSurfaceSize,_graphics.CurrentSettings.AdapterFormat, true, DepthFormat.D16);
            renderTexture = new Texture(_graphics.Device, renderSurfaceSize, renderSurfaceSize, 1, Usage.RenderTarget, _graphics.CurrentSettings.AdapterFormat, Microsoft.DirectX.Direct3D.Pool.Default);
            renderSurface = renderTexture.GetSurfaceLevel(0);

            System.Drawing.Font localFont = new System.Drawing.Font("Arial", 24.0f);
            font = new Microsoft.DirectX.Direct3D.Font(_graphics.Device, localFont);

            material = new Material();
            material.Diffuse = Color.White;
            material.Ambient = Color.White;
            material.Specular = Color.White;
            material.Emissive = Color.White;

            RenderIntoSurface();


        }




Pre-renderingi the texture:
        private void RenderIntoSurface()
        {
            Viewport view = new Viewport();
            view.Width = renderSurfaceSize;
            view.Height = renderSurfaceSize;
            view.MaxZ = 1.0f;

            rts.BeginScene(renderSurface, view);
            _graphics.Device.Clear(ClearFlags.Target | ClearFlags.ZBuffer, Color.Blue, 1.0f, 0);
            rts.Device.Material = material;

            font.DrawString(null, "Blah", new Rectangle(0, 0, 256, 256), DrawStringFormat.Center | DrawStringFormat.VerticalCenter, Color.Green);

            rts.EndScene(Filter.None);
}


And finally the render loop:
        private void OnRenderFrame()
        {
            _graphics.Device.Clear(ClearFlags.Target | ClearFlags.ZBuffer, Color.Black, 1.0f, 0);
            _graphics.Device.BeginScene();

            _graphics.Device.SetRenderState(RenderStates.ZEnable, true);
            _graphics.Device.SetRenderState(RenderStates.ZBufferWriteEnable, true);
            _graphics.Device.SetRenderState(RenderStates.AlphaBlendEnable, false);
            _graphics.Device.RenderState.CullMode = Cull.None;
            _graphics.Device.RenderState.Ambient = Color.White;

          
            _graphics.Device.Material = material;
            _graphics.Device.VertexFormat = PositionNormalTextured.Format;
            _graphics.Device.SetTexture(0, renderTexture);
            _graphics.Device.DrawUserPrimitives(PrimitiveType.TriangleList, 2, verts);

            _graphics.Device.EndScene();
            _graphics.Device.Present();
        }




[Edited by - Nemesis_001 on May 1, 2006 7:03:42 AM]

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this