public void Draw(Camera camera, bool ztest)
        {
            //Disable culling
            GL.Disable(EnableCap.CullFace);
            // Disable z-testing if specified
            if (!ztest)
            {
                GL.Disable(EnableCap.DepthTest);
            }

            // Bind debug shader
            GL.UseProgram(shaderProgram);

            var uniformLocation = GL.GetUniformLocation(shaderProgram, "uProjection");
            GL.UniformMatrix4(uniformLocation, false, ref camera.ProjectionMatrix);
            uniformLocation = GL.GetUniformLocation(shaderProgram, "uView");
            GL.UniformMatrix4(uniformLocation, false, ref camera.CameraViewMatrix);

            foreach (var obj in objects)
            {
                uniformLocation = GL.GetUniformLocation(shaderProgram, "uTransform");
                var transform = obj.Transform;
                GL.UniformMatrix4(uniformLocation, false, ref transform);

                // Bind VAO to draw
                GL.BindVertexArray(obj.VAO);
                GL.DrawArrays(PrimitiveType.Triangles, 0, obj.Size);
                GL.BindVertexArray(0);
            }

            GL.UseProgram(0);

            // Re-enable depth testing
            if (!ztest)
            {
                GL.Enable(EnableCap.DepthTest);
            }

            // Re-enable culling
            GL.Enable(EnableCap.CullFace);
        }
        private void CameraBox_ItemCheck(object sender, ItemCheckEventArgs e)
        {
            //https://social.msdn.microsoft.com/Forums/windows/en-US/5333cdf2-a669-467c-99ae-1530e91da43a/checkedlistbox-allow-only-one-item-to-be-selected?forum=winforms
            if (e.NewValue == CheckState.Checked)
            {
                for (var ix = 0; ix < cameraBox.Items.Count; ++ix)
                {
                    if (e.Index != ix)
                    {
                        cameraBox.ItemCheck -= CameraBox_ItemCheck;
                        cameraBox.SetItemChecked(ix, false);
                        cameraBox.ItemCheck += CameraBox_ItemCheck;
                    }
                }

                ActiveCamera = cameraBox.Items[e.Index] as Camera;
            }
            else if (e.CurrentValue == CheckState.Checked && cameraBox.CheckedItems.Count == 1)
            {
                e.NewValue = CheckState.Checked;
            }
        }
        private void MeshControl_Load(object sender, EventArgs e)
        {
            meshControl.MakeCurrent();

            Console.WriteLine("OpenGL version: " + GL.GetString(StringName.Version));
            Console.WriteLine("OpenGL vendor: " + GL.GetString(StringName.Vendor));
            Console.WriteLine("GLSL version: " + GL.GetString(StringName.ShadingLanguageVersion));

            CheckOpenGL();
            LoadBoundingBox();

            GL.Enable(EnableCap.CullFace);
            GL.Enable(EnableCap.DepthTest);

            GL.ClearColor(Settings.BackgroundColor);

            GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit);

            InitializeInputTick();

            ActiveCamera = new Camera(tabs.Width, tabs.Height, MinBounds, MaxBounds);
            cameraBox.Items.Add(ActiveCamera, true);

            foreach (var cameraInfo in cameras)
            {
                var camera = new Camera(tabs.Width, tabs.Height, cameraInfo.Item2, cameraInfo.Item1);
                cameraBox.Items.Add(camera);
            }

            ActiveAnimation = Animations.Count > 0 ? Animations[0] : null;
            animationBox.Items.AddRange(Animations.ToArray());

            foreach (var obj in MeshesToRender)
            {
                obj.LoadFromResource(MaterialLoader);
            }

            #if DEBUG
            Debug.Setup();
            //Skeleton.DebugDraw(Debug);
            #endif

            // Create animation texture
            AnimationTexture = GL.GenTexture();
            GL.BindTexture(TextureTarget.Texture2D, AnimationTexture);
            // Set clamping to edges
            GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapS, (int)TextureWrapMode.ClampToEdge);
            GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapT, (int)TextureWrapMode.ClampToEdge);
            // Set nearest-neighbor sampling since we don't want to interpolate matrix rows
            GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMinFilter, (int)TextureMinFilter.Nearest);
            GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMagFilter, (int)TextureMinFilter.Nearest);
            //Unbind texture again
            GL.BindTexture(TextureTarget.Texture2D, 0);

            // TODO: poor hack
            FileExtensions.ClearCache();

            Loaded = true;

            Console.WriteLine("{0} draw calls total", MeshesToRender.Sum(x => x.DrawCalls.Count));
        }
Exemple #4
0
        private void MeshControl_Load(object sender, EventArgs e)
        {
            meshControl.MakeCurrent();

            Console.WriteLine("OpenGL version: " + GL.GetString(StringName.Version));
            Console.WriteLine("OpenGL vendor: " + GL.GetString(StringName.Vendor));
            Console.WriteLine("GLSL version: " + GL.GetString(StringName.ShadingLanguageVersion));

            CheckOpenGL();
            LoadBoundingBox();

            GL.Enable(EnableCap.CullFace);
            GL.Enable(EnableCap.DepthTest);

            GL.ClearColor(Settings.BackgroundColor);

            GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit);

            InitializeInputTick();

            // If no defaul camera was found, make one up
            if (ActiveCamera == null)
            {
                ActiveCamera = new Camera(MinBounds, MaxBounds);
            }

            cameraBox.Items.Add(ActiveCamera, true);

            // Set camera viewport size
            ActiveCamera.SetViewportSize(meshControl.Width, meshControl.Width);

            foreach (var cameraInfo in cameras)
            {
                var camera = new Camera(cameraInfo.Item2, cameraInfo.Item1);
                cameraBox.Items.Add(camera);
            }

            ActiveAnimation = Animations.Count > 0 ? Animations[0] : null;
            animationBox.Items.AddRange(Animations.ToArray());

            foreach (var obj in MeshesToRender)
            {
                obj.LoadFromResource(MaterialLoader);
            }

            // Gather render modes
            var renderModes = new string[0];

            foreach (var mesh in MeshesToRender)
            {
                foreach (var call in mesh.DrawCalls)
                {
                    renderModes = renderModes.Union(call.Shader.RenderModes).ToArray();
                }
            }

            if (SubjectType == RenderSubject.Model)
            {
                renderModeComboBox.Items.Clear();
                renderModeComboBox.Items.Add("Change render mode...");
                renderModeComboBox.Items.AddRange(renderModes);
                renderModeComboBox.SelectedIndex = 0;
            }

#if DEBUG
            Debug.Setup();
#endif

            // Create animation texture
            AnimationTexture = GL.GenTexture();
            GL.BindTexture(TextureTarget.Texture2D, AnimationTexture);
            // Set clamping to edges
            GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapS, (int)TextureWrapMode.ClampToEdge);
            GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapT, (int)TextureWrapMode.ClampToEdge);
            // Set nearest-neighbor sampling since we don't want to interpolate matrix rows
            GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMinFilter, (int)TextureMinFilter.Nearest);
            GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMagFilter, (int)TextureMinFilter.Nearest);
            //Unbind texture again
            GL.BindTexture(TextureTarget.Texture2D, 0);

            // TODO: poor hack
            FileExtensions.ClearCache();

            Loaded = true;

            Console.WriteLine("{0} draw calls total", MeshesToRender.Sum(x => x.DrawCalls.Count));
        }