.NET FoxGL Framework

FoxGL is a custom OpenGL framework, designed for .NET use and ObjectOriented Programming.

FoxGL is an intuitive abstraction of the ‘C’ OpenGL API, giving a powerful C# language typing.

FoxGL makes graphics programming easy, giving a lot of Shapes Functions, Controls, Properties and yet more!

How it started

FoxGL is my final graphics assignment project. I decided to create my own implementation of the OpenGL API for .NET. That’s how it appeared.

Student: Kevin Martin del Campo Fdz.
Teacher: Edmundo Ollervides
School: Instituto Tecnológico de la Laguna
Year: 2016
Generation: 2013
Country: México

NOTE: FoxGL is a STUDENT’S PROJECT. FoxGL will be updated and MAY not be finished or implement all OpenGL functions.

Example of use

My framework is destined to be Windows GUI (Forms and Controls) compatible in an easy way, making hybrid GL controls, compatible with Windows.Forms and FoxGL Functions/Properties.

Example:

*Creating a GLContext over a Window (Form)
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;
using FoxGL;

namespace GL_Test
{ 
     public MainForm()
     {
          InitializeComponent();
          GLControl control1 = new GLControl(this, GLControl.ViewType.ThreeDimensions);
     }
}

That’s it. You now have a OpenGL Window which has support for OpenGL and Windows.Forms in a 3D context (You can choose 2D in ViewType enum parameter)

How to draw with OpenGL

Now that we have our GLContext, named control1 from type GLControl we can call or create OpenGL Shapes.

Creating shapes:

Now, shapes are objects and we have a lot of shapes and object properties to play with.
This is the base class for primitive shapes:

public abstract class PrimitiveShapes<T>
{
 public T X { get; set; }
 public T Y { get; set; }
 public T Z { get; set; }
 public T AngleX { get; set; }
 public T AngleY { get; set; }
 public T AngleZ { get; set; }
 public ShapeScale Scale { get; set; }
 public Single LineWidth { get; set; }
 public List<Material> Materials { get; set; }

 public PrimitiveShapes()
 {
  dynamic Zero = 0;
  X = Y = Z = AngleX = AngleY = AngleZ = Zero;
  LineWidth = 1.0f;
  Scale = new ShapeScale() { X = 1, Y = 1, Z = 1 };
  Materials = new List<Material>();
 }

 public abstract void Draw(DrawMode Mode);
 public virtual void Draw()
 {
  OpenGL.Scale(Scale.X, Scale.Y, Scale.Z);
  foreach (Material m in Materials)
  m.DrawMaterial();
 }
}

Class specifications

///<Class Material>
/// This is the class that allows to interact with materials as Material list
///</Class Material>
public class Material
{
 public OpenGL.Faces Face { get; set; }
 public OpenGL.Lightning Light { get; set; }
 public System.Drawing.Color Color { get; set; }

 public Material(OpenGL.Faces Face, OpenGL.Lightning Light, System.Drawing.Color Color)
 {
  this.Face = Face;
  this.Light = Light;
  this.Color = Color;
 }

 public Material()
 {
  Face = OpenGL.Faces.FRONT;
  Light = OpenGL.Lightning.AMBIENT_AND_DIFFUSE;
  Color = System.Drawing.Color.White;
 }

 public void DrawMaterial()
 {
  OpenGL.SetMaterial(Face, Light, GLConverter.ConvertToRGBA(Color));
 }
}

 

//Basic structure for managing scaling of each shape object
public class ShapeScale
{
  public Double X { get; set; }
  public Double Y { get; set; }
  public Double Z { get; set; }
}
//Drawing modes:
//RotateTranslate: First rotate shape angles vector <AngleX,AngleY,AngleZ> in 
//coordinates vector <0,0,0> and then
//translate to coordinates vector <X,Y,Z>.
//TranslateRotate: First translate shape in coordinates vector <X,Y,Z> and then
//rotate coordinates vector <AngleX,AngleY,AngleZ>.
//Draw(): Just draws in origin 0, 0, 0 with default angles 0, 0, 0

public enum DrawMode
{
 RotateTranslate,
 TranslateRotate
};

Control properties

Each openGL control has it’s default control properties as Lightning, Normalization, Antialising (Simple), MotionBlur (By Buffer), Optimizations, CameraType, etc.
All of the properties are predefined but you can change them:

control1.Properties.Antialiasing = true;
control1.EnableMotionBlur = true;

control1.Properties.CameraType = OpenGL.CameraType.Perspective;
control1.Properties.ClearColor = Color.Black;
control1.EnableMouseInteraction = true;
control1.EnableKeyboardInteraction = true;
control1.Properties.Lightning = true;
control1.Properties.Normalization = true;
control1.Properties.EnableColorMaterialOptimization = false;
//If enabled, allows OpenGL to use Color mixed with lightning instead of Material
//but won't work always as expected
control1.Properties.ShadeModel = OpenGL.ShadeModes.SMOOTH;
control1.CameraPerspectiveView.zFar = 100000;

Creating shapes

