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!