From: Tim R. <ti...@us...> - 2004-08-02 08:31:57
|
Update of /cvsroot/csdopenglnet/tutorials/gtk In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv25834/gtk Added Files: Framework.cs Makefile lesson01.cs lesson02.cs Log Message: gtk widget --- NEW FILE: lesson01.cs --- using csDragons.OpenGL; using GLib; using GdkSharp; using Gtk; using GtkSharp; using System; using System.Diagnostics; public class Lesson01 : Framework { public Lesson01() : base( "Lesson 01" ) {} public static void Main( string[] args) { Debug.Listeners.Add( new TextWriterTraceListener( Console.Out ) ); Debug.AutoFlush = true; Lesson01 gl = new Lesson01(); } } --- NEW FILE: Framework.cs --- using csDragons.OpenGL; using GLib; using GdkSharp; using Gtk; using GtkSharp; using System; using System.Diagnostics; public class Framework : csdGL { protected Gtk.Window window; protected GtkGLArea glarea; protected Entry rotX_entry; protected Entry rotY_entry; protected Entry rotZ_entry; protected Entry speedX_entry; protected Entry speedY_entry; protected Entry speedZ_entry; protected Button reset_btn; protected ToggleButton animate_tglbtn; protected Button update_btn; protected float propRotX, propRotY, propRotZ; protected float propSpeedX, propSpeedY, propSpeedZ; protected bool propAnimate; protected bool stop = true; protected double mouseX = 0.0; protected double mouseY = 0.0; public Framework( string title ) : this() { window.Title = title; } public Framework() { Debug.Indent(); Debug.WriteLine( "Entering GtkAdvaned()" ); Debug.WriteLine( "Init Application" ); Application.Init(); Debug.WriteLine( "Init Window" ); window = new Gtk.Window( "Gtk Frame" ); window.ReallocateRedraws = true; window.DeleteEvent += new DeleteEventHandler( OnDeleteEvent ); window.Resize( 640, 480 ); Debug.WriteLine( "Init GtkGLArea" ); glarea = new GtkGLArea(); glarea.Events = Gdk.EventMask.PointerMotionMask | Gdk.EventMask.ButtonPressMask | Gdk.EventMask.ButtonReleaseMask; glarea.Realized += new EventHandler( OnRealized ); glarea.ConfigureEvent += new ConfigureEventHandler( OnConfigure ); glarea.ExposeEvent += new ExposeEventHandler( OnExpose ); glarea.MapEvent += new MapEventHandler( OnMap ); glarea.UnmapEvent += new UnmapEventHandler( OnUnmap ); glarea.MotionNotifyEvent += new MotionNotifyEventHandler( OnMotionNotify ); glarea.ButtonReleaseEvent += new ButtonReleaseEventHandler( OnButtonRelease ); glarea.ButtonPressEvent += new ButtonPressEventHandler( OnButtonPress ); Debug.WriteLine( "Init rotation entry fields" ); rotX_entry = new Entry(); rotX_entry.ActivatesDefault = true; rotY_entry = new Entry(); rotY_entry.ActivatesDefault = true; rotZ_entry = new Entry(); rotZ_entry.ActivatesDefault = true; Debug.WriteLine( "Init speed entry fields" ); speedX_entry = new Entry(); speedX_entry.ActivatesDefault = true; speedY_entry = new Entry(); speedY_entry.ActivatesDefault = true; speedZ_entry = new Entry(); speedZ_entry.ActivatesDefault = true; Debug.WriteLine( "Init Table-Layout rotTable" ); Table rotTable = new Table( 2, 3, false ); rotTable.Attach( new Label( "x-rotation: " ), 0, 1, 0, 1 ); rotTable.Attach( rotX_entry, 1, 2, 0, 1 ); rotTable.Attach( new Label( "y-rotation: " ), 0, 1, 1, 2 ); rotTable.Attach( rotY_entry, 1, 2, 1, 2 ); rotTable.Attach( new Label( "z-rotation: " ), 0, 1, 2, 3 ); rotTable.Attach( rotZ_entry, 1, 2, 2, 3 ); Debug.WriteLine( "Init Table-Layout speedTable" ); Table speedTable = new Table( 2, 3, false ); speedTable.Attach( new Label( "x-speed: " ), 0, 1, 0, 1 ); speedTable.Attach( speedX_entry, 1, 2, 0, 1 ); speedTable.Attach( new Label( "y-speed: " ), 0, 1, 1, 2 ); speedTable.Attach( speedY_entry, 1, 2, 1, 2 ); speedTable.Attach( new Label( "z-speed: " ), 0, 1, 2, 3 ); speedTable.Attach( speedZ_entry, 1, 2, 2, 3 ); Debug.WriteLine( "Init buttons" ); animate_tglbtn = new ToggleButton( "Animate" ); animate_tglbtn.Toggled += new EventHandler( OnButton ); reset_btn = new Button( "Reset" ); reset_btn.Clicked += new EventHandler( OnButton ); update_btn = new Button( "Update" ); update_btn.Clicked += new EventHandler( OnButton ); update_btn.CanFocus = true; update_btn.CanDefault = true; window.Default = update_btn; Debug.WriteLine( "Init Table-Layout buttonTable" ); Table buttonTable = new Table( 3, 1, true ); buttonTable.Attach( animate_tglbtn, 0, 1, 0, 1 ); buttonTable.Attach( reset_btn, 0, 1, 1, 2 ); buttonTable.Attach( update_btn, 0, 1, 2, 3 ); Debug.WriteLine( "Init HBox-Layout hbox" ); HBox hbox = new HBox( false, 4 ); hbox.PackStart( rotTable, false, true, 5 ); hbox.PackStart( speedTable, false, true, 5 ); hbox.PackEnd( buttonTable, false, true, 5 ); Debug.WriteLine( "Init VBox-Layout vbox" ); VBox vbox = new VBox( false, 2 ); vbox.PackStart( glarea, true, true, 5 ); vbox.PackEnd( hbox, false, true, 5 ); Debug.WriteLine( "Display Window" ); window.Add( vbox ); window.ShowAll(); setDefaults(); Debug.WriteLine( "MainLoop" ); Application.Run (); Debug.WriteLine( "Exiting GtkAdvaned()" ); Debug.Unindent(); } protected void updateValues() { Debug.Indent(); Debug.WriteLine( "Entering GtkAdvanced.updateValues()" ); bool p = animate; animate = false; Debug.WriteLine( "Setting speed" ); try { speedX = System.Convert.ToSingle( speedX_entry.Text ); } catch { speedX_entry.Text = speedX.ToString(); } try { speedY = System.Convert.ToSingle( speedY_entry.Text ); } catch { speedY_entry.Text = speedY.ToString(); } try { speedZ = System.Convert.ToSingle( speedZ_entry.Text ); } catch { speedZ_entry.Text = speedZ.ToString(); } Debug.WriteLine( "Setting angle" ); try { rotX = System.Convert.ToSingle( rotX_entry.Text ); } catch { rotX_entry.Text = rotX.ToString(); } try { rotY = System.Convert.ToSingle( rotY_entry.Text ); } catch { rotY_entry.Text = rotY.ToString(); } try { rotZ = System.Convert.ToSingle( rotZ_entry.Text ); } catch { rotZ_entry.Text = rotZ.ToString(); } animate = p; Debug.WriteLine( "Exiting GtkAdvanced.updateValues()" ); Debug.Unindent(); } protected void setDefaults() { Debug.Indent(); Debug.WriteLine( "Entering GtkAdvanced.setDefaults()" ); animate = false; rotX = 0.0f; rotY = 0.0f; rotZ = 0.0f; speedX = 0.1f; speedY = 0.2f; speedZ = 0.5f; Debug.WriteLine( "Exiting GtkAdvanced.setDefaults()" ); Debug.Unindent(); } protected bool Animate () { Debug.Indent(); Debug.WriteLine( "Entering GtkAdvanced.Animate()" ); rotX += speedX; rotY += speedY; rotZ += speedZ; if ( (glarea!=null) && (!stop) ) glarea.QueueDraw (); Debug.WriteLine( "Entering GtkAdvanced.Animate() *" ); Debug.Unindent(); return animate; } protected void OnMap (object obj, MapEventArgs args) { Debug.Indent(); Debug.WriteLine( "Enering GtkAdvanced.OnMap( object, MapEventArgs )" ); stop = false; Debug.WriteLine( "Exiting GtkAdvanced.OnMap( object, MapEventArgs )" ); Debug.Unindent(); } protected void OnUnmap (object obj, UnmapEventArgs args) { Debug.Indent(); Debug.WriteLine( "Enering GtkAdvanced.OnUnmap( object, UnapEventArgs )" ); stop = true; Debug.WriteLine( "Exiting GtkAdvanced.OnUnmap( object, UnapEventArgs )" ); Debug.Unindent(); } protected virtual void OnExpose (object obj, ExposeEventArgs args) { Debug.Indent(); Debug.WriteLine( "Entering GtkAdvanced.OnExpose( object, ExposeEventArgs )" ); if (glarea.MakeCurrent()) { glClearColor( 0, 0, 0, 0 ); glClear ( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT ); glPushMatrix(); glRotatef( rotX, 1.0f, 0.0f, 0.0f ); glRotatef( rotY, 0.0f, 1.0f, 0.0f ); glRotatef( rotZ, 0.0f, 0.0f, 1.0f ); draw(); glPopMatrix(); glarea.SwapBuffers(); } else Debug.WriteLine( "MakeCurrent() failed" ); Debug.WriteLine( "Exiting GtkAdvanced.OnExpose( object, ExposeEventArgs )" ); Debug.Unindent(); } protected void OnMotionNotify( object obj, MotionNotifyEventArgs args) { Debug.Indent(); Debug.WriteLine( "Entering GtkAdvanced.OnMotionNotify( object, MotionNotifyEventArgs )" ); if ( (args.Event.State==Gdk.ModifierType.Button1Mask) || (args.Event.State==Gdk.ModifierType.Button3Mask) ) { int w = 0; int h = 0; glarea.GdkWindow.GetSize( out w, out h ); if ( args.Event.State==Gdk.ModifierType.Button1Mask ) { if ( mouseX>=0.0 ) { Debug.WriteLine( "y-rotation" ); rotY += ((float)(args.Event.X-mouseX)) * 360.0f / w; } else mouseX = args.Event.X; if ( mouseY>=0.0 ) { Debug.WriteLine( "x-rotation" ); rotX += ((float)(args.Event.Y-mouseY)) * 360.0f / h; mouseY = args.Event.Y; } else mouseY = args.Event.Y; } if ( args.Event.State==Gdk.ModifierType.Button3Mask ) { if ( mouseX>=0.0 ) { Debug.WriteLine( "y-rotation" ); rotZ += ((float)(args.Event.X-mouseX)) * 360.0f / w; } else mouseX = args.Event.X; } mouseX = args.Event.X; } Debug.WriteLine( "Exiting GtkAdvanced.OnMotionNotify( object, MotionNotifyEventArgs )" ); Debug.Unindent(); } protected void OnButtonPress( object obj, ButtonPressEventArgs args ) { Debug.Indent(); Debug.WriteLine( "Entering GtkAdvanced.OnButtonPress( object, ButtonPressEventArgs )" ); mouseX = args.Event.X; mouseY = args.Event.Y; Debug.WriteLine( "Exiting GtkAdvanced.OnButtonPress( object, ButtonPressEventArgs )" ); Debug.Unindent(); } protected void OnButtonRelease( object obj, ButtonReleaseEventArgs args ) { Debug.Indent(); Debug.WriteLine( "Entering GtkAdvanced.OnButtonRelease( object, ButtonReleaseEventArgs )" ); mouseX = -1.0; mouseY = -1.0; Debug.WriteLine( "Exiting GtkAdvanced.OnButtonRelease( object, ButtonReleaseEventArgs )" ); Debug.Unindent(); } protected virtual void draw() { Debug.Indent(); Debug.WriteLine( "Entering GtkAdvanced.draw()" ); Debug.WriteLine( "Exiting GtkAdvanced.draw()" ); Debug.Unindent(); } protected virtual void OnConfigure (object obj, ConfigureEventArgs args) { Debug.Indent(); Debug.WriteLine( "Entering GtkAdvanced.OnConfigure( object, ConfigureEventArgs )" ); if (glarea.MakeCurrent()) { glViewport(0, 0, glarea.Allocation.Width, glarea.Allocation.Height); glMatrixMode( GL_PROJECTION ); glLoadIdentity(); double h = (double) glarea.Allocation.Height / (double) (glarea.Allocation.Width); glFrustum( -1.0, 1.0, -h, h, 5.0f, 5.0 ); Console.WriteLine( glGetError() ); glMatrixMode( GL_MODELVIEW ); glLoadIdentity(); } else Debug.WriteLine( "MakeCurrent() failed" ); Debug.WriteLine( "Exiting GtkAdvanced.OnConfigure( object, ConfigureEventArgs )" ); Debug.Unindent(); } protected void OnRealized (object obj, EventArgs args) { Debug.Indent(); Debug.WriteLine( "Entering GtkAdvanced.OnRealized( object, EventArgs )" ); if (glarea.MakeCurrent()) { glShadeModel( GL_SMOOTH ); glClearColor( 0.0f, 0.0f, 0.0f, 0.5f ); glClearDepth( 1.0f ); //glEnable( GL_DEPTH_TEST ); DepthTest = true; glDepthFunc( GL_LEQUAL ); glHint( GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST ); } else Debug.WriteLine( "MakeCurrent() failed" ); Debug.WriteLine( "Exiting GtkAdvanced.OnRealized( object, EventArgs )" ); Debug.Unindent(); } protected void OnButton( object obj, EventArgs args ) { Debug.Indent(); Debug.WriteLine( "Entering GtkAdvanced.OnButton( objec, EventArgs )" ); if (obj==reset_btn) { setDefaults(); } else if (obj==animate_tglbtn) { animate = animate_tglbtn.Active; } else if (obj==update_btn) { updateValues(); } Debug.WriteLine( "Exiting GtkAdvanced.OnButton( objec, EventArgs )" ); Debug.Unindent(); } protected void OnDeleteEvent (object obj, DeleteEventArgs args) { Debug.Indent(); Debug.WriteLine( "Entering GtkAdvanced.OnDeleteEvent( object, DeleteEventArgs )" ); Application.Quit (); Debug.WriteLine( "Exiting GtkAdvanced.OnDeleteEvent( object, DeleteEventArgs )" ); Debug.Unindent(); } protected float rotX { get { Debug.WriteLine( "rotX->get" ); return propRotX; } set { Debug.WriteLine( "rotX->set" ); propRotX = value; while (propRotX<0) propRotX += 360.0f; while (propRotX>=360.0f) propRotX -= 360.0f; rotX_entry.Text = propRotX.ToString(); if ( (glarea!=null) && (!stop) ) glarea.QueueDraw (); } } protected float rotY { get { Debug.WriteLine( "rotY->get" ); return propRotY; } set { Debug.WriteLine( "rotY->set" ); propRotY = value; while (propRotY<0) propRotY += 360.0f; while (propRotY>=360.0f) propRotY -= 360.0f; rotY_entry.Text = propRotY.ToString(); if ( (glarea!=null) && (!stop) ) glarea.QueueDraw (); } } protected float rotZ { get { Debug.WriteLine( "rotZ->get" ); return propRotZ; } set { Debug.WriteLine( "rotZ->set" ); propRotZ = value; while (propRotZ<0) propRotZ += 360.0f; while (propRotZ>=360.0f) propRotZ -= 360.0f; rotZ_entry.Text = propRotZ.ToString(); if ( (glarea!=null) && (!stop) ) glarea.QueueDraw (); } } protected float speedX { get { Debug.WriteLine( "speedX->get" ); return propSpeedX; } set { Debug.WriteLine( "speedX->set" ); propSpeedX = value; while (propSpeedX<=-360.0f) propSpeedX += 360.0f; while (propSpeedX>=360.0f) propSpeedX -= 360.0f; speedX_entry.Text = propSpeedX.ToString(); } } protected float speedY { get { Debug.WriteLine( "speedY->get" ); return propSpeedY; } set { Debug.WriteLine( "speedY->set" ); propSpeedY = value; while (propSpeedY<=-360.0f) propSpeedY += 360.0f; while (propSpeedY>=360.0f) propSpeedY -= 360.0f; speedY_entry.Text = propSpeedY.ToString(); } } protected float speedZ { get { Debug.WriteLine( "speedZ->get" ); return propSpeedZ; } set { Debug.WriteLine( "speedZ->set" ); propSpeedZ = value; while (propSpeedZ<=-360.0f) propSpeedZ += 360.0f; while (propSpeedZ>=360.0f) propSpeedZ -= 360.0f; speedZ_entry.Text = propSpeedZ.ToString(); } } protected bool animate { get { Debug.WriteLine( "animate->get" ); return propAnimate; } set { Debug.WriteLine( "animate->set" ); if ( value && !propAnimate ) GLib.Idle.Add (new IdleHandler (Animate)); propAnimate = value; animate_tglbtn.Active = value; } } } --- NEW FILE: Makefile --- CC=mcs DEBUG=/d:DEBUG DEBUG= OPTS=$(DEBUG) LIBS=csdGL_Gtk.dll csdMath.dll csdGLtools.dll MCS=$(CC) $(OPTS) -lib:/usr/lib/mono/gtk-sharp -lib:/usr/lib/mono/csDragons PACKAGE=csDragons LIBOPTS=$(foreach lib,$(LIBS),-r $(lib)) all: lesson01.exe lesson02.exe %.exe:%.cs Framework.cs $(MCS) -r:glib-sharp.dll -r:gdk-sharp.dll -r:gtk-sharp.dll -r:csdGL_Gtk.dll $^ -o $@ clean: rm -f *.exe .IGNORE: clean .EXPORT_ALL_VARIABLES: .PHONY: clean --- NEW FILE: lesson02.cs --- using csDragons.OpenGL; using System; using System.Diagnostics; public class Lesson02 : Framework { public Lesson02() : base( "Lesson 02" ) {} protected override void draw() { if (glarea.MakeCurrent()) { glClear ( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT ); glPushMatrix(); glLoadIdentity(); glTranslatef(-1.5f,0.0f,-6.0f); glBegin(GL_TRIANGLES); glVertex3f( 0.0f, 1.0f, 0.0f); glVertex3f(-1.0f,-1.0f, 0.0f); glVertex3f( 1.0f,-1.0f, 0.0f); glEnd(); glTranslatef( 3.0f, 0.0f, 0.0f); glBegin(GL_QUADS); glVertex3f(-1.0f, 1.0f, 0.0f); glVertex3f( 1.0f, 1.0f, 0.0f); glVertex3f( 1.0f,-1.0f, 0.0f); glVertex3f(-1.0f,-1.0f, 0.0f); glEnd(); glPopMatrix(); } else Debug.WriteLine( "MakeCurrent() failed!" ); } public static void Main( string[] args) { Debug.Listeners.Add( new TextWriterTraceListener( Console.Out ) ); Debug.AutoFlush = true; Lesson02 gl = new Lesson02(); } } |