Skip to content

Commit e1d75c5

Browse files
committed
OpenTK: upgrade to custom build (2.0.0),
086shader: support geometry functions (UnProject demo), Trackball: more general API (mouse button), some compile-fixes. git-svn-id: svn://cgg.mff.cuni.cz/grcis/trunk@449 c021de9b-3f10-4dc4-9d02-502951e1e0de
1 parent fcdd8ff commit e1d75c5

14 files changed

+449
-44
lines changed

038trackball/Trackball.cs

+9-4
Original file line numberDiff line numberDiff line change
@@ -94,10 +94,15 @@ public partial class Form1
9494
/// </summary>
9595
private float fov = 1.0f;
9696

97+
/// <summary>
98+
/// Camera's near point.
99+
/// </summary>
100+
private float near = 0.1f;
101+
97102
/// <summary>
98103
/// Camera's far point.
99104
/// </summary>
100-
private float far = 200.0f;
105+
private float far = 5.0f;
101106

102107
#endregion
103108

@@ -132,11 +137,11 @@ private void SetupViewport ()
132137
GL.Viewport( 0, 0, width, height );
133138

134139
// 2. set projection matrix
135-
perspectiveProjection = Matrix4.CreatePerspectiveFieldOfView( fov, (float)width / (float)height, 0.1f, far );
140+
perspectiveProjection = Matrix4.CreatePerspectiveFieldOfView( fov, width / (float)height, near, far );
136141
float minSize = 2.0f * Math.Min( width, height );
137-
ortographicProjection = Matrix4.CreateOrthographic( diameter * width / minSize,
142+
ortographicProjection = Matrix4.CreateOrthographic( diameter * width / minSize,
138143
diameter * height / minSize,
139-
0.1f, far );
144+
near, far );
140145
setProjection();
141146

142147
setEllipse();

039terrain/Support.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -103,8 +103,8 @@ private void SetCamera ()
103103
Matrix4 rotateX = Matrix4.CreateRotationX( (float)-elevationAngle );
104104
Matrix4 rotateY = Matrix4.CreateRotationY( (float)azimuthAngle );
105105

106-
cameraPosition = Vector3.Transform( cameraPosition, rotateX );
107-
cameraPosition = Vector3.Transform( cameraPosition, rotateY );
106+
cameraPosition = Vector3.TransformPosition( cameraPosition, rotateX );
107+
cameraPosition = Vector3.TransformPosition( cameraPosition, rotateY );
108108

109109
GL.MatrixMode( MatrixMode.Modelview );
110110
Matrix4 lookAt = Matrix4.LookAt( cameraPosition, Vector3.Zero, up );

056avatar/Avatar.cs

+4-4
Original file line numberDiff line numberDiff line change
@@ -179,17 +179,17 @@ private void SetDefault ()
179179
private void Elevator ( ref Vector3 dir, ref Vector3 up, float angle )
180180
{
181181
Vector3 left = Vector3.Cross( up, dir );
182-
Matrix4 rot = Matrix4.CreateFromAxisAngle( left, angle );
182+
Matrix3 rot = Matrix3.CreateFromAxisAngle( left, angle );
183183
dir = Vector3.Transform( dir, rot );
184-
up = Vector3.Transform( up, rot );
184+
up = Vector3.Transform( up, rot );
185185
}
186186

187187
/// <summary>
188188
/// Rotate around local y axis.
189189
/// </summary>
190190
private void Rudder ( ref Vector3 dir, Vector3 up, float angle )
191191
{
192-
Matrix4 rot = Matrix4.CreateFromAxisAngle( up, angle );
192+
Matrix3 rot = Matrix3.CreateFromAxisAngle( up, angle );
193193
dir = Vector3.Transform( dir, rot );
194194
}
195195

@@ -198,7 +198,7 @@ private void Rudder ( ref Vector3 dir, Vector3 up, float angle )
198198
/// </summary>
199199
private void Rotation ( Vector3 dir, ref Vector3 up, float angle )
200200
{
201-
Matrix4 rot = Matrix4.CreateFromAxisAngle( dir, angle );
201+
Matrix3 rot = Matrix3.CreateFromAxisAngle( dir, angle );
202202
up = Vector3.Transform( up, rot );
203203
}
204204

