BrawlCrate v0.41
Wii File Editor
Loading...
Searching...
No Matches
Public Member Functions | Static Public Member Functions | Public Attributes | Static Public Attributes | Properties | Events | List of all members
BrawlLib.OpenGL.TKContext Class Reference
Inheritance diagram for BrawlLib.OpenGL.TKContext:

Public Member Functions

void Unbind ()
 
 TKContext (Control window)
 
delegate void ContextChangedEventHandler (bool isCurrent)
 
void Dispose ()
 
void CheckErrors ()
 
void Capture (bool force=false)
 
void Swap ()
 
void Reset ()
 
void Release ()
 
void Update ()
 
GLDisplayList GetLine ()
 

Static Public Member Functions

static T FindOrCreate< T > (string name, GLCreateHandler< T > handler)
 
static void InvalidateModelPanels (IRenderedObject obj)
 
static void DrawWireframeBox (Box value)
 
static void DrawWireframeBox (Vector3 min, Vector3 max)
 
static void DrawBox (Box value)
 
static void DrawBox (Vector3 p1, Vector3 p2)
 
static void DrawInvertedBox (Box value)
 
static void DrawInvertedBox (Vector3 p1, Vector3 p2)
 
static void DrawCube (Vector3 p, float radius)
 
static void DrawInvertedCube (Vector3 p, float radius)
 
static void DrawRing (float radius)
 
static GLDisplayList GetRingList ()
 
static GLDisplayList GetSquareList ()
 
static GLDisplayList GetAxisList ()
 
static GLDisplayList GetCubeList ()
 
static GLDisplayList GetCircleList ()
 
static void DrawSphere (float radius)
 
static GLDisplayList GetSphereList ()
 
static GLDisplayList CreateSphere ()
 
static void DrawSphere (Vector3 center, float radius, uint precision)
 

Public Attributes

Control _window
 

Static Public Attributes

static List< TKContext_boundContexts
 
static TKContext CurrentContext
 
static bool _shadersSupported = true
 
static int _versionMax
 
static int _versionMin
 
static bool _anyContextInitialized
 

Properties

IWindowInfo WindowInfo [get]
 
static List< TKContextBoundContexts [get]
 
bool Resetting [get]
 

Events

ContextChangedEventHandler ContextChanged
 
EventHandler ResetOccured
 

Constructor & Destructor Documentation

◆ TKContext()

BrawlLib.OpenGL.TKContext.TKContext ( Control  window)
inline
76 {
77 _window = window;
78 _winInfo = Utilities.CreateWindowsWindowInfo(_window.Handle);
79 _context = new GraphicsContext(GraphicsMode.Default, _winInfo);
80 _context.MakeCurrent(WindowInfo);
81 _context.LoadAll();
82
84 {
85 // Check for GLSL support
86 string[] version = GL.GetString(StringName.Version).Split('.', ' ');
87 _versionMax = int.Parse(version[0]);
88 _versionMin = int.Parse(version[1]);
89
90 //Need OpenGL 2.1 to use GLSL 120
93 }
94
95 BoundContexts.Add(this);
96 }
static int _versionMax
Definition: TKContext.cs:69
Control _window
Definition: TKContext.cs:73
static bool _anyContextInitialized
Definition: TKContext.cs:71
static List< TKContext > BoundContexts
Definition: TKContext.cs:21
static bool _shadersSupported
Definition: TKContext.cs:68
IWindowInfo WindowInfo
Definition: TKContext.cs:18
static int _versionMin
Definition: TKContext.cs:70

Member Function Documentation

◆ Capture()

void BrawlLib.OpenGL.TKContext.Capture ( bool  force = false)
inline
127 {
128 try
129 {
130 //Only proceed if this window is not already current
131 if (force || CurrentContext != this)
132 {
133 //Release the current context if it exists
135
136 //Make this context the current one
137 CurrentContext = this;
138 if (!_context.IsCurrent)
139 {
140 _context.MakeCurrent(WindowInfo);
141 }
142
143 ContextChanged?.Invoke(true);
144 }
145 }
146 catch //(Exception x)
147 {
148 //MessageBox.Show(x.ToString());
149 Reset();
150 }
151 }
void Release()
Definition: TKContext.cs:197
static TKContext CurrentContext
Definition: TKContext.cs:23
void Reset()
Definition: TKContext.cs:175
ContextChangedEventHandler ContextChanged
Definition: TKContext.cs:100

