private void GL_Init(object sender, OpenGLRoutedEventArgs args) { OpenGL gl = args.OpenGL; gl.Enable(OpenGL.GL_DEPTH_TEST); float[] global_ambient = new float[] { 0.5f, 0.5f, 0.5f, 1.0f }; float[] light0pos = new float[] { 0.0f, 5.0f, 10.0f, 1.0f }; float[] light0ambient = new float[] { 0.2f, 0.2f, 0.2f, 1.0f }; float[] light0diffuse = new float[] { 0.3f, 0.3f, 0.3f, 1.0f }; float[] light0specular = new float[] { 0.8f, 0.8f, 0.8f, 1.0f }; float[] lmodel_ambient = new float[] { 0.2f, 0.2f, 0.2f, 1.0f }; gl.LightModel(OpenGL.GL_LIGHT_MODEL_AMBIENT, lmodel_ambient); gl.LightModel(OpenGL.GL_LIGHT_MODEL_AMBIENT, global_ambient); gl.Light(OpenGL.GL_LIGHT0, OpenGL.GL_POSITION, light0pos); gl.Light(OpenGL.GL_LIGHT0, OpenGL.GL_AMBIENT, light0ambient); gl.Light(OpenGL.GL_LIGHT0, OpenGL.GL_DIFFUSE, light0diffuse); gl.Light(OpenGL.GL_LIGHT0, OpenGL.GL_SPECULAR, light0specular); gl.Enable(OpenGL.GL_LIGHTING); gl.Enable(OpenGL.GL_LIGHT0); gl.ShadeModel(OpenGL.GL_SMOOTH); grid.GenerateGeometry(gl); ShaderStore.CompileShaders(gl); }
private void GL_Draw(object sender, OpenGLRoutedEventArgs args) { OpenGL gl = args.OpenGL; foreach (var sceneObject in Hierarchy.HierarchyObjectList) { if (!sceneObject.MeshRenderer.Mesh.GeometryGenerated) { sceneObject.MeshRenderer.Mesh.GenerateGeometry(gl); } } #if DEBUG #region Hot Reload Shaders ShaderStore.CompileShadersHotReload(gl); OnShaderError(ShaderStore.GetShaderErrors()); #endregion #endif gl.Clear(OpenGL.GL_COLOR_BUFFER_BIT | OpenGL.GL_DEPTH_BUFFER_BIT | OpenGL.GL_STENCIL_BUFFER_BIT); gl.ClearColor((float)37 / 255, (float)37 / 255, (float)38 / 255, 1); ShaderProgram program = ShaderStore.programs["unlitColor"]; program.Push(gl, null); // Set the variables for the shader program. gl.Uniform3(program.GetUniformLocation("AmbientMaterial"), 0.04f, 0.04f, 0.04f); gl.Uniform3(program.GetUniformLocation("SpecularMaterial"), 0.5f, 0.5f, 0.5f); gl.Uniform1(program.GetUniformLocation("Shininess"), 50f); // Set the light position. gl.Uniform3(program.GetUniformLocation("LightPosition"), 0.25f, 0.25f, 1f); // Set the matrices. gl.UniformMatrix4(program.GetUniformLocation("Projection"), 1, false, projectionMatrix.ToArray()); gl.UniformMatrix4(program.GetUniformLocation("View"), 1, false, cameraTransform.ToArray()); gl.UniformMatrix3(program.GetUniformLocation("NormalMatrix"), 1, false, normalMatrix.ToArray()); // Draw Grid grid.Draw(gl, program); foreach (var sceneObject in Hierarchy.HierarchyObjectList) { GLColor col = sceneObject.MeshRenderer.Color; gl.Uniform3(program.GetUniformLocation("DiffuseMaterial"), col.R, col.G, col.B); gl.UniformMatrix4(program.GetUniformLocation("Model"), 1, false, sceneObject.Transform.Matrix.ToArray()); sceneObject.MeshRenderer.Mesh.Draw(gl); } program.Pop(gl, null); }