086shader/Form1.cs

+82-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using System.Collections.Generic;
23
using System.Drawing;
34
using System.Drawing.Imaging;
45
using System.IO;
@@ -30,13 +31,26 @@ public partial class Form1 : Form
3031
/// </summary>
3132
float diameter = 4.0f;
3233

34+
float near = 0.1f;
35+
float far = 5.0f;
36+
3337
Vector3 light = new Vector3( -2, 1, 1 );
3438

3539
/// <summary>
3640
/// Point in the 3D scene pointed out by an user, or null.
3741
/// </summary>
3842
Vector3? spot = null;
3943

44+
Vector3? pointOrigin = null;
45+
Vector3 pointTarget;
46+
47+
bool pointDirty = false;
48+
49+
/// <summary>
50+
/// Frustum vertices, 0 or 8 vertices
51+
/// </summary>
52+
List<Vector3> frustumFrame = new List<Vector3>();
53+
4054
/// <summary>
4155
/// GLControl guard flag.
4256
/// </summary>
@@ -69,7 +83,7 @@ private void glControl1_Load ( object sender, EventArgs e )
6983
{
7084
InitOpenGL();
7185
UpdateParams( textParam.Text );
72-
tb.GLsetupViewport( glControl1.Width, glControl1.Height );
86+
tb.GLsetupViewport( glControl1.Width, glControl1.Height, near, far );
7387

7488
loaded = true;
7589
Application.Idle += new EventHandler( Application_Idle );
@@ -79,7 +93,7 @@ private void glControl1_Resize ( object sender, EventArgs e )
7993
{
8094
if ( !loaded ) return;
8195

82-
tb.GLsetupViewport( glControl1.Width, glControl1.Height );
96+
tb.GLsetupViewport( glControl1.Width, glControl1.Height, near, far );
8397
glControl1.Invalidate();
8498
}
8599

@@ -247,6 +261,9 @@ private void glControl1_MouseDown ( object sender, MouseEventArgs e )
247261
if ( checkAxes.Checked )
248262
{
249263
// pointing to the scene:
264+
pointOrigin = convertScreenToWorldCoords( e.X, e.Y, 0.0f );
265+
pointTarget = convertScreenToWorldCoords( e.X, e.Y, 1.0f );
266+
pointDirty = true;
250267
}
251268
}
252269

@@ -272,7 +289,28 @@ private void glControl1_KeyDown ( object sender, KeyEventArgs e )
272289

273290
private void glControl1_KeyUp ( object sender, KeyEventArgs e )
274291
{
275-
tb.KeyUp( e );
292+
if ( !tb.KeyUp( e ) )
293+
if ( e.KeyCode == Keys.F )
294+
{
295+
e.Handled = true;
296+
if ( frustumFrame.Count > 0 )
297+
frustumFrame.Clear();
298+
else
299+
{
300+
float N = 0.0f;
301+
float F = 1.0f;
302+
int R = glControl1.Width - 1;
303+
int B = glControl1.Height - 1;
304+
frustumFrame.Add( convertScreenToWorldCoords( 0, 0, N ) );
305+
frustumFrame.Add( convertScreenToWorldCoords( R, 0, N ) );
306+
frustumFrame.Add( convertScreenToWorldCoords( 0, B, N ) );
307+
frustumFrame.Add( convertScreenToWorldCoords( R, B, N ) );
308+
frustumFrame.Add( convertScreenToWorldCoords( 0, 0, F ) );
309+
frustumFrame.Add( convertScreenToWorldCoords( R, 0, F ) );
310+
frustumFrame.Add( convertScreenToWorldCoords( 0, B, F ) );
311+
frustumFrame.Add( convertScreenToWorldCoords( R, B, F ) );
312+
}
313+
}
276314
}
277315