◆ CheckErrors()

void BrawlLib.OpenGL.TKContext.CheckErrors ( )
inline
115 {
116 ErrorCode code = GL.GetError();
117 if (code == ErrorCode.NoError)
118 {
119 return;
120 }
121
122 //throw new Exception(code.ToString());
123 Reset();
124 }

◆ ContextChangedEventHandler()

delegate void BrawlLib.OpenGL.TKContext.ContextChangedEventHandler ( bool  isCurrent)

◆ CreateSphere()

static GLDisplayList BrawlLib.OpenGL.TKContext.CreateSphere ( )
inlinestatic
561 {
562 GLDisplayList dl = new GLDisplayList();
563
564 dl.Begin();
565 DrawSphere(new Vector3(), 1.0f, 40);
566 dl.End();
567
568 return dl;
569 }
static void DrawSphere(float radius)
Definition: TKContext.cs:547
Definition: Vector3.cs:40

◆ Dispose()

void BrawlLib.OpenGL.TKContext.Dispose ( )
inline
104 {
105 Release();
106 _context?.Dispose();
107
108 if (BoundContexts.Contains(this))
109 {
110 BoundContexts.Remove(this);
111 }
112 }

◆ DrawBox() [1/2]

static void BrawlLib.OpenGL.TKContext.DrawBox ( Box  value)
inlinestatic
268 {
269 DrawBox(value.Min, value.Max);
270 }
static void DrawBox(Box value)
Definition: TKContext.cs:267
Vector3 Min
Definition: Box.cs:12
Vector3 Max
Definition: Box.cs:18

◆ DrawBox() [2/2]

static void BrawlLib.OpenGL.TKContext.DrawBox ( Vector3  p1,
Vector3  p2 
)
inlinestatic
273 {
274 GL.Begin(BeginMode.QuadStrip);
275
276 GL.Vertex3(p1._x, p1._y, p1._z);
277 GL.Vertex3(p1._x, p2._y, p1._z);
278 GL.Vertex3(p2._x, p1._y, p1._z);
279 GL.Vertex3(p2._x, p2._y, p1._z);
280 GL.Vertex3(p2._x, p1._y, p2._z);
281 GL.Vertex3(p2._x, p2._y, p2._z);
282 GL.Vertex3(p1._x, p1._y, p2._z);
283 GL.Vertex3(p1._x, p2._y, p2._z);
284 GL.Vertex3(p1._x, p1._y, p1._z);
285 GL.Vertex3(p1._x, p2._y, p1._z);
286
287 GL.End();
288
289 GL.Begin(BeginMode.Quads);
290
291 GL.Vertex3(p1._x, p2._y, p1._z);
292 GL.Vertex3(p1._x, p2._y, p2._z);
293 GL.Vertex3(p2._x, p2._y, p2._z);
294 GL.Vertex3(p2._x, p2._y, p1._z);
295
296 GL.Vertex3(p1._x, p1._y, p1._z);
297 GL.Vertex3(p1._x, p1._y, p2._z);
298 GL.Vertex3(p2._x, p1._y, p2._z);
299 GL.Vertex3(p2._x, p1._y, p1._z);
300
301 GL.End();
302 }
float _y
Definition: Vector3.cs:41
float _z
Definition: Vector3.cs:41
float _x
Definition: Vector3.cs:41

◆ DrawCube()

static void BrawlLib.OpenGL.TKContext.DrawCube ( Vector3  p,
float  radius 
)
inlinestatic
342 {
343 Vector3 p1 = new Vector3(p._x + radius, p._y + radius, p._z + radius);
344 Vector3 p2 = new Vector3(p._x - radius, p._y - radius, p._z - radius);
345 DrawBox(p2, p1);
346 }

◆ DrawInvertedBox() [1/2]

static void BrawlLib.OpenGL.TKContext.DrawInvertedBox ( Box  value)
inlinestatic
305 {
306 DrawInvertedBox(value.Min, value.Max);
307 }
static void DrawInvertedBox(Box value)
Definition: TKContext.cs:304

◆ DrawInvertedBox() [2/2]

