/// <summary> /// Creates a new graphics context. /// </summary> private void _createGraphicsContext(DevicePixelFormat pixelFormat) { _graphicsContext = GraphicsManager.CreateGraphicsContext(_getWindowHandle()); #region Set Pixel Format DevicePixelFormatCollection pixelFormats = _graphicsContext.PixelsFormats; System.Collections.Generic.List <DevicePixelFormat> matchingPixelFormats = pixelFormats.Choose(pixelFormat); if ((matchingPixelFormats.Count == 0) && pixelFormat.MultisampleBits > 0) { // Try to select the maximum multisample configuration int multisampleBits = 0; pixelFormats.ForEach(delegate(DevicePixelFormat item) { multisampleBits = Math.Max(multisampleBits, item.MultisampleBits); }); pixelFormat.MultisampleBits = multisampleBits; matchingPixelFormats = pixelFormats.Choose(pixelFormat); } if ((matchingPixelFormats.Count == 0) && pixelFormat.DoubleBuffer) { // Try single buffered pixel formats pixelFormat.DoubleBuffer = false; matchingPixelFormats = pixelFormats.Choose(pixelFormat); if (matchingPixelFormats.Count == 0) { throw new InvalidOperationException(String.Format("unable to find a suitable pixel format: {0}", pixelFormats.GuessChooseError(pixelFormat))); } } else if (matchingPixelFormats.Count == 0) { throw new InvalidOperationException(String.Format("unable to find a suitable pixel format: {0}", pixelFormats.GuessChooseError(pixelFormat))); } _graphicsContext.SetPixelFormat(matchingPixelFormats[0]); #endregion // TODO: Platform specific extension checker if (WGL.IsExtensionSupported(_graphicsContext.DeviceHandle.Handle, WGL.EXT.SwapControl)) { // TODO: Handle tear WGL.SwapIntervalEXT(SwapInterval); } }
public static void SetupGL() { WGL.SwapIntervalEXT(1); // 60 fps is enough for us :) if (ConsoleManager.IsOpen) { GL.ClearColor(0.0f, 0.0f, 0.0f, 0.0f); GL.Color3f(1.0f, 1.0f, 1.0f); GL.Enable(GL.GL_DEPTH_TEST); GL.DepthFunc(GL.GL_LEQUAL); GL.ShadeModel(GL.GL_SMOOTH); GL.PixelStorei(GL.GL_UNPACK_ALIGNMENT, 1); GL.Enable(GL.GL_TEXTURE_2D); GL.Hint(GL.GL_PERSPECTIVE_CORRECTION_HINT, GL.GL_NICEST); GL.Disable(GL.GL_DITHER); // Actually we shouldn't have DITHERING in RGBA mode, but anyway... GL.Enable(GL.GL_BLEND); GL.BlendFunc(GL.GL_ONE, GL.GL_ONE); } else { if (ConsoleVarManager.GetValueToByte("DemoFreeglut") == 1) { GL.ClearColor(0.0f, 0.0f, 0.0f, 0.0f); GL.Color3f(1.0f, 1.0f, 1.0f); GL.Disable(GL.GL_DEPTH_TEST); GL.ShadeModel(GL.GL_FLAT); GL.Disable(GL.GL_TEXTURE_2D); GL.Hint(GL.GL_PERSPECTIVE_CORRECTION_HINT, GL.GL_NICEST); GL.Disable(GL.GL_DITHER); GL.Disable(GL.GL_BLEND); } if (ConsoleVarManager.GetValueToByte("DemoCubemapping") == 1) { GL.ClearColor(0.0f, 0.0f, 0.0f, 0.0f); // Black background GL.Color3f(1.0f, 1.0f, 1.0f); GL.ShadeModel(GL.GL_SMOOTH); // Enables smooth color shading GL.ClearDepth(1.0); // Depth buffer setup GL.Enable(GL.GL_DEPTH_TEST); // Enable depth buffer GL.DepthFunc(GL.GL_LESS); // Type of depth test to do GL.Hint(GL.GL_PERSPECTIVE_CORRECTION_HINT, GL.GL_NICEST); // Really nice perspective calculations GL.Enable(GL.GL_TEXTURE_2D); // Enable texture mapping GL.Disable(GL.GL_DITHER); GL.Disable(GL.GL_BLEND); CM_BACK = TextureManager.LoadTexture("data/cm_back.jpg", GL.GL_TEXTURE_2D, TextureManager.TM_PP_FLIP_IMAGE); CM_X_POSITIVE = TextureManager.LoadCubemapTexture("data/cm_xpos.jpg", GL.GL_TEXTURE_CUBE_MAP_POSITIVE_X_EXT, TextureManager.TM_PP_FLIP_IMAGE); CM_X_NEGATIVE = TextureManager.LoadCubemapTexture("data/cm_xneg.jpg", GL.GL_TEXTURE_CUBE_MAP_NEGATIVE_X_EXT, TextureManager.TM_PP_FLIP_IMAGE); CM_Y_POSITIVE = TextureManager.LoadCubemapTexture("data/cm_ypos.jpg", GL.GL_TEXTURE_CUBE_MAP_POSITIVE_Y_EXT, TextureManager.TM_PP_FLIP_IMAGE); CM_Y_NEGATIVE = TextureManager.LoadCubemapTexture("data/cm_yneg.jpg", GL.GL_TEXTURE_CUBE_MAP_NEGATIVE_Y_EXT, TextureManager.TM_PP_FLIP_IMAGE); CM_Z_POSITIVE = TextureManager.LoadCubemapTexture("data/cm_zpos.jpg", GL.GL_TEXTURE_CUBE_MAP_POSITIVE_Z_EXT, TextureManager.TM_PP_FLIP_IMAGE); CM_Z_NEGATIVE = TextureManager.LoadCubemapTexture("data/cm_zneg.jpg", GL.GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_EXT, TextureManager.TM_PP_FLIP_IMAGE); // Enable S T and R Texture Coordinate Generation as a reflection map GL.TexGeni(GL.GL_S, GL.GL_TEXTURE_GEN_MODE, (int)GL.GL_REFLECTION_MAP_EXT); GL.TexGeni(GL.GL_T, GL.GL_TEXTURE_GEN_MODE, (int)GL.GL_REFLECTION_MAP_EXT); GL.TexGeni(GL.GL_R, GL.GL_TEXTURE_GEN_MODE, (int)GL.GL_REFLECTION_MAP_EXT); if (GLConfig.GL_VERSION_1_2) { GL.TexParameteri(GL.GL_TEXTURE_CUBE_MAP_EXT, GL.GL_TEXTURE_WRAP_S, (int)GL.GL_CLAMP_TO_EDGE); GL.TexParameteri(GL.GL_TEXTURE_CUBE_MAP_EXT, GL.GL_TEXTURE_WRAP_T, (int)GL.GL_CLAMP_TO_EDGE); GL.TexParameteri(GL.GL_TEXTURE_CUBE_MAP_EXT, GL.GL_TEXTURE_WRAP_R, (int)GL.GL_CLAMP_TO_EDGE); } else { GL.TexParameteri(GL.GL_TEXTURE_CUBE_MAP_EXT, GL.GL_TEXTURE_WRAP_S, (int)GL.GL_CLAMP); GL.TexParameteri(GL.GL_TEXTURE_CUBE_MAP_EXT, GL.GL_TEXTURE_WRAP_T, (int)GL.GL_CLAMP); GL.TexParameteri(GL.GL_TEXTURE_CUBE_MAP_EXT, GL.GL_TEXTURE_WRAP_R, (int)GL.GL_CLAMP); } SphereQuadratic = GLU.NewQuadric(); GLU.QuadricNormals(SphereQuadratic, GLU.GLU_SMOOTH); } if (ConsoleVarManager.GetValueToByte("DemoGUI") == 1) { GL.ClearColor(0.0f, 0.0f, 0.0f, 0.0f); // Black background GL.ShadeModel(GL.GL_SMOOTH); // Enables smooth color shading GL.ClearDepth(1.0); // Depth buffer setup GL.Enable(GL.GL_DEPTH_TEST); // Enable depth buffer GL.DepthFunc(GL.GL_LESS); // Type of depth test to do GL.Hint(GL.GL_PERSPECTIVE_CORRECTION_HINT, GL.GL_NICEST); // Really nice perspective calculations GL.Disable(GL.GL_TEXTURE_2D); // Enable texture mapping GL.Disable(GL.GL_DITHER); GL.Enable(GL.GL_BLEND); GL.BlendFunc(GL.GL_SRC_ALPHA, GL.GL_ONE_MINUS_SRC_ALPHA); } } }