278316
private void buttonReset_Click ( object sender, EventArgs e )
@@ -311,5 +349,46 @@ private void labelFile_MouseHover ( object sender, EventArgs e )
311349
tt.Show( Util.TargetFramework + " (" + Util.RunningFramework + "), OpenTK " + Util.AssemblyVersion( typeof( Vector3 ) ),
312350
(IWin32Window)sender, 10, -25, 4000 );
313351
}
352+
353+
// Unproject support functions:
354+
355+
public Vector3 convertScreenToWorldCoords ( int x, int y, float z =0.0f )
356+
{
357+
Matrix4 modelViewMatrix, projectionMatrix;
358+
GL.GetFloat( GetPName.ModelviewMatrix, out modelViewMatrix );
359+
GL.GetFloat( GetPName.ProjectionMatrix, out projectionMatrix );
360+
361+
Vector2 mouse;
362+
mouse.X = x;
363+
mouse.Y = glControl1.Height - y;
364+
Vector3 vector = UnProject( ref projectionMatrix, modelViewMatrix, new Size( glControl1.Width, glControl1.Height ), mouse, z );
365+
return vector;
366+
}
367+
368+
public static Vector3 UnProject ( ref Matrix4 projection, Matrix4 view, Size viewport, Vector2 mouse, float z =0.0f )
369+
{
370+
Vector4 vec;
371+
vec.X = 2.0f * mouse.X / (float)viewport.Width - 1;
372+
vec.Y = 2.0f * mouse.Y / (float)viewport.Height - 1;
373+
vec.Z = z;
374+
vec.W = 1.0f;
375+
376+
Matrix4 viewInv = Matrix4.Invert( view );
377+
Matrix4 projInv = Matrix4.Invert( projection );
378+
379+
Vector4.Transform( ref vec, ref projInv, out vec );
380+
Vector4.Transform( ref vec, ref viewInv, out vec );
381+
382+
if ( vec.W > float.Epsilon ||
383+
vec.W < float.Epsilon )
384+
{
385+
vec.X /= vec.W;
386+
vec.Y /= vec.W;
387+
vec.Z /= vec.W;
388+
vec.W = 1.0f;
389+
}
390+
391+
return new Vector3( vec );
392+
}
314393
}
315394
}

086shader/RenderScene.cs

+101-6
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,45 @@ void Application_Idle ( object sender, EventArgs e )
8686
labelFps.Text = string.Format( CultureInfo.InvariantCulture, "Fps: {0:f1}, Tps: {1:f1}m",
8787
lastFps, (lastTps * 1.0e-6) );
8888
}
89+
90+
// pointing:
91+
if ( pointOrigin != null &&
92+
pointDirty )
93+
{
94+
Vector3d p0 = new Vector3d( pointOrigin.Value.X, pointOrigin.Value.Y, pointOrigin.Value.Z );
95+
Vector3d p1 = new Vector3d( pointTarget.X, pointTarget.Y, pointTarget.Z ) - p0;
96+
Vector2d uv;
97+
double nearest = double.PositiveInfinity;
98+
99+
if ( scene != null &&
100+
scene.Triangles > 0 )
101+
{
102+
Vector3 A, B, C;
103+
for ( int i = 0; i < scene.Triangles; i++ )
104+
{
105+
scene.GetTriangleVertices( i, out A, out B, out C );
106+
double curr = Geometry.RayTriangleIntersection( ref p0, ref p1, ref A, ref B, ref C, out uv );
107+
if ( !double.IsInfinity( curr ) &&
108+
curr < nearest )
109+
nearest = curr;
110+
}
111+
}
112+
else
113+
{
114+
Vector3d ul = new Vector3d( -1.0, -1.0, -1.0 );
115+
Vector3d size = new Vector3d( 2.0, 2.0, 2.0 );
116+
if ( Geometry.RayBoxIntersection( ref p0, ref p1, ref ul, ref size, out uv ) )
117+
nearest = uv.X;
118+
}
119+
120+
if ( double.IsInfinity( nearest ) )
121+
spot = null;
122+
else
123+
spot = new Vector3( (float)(p0.X + nearest * p1.X),
124+
(float)(p0.Y + nearest * p1.Y),
125+
(float)(p0.Z + nearest * p1.Z) );
126+
pointDirty = false;
127+
}
89128
}
90129
}
91130

@@ -295,7 +334,7 @@ void UpdateParams ( string param )
295334
if ( !Util.TryParse( p, "fov", ref fov ) )
296335
{
297336
tb.Fov = Arith.Clamp( fov, 0.1f, 2.0f );
298-
tb.GLsetupViewport( glControl1.Width, glControl1.Height );
337+
tb.GLsetupViewport( glControl1.Width, glControl1.Height, near, far );
299338
}
300339

