Results 1 to 6 of 6

Thread: getting started with lighting

  1. #1
    Junior Member Newbie
    Join Date
    Jan 2011
    Posts
    4

    getting started with lighting

    Hi,

    I'm just getting started with OpenGL. I composed a piece of code assembled from various tutorials, hoping to end up with a simple cube which is lit from one side. Unfortunately all faces are flat gray, with no difference whatsoever between them. Can anyone point me in the right direction? The documentation and tutorials I found online are somewhat overwhelming.

    By the way, this is for a computer graphics course which is based on C# and Direct3D, but being a bit too fond of my Linux system I decided to give it a try with Mono and OpenTK.

    Code :
    using System;
    using System.Drawing;
    using System.Data;
    using System.Xml;
    using System.Windows.Forms;
    using OpenTK;
    using OpenTK.Graphics.OpenGL;
     
    namespace shape {
    	public class ShapeApp : Form {
     
    		private GLControl GLBox = new GLControl();
     
    		[STAThread]
    		static public void Main () {
    			Application.EnableVisualStyles();
    			Application.Run (new ShapeApp ());
    		}
     
    		public ShapeApp() {
    			this.Text = "OpenGL demo";
    			this.BackColor = Color.White;
    			this.Size = new Size(400,400);
    			GLBox.Parent = this;
    			GLBox.Dock = DockStyle.Fill;
    			GLBox.Paint += new PaintEventHandler(GLBoxPaint);
    			GLBox.Load += new EventHandler(GLBoxLoad);
    		}	
     
    		 private void GLBoxLoad(object sender, EventArgs e) {
    			GL.ClearColor(Color.Black);
    			SetupViewport();
    		}
     
    		private void SetupViewport() {
    			int w = GLBox.Width;
    			int h = GLBox.Height;
    			GL.MatrixMode(MatrixMode.Projection);
    			GL.LoadIdentity();
    			GL.Ortho(-2, 2, -2, 2, -1, 1);
    			GL.Viewport(0, 0, w, h);
    		}
     
    		private void GLBoxPaint(object sender, PaintEventArgs e) {
     
    			GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit);
    			GL.MatrixMode(MatrixMode.Modelview);
    			GL.LoadIdentity();
     
    			GL.Rotate(20, 1, 0, 0);
    			GL.Rotate(30, 0, 1, 0);
     
    			float[] mat_diffuse = {1.0f,1.0f,1.0f};
    			GL.Material(MaterialFace.FrontAndBack, MaterialParameter.AmbientAndDiffuse, mat_diffuse);
    			GL.Light(LightName.Light0, LightParameter.Position, new float[] { 1.0f, 1.0f, 0.0f, 1.0f });
    			GL.Light(LightName.Light0, LightParameter.Diffuse, new float[] { 1.0f, 1.0f, 1.0f, 1.0f });
     
    			GL.Enable(EnableCap.Lighting);
    			GL.Enable(EnableCap.Light0);
    			GL.Enable(EnableCap.ColorMaterial);
    			GL.Enable(EnableCap.DepthTest);
     
    			GL.Begin(BeginMode.Quads);
     
    			// front face
    			GL.Normal3(0, 0, 1);
    			GL.Vertex3(-0.5, -0.5, 0.5);
    			GL.Vertex3(0.5, -0.5, 0.5);
    			GL.Vertex3(0.5, 0.5, 0.5);
    			GL.Vertex3(-0.5, 0.5, 0.5);
     
    			// back face
    			GL.Normal3(0, 0, -1);
    			GL.Vertex3(-0.5, -0.5, -0.5);
    			GL.Vertex3(-0.5, 0.5, -0.5);
    			GL.Vertex3(0.5, 0.5, -0.5);
    			GL.Vertex3(0.5, -0.5, -0.5);
     
    			// top face
    			GL.Normal3(0, 1, 0);
    			GL.Vertex3(-0.5, 0.5, -0.5);
    			GL.Vertex3(-0.5, 0.5, 0.5);
    			GL.Vertex3(0.5, 0.5, 0.5);
    			GL.Vertex3(0.5, 0.5, -0.5);
     
    			// bottom face
    			GL.Normal3(0, -1, 0);
    			GL.Vertex3(-0.5, -0.5, -0.5);
    			GL.Vertex3(0.5, -0.5, -0.5);
    			GL.Vertex3(0.5, -0.5, 0.5);
    			GL.Vertex3(-0.5, -0.5, 0.5);
     
    			// right face
    			GL.Normal3(1, 0, 0);
    			GL.Vertex3(0.5, -0.5, -0.5);
    			GL.Vertex3(0.5, 0.5, -0.5);
    			GL.Vertex3(0.5, 0.5, 0.5);
    			GL.Vertex3(0.5, -0.5, 0.5);
     
    			// left face
    			GL.Normal3(-1, 0, 0);
    			GL.Vertex3(-0.5, -0.5, -0.5);
    			GL.Vertex3(-0.5, -0.5, 0.5);
    			GL.Vertex3(-0.5, 0.5, 0.5);
    			GL.Vertex3(-0.5, 0.5, -0.5);
     
    			GL.End();
     			GLBox.SwapBuffers();
    		}
    	}
    }

    Thanks!

  2. #2
    Newbie Frequent Contributor
    Join Date
    Mar 2016
    Posts
    896

    Re: getting started with lighting

    Code :
    GL.Enable(EnableCap.ColorMaterial);

    this tells OpenGL to use the current (vertex) color for the diffuse and ambient material property (see glColorMaterial), so that your earlier call to glMaterial has no effect here.

  3. #3
    Junior Member Newbie
    Join Date
    Jan 2011
    Posts
    4

    Re: getting started with lighting

    Carsten, should removing that line help? Because it doesn't seem to make a difference. If anyone is interested, the I zipped the project: http://bit.ly/gvZPUb

    Thanks

  4. #4
    Junior Member Newbie
    Join Date
    Jan 2011
    Posts
    4

    Re: getting started with lighting

    Anyone?

  5. #5
    Senior Member Frequent Contributor
    Join Date
    Mar 2009
    Location
    Karachi, Pakistan
    Posts
    810

    Re: getting started with lighting

    Hi,
    Explicitly give your normals as a floating point value like so
    Code :
    // front face
    GL.Normal3(0, 0, 1.0 ); 
    GL.Vertex3(-0.5, -0.5, 0.5);
    GL.Vertex3(0.5, -0.5, 0.5);
    GL.Vertex3(0.5, 0.5, 0.5);
    GL.Vertex3(-0.5, 0.5, 0.5);
     
    // back face
    GL.Normal3(0, 0, -1.0 ); 
    GL.Vertex3(-0.5, -0.5, -0.5);
    GL.Vertex3(-0.5, 0.5, -0.5);
    GL.Vertex3(0.5, 0.5, -0.5);
    GL.Vertex3(0.5, -0.5, -0.5);
     
    // top face
    GL.Normal3(0, 1.0 , 0); 
    GL.Vertex3(-0.5, 0.5, -0.5);
    GL.Vertex3(-0.5, 0.5, 0.5);
    GL.Vertex3(0.5, 0.5, 0.5);
    GL.Vertex3(0.5, 0.5, -0.5);
     
    // bottom face
    GL.Normal3(0, -1.0 , 0); 
    GL.Vertex3(-0.5, -0.5, -0.5);
    GL.Vertex3(0.5, -0.5, -0.5);
    GL.Vertex3(0.5, -0.5, 0.5);
    GL.Vertex3(-0.5, -0.5, 0.5);
     
    // right face
    GL.Normal3(1.0, 0, 0); 
    GL.Vertex3(0.5, -0.5, -0.5);
    GL.Vertex3(0.5, 0.5, -0.5);
    GL.Vertex3(0.5, 0.5, 0.5);
    GL.Vertex3(0.5, -0.5, 0.5);
     
    // left face
    GL.Normal3(-1.0 , 0, 0); 
    GL.Vertex3(-0.5, -0.5, -0.5);
    GL.Vertex3(-0.5, -0.5, 0.5);
    GL.Vertex3(-0.5, 0.5, 0.5);
    GL.Vertex3(-0.5, 0.5, -0.5);
    I changed my material to this
    Code :
    float[] mat_diffuse = {1.0f,0.0f,0.0f};
    It gives me this output.
    Regards,
    Mobeen

  6. #6
    Junior Member Newbie
    Join Date
    Jan 2011
    Posts
    4

    Re: getting started with lighting

    Thank you!

Similar Threads

  1. How do I get started
    By in forum OpenGL: macOS
    Replies: 1
    Last Post: 03-23-2003, 02:04 PM
  2. lighting back facing triangles _without_ using two sided lighting
    By Nicolas Lelong in forum OpenGL: Advanced Coding
    Replies: 5
    Last Post: 02-07-2003, 03:05 AM
  3. How should I get started?
    By in forum OpenGL: macOS
    Replies: 3
    Last Post: 10-29-2002, 05:40 AM
  4. Please help, just started
    By imported_sergey in forum OpenGL: Basic Coding
    Replies: 1
    Last Post: 10-14-2002, 01:49 AM
  5. About lighting (not OpenGL lighting, but general lighting)
    By Anonymous Coward in forum OpenGL: Basic Coding
    Replies: 2
    Last Post: 04-04-2001, 09:10 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Proudly hosted by Digital Ocean