PDA

View Full Version : getting started with lighting



tombaugh
01-03-2011, 02:52 PM
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.


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!

carsten neumann
01-03-2011, 03:15 PM
GL.Enable(EnableCap.ColorMaterial);


this tells OpenGL to use the current (vertex) color for the diffuse and ambient material property (see glColorMaterial (http://www.opengl.org/sdk/docs/man/xhtml/glColorMaterial.xml)), so that your earlier call to glMaterial has no effect here.

tombaugh
01-04-2011, 05:05 PM
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

tombaugh
01-06-2011, 05:26 PM
Anyone?

mobeen
01-06-2011, 11:40 PM
Hi,
Explicitly give your normals as a floating point value like so


// 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

float[] mat_diffuse = {1.0f,0.0f,0.0f};

It gives me this output.
http://www3.ntu.edu.sg/home2007/mova0002/test/Test.png

tombaugh
01-07-2011, 12:39 PM
Thank you!