static void BrawlLib.OpenGL.TKContext.DrawInvertedBox ( Vector3  p1,
Vector3  p2 
)
inlinestatic
310 {
311 GL.Begin(BeginMode.QuadStrip);
312
313 GL.Vertex3(p1._x, p1._y, p1._z);
314 GL.Vertex3(p1._x, p2._y, p1._z);
315 GL.Vertex3(p1._x, p1._y, p2._z);
316 GL.Vertex3(p1._x, p2._y, p2._z);
317 GL.Vertex3(p2._x, p1._y, p2._z);
318 GL.Vertex3(p2._x, p2._y, p2._z);
319 GL.Vertex3(p2._x, p1._y, p1._z);
320 GL.Vertex3(p2._x, p2._y, p1._z);
321 GL.Vertex3(p1._x, p1._y, p1._z);
322 GL.Vertex3(p1._x, p2._y, p1._z);
323
324 GL.End();
325
326 GL.Begin(BeginMode.Quads);
327
328 GL.Vertex3(p2._x, p2._y, p1._z);
329 GL.Vertex3(p2._x, p2._y, p2._z);
330 GL.Vertex3(p1._x, p2._y, p2._z);
331 GL.Vertex3(p1._x, p2._y, p1._z);
332
333 GL.Vertex3(p1._x, p1._y, p1._z);
334 GL.Vertex3(p1._x, p1._y, p2._z);
335 GL.Vertex3(p2._x, p1._y, p2._z);
336 GL.Vertex3(p2._x, p1._y, p1._z);
337
338 GL.End();
339 }

◆ DrawInvertedCube()

static void BrawlLib.OpenGL.TKContext.DrawInvertedCube ( Vector3  p,
float  radius 
)
inlinestatic
349 {
350 Vector3 p1 = new Vector3(p._x + radius, p._y + radius, p._z + radius);
351 Vector3 p2 = new Vector3(p._x - radius, p._y - radius, p._z - radius);
352 DrawInvertedBox(p2, p1);
353 }

◆ DrawRing()

static void BrawlLib.OpenGL.TKContext.DrawRing ( float  radius)
inlinestatic
356 {
357 GL.PushMatrix();
358 GL.Scale(radius, radius, radius);
359 GetRingList().Call();
360 GL.PopMatrix();
361 }
void Call()
Definition: GLDisplayList.cs:32
static GLDisplayList GetRingList()
Definition: TKContext.cs:382

◆ DrawSphere() [1/2]

static void BrawlLib.OpenGL.TKContext.DrawSphere ( float  radius)
inlinestatic
548 {
549 GL.PushMatrix();
550 GL.Scale(radius, radius, radius);
552 GL.PopMatrix();
553 }
static GLDisplayList GetSphereList()
Definition: TKContext.cs:555

◆ DrawSphere() [2/2]

static void BrawlLib.OpenGL.TKContext.DrawSphere ( Vector3  center,
float  radius,
uint  precision 
)
inlinestatic
572 {
573 if (radius < 0.0f)
574 {
575 radius = -radius;
576 }
577
578 if (radius == 0.0f)
579 {
580 throw new DivideByZeroException("DrawSphere: Radius cannot be zero.");
581 }
582
583 if (precision == 0)
584 {
585 throw new DivideByZeroException("DrawSphere: Precision of 8 or greater is required.");
586 }
587
588 float halfPI = (float) (Math.PI * 0.5);
589 float oneThroughPrecision = 1.0f / precision;
590 float twoPIThroughPrecision = (float) (Math.PI * 2.0 * oneThroughPrecision);
591
592 float theta1, theta2, theta3;
593 Vector3 norm, pos;
594
595 for (uint j = 0; j < precision / 2; j++)
596 {
597 theta1 = j * twoPIThroughPrecision - halfPI;
598 theta2 = (j + 1) * twoPIThroughPrecision - halfPI;
599
600 GL.Begin(BeginMode.TriangleStrip);
601 for (uint i = 0; i <= precision; i++)
602 {
603 theta3 = i * twoPIThroughPrecision;
604
605 norm._x = (float) (Math.Cos(theta2) * Math.Cos(theta3));
606 norm._y = (float) Math.Sin(theta2);
607 norm._z = (float) (Math.Cos(theta2) * Math.Sin(theta3));
608 pos._x = center._x + radius * norm._x;
609 pos._y = center._y + radius * norm._y;
610 pos._z = center._z + radius * norm._z;
611
612 GL.Normal3(norm._x, norm._y, norm._z);
613 GL.TexCoord2(i * oneThroughPrecision, 2.0f * (j + 1) * oneThroughPrecision);
614 GL.Vertex3(pos._x, pos._y, pos._z);
615
616 norm._x = (float) (Math.Cos(theta1) * Math.Cos(theta3));
617 norm._y = (float) Math.Sin(theta1);
618 norm._z = (float) (Math.Cos(theta1) * Math.Sin(theta3));
619 pos._x = center._x + radius * norm._x;
620 pos._y = center._y + radius * norm._y;
621 pos._z = center._z + radius * norm._z;
622
623 GL.Normal3(norm._x, norm._y, norm._z);
624 GL.TexCoord2(i * oneThroughPrecision, 2.0f * j * oneThroughPrecision);
625 GL.Vertex3(pos._x, pos._y, pos._z);
626 }
627
628 GL.End();
629 }
630 }