Now we can create shapes in a very easy and abstract way:

 IAxis3D<Double> Axis = new IAxis3D<double>()
 {
 X1 = -50,
 X2 = 50,
 Y1 = -50,
 Y2 = 50,
 Z1 = -50,
 Z2 = 50,
 AxisColorX = Color.Red,
 AxisColorY = Color.Green,
 AxisColorZ = Color.Blue
 };

 GLShapes.Sphere<Double> Moon = new GLShapes.Sphere<double>(15, 90, 90, Color.DarkOrange, 2)
 {
 QuadricNormal = GLU.QuadricNormals.SMOOTH,
 QuadricStyle = GLU.QuadricDrawStyles.FILL,
 X = 0,
 Y = 15,
 Z = 0,
 };
 Moon.Materials.Add(new GLShapes.Material(OpenGL.Faces.FRONT, OpenGL.Lightning.AMBIENT_AND_DIFFUSE, Color.DarkOrange));
 Moon.Materials.Add(new GLShapes.Material(OpenGL.Faces.FRONT, OpenGL.Lightning.EMISSION, Color.DarkGoldenrod));

 GLShapes.Cone<Double> Lamp = new GLShapes.Cone<double>(5, 10, 30, 30, Color.Yellow, 1)
 {
 X = 50,
 Y = 50,
 Z = -50,
 AngleX = 220,
 AngleY = 40,
 IsCovered = false,
 QuadricStyle = GLU.QuadricDrawStyles.FILL,
 QuadricNormal = GLU.QuadricNormals.SMOOTH
 };
 Lamp.Materials.Add(new GLShapes.Material(OpenGL.Faces.FRONT, OpenGL.Lightning.AMBIENT_AND_DIFFUSE, Color.AliceBlue));
 Lamp.Materials.Add(new GLShapes.Material(OpenGL.Faces.FRONT, OpenGL.Lightning.SHININESS, Color.Yellow));

 GLShapes.LightSource light = new GLShapes.LightSource(Color.Black, Color.DarkGoldenrod, Color.Black)
 {
 LightModel = OpenGL.LightModel.LIGHT_MODEL_TWO_SIDE,
 ModelValue = true,
 LightType = OpenGL.LightType.LIGHT0,
 LightPosition = new GLShapes.LightPoint() { X = 49, Y = 49, Z = -49, W = 1.0f },
 IsSpotLightning = true,
 Spot = new GLShapes.LightSpot() { CutoffAngle = 180, X = 0, Y = 0, Z = -0, Exponent = 1f }
 
 };

 GLShapes.Sphere<Double> Foco = new GLShapes.Sphere<double>(2, 30, 30, Color.Wheat, 1)
 {
 X = 51,
 Y = 51,
 Z = -51,
 QuadricNormal = GLU.QuadricNormals.SMOOTH,
 QuadricStyle = GLU.QuadricDrawStyles.FILL
 };
 Foco.Materials.Add(new GLShapes.Material(OpenGL.Faces.FRONT_AND_BACK, OpenGL.Lightning.AMBIENT_AND_DIFFUSE, Color.DarkGoldenrod));
 Foco.Materials.Add(new GLShapes.Material(OpenGL.Faces.FRONT_AND_BACK, OpenGL.Lightning.SPOT_DIRECTION, Color.DarkGoldenrod));
 Foco.Materials.Add(new GLShapes.Material(OpenGL.Faces.FRONT, OpenGL.Lightning.SHININESS, Color.Orange));

Drawing

To draw, we just need to call SomeShape.Draw() or SomeShape.Draw(DrawMode) to draw:

control1.OnPaint += () =>
{
 OpenGL.Disable(OpenGL.Caps.LIGHTING);
 OpenGL.Enable3DGradientBackground(control1.ZoomedFarGradient, Color.Black, Color.White);
 Shapes<Double>.ThreeDimensions.Draw3DAxis(Axis, GLShapes.AxisType.OnlyPositives, 3f, 0.5);
 Shapes<Double>.ThreeDimensions.Draw3DMesh(150, 5, Color.Wheat, 1f, 0.5);
 OpenGL.Enable(OpenGL.Caps.LIGHTING);

 Moon.Draw(GLShapes.DrawMode.RotateTranslate);
 OpenGL.MakeMatrix(() =>
 {
  OpenGL.Rotate(x += 0.1, false, true, false);
  Lamp.Draw(GLShapes.DrawMode.RotateTranslate);
  Foco.Draw(GLShapes.DrawMode.RotateTranslate);
  light.DrawLight(GLShapes.DrawMode.TranslateRotate);
 });
};

OpenGL.EnableDoubleBuffer = true;

Result:

result1

<< This page is not finished. Last update post: 06/01/2016 -> 23:55 GMT-5>>

DOWNLOAD DEMO PREVIEW << FoxGL 0.0.1  NOT STABLE >>

What’s new:

  • Support for 3D (2D is disabled in this demo version)
  • Support controls and Windows (Window just working in this demo
  • Support for lightning and materials as objects
A %d blogueros les gusta esto: