Having a hard time getting sfml to work with opentk. I am creating an sfml window and trying to draw a 3d cube using opengl while using sfml's Text classes to draw text on top. However, on window.resize it bugs the program out. I am changing the model-view and setting the view-port, then it bugs the program where the cube is no longer being drawn. Also when I try to render Text, it is really buggy and flashy.
Am I doing something fundamentally wrong here?
using System;using SFML.Window;using SFML.Graphics;using OpenTK.Graphics.OpenGL;using OpenTK;namespace SFML
.NET_2015{ class Program
{ static void Main
(string[] args
) { ContextSettings contextSettings
= new ContextSettings
(24,
0,
0); SFML
.Graphics.RenderWindow win
= new RenderWindow
(new VideoMode
(640,
400),
"SFML.NET", Styles
.Default, contextSettings
); win
.SetFramerateLimit(144); win
.SetMouseCursorVisible(true); win
.Closed += (o, e
) => { win
.Close(); }; win
.Resized += (o, e
) => { GL
.Viewport(0,
0,
(int)win
.Size.X,
(int)win
.Size.Y); Matrix4 matr
= OpenTK
.Matrix4.CreatePerspectiveFieldOfView((float)Math
.PI * 50f
/ 180f, win
.Size.X / (float)win
.Size.Y,
.1f, 10f
); GL
.LoadMatrix(ref matr
); }; Toolkit
.Init(); OpenTK
.Graphics.GraphicsMode graphicsMode
= new OpenTK
.Graphics.GraphicsMode(32,
(int)contextSettings
.DepthBits,
(int)contextSettings
.StencilBits,
(int)contextSettings
.AntialiasingLevel); OpenTK
.Platform.IWindowInfo windowInfo
= OpenTK
.Platform.Utilities.CreateWindowsWindowInfo(win
.SystemHandle); OpenTK
.Graphics.GraphicsContext context
= new OpenTK
.Graphics.GraphicsContext(graphicsMode, windowInfo
); context
.MakeCurrent(windowInfo
); context
.LoadAll(); GL
.EnableClientState(ArrayCap
.VertexArray); GL
.MatrixMode(MatrixMode
.Projection); Matrix4 mat
= OpenTK
.Matrix4.CreatePerspectiveFieldOfView((float)Math
.PI * 50f
/ 180f, win
.Size.X / (float)win
.Size.Y,
.1f, 10f
); GL
.LoadMatrix(ref mat
); GL
.MatrixMode(MatrixMode
.Modelview); GL
.Translate(0,
0,
-4); #region Verts float[] verts
= { -1
.0f,
-1
.0f,
-1
.0f,
// triangle 1 : begin -1
.0f,
-1
.0f, 1
.0f,
-1
.0f, 1
.0f, 1
.0f,
// triangle 1 : end 1
.0f, 1
.0f,
-1
.0f,
// triangle 2 : begin -1
.0f,
-1
.0f,
-1
.0f,
-1
.0f, 1
.0f,
-1
.0f,
// triangle 2 : end 1
.0f,
-1
.0f, 1
.0f,
-1
.0f,
-1
.0f,
-1
.0f,
1
.0f,
-1
.0f,
-1
.0f,
1
.0f, 1
.0f,
-1
.0f,
1
.0f,
-1
.0f,
-1
.0f,
-1
.0f,
-1
.0f,
-1
.0f,
-1
.0f,
-1
.0f,
-1
.0f,
-1
.0f, 1
.0f, 1
.0f,
-1
.0f, 1
.0f,
-1
.0f,
1
.0f,
-1
.0f, 1
.0f,
-1
.0f,
-1
.0f, 1
.0f,
-1
.0f,
-1
.0f,
-1
.0f,
-1
.0f, 1
.0f, 1
.0f,
-1
.0f,
-1
.0f, 1
.0f,
1
.0f,
-1
.0f, 1
.0f,
1
.0f, 1
.0f, 1
.0f,
1
.0f,
-1
.0f,
-1
.0f,
1
.0f, 1
.0f,
-1
.0f,
1
.0f,
-1
.0f,
-1
.0f,
1
.0f, 1
.0f, 1
.0f,
1
.0f,
-1
.0f, 1
.0f,
1
.0f, 1
.0f, 1
.0f,
1
.0f, 1
.0f,
-1
.0f,
-1
.0f, 1
.0f,
-1
.0f,
1
.0f, 1
.0f, 1
.0f,
-1
.0f, 1
.0f,
-1
.0f,
-1
.0f, 1
.0f, 1
.0f,
1
.0f, 1
.0f, 1
.0f,
-1
.0f, 1
.0f, 1
.0f,
1
.0f,
-1
.0f, 1
.0f
}; #endregion int buff
= GL
.GenBuffer(); GL
.BindBuffer(BufferTarget
.ArrayBuffer, buff
); GL
.BufferData(BufferTarget
.ArrayBuffer,
(IntPtr
)(verts
.Length * sizeof(float)), verts, BufferUsageHint
.StaticDraw); GL
.VertexPointer(3, VertexPointerType
.Float,
3 * sizeof(float),
0); Text t
= new Text
("Hello Cube",
new Font
("andyb.ttf")); t
.Position = new System.Vector2f(200,
200); while (win
.IsOpen) { win
.DispatchEvents(); GL
.Rotate(.1f,
0,
1,
0); GL
.DrawArrays(OpenTK
.Graphics.OpenGL.PrimitiveType.Triangles,
0, verts
.Length); GL
.End(); win
.PushGLStates(); win
.Draw(t
); win
.PopGLStates(); win
.Display(); } } }}
Hmm. That sounds like states or matrices being changed... Oh. What happens if you add your GL.MatrixMode(MatrixMode.Modelview);
GL.Translate(0, 0, -4);
before your GL.Rotate() in your while loop?
If that doesn't fix anything, can you post the current code?
(Sorry for the laggy responses, I only have time to check the forums between classes and work. :) )
Here is the full code. Adding the -4 translation didn't do anything.
using System;using SFML.Window;using SFML.Graphics;using OpenTK.Graphics.OpenGL;using OpenTK;namespace SFML
.NET_2015{ class Program
{ static void Main
(string[] args
) { ContextSettings contextSettings
= new ContextSettings
(24,
0,
0); SFML
.Graphics.RenderWindow win
= new RenderWindow
(new VideoMode
(640,
400),
"SFML.NET", Styles
.Default, contextSettings
); win
.SetFramerateLimit(144); win
.SetMouseCursorVisible(true); win
.Closed += (o, e
) => { win
.Close(); }; win
.Resized += (o, e
) => { GL
.Viewport(0,
0,
(int)win
.Size.X,
(int)win
.Size.Y); GL
.MatrixMode(MatrixMode
.Projection); Matrix4 matr
= OpenTK
.Matrix4.CreatePerspectiveFieldOfView((float)Math
.PI * 50f
/ 180f, win
.Size.X / (float)win
.Size.Y,
.1f, 10f
); GL
.LoadMatrix(ref matr
); }; Toolkit
.Init(); OpenTK
.Graphics.GraphicsMode graphicsMode
= new OpenTK
.Graphics.GraphicsMode(32,
(int)contextSettings
.DepthBits,
(int)contextSettings
.StencilBits,
(int)contextSettings
.AntialiasingLevel); OpenTK
.Platform.IWindowInfo windowInfo
= OpenTK
.Platform.Utilities.CreateWindowsWindowInfo(win
.SystemHandle); OpenTK
.Graphics.GraphicsContext context
= new OpenTK
.Graphics.GraphicsContext(graphicsMode, windowInfo
); context
.MakeCurrent(windowInfo
); context
.LoadAll(); GL
.EnableClientState(ArrayCap
.VertexArray); #region Verts float[] verts
= { -1
.0f,
-1
.0f,
-1
.0f,
// triangle 1 : begin -1
.0f,
-1
.0f, 1
.0f,
-1
.0f, 1
.0f, 1
.0f,
// triangle 1 : end 1
.0f, 1
.0f,
-1
.0f,
// triangle 2 : begin -1
.0f,
-1
.0f,
-1
.0f,
-1
.0f, 1
.0f,
-1
.0f,
// triangle 2 : end 1
.0f,
-1
.0f, 1
.0f,
-1
.0f,
-1
.0f,
-1
.0f,
1
.0f,
-1
.0f,
-1
.0f,
1
.0f, 1
.0f,
-1
.0f,
1
.0f,
-1
.0f,
-1
.0f,
-1
.0f,
-1
.0f,
-1
.0f,
-1
.0f,
-1
.0f,
-1
.0f,
-1
.0f, 1
.0f, 1
.0f,
-1
.0f, 1
.0f,
-1
.0f,
1
.0f,
-1
.0f, 1
.0f,
-1
.0f,
-1
.0f, 1
.0f,
-1
.0f,
-1
.0f,
-1
.0f,
-1
.0f, 1
.0f, 1
.0f,
-1
.0f,
-1
.0f, 1
.0f,
1
.0f,
-1
.0f, 1
.0f,
1
.0f, 1
.0f, 1
.0f,
1
.0f,
-1
.0f,
-1
.0f,
1
.0f, 1
.0f,
-1
.0f,
1
.0f,
-1
.0f,
-1
.0f,
1
.0f, 1
.0f, 1
.0f,
1
.0f,
-1
.0f, 1
.0f,
1
.0f, 1
.0f, 1
.0f,
1
.0f, 1
.0f,
-1
.0f,
-1
.0f, 1
.0f,
-1
.0f,
1
.0f, 1
.0f, 1
.0f,
-1
.0f, 1
.0f,
-1
.0f,
-1
.0f, 1
.0f, 1
.0f,
1
.0f, 1
.0f, 1
.0f,
-1
.0f, 1
.0f, 1
.0f,
1
.0f,
-1
.0f, 1
.0f
}; #endregion int buff
= GL
.GenBuffer(); GL
.BindBuffer(BufferTarget
.ArrayBuffer, buff
); GL
.BufferData(BufferTarget
.ArrayBuffer,
(IntPtr
)(verts
.Length * sizeof(float)), verts, BufferUsageHint
.StaticDraw); GL
.VertexPointer(3, VertexPointerType
.Float,
3 * sizeof(float),
0); Text t
= new Text
("Hello Cube",
new Font
("andyb.ttf")); t
.Position = new System.Vector2f(200, 000
); while (win
.IsOpen) { win
.DispatchEvents(); //win.Clear(); GL
.Clear(ClearBufferMask
.ColorBufferBit | ClearBufferMask
.DepthBufferBit); //GL.Viewport(0, 0, (int)win.Size.X, (int)win.Size.Y); GL
.MatrixMode(MatrixMode
.Projection); Matrix4 matr
= OpenTK
.Matrix4.CreatePerspectiveFieldOfView((float)Math
.PI * 50f
/ 180f, win
.Size.X / (float)win
.Size.Y,
.1f, 10f
); GL
.LoadMatrix(ref matr
); GL
.MatrixMode(MatrixMode
.Modelview); GL
.LoadIdentity(); GL
.Translate(0,
0,
-4); GL
.Rotate(50f,
0,
1,
0); GL
.DrawArrays(OpenTK
.Graphics.OpenGL.PrimitiveType.Triangles,
0, verts
.Length); win
.PushGLStates(); win
.Draw(t
); win
.PopGLStates(); win
.Display(); } } }}
Without the
win.PushGLStates();
win.Draw(t);
win.PopGLStates();
It draws the cube, rotated so an edge faces the screen.
I can reproduce it on my home machine (driver 365.10).
Investigating, it looks like I may be correct, seems it may be a race condition:
at OpenTK.Graphics.OpenGL.GL.Viewport(Int32 x, Int32 y, Int32 width, Int32 height)
at SFML.NET_2015.Program.Main(String[] args) in g:\Code\CS_SFMLAndOpenGLTest\CS_SFMLAndOpenGLTest\Program.cs:line 83
at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()
NVIDIA's driver may potentially be doing some extra threading that interferes with both SFML and OpenTK using the context, that Intel's driver does not do.
Unfortunately, this is getting out of the realm of my knowledge, so I'm not really sure where to go from here.