-
Notifications
You must be signed in to change notification settings - Fork 148
Opengl 3D Samples
rollynoel edited this page Jun 13, 2013
·
2 revisions
Added by dholton dholton
These samples use the Tao Framework for OpenGL on .NET and Mono. Tao is constantly changing (and moving), so these samples may be out of date.
//cube.boo
//when compiling with booc, add references to
//Tao.OpenGl.dll and Tao.FreeGlut.dll
//You may need to find a build of the freeglut.dll. I don't
//know where such a build is now.
import Tao.FreeGlut
import Tao.OpenGl
def Init():
Gl.glClearColor(0.0, 0.0, 0.0, 0.0) //floats
Gl.glShadeModel(Gl.GL_FLAT)
def Display():
Gl.glClear(Gl.GL_COLOR_BUFFER_BIT)
Gl.glColor3f(1.0, 1.0, 1.0) //floats
// Clear the matrix
Gl.glLoadIdentity()
// Viewing transformation
Glu.gluLookAt(0.0, 0.0, 5.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0)
// Modeling transformation
Gl.glScalef(1.0, 2.0, 1.0) //floats
Glut.glutWireCube(1.0)
Gl.glFlush()
def Keyboard(key as byte, x as int, y as int):
if key == 27: //escape key
System.Environment.Exit(0)
def Reshape(w as int, h as int):
Gl.glViewport(0, 0, w, h)
Gl.glMatrixMode(Gl.GL_PROJECTION)
Gl.glLoadIdentity()
Gl.glFrustum(-1.0, 1.0, -1.0, 1.0, 1.5, 20.0)
Gl.glMatrixMode(Gl.GL_MODELVIEW)
Glut.glutInit()
Glut.glutInitDisplayMode(Glut.GLUT_SINGLE | Glut.GLUT_RGB)
Glut.glutInitWindowSize(500, 500)
Glut.glutInitWindowPosition(100, 100)
Glut.glutCreateWindow("Cube")
Init()
Glut.glutDisplayFunc(Display)
Glut.glutKeyboardFunc(Keyboard)
Glut.glutReshapeFunc(Reshape)
Glut.glutMainLoop()
This is a port of the base code sample from the NeHe OpenGL tutorials site. It uses the Tao Framework's SimpleOpenGlControl.
/*
nehe.boo
Requires boo: http://boo.codehaus.org/
and the Tao OpenGl Framework: http://www.mono-project.com/Tao
Compile using the booc compile line tool:
booc.exe -r:Tao.OpenGl.dll -r:Tao.OpenGl.Glu.dll -r:Tao.Platform.Windows.dll -out:nehe.exe NeHe.boo
*/
namespace NeheTest
import System
import System.Windows.Forms
import System.Drawing
import System.ComponentModel
import Tao.OpenGl
import Tao.Platform.Windows
private class MainForm(Form):
private components as Container = null
private glControl = SimpleOpenGlControl()
def constructor():
InitializeComponent()
def InitializeComponent():
SuspendLayout()
glControl.Location = Point(0, 0)
glControl.Dock = DockStyle.Fill
glControl.Visible = true
glControl.KeyDown += HandleKeyDown
Controls.Add(glControl)
//the "self" is always optional:
self.AutoScaleBaseSize = System.Drawing.Size(5, 13)
self.ClientSize = System.Drawing.Size(292, 273)
self.Name = 'MainForm'
self.Text = 'NeHe Boo Example'
ResumeLayout(false)
protected override def OnLoad(e as EventArgs):
super(e)
InitGL()
protected override def OnResize(e as EventArgs):
super(e)
ResizeGL(glControl.Width, glControl.Height)
def Run(): //main loop
while Created:
Invalidate(true)
DrawGL()
Application.DoEvents()
protected override def Dispose(disposing as bool):
if disposing:
if components != null:
components.Dispose()
super(disposing)
private def HandleKeyDown(sender, e as KeyEventArgs):
if e.KeyCode == Keys.Escape: //char type will be in boo soon
Close()
private def InitGL():
glControl.InitializeContexts()
OnResize(null)
private def ResizeGL(w as int, h as int):
Gl.glViewport( 0, 0, w, h)
Gl.glMatrixMode ( Gl.GL_PROJECTION )
Gl.glLoadIdentity()
Glu.gluPerspective( 60.0, cast(double,w) / h, 1.0,1000.0)
Gl.glMatrixMode ( Gl.GL_MODELVIEW )
Gl.glLoadIdentity()
///////////////////// Drawing code ///////////////////////////
private _lastMs = 0
private _angle = 0.0
private def DrawGL():
if _lastMs == 0:
_lastMs = DateTime.Now.Ticks
currentMs = DateTime.Now.Ticks
//int division will change from / to \ in future:
milliseconds as long = (currentMs - _lastMs) / 10000
_lastMs = currentMs
Gl.glClear(Gl.GL_COLOR_BUFFER_BIT | Gl.GL_DEPTH_BUFFER_BIT)
Gl.glLoadIdentity()
Gl.glTranslatef(0, 0, -6)
Gl.glRotatef(_angle, 0, 1, 0)
rot1 = 0
while rot1 < 2.0:
Gl.glRotatef(90, 0, 1, 0)
Gl.glRotatef(180, 1, 0, 0)
rot2 = 0
while rot2 < 2:
Gl.glRotatef(180, 0, 1, 0)
Gl.glBegin(Gl.GL_TRIANGLES)
Gl.glColor3f(1, 0, 0)
Gl.glVertex3f(0, 1, 0)
Gl.glColor3f(0, 1, 0)
Gl.glVertex3f(-1, -1, 1)
Gl.glColor3f(0, 0, 1)
Gl.glVertex3f(1, -1, 1)
Gl.glEnd()
rot2 += 1
rot1 += 1
Gl.glFlush()
_angle += milliseconds / 5.0
////////////// main part of script ////////////////////
res = MessageBox.Show(null, 'Would You Like To Run In Fullscreen Mode?',
'Start Fullscreen?',
MessageBoxButtons.YesNo,
MessageBoxIcon.Information )
form = MainForm()
if res == DialogResult.Yes:
form.FormBorderStyle = FormBorderStyle.None
form.Location = Point(0, 0)
form.Size = Screen.PrimaryScreen.Bounds.Size
form.Show()
form.Run()