Jump to content
  • Advertisement
Sign in to follow this  
fguihen

why does this work without vertex normals or a light source( from tom millers book)

This topic is 5027 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

im using Tom Millers kick start in managed directX to learn about 3d programming. im having a big problem. in the first chapter we learn to set up a light source, and give each vertex a "normal". now, in chapter 3 , where vertex buffers come in, in his code examples the code to difine a light source is not there , and also, the normal for each vertex is gone. when i modify my code to look the same as his by removing the lighting code and the vertex normals, i have no light. just a black triangle moving round the window. why does he not have to difine a normal and light source in his example in chapter 3 on vertex buffers? here is the code. it may look long but it should be simple to all you experts out there :
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using Microsoft.DirectX;
using Microsoft.DirectX.Direct3D;

namespace Chapter3Code
{
	public class Form1 : System.Windows.Forms.Form
	{
        private Device device = null;
        private VertexBuffer vb = null;
private   System.ComponentModel.Container components = null;
        private float angle = 0.0f;

		public Form1()
		{
						InitializeComponent();

            this.Setstyle(Controlstyles.AllPaintingInWmPaint |  Controlstyles.Opaque, true);
		}

               public void InitializeGraphics()
        {
            // Set our presentation parameters
            PresentParameters presentParams = new PresentParameters();

            presentParams.Windowed = true;
            presentParams.SwapEffect = SwapEffect.Discard;

            // Create our device
            device = new Device(0, DeviceType.Hardware, this, CreateFlags.SoftwareVertexProcessing, presentParams);

            vb = new VertexBuffer(typeof(CustomVertex.PositionColored), 3, device, Usage.Dynamic | Usage.WriteOnly, CustomVertex.PositionColored.Format, Pool.Default);
            vb.Created += new EventHandler(this.OnVertexBufferCreate);
            OnVertexBufferCreate(vb, null);
        }

        private void OnVertexBufferCreate(object sender, EventArgs e)
        {
            VertexBuffer buffer = (VertexBuffer)sender;

            CustomVertex.PositionColored[] verts = new 				CustomVertex.PositionColored[3];
            verts[0].SetPosition(new Vector3(0.0f, 1.0f, 1.0f));
            verts[0].Color = System.Drawing.Color.Aqua.ToArgb();
            verts[1].SetPosition(new Vector3(-1.0f, -1.0f, 1.0f));
            verts[1].Color = System.Drawing.Color.Black.ToArgb();
            verts[2].SetPosition(new Vector3(1.0f, -1.0f, 1.0f));
            verts[2].Color = System.Drawing.Color.Purple.ToArgb();

            buffer.SetData(verts, 0, LockFlags.None);
        }

        private void SetupCamera()
        {
            device.RenderState.CullMode = Cull.None;
            device.Transform.World = Matrix.RotationAxis(new Vector3(angle / ((float)Math.PI * 2.0f), angle / ((float)Math.PI * 4.0f), angle / ((float)Math.PI * 6.0f)),  angle / (float)Math.PI);
            angle += 0.1f;

            device.Transform.Projection = Matrix.PerspectiveFovLH			((float)Math.PI / 4, this.Width / this.Height,1.0f,100.0f);
            
device.Transform.View = Matrix.LookAtLH(new Vector3(0,0,5.0f),  							new Vector3(), new Vector3(0,1,0));
            device.RenderState.Lighting = false;
        }

        protected override void OnPaint(System.Windows.Forms.PaintEventArgs e)
        {
            device.Clear(ClearFlags.Target, 							System.Drawing.Color.CornflowerBlue, 1.0f, 0);

            SetupCamera();

            device.BeginScene();
            device.VertexFormat = CustomVertex.PositionColored.Format;
            device.SetStreamSource(0, vb, 0);
            device.DrawPrimitives(PrimitiveType.TriangleList, 0, 1);
            device.EndScene();

            device.Present();

            this.Invalidate();
        }
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if (components != null) 
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Windows Form Designer generated code
		private void InitializeComponent()
		{
		this.components = new System.ComponentModel.Contains();
			this.Size = new System.Drawing.Size(300,300);
			this.Text = "Form1";
		}
		#endregion>
        static void Main() 
        {
            using (Form1 frm = new Form1())
            {
                frm.Show();
                frm.InitializeGraphics();
                Application.Run(frm);
            }
        }
	}
}

< Mod > Try source tags next time! [Edited by - Etnu on October 9, 2004 4:04:34 PM]

Share this post


Link to post
Share on other sites
Advertisement
your code looks fine to me, actually it runs correctly on my card. what I suggest is add
device.RenderState.Lighting = false;
at the end of InitializeGraphics or change

device = new Device(0, DeviceType.Hardware, this, CreateFlags.SoftwareVertexProcessing, presentParams);
into

device = new Device(0, DeviceType.Reference, this, CreateFlags.SoftwareVertexProcessing, presentParams);
to see if make any difference.

Share this post


Link to post
Share on other sites
Are you sure setupcamera() is being called? (Doesn't look like it) because that's the only place you call Device.Lighting = false.

If it isn't called, the adapter defaults will be used, and that varies from card to card.

If the lighting is enabled and there are no lights, then everything will be black. Methinks this is your problem. You should disable lighting when you first create the device, IMO. Makes life that much easier [wink]

EDIT: someone beat me too it >_<

Share this post


Link to post
Share on other sites
Lighting is disabled, so it's using the specified vertex colors. This is done with the following line:

pDev->SetRenderState(D3DRS_LIGHTING, false);
or
device.RenderState.Lighting = false;

edit: Damn two people beat me to it.

Share this post


Link to post
Share on other sites
but how come the code doesnt have to declare the type of lite, difuse rate, position etc? does that line "device.RenderState.Lighting = false" set up some sort of default light??, therefore a normal isnt needed to bring the vertices , for lack of a better way of saying it, onto the same plane as the light?hope you understand what im saying. i just want to know if what i think i understand here is correct

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!