◆ DrawWireframeBox() [1/2]

static void BrawlLib.OpenGL.TKContext.DrawWireframeBox ( Box  value)
inlinestatic
231 {
232 DrawWireframeBox(value.Min, value.Max);
233 }
static void DrawWireframeBox(Box value)
Definition: TKContext.cs:230

◆ DrawWireframeBox() [2/2]

static void BrawlLib.OpenGL.TKContext.DrawWireframeBox ( Vector3  min,
Vector3  max 
)
inlinestatic
236 {
237 GL.Begin(BeginMode.LineStrip);
238
239 GL.Vertex3(max._x, max._y, max._z);
240 GL.Vertex3(max._x, max._y, min._z);
241 GL.Vertex3(min._x, max._y, min._z);
242 GL.Vertex3(min._x, min._y, min._z);
243 GL.Vertex3(min._x, min._y, max._z);
244 GL.Vertex3(max._x, min._y, max._z);
245 GL.Vertex3(max._x, max._y, max._z);
246
247 GL.End();
248
249 GL.Begin(BeginMode.Lines);
250
251 GL.Vertex3(min._x, max._y, max._z);
252 GL.Vertex3(max._x, max._y, max._z);
253 GL.Vertex3(min._x, max._y, max._z);
254 GL.Vertex3(min._x, min._y, max._z);
255 GL.Vertex3(min._x, max._y, max._z);
256 GL.Vertex3(min._x, max._y, min._z);
257 GL.Vertex3(max._x, min._y, min._z);
258 GL.Vertex3(min._x, min._y, min._z);
259 GL.Vertex3(max._x, min._y, min._z);
260 GL.Vertex3(max._x, max._y, min._z);
261 GL.Vertex3(max._x, min._y, min._z);
262 GL.Vertex3(max._x, min._y, max._z);
263
264 GL.End();
265 }

◆ FindOrCreate< T >()

static T BrawlLib.OpenGL.TKContext.FindOrCreate< T > ( string  name,
GLCreateHandler< T >  handler 
)
inlinestatic
28 {
29 if (CurrentContext == null)
30 {
31 return default(T);
32 }
33
34 if (CurrentContext._states.ContainsKey(name))
35 {
36 return (T) CurrentContext._states[name];
37 }
38
39 T obj = handler();
40 CurrentContext._states[name] = obj;
41 return obj;
42 }

◆ GetAxisList()

static GLDisplayList BrawlLib.OpenGL.TKContext.GetAxisList ( )
inlinestatic
431 {
432 return FindOrCreate("Axes", CreateAxes);
433 }

◆ GetCircleList()

static GLDisplayList BrawlLib.OpenGL.TKContext.GetCircleList ( )
inlinestatic
522 {
523 return FindOrCreate("Circle", CreateCircle);
524 }

◆ GetCubeList()

static GLDisplayList BrawlLib.OpenGL.TKContext.GetCubeList ( )
inlinestatic
476 {
477 return FindOrCreate("Cube", CreateCube);
478 }

◆ GetLine()

GLDisplayList BrawlLib.OpenGL.TKContext.GetLine ( )
inline
364 {
365 return FindOrCreate("Line", CreateLine);
366 }

◆ GetRingList()

static GLDisplayList BrawlLib.OpenGL.TKContext.GetRingList ( )
inlinestatic
383 {
384 return FindOrCreate("Ring", CreateRing);
385 }

◆ GetSphereList()

static GLDisplayList BrawlLib.OpenGL.TKContext.GetSphereList ( )
inlinestatic
556 {
557 return FindOrCreate("Sphere", CreateSphere);
558 }
static GLDisplayList CreateSphere()
Definition: TKContext.cs:560