301340
// shading: relative light position
@@ -622,6 +661,9 @@ void RenderScene ()
622661
// Support: axes
623662
if ( checkAxes.Checked )
624663
{
664+
float origWidth = GL.GetFloat( GetPName.LineWidth );
665+
float origPoint = GL.GetFloat( GetPName.PointSize );
666+
625667
// axes:
626668
GL.LineWidth( 2.0f );
627669
GL.Begin( PrimitiveType.Lines );
@@ -640,17 +682,70 @@ void RenderScene ()
640682

641683
GL.End();
642684

643-
// Support: spot
644-
if ( spot != null )
685+
// Support: pointing
686+
if ( pointOrigin != null )
645687
{
646-
GL.PointSize( 2.0f );
688+
GL.Begin( PrimitiveType.Lines );
689+
GL.Color3( 1.0f, 1.0f, 0.0f );
690+
GL.Vertex3( pointOrigin.Value );
691+
GL.Vertex3( pointTarget );
692+
GL.End();
693+
694+
GL.PointSize( 4.0f );
647695
GL.Begin( PrimitiveType.Points );
696+
GL.Color3( 1.0f, 0.0f, 0.0f );
697+
GL.Vertex3( pointOrigin.Value );
698+
GL.Color3( 0.0f, 1.0f, 0.2f );
699+
GL.Vertex3( pointTarget );
700+
if ( spot != null )
701+
{
702+
GL.Color3( 1.0f, 1.0f, 1.0f );
703+
GL.Vertex3( spot.Value );
704+
}
705+
GL.End();
706+
}
648707

649-
GL.Color3( 1.0f, 1.0f, 0.0f );
650-
GL.Vertex3( spot.Value );
708+
// Support: frustum
709+
if ( frustumFrame.Count >= 8 )
710+
{
711+
GL.LineWidth( 2.0f );
712+
GL.Begin( PrimitiveType.Lines );
713+
714+
GL.Color3( 1.0f, 0.0f, 0.0f );
715+
GL.Vertex3( frustumFrame[ 0 ] );
716+
GL.Vertex3( frustumFrame[ 1 ] );
717+
GL.Vertex3( frustumFrame[ 1 ] );
718+
GL.Vertex3( frustumFrame[ 3 ] );
719+
GL.Vertex3( frustumFrame[ 3 ] );
720+
GL.Vertex3( frustumFrame[ 2 ] );
721+
GL.Vertex3( frustumFrame[ 2 ] );
722+
GL.Vertex3( frustumFrame[ 0 ] );
723+
724+
GL.Color3( 1.0f, 1.0f, 1.0f );
725+
GL.Vertex3( frustumFrame[ 0 ] );
726+
GL.Vertex3( frustumFrame[ 4 ] );
727+
GL.Vertex3( frustumFrame[ 1 ] );
728+
GL.Vertex3( frustumFrame[ 5 ] );
729+
GL.Vertex3( frustumFrame[ 2 ] );
730+
GL.Vertex3( frustumFrame[ 6 ] );
731+
GL.Vertex3( frustumFrame[ 3 ] );
732+
GL.Vertex3( frustumFrame[ 7 ] );
733+
734+
GL.Color3( 0.0f, 1.0f, 0.0f );
735+
GL.Vertex3( frustumFrame[ 4 ] );
736+
GL.Vertex3( frustumFrame[ 5 ] );
737+
GL.Vertex3( frustumFrame[ 5 ] );
738+
GL.Vertex3( frustumFrame[ 7 ] );
739+
GL.Vertex3( frustumFrame[ 7 ] );
740+
GL.Vertex3( frustumFrame[ 6 ] );
741+
GL.Vertex3( frustumFrame[ 6 ] );
742+
GL.Vertex3( frustumFrame[ 4 ] );
651743

652744
GL.End();
653745
}
746+
747+
GL.LineWidth( origWidth );
748+
GL.PointSize( origPoint );
654749
}
655750
}
656751
}

0 commit comments

Comments
 (0)