◆ GetSquareList()

static GLDisplayList BrawlLib.OpenGL.TKContext.GetSquareList ( )
inlinestatic
407 {
408 return FindOrCreate("Square", CreateSquare);
409 }

◆ InvalidateModelPanels()

static void BrawlLib.OpenGL.TKContext.InvalidateModelPanels ( IRenderedObject  obj)
inlinestatic
217 {
218 if (_boundContexts != null)
219 {
220 foreach (TKContext x in _boundContexts)
221 {
222 if (x._window is ModelPanel /* && ((ModelPanel)x._window)._renderList.Contains(obj)*/)
223 {
224 x._window.Invalidate();
225 }
226 }
227 }
228 }
static List< TKContext > _boundContexts
Definition: TKContext.cs:22

◆ Release()

void BrawlLib.OpenGL.TKContext.Release ( )
inline
198 {
199 if (CurrentContext == this && !_context.IsDisposed && _context.IsCurrent)
200 {
201 CurrentContext = null;
202 _context.MakeCurrent(null);
203
204 ContextChanged?.Invoke(false);
205 }
206 }

◆ Reset()

void BrawlLib.OpenGL.TKContext.Reset ( )
inline
176 {
177 if (_resetting) //Prevent a possible infinite loop
178 {
179 return;
180 }
181
182 _resetting = true;
183 _window.Reset();
184 Dispose();
185
186 _winInfo = Utilities.CreateWindowsWindowInfo(_window.Handle);
187 _context = new GraphicsContext(GraphicsMode.Default, WindowInfo);
188 Capture(true);
189 _context.LoadAll();
190 Update();
191
192 ResetOccured?.Invoke(this, EventArgs.Empty);
193
194 _resetting = false;
195 }
void Capture(bool force=false)
Definition: TKContext.cs:126
EventHandler ResetOccured
Definition: TKContext.cs:101
void Update()
Definition: TKContext.cs:208
void Dispose()
Definition: TKContext.cs:103

◆ Swap()

void BrawlLib.OpenGL.TKContext.Swap ( )
inline
154 {
155 try
156 {
157 if (CurrentContext == this &&
158 _context != null &&
159 _context.IsCurrent &&
160 !_context.IsDisposed)
161 {
162 _context.SwapBuffers();
163 }
164 }
165 catch //(Exception x)
166 {
167 //MessageBox.Show(x.ToString());
168 Reset();
169 }
170 }

◆ Unbind()

void BrawlLib.OpenGL.TKContext.Unbind ( )
inline
45 {
46 try
47 {
48 foreach (object o in _states.Values)
49 {
50 if (o is GLDisplayList)
51 {
52 (o as GLDisplayList).Delete();
53 }
54 else
55 {
56 (o as GLTexture)?.Delete();
57 }
58 }
59 }
60 catch
61 {
62 // ignored
63 }
64
65 _states.Clear();
66 }

◆ Update()

void BrawlLib.OpenGL.TKContext.Update ( )
inline
209 {
210 if (CurrentContext == this)
211 {
212 _context.Update(WindowInfo);
213 }
214 }

Member Data Documentation

◆ _anyContextInitialized

bool BrawlLib.OpenGL.TKContext._anyContextInitialized
static

◆ _boundContexts

List<TKContext> BrawlLib.OpenGL.TKContext._boundContexts
static

◆ _shadersSupported

bool BrawlLib.OpenGL.TKContext._shadersSupported = true
static

◆ _versionMax

int BrawlLib.OpenGL.TKContext._versionMax
static

◆ _versionMin

int BrawlLib.OpenGL.TKContext._versionMin
static

◆ _window

Control BrawlLib.OpenGL.TKContext._window

◆ CurrentContext

TKContext BrawlLib.OpenGL.TKContext.CurrentContext
static

Property Documentation

◆ BoundContexts

List<TKContext> BrawlLib.OpenGL.TKContext.BoundContexts
staticget

◆ Resetting

bool BrawlLib.OpenGL.TKContext.Resetting
get

◆ WindowInfo

IWindowInfo BrawlLib.OpenGL.TKContext.WindowInfo
get

Event Documentation

◆ ContextChanged

ContextChangedEventHandler BrawlLib.OpenGL.TKContext.ContextChanged

◆ ResetOccured

EventHandler BrawlLib.OpenGL.TKContext.ResetOccured

The documentation for this class was generated from the following file: